2002-09-26 18:11:06 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
|
|
|
|
|
|
|
|
//
|
2002-09-26 05:57:10 +00:00
|
|
|
// validate - check and validate new results, and grant credit
|
2002-12-03 18:57:40 +00:00
|
|
|
// -app appname
|
|
|
|
// -quorum n // example WUs only with this many done results
|
|
|
|
// [-one_pass] // make one pass through WU table, then exit
|
|
|
|
// [-asynch] // fork, run in separate process
|
2002-09-02 17:25:55 +00:00
|
|
|
//
|
|
|
|
// This program must be linked with two project-specific functions:
|
|
|
|
//
|
2002-09-25 19:40:19 +00:00
|
|
|
// int check_set(vector<RESULT>, int& canonical, double& credit)
|
|
|
|
// Compare a set of results.
|
|
|
|
// If a canonical result is found, return its ID,
|
|
|
|
// and set the "validate_state" field of all the results
|
2002-09-02 17:25:55 +00:00
|
|
|
// according to whether they match the canonical result.
|
2002-09-25 19:40:19 +00:00
|
|
|
// Also return the "canonical credit" (e.g. the average or median)
|
2002-09-02 17:25:55 +00:00
|
|
|
//
|
|
|
|
// int pair_check(RESULT& new_result, RESULT& canonical, bool& valid);
|
|
|
|
// return valid=true iff the new result matches the canonical one
|
|
|
|
//
|
|
|
|
// Both functions return nonzero if an error occurred,
|
|
|
|
// in which case other outputs are undefined
|
|
|
|
|
2002-10-09 18:48:26 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2002-09-25 19:40:19 +00:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <math.h>
|
2002-09-02 17:25:55 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "db.h"
|
2002-10-03 18:33:46 +00:00
|
|
|
#include "config.h"
|
2002-09-02 17:25:55 +00:00
|
|
|
|
2002-10-19 17:14:08 +00:00
|
|
|
extern int check_set(vector<RESULT>&, int& canonical, double& credit);
|
2002-09-25 19:40:19 +00:00
|
|
|
extern int check_pair(RESULT&, RESULT&, bool&);
|
|
|
|
|
2002-10-03 18:33:46 +00:00
|
|
|
CONFIG config;
|
2002-10-19 17:14:08 +00:00
|
|
|
char app_name[256];
|
|
|
|
int min_quorum;
|
2002-10-03 18:33:46 +00:00
|
|
|
|
2002-10-22 01:26:21 +00:00
|
|
|
// "average credit" uses an exponential decay so that recent
|
|
|
|
// activity is weighted more heavily.
|
|
|
|
// H is the "half-life" period: the average goes down by 1/2
|
|
|
|
// if idle for this period.
|
|
|
|
// Specifically, the weighting function W(t) is
|
|
|
|
// W(t) = exp(t/(H*log(2))*H*log(2).
|
|
|
|
// The average credit is the sum of X*W(t(X))
|
|
|
|
// over units of credit X that were granted t(X) time ago.
|
|
|
|
|
|
|
|
#define LOG2 M_LN2
|
|
|
|
// log(2)
|
2002-09-25 19:40:19 +00:00
|
|
|
#define SECONDS_IN_DAY (3600*24)
|
2002-10-22 01:26:21 +00:00
|
|
|
#define AVG_HALF_LIFE (SECONDS_IN_DAY*7)
|
2003-01-02 21:52:13 +00:00
|
|
|
#define ALPHA (LOG2/AVG_HALF_LIFE)
|
2002-09-25 19:40:19 +00:00
|
|
|
|
2003-01-07 01:02:08 +00:00
|
|
|
void write_log(char* p) {
|
|
|
|
time_t now = time(0);
|
|
|
|
char* timestr = ctime(&now);
|
|
|
|
*(strchr(timestr, '\n')) = 0;
|
|
|
|
fprintf(stderr, "%s: %s", timestr, p);
|
|
|
|
}
|
|
|
|
|
2002-10-22 01:26:21 +00:00
|
|
|
// update an exponential average of credit per second.
|
2002-09-25 19:40:19 +00:00
|
|
|
//
|
2003-01-02 21:52:13 +00:00
|
|
|
void update_average(double credit_assigned_time, double credit, double& avg, double& avg_time) {
|
2002-09-25 19:40:19 +00:00
|
|
|
time_t now = time(0);
|
|
|
|
|
2002-10-22 01:26:21 +00:00
|
|
|
// decrease existing average according to how long it's been
|
|
|
|
// since it was computed
|
|
|
|
//
|
2002-09-25 19:40:19 +00:00
|
|
|
if (avg_time) {
|
|
|
|
double deltat = now - avg_time;
|
2003-01-02 21:52:13 +00:00
|
|
|
avg *= exp(-deltat*ALPHA);
|
2002-09-25 19:40:19 +00:00
|
|
|
}
|
2003-01-02 21:52:13 +00:00
|
|
|
double deltat = now - credit_assigned_time;
|
|
|
|
// Add (credit)/(number of days to return result) to credit, which
|
|
|
|
// is the average number of cobblestones per day
|
|
|
|
avg += credit/(deltat/86400);
|
2002-09-25 19:40:19 +00:00
|
|
|
avg_time = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
// here when a result has been validated;
|
|
|
|
// grant credit to host and user
|
|
|
|
//
|
|
|
|
int grant_credit(RESULT& result, double credit) {
|
|
|
|
USER user;
|
|
|
|
HOST host;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
retval = db_host(result.hostid, host);
|
|
|
|
if (retval) return retval;
|
|
|
|
retval = db_user(host.userid, user);
|
|
|
|
if (retval) return retval;
|
|
|
|
|
|
|
|
user.total_credit += credit;
|
2003-01-02 21:52:13 +00:00
|
|
|
update_average(result.sent_time, credit, user.expavg_credit, user.expavg_time);
|
2002-09-25 19:40:19 +00:00
|
|
|
retval = db_user_update(user);
|
|
|
|
if (retval) return retval;
|
|
|
|
|
|
|
|
host.total_credit += credit;
|
2003-01-02 21:52:13 +00:00
|
|
|
update_average(result.sent_time, credit, host.expavg_credit, host.expavg_time);
|
2002-09-25 19:40:19 +00:00
|
|
|
retval = db_host_update(host);
|
|
|
|
if (retval) return retval;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2002-09-02 17:25:55 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
void handle_wu(WORKUNIT& wu) {
|
2002-09-02 17:25:55 +00:00
|
|
|
RESULT result, canonical_result;
|
2003-01-23 08:07:48 +00:00
|
|
|
bool match, update_result;
|
2002-09-25 19:40:19 +00:00
|
|
|
int retval, canonicalid;
|
|
|
|
double credit;
|
|
|
|
unsigned int i;
|
2003-01-07 01:02:08 +00:00
|
|
|
char buf[256];
|
2002-09-02 17:25:55 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
if (wu.canonical_resultid) {
|
|
|
|
sprintf(buf,
|
|
|
|
"validating WU %s; already have canonical result\n", wu.name
|
|
|
|
);
|
|
|
|
write_log(buf);
|
2002-09-25 19:40:19 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
// Here if WU already has a canonical result.
|
|
|
|
// Get unchecked results and see if they match the canonical result
|
|
|
|
//
|
|
|
|
retval = db_result(wu.canonical_resultid, canonical_result);
|
|
|
|
if (retval) {
|
|
|
|
write_log("can't read canonical result\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// scan this WU's results, and check the unchecked ones
|
|
|
|
//
|
|
|
|
result.workunitid = wu.id;
|
|
|
|
while (!db_result_enum_wuid(result)) {
|
|
|
|
if (result.validate_state == VALIDATE_STATE_INIT
|
|
|
|
&& result.server_state == RESULT_SERVER_STATE_OVER
|
|
|
|
&& result.outcome == RESULT_OUTCOME_SUCCESS
|
|
|
|
) {
|
2002-09-25 19:40:19 +00:00
|
|
|
retval = check_pair(result, canonical_result, match);
|
2002-09-02 17:25:55 +00:00
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf,
|
2002-09-02 17:25:55 +00:00
|
|
|
"validate: pair_check failed for result %d\n",
|
|
|
|
result.id
|
|
|
|
);
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log(buf);
|
2002-09-02 17:25:55 +00:00
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (match) {
|
2002-09-25 19:40:19 +00:00
|
|
|
result.validate_state = VALIDATE_STATE_VALID;
|
2002-10-20 22:12:53 +00:00
|
|
|
result.granted_credit = wu.canonical_credit;
|
|
|
|
printf("setting result %d to valid; credit %f\n", result.id, result.granted_credit);
|
2002-09-02 17:25:55 +00:00
|
|
|
} else {
|
2002-09-25 19:40:19 +00:00
|
|
|
result.validate_state = VALIDATE_STATE_INVALID;
|
2002-10-20 22:12:53 +00:00
|
|
|
printf("setting result %d to invalid\n", result.id);
|
2002-09-02 17:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
2002-09-25 19:40:19 +00:00
|
|
|
retval = db_result_update(result);
|
2002-10-22 01:26:21 +00:00
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("Can't update result\n");
|
2002-10-22 01:26:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-10-20 22:12:53 +00:00
|
|
|
retval = grant_credit(result, result.granted_credit);
|
2002-10-22 01:26:21 +00:00
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("Can't grant credit\n");
|
2002-10-22 01:26:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-09-02 17:25:55 +00:00
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
vector<RESULT> results;
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
// Here if WU doesn't have a canonical result yet.
|
|
|
|
// Try to get one
|
|
|
|
|
|
|
|
sprintf(buf, "validating WU %s; no canonical result\n", wu.name);
|
|
|
|
write_log(buf);
|
|
|
|
|
|
|
|
result.workunitid = wu.id;
|
|
|
|
while (!db_result_enum_wuid(result)) {
|
|
|
|
if (result.server_state == RESULT_SERVER_STATE_OVER
|
|
|
|
&& result.outcome == RESULT_OUTCOME_SUCCESS
|
|
|
|
) {
|
|
|
|
results.push_back(result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sprintf(buf, "found %d successful results\n", results.size());
|
|
|
|
write_log(buf);
|
|
|
|
if (results.size() >= (unsigned int)min_quorum) {
|
|
|
|
retval = check_set(results, canonicalid, credit);
|
|
|
|
if (!retval && canonicalid) {
|
|
|
|
write_log("found a canonical result\n");
|
|
|
|
wu.canonical_resultid = canonicalid;
|
|
|
|
wu.canonical_credit = credit;
|
|
|
|
wu.assimilate_state = ASSIMILATE_READY;
|
|
|
|
for (i=0; i<results.size(); i++) {
|
|
|
|
result = results[i];
|
|
|
|
update_result = false;
|
|
|
|
|
|
|
|
// grant credit for valid results
|
|
|
|
//
|
|
|
|
if (result.validate_state == VALIDATE_STATE_VALID) {
|
|
|
|
update_result = true;
|
|
|
|
retval = grant_credit(result, credit);
|
|
|
|
if (retval) {
|
|
|
|
sprintf(buf,
|
|
|
|
"validate: grant_credit %d\n", retval
|
|
|
|
);
|
|
|
|
write_log(buf);
|
2002-09-25 19:40:19 +00:00
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
result.granted_credit = credit;
|
|
|
|
sprintf(buf,
|
|
|
|
"updating result %d to %d; credit %f\n",
|
|
|
|
result.id, result.validate_state, credit
|
|
|
|
);
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log(buf);
|
2003-01-23 08:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// don't send any unsent results
|
|
|
|
//
|
|
|
|
if (result.server_state == RESULT_SERVER_STATE_UNSENT) {
|
|
|
|
update_result = true;
|
|
|
|
result.server_state = RESULT_SERVER_STATE_OVER;
|
|
|
|
result.outcome = RESULT_OUTCOME_DIDNT_NEED;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update_result) {
|
|
|
|
retval = db_result_update(result);
|
2002-10-19 17:14:08 +00:00
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf,
|
2002-10-19 17:14:08 +00:00
|
|
|
"validate: db_result_update %d\n", retval
|
|
|
|
);
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log(buf);
|
2002-10-19 17:14:08 +00:00
|
|
|
}
|
2002-09-25 19:40:19 +00:00
|
|
|
}
|
|
|
|
}
|
2002-09-02 17:25:55 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
}
|
2002-09-25 19:40:19 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
// we've checked all results for this WU, so turn off flag
|
|
|
|
//
|
|
|
|
wu.need_validate = 0;
|
|
|
|
retval = db_workunit_update(wu);
|
|
|
|
if (retval) {
|
|
|
|
sprintf(buf, "db_workunit_update: %d\n", retval);
|
|
|
|
write_log(buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// make one pass through the workunits with need_validate set.
|
|
|
|
// return true if there were any
|
|
|
|
//
|
|
|
|
bool do_validate_scan(APP& app, int min_quorum) {
|
|
|
|
WORKUNIT wu;
|
|
|
|
bool found=false;
|
|
|
|
|
|
|
|
wu.appid = app.id;
|
|
|
|
while(!db_workunit_enum_app_need_validate(wu)) {
|
|
|
|
handle_wu(wu);
|
|
|
|
found = true;
|
2002-09-02 17:25:55 +00:00
|
|
|
}
|
2002-09-25 19:40:19 +00:00
|
|
|
return found;
|
2002-09-02 17:25:55 +00:00
|
|
|
}
|
|
|
|
|
2002-12-03 18:57:40 +00:00
|
|
|
int main_loop(bool one_pass) {
|
2002-10-19 17:14:08 +00:00
|
|
|
int retval;
|
2002-09-02 17:25:55 +00:00
|
|
|
APP app;
|
|
|
|
bool did_something;
|
2003-01-07 01:02:08 +00:00
|
|
|
char buf[256];
|
2002-09-02 17:25:55 +00:00
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
retval = boinc_db_open(config.db_name, config.db_passwd);
|
2002-09-02 17:25:55 +00:00
|
|
|
if (retval) {
|
2003-01-09 07:24:27 +00:00
|
|
|
sprintf(buf, "boinc_db_open: %d\n", retval);
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log(buf);
|
2002-09-02 17:25:55 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-10-19 17:14:08 +00:00
|
|
|
strcpy(app.name, app_name);
|
2002-09-02 17:25:55 +00:00
|
|
|
retval = db_app_lookup_name(app);
|
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf, "can't find app %s\n", app.name);
|
|
|
|
write_log(buf);
|
2002-09-02 17:25:55 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-10-19 17:14:08 +00:00
|
|
|
while (1) {
|
|
|
|
did_something = do_validate_scan(app, min_quorum);
|
2002-12-03 18:57:40 +00:00
|
|
|
if (one_pass) break;
|
2002-10-19 17:14:08 +00:00
|
|
|
if (!did_something) {
|
|
|
|
sleep(1);
|
|
|
|
}
|
|
|
|
}
|
2002-12-03 18:57:40 +00:00
|
|
|
return 0;
|
2002-10-19 17:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
int i, retval;
|
2002-12-03 18:57:40 +00:00
|
|
|
bool asynch = false, one_pass = false;
|
2003-01-07 01:02:08 +00:00
|
|
|
char buf[256];
|
2002-10-19 17:14:08 +00:00
|
|
|
|
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-asynch")) {
|
|
|
|
asynch = true;
|
2002-12-03 18:57:40 +00:00
|
|
|
} else if (!strcmp(argv[i], "-one_pass")) {
|
|
|
|
one_pass = true;
|
2002-10-19 17:14:08 +00:00
|
|
|
} else if (!strcmp(argv[i], "-app")) {
|
|
|
|
strcpy(app_name, argv[++i]);
|
|
|
|
} else if (!strcmp(argv[i], "-quorum")) {
|
|
|
|
min_quorum = atoi(argv[++i]);
|
2002-12-17 19:00:43 +00:00
|
|
|
} else {
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf, "unrecognized arg: %s\n", argv[i]);
|
|
|
|
write_log(buf);
|
2002-10-19 17:14:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-09-02 17:25:55 +00:00
|
|
|
if (min_quorum < 1 || min_quorum > 10) {
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf, "bad min_quorum: %d\n", min_quorum);
|
|
|
|
write_log(buf);
|
2002-09-02 17:25:55 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf, "starting validator; min_quorum %d\n", min_quorum);
|
|
|
|
write_log(buf);
|
|
|
|
|
2002-10-19 17:14:08 +00:00
|
|
|
retval = config.parse_file();
|
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
write_log("Can't parse config file\n");
|
2002-10-19 17:14:08 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asynch) {
|
2002-12-03 18:57:40 +00:00
|
|
|
if (fork()) {
|
|
|
|
exit(0);
|
2002-10-19 17:14:08 +00:00
|
|
|
}
|
2002-09-02 17:25:55 +00:00
|
|
|
}
|
2002-12-03 18:57:40 +00:00
|
|
|
main_loop(one_pass);
|
2002-09-02 17:25:55 +00:00
|
|
|
}
|