// Berkeley Open Infrastructure for Network Computing // http://boinc.berkeley.edu // Copyright (C) 2005 University of California // // This is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; // either version 2.1 of the License, or (at your option) any later version. // // This software is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. // See the GNU Lesser General Public License for more details. // // To view the GNU Lesser General Public License visit // http://www.gnu.org/copyleft/lesser.html // or write to the Free Software Foundation, Inc., // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // validator - check and validate new results, and grant credit // -app appname // [-d debug_level] // [-one_pass_N_WU N] // Validate only N WU in one pass, then exit // [-one_pass] // make one pass through WU table, then exit // [-asynch] // fork, run in separate process // // This program must be linked with two project-specific functions: // check_set() and check_pair(). // See doc/validate.php for a description. using namespace std; #include #include #include #include "boinc_db.h" #include "util.h" #include "error_numbers.h" #include "sched_config.h" #include "sched_util.h" #include "sched_msgs.h" #define LOCKFILE "validate.out" #define PIDFILE "validate.pid" #define SELECT_LIMIT 1000 #define SLEEP_PERIOD 5 extern int check_set( vector&, WORKUNIT& wu, int& canonical, double& credit, bool& retry ); extern int check_pair( RESULT & new_result, RESULT const& canonical_result, bool& retry ); SCHED_CONFIG config; char app_name[256]; // here when a result has been validated; // grant credit to host, user and team // int grant_credit(RESULT& result, double credit) { DB_USER user; DB_HOST host; DB_TEAM team; int retval; char buf[256]; retval = host.lookup_id(result.hostid); if (retval) { log_messages.printf( SCHED_MSG_LOG::CRITICAL, "[RESULT#%d] lookup of host %d failed %d\n", result.id, result.hostid, retval ); return retval; } retval = user.lookup_id(host.userid); if (retval) { log_messages.printf( SCHED_MSG_LOG::CRITICAL, "[RESULT#%d] lookup of user %d failed %d\n", result.id, host.userid, retval ); return retval; } user.total_credit += credit; update_average(result.sent_time, credit, CREDIT_HALF_LIFE, user.expavg_credit, user.expavg_time); sprintf( buf, "total_credit=%f, expavg_credit=%f, expavg_time=%f", user.total_credit, user.expavg_credit, user.expavg_time ); retval = user.update_field(buf); if (retval) { log_messages.printf( SCHED_MSG_LOG::CRITICAL, "[RESULT#%d] update of user %d failed %d\n", result.id, host.userid, retval ); } host.total_credit += credit; update_average(result.sent_time, credit, CREDIT_HALF_LIFE, host.expavg_credit, host.expavg_time); double turnaround = result.received_time - result.sent_time; if (host.avg_turnaround == 0) { host.avg_turnaround = turnaround; } else { host.avg_turnaround = .7*host.avg_turnaround + .3*turnaround; } sprintf( buf, "total_credit=%f, expavg_credit=%f, expavg_time=%f, avg_turnaround=%f", host.total_credit, host.expavg_credit, host.expavg_time, host.avg_turnaround ); retval = host.update_field(buf); if (retval) { log_messages.printf( SCHED_MSG_LOG::CRITICAL, "[RESULT#%d] update of host %d failed %d\n", result.id, result.hostid, retval ); } if (user.teamid) { retval = team.lookup_id(user.teamid); if (retval) { log_messages.printf( SCHED_MSG_LOG::CRITICAL, "[RESULT#%d] lookup of team %d failed %d\n", result.id, user.teamid, retval ); return retval; } team.total_credit += credit; update_average(result.sent_time, credit, CREDIT_HALF_LIFE, team.expavg_credit, team.expavg_time); sprintf( buf, "total_credit=%f, expavg_credit=%f, expavg_time=%f", team.total_credit, team.expavg_credit, team.expavg_time ); retval = team.update_field(buf); if (retval) { log_messages.printf( SCHED_MSG_LOG::CRITICAL, "[RESULT#%d] update of team %d failed %d\n", result.id, team.id, retval ); } } return 0; } void handle_wu( DB_VALIDATOR_ITEM_SET& validator, std::vector& items ) { int canonical_result_index = -1; bool update_result, retry; bool need_immediate_transition = false, need_delayed_transition = false; int retval = 0, canonicalid = 0; double credit = 0; unsigned int i; WORKUNIT& wu = items[0].wu; if (wu.canonical_resultid) { log_messages.printf( SCHED_MSG_LOG::NORMAL, "[WU#%d %s] handle_wu(): Already has canonical result %d\n", wu.id, wu.name, wu.canonical_resultid ); ++log_messages; // Here if WU already has a canonical result. // Get unchecked results and see if they match the canonical result // for (i=0; i results; int nsuccess_results; // Here if WU doesn't have a canonical result yet. // Try to get one log_messages.printf( SCHED_MSG_LOG::NORMAL, "[WU#%d %s] handle_wu(): No canonical result yet\n", wu.id, wu.name ); ++log_messages; // make a vector of only successful results // for (i=0; i= (unsigned int)wu.min_quorum) { log_messages.printf( SCHED_MSG_LOG::DEBUG, "[WU#%d %s] Enough for quorum, checking set.\n", wu.id, wu.name ); retval = check_set(results, wu, canonicalid, credit, retry); if (retval) { log_messages.printf( SCHED_MSG_LOG::CRITICAL, "[WU#%d %s] check_set returned %d, exiting\n", wu.id, wu.name, retval ); exit(retval); } if (retry) need_delayed_transition = true; // scan results. // update as needed, and count the # of results // that are still outcome=SUCCESS // (some may have changed to VALIDATE_ERROR) // nsuccess_results = 0; for (i=0; i wu.max_success_results) { wu.error_mask |= WU_ERROR_TOO_MANY_SUCCESS_RESULTS; need_immediate_transition = true; } // if #success results == than target_nresults, // we need more results, so bump target_nresults // NOTE: nsuccess_results should never be > target_nresults, // but accommodate that if it should happen // if (nsuccess_results >= wu.target_nresults) { wu.target_nresults = nsuccess_results+1; need_immediate_transition = true; } } } } --log_messages; if (need_immediate_transition) { wu.transition_time = time(0); } else if (need_delayed_transition) { int x = time(0) + 6*3600; if (x < wu.transition_time) wu.transition_time = x; } wu.need_validate = 0; retval = validator.update_workunit(wu); if (retval) { log_messages.printf( SCHED_MSG_LOG::CRITICAL, "[WU#%d %s] update_workunit() failed: %d; exiting\n", wu.id, wu.name, retval ); exit(1); } } int one_pass_N_WU=0; // make one pass through the workunits with need_validate set. // return true if there were any // bool do_validate_scan(APP& app) { DB_VALIDATOR_ITEM_SET validator; std::vector items; bool found=false; // loop over entries that need to be checked // while (!validator.enumerate(app.id, one_pass_N_WU?one_pass_N_WU:SELECT_LIMIT, items)) { handle_wu(validator, items); found = true; } return found; } int main_loop(bool one_pass) { int retval; DB_APP app; bool did_something; char buf[256]; retval = boinc_db.open(config.db_name, config.db_host, config.db_user, config.db_passwd); if (retval) { log_messages.printf(SCHED_MSG_LOG::CRITICAL, "boinc_db.open failed: %d\n", retval); exit(1); } sprintf(buf, "where name='%s'", app_name); retval = app.lookup(buf); if (retval) { log_messages.printf(SCHED_MSG_LOG::CRITICAL, "can't find app %s\n", app_name); exit(1); } while (1) { check_stop_daemons(); did_something = do_validate_scan(app); if (one_pass) break; if (!did_something) { sleep(SLEEP_PERIOD); } } return 0; } // For use by user routines check_set() and check_match() that link to // this code. int boinc_validator_debuglevel=0; int main(int argc, char** argv) { int i, retval; bool asynch = false, one_pass = false; check_stop_daemons(); for (i=1; i