2002-12-03 18:57:40 +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
|
2003-06-11 23:12:51 +00:00
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
2002-12-03 18:57:40 +00:00
|
|
|
// 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
|
2003-06-11 23:12:51 +00:00
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
2002-12-03 18:57:40 +00:00
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
2003-06-11 23:12:51 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002, 2003
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-12-03 18:57:40 +00:00
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
// timeout_check - do various time-based tasks
|
|
|
|
// - time out results
|
|
|
|
// - create new results to make up for lost ones
|
|
|
|
// - check for WU error conditions
|
2002-10-24 08:25:42 +00:00
|
|
|
//
|
2003-01-23 08:07:48 +00:00
|
|
|
// timeout_check
|
2002-11-07 19:31:34 +00:00
|
|
|
// -app appname
|
|
|
|
// [ -nerror n ] if get this many errors, bail on WU
|
|
|
|
// [ -ndet n ] if get this results w/o consensus, bail
|
|
|
|
// [ -nredundancy n ] try to get at least this many done results
|
|
|
|
// [ -asynch ] be asynchronous
|
2002-10-24 08:25:42 +00:00
|
|
|
|
2002-11-08 08:54:16 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2002-11-05 18:36:09 +00:00
|
|
|
#include <vector>
|
2002-11-07 19:31:34 +00:00
|
|
|
#include <unistd.h>
|
2002-11-05 18:36:09 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
2003-04-07 19:06:00 +00:00
|
|
|
#include "boinc_db.h"
|
2003-02-10 19:51:32 +00:00
|
|
|
#include "util.h"
|
2002-11-07 19:31:34 +00:00
|
|
|
#include "backend_lib.h"
|
|
|
|
#include "config.h"
|
2003-03-08 00:09:40 +00:00
|
|
|
#include "sched_util.h"
|
2002-11-05 18:36:09 +00:00
|
|
|
|
2003-03-12 21:37:50 +00:00
|
|
|
#define LOCKFILE "timeout_check.out"
|
2003-06-11 23:12:51 +00:00
|
|
|
#define PIDFILE "timeout_check.pid"
|
2002-12-12 23:01:26 +00:00
|
|
|
|
2002-11-05 18:36:09 +00:00
|
|
|
int max_errors = 999;
|
|
|
|
int max_done = 999;
|
2002-11-07 19:31:34 +00:00
|
|
|
int nredundancy = 0;
|
2002-11-05 18:36:09 +00:00
|
|
|
int startup_time;
|
2002-11-07 19:31:34 +00:00
|
|
|
CONFIG config;
|
|
|
|
R_RSA_PRIVATE_KEY key;
|
|
|
|
char app_name[256];
|
2002-11-05 18:36:09 +00:00
|
|
|
|
|
|
|
// The scheme for generating unique output filenames is as follows.
|
|
|
|
// If the original filename is of the form x__y,
|
|
|
|
// then y is replaced with a string of the form time_seqno,
|
|
|
|
// where "time" is when this program started up.
|
|
|
|
// NOTE: if you ever need to start up multiple copies of this,
|
|
|
|
// you'll need to add a PID in there somewhere.
|
|
|
|
//
|
|
|
|
// If the original filename doesn't have __, add a string
|
|
|
|
// of the form __time_seqno
|
|
|
|
|
|
|
|
void make_unique_name(char* name) {
|
|
|
|
char buf[256], *p;
|
|
|
|
static int seqno;
|
|
|
|
|
2002-11-07 19:31:34 +00:00
|
|
|
sprintf(buf, "%d_%d", startup_time, seqno++);
|
2002-11-05 18:36:09 +00:00
|
|
|
p = strstr(name, "__");
|
|
|
|
if (p) {
|
|
|
|
strcpy(p+2, buf);
|
|
|
|
} else {
|
2002-11-07 19:31:34 +00:00
|
|
|
strcat(name, "__");
|
2002-11-05 18:36:09 +00:00
|
|
|
strcat(name, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-07 19:31:34 +00:00
|
|
|
// convert a result's XML document to generate new output filenames.
|
|
|
|
// The input has the form
|
|
|
|
// <file_info>
|
|
|
|
// <name>xxx</name>
|
|
|
|
// ...
|
|
|
|
// </file_info>
|
|
|
|
// ...
|
|
|
|
// <result>
|
|
|
|
// <file_ref>
|
|
|
|
// <file_name>xxx</file_name>
|
|
|
|
// ...
|
|
|
|
// </file_ref>
|
|
|
|
// ...
|
|
|
|
// </result>
|
|
|
|
//
|
|
|
|
// Look for <name>...</name> elements within <file_info>
|
|
|
|
// and make a unique name based on it;
|
2002-11-05 18:36:09 +00:00
|
|
|
// apply the same conversion to the <file_name> element later on.
|
|
|
|
//
|
2002-11-07 19:31:34 +00:00
|
|
|
// TODO: this is ad-hoc. Would be nice to use some generic
|
|
|
|
// XML parsing routines, or XSLT or something.
|
|
|
|
//
|
|
|
|
int assign_new_names(char* in) {
|
|
|
|
char *p = in, *n1, *n2, *r;
|
|
|
|
char name[256], newname[256], element[256], buf[MAX_BLOB_SIZE];
|
2002-11-05 18:36:09 +00:00
|
|
|
int len;
|
|
|
|
|
2002-11-07 19:31:34 +00:00
|
|
|
// notice where the <result> is so we don't try to convert
|
|
|
|
// the result name
|
|
|
|
//
|
|
|
|
r = strstr(in, "<result>");
|
|
|
|
|
2002-11-05 18:36:09 +00:00
|
|
|
while (1) {
|
|
|
|
n1 = strstr(p, "<name>");
|
|
|
|
if (!n1) break;
|
2002-11-07 19:31:34 +00:00
|
|
|
|
|
|
|
if (n1 > r) break; // don't go past <result>
|
|
|
|
|
2002-11-05 18:36:09 +00:00
|
|
|
n1 += strlen("<name>");
|
|
|
|
n2 = strstr(p, "</name>");
|
|
|
|
if (!n2) {
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_CRITICAL, "assign_new_names(): malformed XML:\n%s", in);
|
2002-11-05 18:36:09 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
len = n2 - n1;
|
|
|
|
memcpy(name, n1, len);
|
|
|
|
name[len] = 0;
|
2002-11-07 19:31:34 +00:00
|
|
|
strcpy(newname, name);
|
|
|
|
make_unique_name(newname);
|
2002-11-05 18:36:09 +00:00
|
|
|
strcpy(buf, n2);
|
2002-11-07 19:31:34 +00:00
|
|
|
strcpy(n1, newname);
|
2002-11-05 18:36:09 +00:00
|
|
|
strcat(n1, buf);
|
2002-11-07 19:31:34 +00:00
|
|
|
|
|
|
|
// replace the name in the <file_name> element
|
|
|
|
//
|
|
|
|
sprintf(element, "<file_name>%s</file_name>", name);
|
|
|
|
n2 = strstr(n1, element);
|
|
|
|
if (!n2) {
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_CRITICAL, "assign_new_names(): no <file_name>:\n%s", in);
|
2002-11-07 19:31:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
strcpy(buf, n2+strlen(element));
|
|
|
|
sprintf(element, "<file_name>%s</file_name>", newname);
|
|
|
|
strcpy(n2, element);
|
|
|
|
strcat(n2, buf);
|
2002-11-05 18:36:09 +00:00
|
|
|
p = n1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
void handle_wu(DB_WORKUNIT& wu) {
|
2003-01-23 08:07:48 +00:00
|
|
|
vector<RESULT> results;
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_RESULT result;
|
2002-11-07 19:31:34 +00:00
|
|
|
int nerrors, ndone, retval;
|
2002-11-05 18:36:09 +00:00
|
|
|
unsigned int i, n;
|
2003-01-07 01:02:08 +00:00
|
|
|
char buf[256];
|
2003-01-23 08:07:48 +00:00
|
|
|
unsigned int now = time(0);
|
|
|
|
bool wu_error = false, all_over;
|
2002-10-24 08:25:42 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
// scan the results for the WU
|
2002-11-03 23:05:12 +00:00
|
|
|
//
|
2003-06-04 17:21:26 +00:00
|
|
|
sprintf(buf, "where workunitid=%d", wu.id);
|
|
|
|
while (!result.enumerate(buf)) {
|
2003-01-23 08:07:48 +00:00
|
|
|
results.push_back(result);
|
|
|
|
}
|
2002-12-18 19:07:03 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
nerrors = 0;
|
|
|
|
ndone = 0;
|
|
|
|
for (i=0; i<results.size(); i++) {
|
|
|
|
result = results[i];
|
|
|
|
|
|
|
|
switch (result.server_state) {
|
|
|
|
case RESULT_SERVER_STATE_IN_PROGRESS:
|
|
|
|
if (result.report_deadline < now) {
|
2003-03-13 23:30:45 +00:00
|
|
|
// clean up any incomplete uploads
|
|
|
|
result.file_delete_state = FILE_DELETE_READY;
|
2003-01-23 08:07:48 +00:00
|
|
|
result.server_state = RESULT_SERVER_STATE_OVER;
|
2003-06-11 23:12:50 +00:00
|
|
|
result.received_time = time(0);
|
2003-01-23 08:07:48 +00:00
|
|
|
result.outcome = RESULT_OUTCOME_NO_REPLY;
|
2003-06-04 17:21:26 +00:00
|
|
|
result.update();
|
2003-01-23 08:07:48 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RESULT_SERVER_STATE_OVER:
|
|
|
|
switch (result.outcome) {
|
|
|
|
case RESULT_OUTCOME_COULDNT_SEND:
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_NORMAL, "WU %s has couldn't-send result\n", wu.name);
|
2003-01-23 08:07:48 +00:00
|
|
|
wu.error_mask |= WU_ERROR_COULDNT_SEND_RESULT;
|
|
|
|
wu_error = true;
|
|
|
|
break;
|
|
|
|
case RESULT_OUTCOME_SUCCESS:
|
|
|
|
ndone++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
nerrors++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2002-11-07 19:31:34 +00:00
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
}
|
2002-11-07 19:31:34 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
// check for too many errors or too many results
|
|
|
|
//
|
|
|
|
if (nerrors > max_errors) {
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_NORMAL, "WU %s has too many errors\n", wu.name);
|
2003-01-23 08:07:48 +00:00
|
|
|
wu.error_mask |= WU_ERROR_TOO_MANY_ERROR_RESULTS;
|
|
|
|
wu_error = true;
|
|
|
|
}
|
|
|
|
if (ndone > max_done) {
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_NORMAL, "WU %s has too many answers\n", wu.name);
|
2003-01-23 08:07:48 +00:00
|
|
|
wu.error_mask |= WU_ERROR_TOO_MANY_RESULTS;
|
|
|
|
wu_error = true;
|
|
|
|
}
|
2002-11-03 23:05:12 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
// if this WU had an error, don't send any unsent results
|
|
|
|
//
|
|
|
|
if (wu_error) {
|
2002-11-03 23:05:12 +00:00
|
|
|
for (i=0; i<results.size(); i++) {
|
2002-11-05 18:36:09 +00:00
|
|
|
result = results[i];
|
2002-12-07 00:56:51 +00:00
|
|
|
if (result.server_state == RESULT_SERVER_STATE_UNSENT) {
|
2003-01-23 08:07:48 +00:00
|
|
|
result.server_state = RESULT_SERVER_STATE_OVER;
|
2003-06-11 23:12:50 +00:00
|
|
|
result.received_time = time(0);
|
2003-01-23 08:07:48 +00:00
|
|
|
result.outcome = RESULT_OUTCOME_DIDNT_NEED;
|
2003-06-04 17:21:26 +00:00
|
|
|
result.update();
|
2002-11-03 23:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
if (wu.assimilate_state == ASSIMILATE_INIT) {
|
2002-12-17 19:00:43 +00:00
|
|
|
wu.assimilate_state = ASSIMILATE_READY;
|
2002-11-03 23:05:12 +00:00
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
} else {
|
|
|
|
// If no error, generate new results if needed.
|
2002-11-05 18:36:09 +00:00
|
|
|
// Munge the XML of an existing result
|
|
|
|
// to create unique new output filenames.
|
2002-11-03 23:05:12 +00:00
|
|
|
//
|
2002-11-07 19:31:34 +00:00
|
|
|
if (nredundancy > ndone) {
|
|
|
|
n = nredundancy - ndone;
|
2003-06-11 23:12:51 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
for (i=0; i<n; i++) {
|
2002-11-07 19:31:34 +00:00
|
|
|
result = results[0];
|
|
|
|
make_unique_name(result.name);
|
|
|
|
initialize_result(result, wu);
|
|
|
|
remove_signatures(result.xml_doc_in);
|
|
|
|
assign_new_names(result.xml_doc_in);
|
|
|
|
add_signatures(result.xml_doc_in, key);
|
2003-06-04 17:21:26 +00:00
|
|
|
retval = result.insert();
|
2002-11-07 19:31:34 +00:00
|
|
|
if (retval) {
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_CRITICAL, "result.insert() %d\n", retval);
|
2002-11-07 19:31:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
}
|
2002-11-07 19:31:34 +00:00
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
// see if all results are OVER and result is assimilated;
|
|
|
|
// if so we don't need to check this WU ever again.
|
|
|
|
//
|
|
|
|
all_over = true;
|
|
|
|
for (i=0; i<results.size(); i++) {
|
|
|
|
result = results[i];
|
|
|
|
if (result.server_state != RESULT_SERVER_STATE_OVER) {
|
|
|
|
all_over = false;
|
|
|
|
break;
|
2002-11-03 23:05:12 +00:00
|
|
|
}
|
2002-11-07 19:31:34 +00:00
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
|
|
|
|
if (all_over && wu.assimilate_state == ASSIMILATE_DONE) {
|
|
|
|
wu.file_delete_state = FILE_DELETE_READY;
|
|
|
|
wu.timeout_check_time = 0;
|
|
|
|
} else {
|
|
|
|
wu.timeout_check_time = now + wu.delay_bound;
|
|
|
|
}
|
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
retval = wu.update();
|
2003-01-23 08:07:48 +00:00
|
|
|
if (retval) {
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_CRITICAL, "workunit.update() %d\n", retval);
|
2003-01-23 08:07:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool do_pass(APP& app) {
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_WORKUNIT wu;
|
|
|
|
char buf[256];
|
2003-01-23 08:07:48 +00:00
|
|
|
bool did_something = false;
|
|
|
|
|
2003-06-11 23:12:51 +00:00
|
|
|
check_stop_trigger();
|
2003-01-23 08:07:48 +00:00
|
|
|
// loop over WUs that are due to be checked
|
|
|
|
//
|
2003-06-04 17:21:26 +00:00
|
|
|
sprintf(buf, "where appid=%d and timeout_check_time>0 and timeout_check_time<%d", app.id, (int)time(0));
|
|
|
|
while (!wu.enumerate(buf)) {
|
2003-01-23 08:07:48 +00:00
|
|
|
did_something = true;
|
|
|
|
handle_wu(wu);
|
|
|
|
}
|
2002-11-07 19:31:34 +00:00
|
|
|
return did_something;
|
|
|
|
}
|
|
|
|
|
2002-12-24 03:03:45 +00:00
|
|
|
void main_loop(bool one_pass) {
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_APP app;
|
2002-11-07 19:31:34 +00:00
|
|
|
int retval;
|
2003-01-07 01:02:08 +00:00
|
|
|
char buf[256];
|
2002-11-07 19:31:34 +00:00
|
|
|
|
2003-01-09 07:24:27 +00:00
|
|
|
retval = boinc_db_open(config.db_name, config.db_passwd);
|
2002-11-07 19:31:34 +00:00
|
|
|
if (retval) {
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_CRITICAL, "boinc_db_open: %d\n", retval);
|
2002-11-07 19:31:34 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
sprintf(buf, "where name='%s'", app_name);
|
|
|
|
retval = app.lookup(buf);
|
2002-11-07 19:31:34 +00:00
|
|
|
if (retval) {
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_CRITICAL, "can't find app %s\n", app.name);
|
2002-11-07 19:31:34 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-06-11 23:12:51 +00:00
|
|
|
if (one_pass) {
|
|
|
|
do_pass(app);
|
|
|
|
} else {
|
|
|
|
while (1) {
|
|
|
|
if (!do_pass(app)) sleep(1);
|
|
|
|
}
|
2002-10-24 08:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
2002-11-07 19:31:34 +00:00
|
|
|
int i, retval;
|
2002-12-24 03:03:45 +00:00
|
|
|
bool asynch = false, one_pass=false;
|
2002-11-07 19:31:34 +00:00
|
|
|
char path[256];
|
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
check_stop_trigger();
|
2002-11-05 18:36:09 +00:00
|
|
|
startup_time = time(0);
|
|
|
|
for (i=1; i<argc; i++) {
|
2002-11-07 19:31:34 +00:00
|
|
|
if (!strcmp(argv[i], "-app")) {
|
|
|
|
strcpy(app_name, argv[++i]);
|
|
|
|
} else if (!strcmp(argv[i], "-nerror")) {
|
2002-11-05 18:36:09 +00:00
|
|
|
max_errors = atoi(argv[++i]);
|
|
|
|
} else if (!strcmp(argv[i], "-ndet")) {
|
|
|
|
max_done = atoi(argv[++i]);
|
|
|
|
} else if (!strcmp(argv[i], "-asynch")) {
|
|
|
|
asynch = true;
|
2002-12-24 03:03:45 +00:00
|
|
|
} else if (!strcmp(argv[i], "-one_pass")) {
|
|
|
|
one_pass = true;
|
2003-06-11 23:12:48 +00:00
|
|
|
} else if (!strcmp(argv[i], "-d")) {
|
|
|
|
set_debug_level(atoi(argv[++i]));
|
2002-11-05 18:36:09 +00:00
|
|
|
} else if (!strcmp(argv[i], "-nredundancy")) {
|
|
|
|
nredundancy = atoi(argv[++i]);;
|
|
|
|
}
|
|
|
|
}
|
2003-02-10 19:51:32 +00:00
|
|
|
|
|
|
|
retval = config.parse_file();
|
|
|
|
if (retval) {
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_CRITICAL, "can't read config file\n");
|
2003-02-10 19:51:32 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
sprintf(path, "%s/upload_private", config.key_dir);
|
|
|
|
retval = read_key_file(path, key);
|
|
|
|
if (retval) {
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_CRITICAL, "can't read key\n");
|
2003-02-10 19:51:32 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2002-11-05 18:36:09 +00:00
|
|
|
if (asynch) {
|
2002-12-03 18:57:40 +00:00
|
|
|
if (fork()) {
|
|
|
|
exit(0);
|
2002-11-05 18:36:09 +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)) {
|
2003-06-11 23:12:53 +00:00
|
|
|
write_log(MSG_NORMAL, "Another copy of timeout_check is already running\n");
|
2003-02-27 19:29:48 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-11 23:12:51 +00:00
|
|
|
write_pid_file(PIDFILE);
|
2003-06-11 23:12:54 +00:00
|
|
|
write_log(MSG_NORMAL, "Starting\n");
|
|
|
|
|
2003-06-11 23:12:51 +00:00
|
|
|
install_sigint_handler();
|
2003-02-27 19:29:48 +00:00
|
|
|
|
2002-12-24 03:03:45 +00:00
|
|
|
main_loop(one_pass);
|
2002-10-24 08:25:42 +00:00
|
|
|
}
|