2005-01-20 23:22:22 +00:00
|
|
|
// Berkeley Open Infrastructure for Network Computing
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2005 University of California
|
2003-07-08 21:30:47 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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.
|
2003-07-08 21:30:47 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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.
|
2003-01-07 08:11:16 +00:00
|
|
|
//
|
2005-01-20 23:22:22 +00:00
|
|
|
// 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
|
2003-01-07 08:11:16 +00:00
|
|
|
|
2004-09-09 23:45:45 +00:00
|
|
|
// This is a framework for an assimilator.
|
|
|
|
// You need to link with with an (application-specific) function
|
|
|
|
// assimilate_handler()
|
|
|
|
// in order to make a complete program.
|
2004-05-25 19:20:45 +00:00
|
|
|
//
|
|
|
|
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstring>
|
|
|
|
#include <cstdlib>
|
2002-12-17 19:00:43 +00:00
|
|
|
#include <unistd.h>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <ctime>
|
2003-01-23 08:07:48 +00:00
|
|
|
#include <vector>
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2003-04-07 19:06:00 +00:00
|
|
|
#include "boinc_db.h"
|
2002-12-17 19:00:43 +00:00
|
|
|
#include "parse.h"
|
2003-02-10 19:51:32 +00:00
|
|
|
#include "util.h"
|
2007-05-11 16:30:13 +00:00
|
|
|
#include "str_util.h"
|
|
|
|
|
2003-08-15 00:45:25 +00:00
|
|
|
#include "sched_config.h"
|
2003-03-08 00:09:40 +00:00
|
|
|
#include "sched_util.h"
|
2004-04-08 08:15:23 +00:00
|
|
|
#include "sched_msgs.h"
|
2003-01-23 08:07:48 +00:00
|
|
|
#include "assimilate_handler.h"
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2004-06-30 18:17:21 +00:00
|
|
|
using std::vector;
|
|
|
|
|
2003-06-20 01:31:03 +00:00
|
|
|
#define LOCKFILE "assimilator.out"
|
|
|
|
#define PIDFILE "assimilator.pid"
|
2003-02-10 19:51:32 +00:00
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
SCHED_CONFIG config;
|
2004-11-26 23:33:11 +00:00
|
|
|
bool update_db = true;
|
2004-11-30 00:28:19 +00:00
|
|
|
bool noinsert = false;
|
2004-11-26 23:33:11 +00:00
|
|
|
|
2005-07-26 22:14:55 +00:00
|
|
|
int wu_id_modulus=0, wu_id_remainder=0;
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2004-12-14 00:57:03 +00:00
|
|
|
#define SLEEP_INTERVAL 10
|
2003-12-31 23:09:21 +00:00
|
|
|
|
2006-10-22 00:42:44 +00:00
|
|
|
int sleep_interval = SLEEP_INTERVAL;
|
|
|
|
|
2005-01-03 17:18:32 +00:00
|
|
|
int one_pass_N_WU=0;
|
|
|
|
|
2003-01-23 08:07:48 +00:00
|
|
|
// assimilate all WUs that need it
|
2002-12-17 19:00:43 +00:00
|
|
|
// return nonzero if did anything
|
|
|
|
//
|
2003-01-23 08:07:48 +00:00
|
|
|
bool do_pass(APP& app) {
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_WORKUNIT wu;
|
|
|
|
DB_RESULT canonical_result, result;
|
2003-08-15 20:35:44 +00:00
|
|
|
bool did_something = false;
|
2004-05-13 18:18:22 +00:00
|
|
|
char buf[256];
|
2005-07-26 22:14:55 +00:00
|
|
|
char mod_clause[256];
|
2004-10-04 23:59:51 +00:00
|
|
|
int retval;
|
2004-11-30 00:08:11 +00:00
|
|
|
int num_assimilated=0;
|
2004-06-24 21:00:13 +00:00
|
|
|
|
2004-05-03 19:30:01 +00:00
|
|
|
check_stop_daemons();
|
2003-03-08 00:09:40 +00:00
|
|
|
|
2005-07-26 22:14:55 +00:00
|
|
|
if (wu_id_modulus) {
|
|
|
|
sprintf(mod_clause, " and workunit.id %% %d = %d ",
|
|
|
|
wu_id_modulus, wu_id_remainder
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
strcpy(mod_clause, "");
|
|
|
|
}
|
|
|
|
|
2005-07-17 19:52:44 +00:00
|
|
|
sprintf(buf,
|
2005-07-26 22:14:55 +00:00
|
|
|
"where appid=%d and assimilate_state=%d %s limit %d",
|
|
|
|
app.id, ASSIMILATE_READY, mod_clause,
|
2005-07-17 19:52:44 +00:00
|
|
|
one_pass_N_WU ? one_pass_N_WU : 1000
|
|
|
|
);
|
2003-06-04 17:21:26 +00:00
|
|
|
while (!wu.enumerate(buf)) {
|
2003-08-15 20:35:44 +00:00
|
|
|
vector<RESULT> results; // must be inside while()!
|
2004-11-26 23:33:11 +00:00
|
|
|
|
2004-11-29 22:26:34 +00:00
|
|
|
// for testing purposes, pretend we did nothing
|
|
|
|
//
|
|
|
|
if (update_db) {
|
|
|
|
did_something = true;
|
|
|
|
}
|
2003-01-07 22:49:42 +00:00
|
|
|
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_DEBUG,
|
2005-07-26 22:14:55 +00:00
|
|
|
"[%s] assimilating boinc WU %d; state=%d\n", wu.name, wu.id, wu.assimilate_state
|
2003-08-12 20:58:24 +00:00
|
|
|
);
|
2003-01-07 22:49:42 +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);
|
|
|
|
if (result.id == wu.canonical_resultid) {
|
|
|
|
canonical_result = result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-20 20:47:25 +00:00
|
|
|
retval = assimilate_handler(wu, results, canonical_result);
|
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
2004-12-20 20:47:25 +00:00
|
|
|
"[%s] handler returned error %d; exiting\n", wu.name, retval
|
|
|
|
);
|
2005-03-30 05:15:54 +00:00
|
|
|
exit(retval);
|
2004-12-20 20:47:25 +00:00
|
|
|
}
|
2003-01-23 08:07:48 +00:00
|
|
|
|
2004-11-29 22:26:34 +00:00
|
|
|
if (update_db) {
|
|
|
|
sprintf(
|
|
|
|
buf, "assimilate_state=%d, transition_time=%d",
|
|
|
|
ASSIMILATE_DONE, (int)time(0)
|
|
|
|
);
|
|
|
|
retval = wu.update_field(buf);
|
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
2004-11-29 22:26:34 +00:00
|
|
|
"[%s] update failed: %d\n", wu.name, retval
|
|
|
|
);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
2004-11-30 00:50:46 +00:00
|
|
|
|
|
|
|
num_assimilated++;
|
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
2004-11-30 00:50:46 +00:00
|
|
|
|
2004-11-15 23:46:50 +00:00
|
|
|
if (did_something) {
|
|
|
|
boinc_db.commit_transaction();
|
|
|
|
}
|
2004-11-30 00:08:11 +00:00
|
|
|
|
2004-12-14 00:57:03 +00:00
|
|
|
if (num_assimilated) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL,
|
2004-11-30 00:08:11 +00:00
|
|
|
"Assimilated %d workunits.\n", num_assimilated
|
2004-12-14 00:57:03 +00:00
|
|
|
);
|
|
|
|
}
|
2004-11-30 00:08:11 +00:00
|
|
|
|
2002-12-17 19:00:43 +00:00
|
|
|
return did_something;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
int retval;
|
2007-04-18 20:49:58 +00:00
|
|
|
bool one_pass = false;
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_APP app;
|
2002-12-17 19:00:43 +00:00
|
|
|
int i;
|
2003-01-07 01:02:08 +00:00
|
|
|
char buf[256];
|
2002-12-17 19:00:43 +00:00
|
|
|
|
2004-05-03 19:30:01 +00:00
|
|
|
check_stop_daemons();
|
2002-12-17 19:00:43 +00:00
|
|
|
for (i=1; i<argc; i++) {
|
2007-04-18 20:49:58 +00:00
|
|
|
if (!strcmp(argv[i], "-one_pass_N_WU")) {
|
2005-01-03 17:18:32 +00:00
|
|
|
one_pass_N_WU = atoi(argv[++i]);
|
|
|
|
one_pass = true;
|
2006-10-22 00:42:44 +00:00
|
|
|
} else if (!strcmp(argv[i], "-sleep_interval")) {
|
|
|
|
sleep_interval = atoi(argv[++i]);
|
2002-12-17 19:00:43 +00:00
|
|
|
} else if (!strcmp(argv[i], "-one_pass")) {
|
|
|
|
one_pass = true;
|
2003-06-11 23:36:40 +00:00
|
|
|
} else if (!strcmp(argv[i], "-d")) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.set_debug_level(atoi(argv[++i]));
|
2002-12-18 01:34:51 +00:00
|
|
|
} else if (!strcmp(argv[i], "-app")) {
|
|
|
|
strcpy(app.name, argv[++i]);
|
2004-11-29 22:26:34 +00:00
|
|
|
} else if (!strcmp(argv[i], "-dont_update_db")) {
|
2004-12-14 00:57:03 +00:00
|
|
|
// This option is for testing your assimilator. When set,
|
|
|
|
// it ensures that the assimilator does not actually modify
|
|
|
|
// the assimilate_state of the workunits, so you can run
|
|
|
|
// your assimilator over and over again without affecting
|
|
|
|
// your project.
|
2004-11-29 22:26:34 +00:00
|
|
|
update_db = false;
|
2004-11-30 00:28:19 +00:00
|
|
|
} else if (!strcmp(argv[i], "-noinsert")) {
|
2004-12-14 00:57:03 +00:00
|
|
|
// This option is also for testing and is used to
|
|
|
|
// prevent the inserting of results into the *backend*
|
|
|
|
// (as opposed to the boinc) DB.
|
2004-11-30 00:28:19 +00:00
|
|
|
noinsert = true;
|
2005-07-26 22:14:55 +00:00
|
|
|
} else if (!strcmp(argv[i], "-mod")) {
|
|
|
|
wu_id_modulus = atoi(argv[++i]);
|
|
|
|
wu_id_remainder = atoi(argv[++i]);
|
2002-12-17 19:00:43 +00:00
|
|
|
} else {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "Unrecognized arg: %s\n", argv[i]);
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-26 22:14:55 +00:00
|
|
|
if (wu_id_modulus) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_DEBUG, "Using mod'ed WU enumeration. modulus = %d remainder = %d\n",
|
2005-07-26 22:14:55 +00:00
|
|
|
wu_id_modulus, wu_id_remainder);
|
|
|
|
}
|
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
retval = config.parse_file("..");
|
2002-12-17 19:00:43 +00:00
|
|
|
if (retval) {
|
2007-05-11 16:30:13 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
|
|
|
"Can't parse ../config.xml: %s\n", boincerror(retval)
|
|
|
|
);
|
2002-12-17 19:00:43 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL, "Starting\n");
|
2003-07-01 00:39:54 +00:00
|
|
|
|
2004-01-15 23:53:13 +00:00
|
|
|
retval = boinc_db.open(config.db_name, config.db_host, config.db_user, config.db_passwd);
|
2002-12-18 01:34:51 +00:00
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "Can't open DB\n");
|
2002-12-18 01:34:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-04 17:21:26 +00:00
|
|
|
sprintf(buf, "where name='%s'", app.name);
|
|
|
|
retval = app.lookup(buf);
|
2002-12-18 01:34:51 +00:00
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "Can't find app\n");
|
2002-12-18 01:34:51 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-12-31 23:09:21 +00:00
|
|
|
install_stop_signal_handler();
|
2005-07-17 19:52:44 +00:00
|
|
|
while (1) {
|
|
|
|
if (!do_pass(app)) {
|
|
|
|
if (one_pass) break;
|
2006-10-22 00:42:44 +00:00
|
|
|
sleep(sleep_interval);
|
2002-12-17 19:00:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-12-06 22:41:19 +00:00
|
|
|
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_7841370789 = "$Id$";
|