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>
|
|
|
|
|
2003-04-07 19:06:00 +00:00
|
|
|
#include "boinc_db.h"
|
2003-02-10 19:51:32 +00:00
|
|
|
#include "util.h"
|
2002-10-03 18:33:46 +00:00
|
|
|
#include "config.h"
|
2003-03-08 00:09:40 +00:00
|
|
|
#include "sched_util.h"
|
2002-09-02 17:25:55 +00:00
|
|
|
|
2003-02-10 19:51:32 +00:00
|
|
|
#define LOCKFILE "validate.out"
|
|
|
|
|
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-09-25 19:40:19 +00:00
|
|
|
// 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;
|
|
|
|
|
2003-04-07 17:11:57 +00:00
|
|
|
retval = boinc_db_host(result.hostid, host);
|
2002-09-25 19:40:19 +00:00
|
|
|
if (retval) return retval;
|
2003-04-07 17:11:57 +00:00
|
|
|
retval = boinc_db_user(host.userid, user);
|
2002-09-25 19:40:19 +00:00
|
|
|
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);
|
2003-04-07 17:11:57 +00:00
|
|
|
retval = boinc_db_user_update(user);
|
2002-09-25 19:40:19 +00:00
|
|
|
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);
|
2003-04-07 17:11:57 +00:00
|
|
|
retval = boinc_db_host_update(host);
|
2002-09-25 19:40:19 +00:00
|
|
|
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;
|
2003-04-07 18:37:38 +00:00
|
|
|
int retval, canonicalid = 0;
|
2002-09-25 19:40:19 +00:00
|
|
|
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
|
|
|
|
//
|
2003-04-07 17:11:57 +00:00
|
|
|
retval = boinc_db_result(wu.canonical_resultid, canonical_result);
|
2003-01-23 08:07:48 +00:00
|
|
|
if (retval) {
|
|
|
|
write_log("can't read canonical result\n");
|
2003-04-07 18:37:38 +00:00
|
|
|
// Mark this WU as validated, otherwise we'll keep checking it
|
|
|
|
goto mark_validated;
|
2003-01-23 08:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// scan this WU's results, and check the unchecked ones
|
|
|
|
//
|
|
|
|
result.workunitid = wu.id;
|
2003-04-07 17:11:57 +00:00
|
|
|
while (!boinc_db_result_enum_wuid(result)) {
|
2003-01-23 08:07:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2003-04-07 17:11:57 +00:00
|
|
|
retval = boinc_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;
|
2003-04-07 17:11:57 +00:00
|
|
|
while (!boinc_db_result_enum_wuid(result)) {
|
2003-01-23 08:07:48 +00:00
|
|
|
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) {
|
2003-04-07 17:11:57 +00:00
|
|
|
retval = boinc_db_result_update(result);
|
2002-10-19 17:14:08 +00:00
|
|
|
if (retval) {
|
2003-01-07 01:02:08 +00:00
|
|
|
sprintf(buf,
|
2003-04-07 17:11:57 +00:00
|
|
|
"validate: boinc_db_result_update %d\n", retval
|
2002-10-19 17:14:08 +00:00
|
|
|
);
|
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-04-07 18:37:38 +00:00
|
|
|
mark_validated:
|
2003-01-23 08:07:48 +00:00
|
|
|
// we've checked all results for this WU, so turn off flag
|
|
|
|
//
|
|
|
|
wu.need_validate = 0;
|
2003-04-07 17:11:57 +00:00
|
|
|
retval = boinc_db_workunit_update(wu);
|
2003-01-23 08:07:48 +00:00
|
|
|
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;
|
2003-04-07 17:11:57 +00:00
|
|
|
while (!boinc_db_workunit_enum_app_need_validate(wu)) {
|
2003-01-23 08:07:48 +00:00
|
|
|
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);
|
2003-04-07 17:11:57 +00:00
|
|
|
retval = boinc_db_app_lookup_name(app);
|
2002-09-02 17:25:55 +00:00
|
|
|
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) {
|
2003-03-08 00:09:40 +00:00
|
|
|
check_stop_trigger();
|
2002-10-19 17:14:08 +00:00
|
|
|
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) {
|
2003-03-28 21:54:07 +00:00
|
|
|
sleep(5);
|
2002-10-19 17:14:08 +00:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
check_stop_trigger();
|
|
|
|
|
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
|
|
|
}
|
2003-02-27 19:29:48 +00:00
|
|
|
|
2003-04-01 07:13:43 +00:00
|
|
|
// Call lock_file after fork(), because file locks are not always inherited
|
2003-02-27 19:29:48 +00:00
|
|
|
if (lock_file(LOCKFILE)) {
|
|
|
|
fprintf(stderr, "Another copy of validate is already running\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-12-03 18:57:40 +00:00
|
|
|
main_loop(one_pass);
|
2002-09-02 17:25:55 +00:00
|
|
|
}
|