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-02 20:57:59 +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-02 20:57:59 +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-07-01 20:37:09 +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
|
2002-12-03 18:57:40 +00:00
|
|
|
|
2002-10-18 16:52:28 +00:00
|
|
|
// make_work
|
|
|
|
// -wu_name name
|
2005-07-17 19:52:44 +00:00
|
|
|
// [ -cushion n ] // make work if fewer than N unsent results
|
|
|
|
// [ -max_wus n ] // don't make work if more than N total WUs
|
|
|
|
// [ -one_pass ] // quit after one pass
|
2002-10-14 23:10:12 +00:00
|
|
|
//
|
2003-12-06 01:49:30 +00:00
|
|
|
// Create WU and result records as needed to maintain a pool of work
|
|
|
|
// (for testing purposes).
|
2002-10-18 16:52:28 +00:00
|
|
|
// Clones the WU of the given name.
|
2002-10-09 04:56:41 +00:00
|
|
|
//
|
|
|
|
|
2005-11-21 18:34:44 +00:00
|
|
|
#include "config.h"
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2005-07-17 19:52:44 +00:00
|
|
|
#include <errno.h>
|
2002-10-09 04:56:41 +00:00
|
|
|
#include <unistd.h>
|
2004-07-13 13:54:09 +00:00
|
|
|
#include <ctime>
|
2002-10-09 04:56:41 +00:00
|
|
|
|
2003-04-07 19:06:00 +00:00
|
|
|
#include "boinc_db.h"
|
2002-10-14 23:10:12 +00:00
|
|
|
#include "crypt.h"
|
2003-02-10 19:51:32 +00:00
|
|
|
#include "util.h"
|
2002-10-14 23:10:12 +00:00
|
|
|
#include "backend_lib.h"
|
2003-08-15 00:45:25 +00:00
|
|
|
#include "sched_config.h"
|
2002-12-20 02:12:27 +00:00
|
|
|
#include "parse.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"
|
2002-10-09 04:56:41 +00:00
|
|
|
|
2003-02-10 19:51:32 +00:00
|
|
|
#define LOCKFILE "make_work.out"
|
2003-06-20 01:31:03 +00:00
|
|
|
#define PIDFILE "make_work.pid"
|
2002-10-09 04:56:41 +00:00
|
|
|
|
2003-08-16 01:02:49 +00:00
|
|
|
int max_wus = 0;
|
2003-09-02 21:16:55 +00:00
|
|
|
int cushion = 300;
|
2005-07-17 19:52:44 +00:00
|
|
|
bool one_pass = false;
|
2003-08-15 23:54:50 +00:00
|
|
|
|
2004-04-13 21:31:55 +00:00
|
|
|
char wu_name[256];
|
2002-10-14 23:10:12 +00:00
|
|
|
|
2003-01-02 23:12:05 +00:00
|
|
|
// edit a WU XML doc, replacing one filename by another
|
|
|
|
// (should appear twice, within <file_info> and <file_ref>)
|
|
|
|
// Also patch the download URL (redundant)
|
|
|
|
//
|
|
|
|
void replace_file_name(
|
|
|
|
char* xml_doc, char* filename, char* new_filename, char* download_url
|
|
|
|
) {
|
2004-05-13 18:18:22 +00:00
|
|
|
char buf[LARGE_BLOB_SIZE], temp[256], download_path[256],
|
2003-01-02 23:12:05 +00:00
|
|
|
new_download_path[256];
|
|
|
|
char * p;
|
2003-06-19 22:55:50 +00:00
|
|
|
|
2003-01-02 23:12:05 +00:00
|
|
|
sprintf(download_path,"%s/%s", download_url, filename);
|
|
|
|
sprintf(new_download_path,"%s/%s", download_url, new_filename);
|
|
|
|
strcpy(buf, xml_doc);
|
|
|
|
p = strtok(buf,"\n");
|
|
|
|
while (p) {
|
|
|
|
if (parse_str(p, "<name>", temp, sizeof(temp))) {
|
|
|
|
if(!strcmp(filename, temp)) {
|
2004-09-13 18:05:54 +00:00
|
|
|
replace_element_contents(
|
|
|
|
xml_doc + (p - buf),"<name>","</name>", new_filename
|
|
|
|
);
|
2003-01-02 23:12:05 +00:00
|
|
|
}
|
|
|
|
} else if (parse_str(p, "<file_name>", temp, sizeof(temp))) {
|
|
|
|
if(!strcmp(filename, temp)) {
|
2004-09-13 18:05:54 +00:00
|
|
|
replace_element_contents(
|
|
|
|
xml_doc+(p-buf), "<file_name>","</file_name>", new_filename
|
|
|
|
);
|
2003-01-02 23:12:05 +00:00
|
|
|
}
|
|
|
|
} else if (parse_str(p, "<url>", temp, sizeof(temp))) {
|
|
|
|
if(!strcmp(temp, download_path)) {
|
2004-09-13 18:05:54 +00:00
|
|
|
replace_element_contents(
|
|
|
|
xml_doc + (p - buf),"<url>","</url>", new_download_path
|
|
|
|
);
|
2003-01-02 23:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p = strtok(0, "\n");
|
2002-12-20 02:12:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-06 01:49:30 +00:00
|
|
|
int count_results(char* query) {
|
2003-08-16 01:02:49 +00:00
|
|
|
int n;
|
|
|
|
DB_RESULT result;
|
2003-12-06 01:49:30 +00:00
|
|
|
int retval = result.count(n, query);
|
2003-08-16 01:02:49 +00:00
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "can't count results\n");
|
2003-08-16 01:02:49 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2003-12-06 01:49:30 +00:00
|
|
|
int count_workunits(const char* query="") {
|
2003-08-16 01:02:49 +00:00
|
|
|
int n;
|
|
|
|
DB_WORKUNIT workunit;
|
|
|
|
int retval = workunit.count(n, const_cast<char*>(query));
|
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "can't count workunits\n");
|
2003-08-16 01:02:49 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2005-01-29 23:29:54 +00:00
|
|
|
void make_new_wu(
|
|
|
|
DB_WORKUNIT& wu, char* starting_xml, int start_time, int& seqno,
|
|
|
|
SCHED_CONFIG& config
|
|
|
|
) {
|
|
|
|
char file_name[256], buf[LARGE_BLOB_SIZE], pathname[256];
|
2005-08-11 17:41:08 +00:00
|
|
|
char new_file_name[256], new_pathname[256];
|
2005-01-29 23:29:54 +00:00
|
|
|
char new_buf[LARGE_BLOB_SIZE];
|
|
|
|
char * p;
|
|
|
|
int retval;
|
|
|
|
|
|
|
|
strcpy(buf, starting_xml);
|
|
|
|
p = strtok(buf, "\n");
|
|
|
|
strcpy(file_name, "");
|
|
|
|
|
|
|
|
// make new hard links to the WU's input files
|
|
|
|
// (don't actually copy files)
|
|
|
|
//
|
|
|
|
while (p) {
|
|
|
|
if (parse_str(p, "<name>", file_name, sizeof(file_name))) {
|
|
|
|
sprintf(
|
|
|
|
new_file_name, "%s__%d_%d", file_name, start_time, seqno++
|
|
|
|
);
|
|
|
|
dir_hier_path(
|
2005-09-23 21:09:00 +00:00
|
|
|
file_name, config.download_dir, config.uldl_dir_fanout,
|
2005-01-29 23:29:54 +00:00
|
|
|
pathname
|
|
|
|
);
|
|
|
|
dir_hier_path(
|
2005-09-23 21:09:00 +00:00
|
|
|
new_file_name, config.download_dir, config.uldl_dir_fanout,
|
2005-01-29 23:29:54 +00:00
|
|
|
new_pathname, true
|
|
|
|
);
|
2005-07-17 19:52:44 +00:00
|
|
|
retval = link(pathname, new_pathname);
|
2005-01-29 23:29:54 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(
|
2005-09-26 23:28:48 +00:00
|
|
|
SCHED_MSG_LOG::MSG_CRITICAL, "link() error %d\n", retval
|
2005-01-29 23:29:54 +00:00
|
|
|
);
|
2005-07-17 19:52:44 +00:00
|
|
|
fprintf(stderr, "link: %d %d\n", errno, retval);
|
|
|
|
perror("link");
|
2005-01-29 23:29:54 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
strcpy(new_buf, starting_xml);
|
|
|
|
replace_file_name(
|
|
|
|
new_buf, file_name, new_file_name, config.download_url
|
|
|
|
);
|
|
|
|
strcpy(wu.xml_doc, new_buf);
|
|
|
|
}
|
|
|
|
p = strtok(0, "\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// set various fields for new WU (all others are copied)
|
|
|
|
//
|
|
|
|
wu.id = 0;
|
|
|
|
wu.create_time = time(0);
|
|
|
|
sprintf(wu.name, "wu_%d_%d", start_time, seqno++);
|
|
|
|
wu.need_validate = false;
|
|
|
|
wu.canonical_resultid = 0;
|
|
|
|
wu.canonical_credit = 0;
|
|
|
|
wu.transition_time = time(0);
|
|
|
|
wu.error_mask = 0;
|
|
|
|
wu.file_delete_state = FILE_DELETE_INIT;
|
|
|
|
wu.assimilate_state = ASSIMILATE_INIT;
|
|
|
|
retval = wu.insert();
|
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL,
|
2005-01-29 23:29:54 +00:00
|
|
|
"Failed to created WU, error %d; exiting\n", retval
|
|
|
|
);
|
|
|
|
exit(retval);
|
|
|
|
}
|
|
|
|
wu.id = boinc_db.insert_id();
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_DEBUG, "[%s] Created new WU\n", wu.name);
|
2005-01-29 23:29:54 +00:00
|
|
|
}
|
|
|
|
|
2002-10-09 04:56:41 +00:00
|
|
|
void make_work() {
|
2003-09-02 21:16:55 +00:00
|
|
|
SCHED_CONFIG config;
|
2003-08-16 01:02:49 +00:00
|
|
|
int retval, start_time=time(0);
|
2004-04-13 21:31:55 +00:00
|
|
|
char keypath[256];
|
2005-01-29 23:29:54 +00:00
|
|
|
char buf[LARGE_BLOB_SIZE];
|
|
|
|
char starting_xml[LARGE_BLOB_SIZE];
|
2002-10-14 23:10:12 +00:00
|
|
|
R_RSA_PRIVATE_KEY key;
|
2003-06-04 17:21:26 +00:00
|
|
|
DB_WORKUNIT wu;
|
2003-01-02 23:12:05 +00:00
|
|
|
int seqno = 0;
|
2003-06-19 22:55:50 +00:00
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
retval = config.parse_file("..");
|
2002-10-09 04:56:41 +00:00
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "can't read config file\n");
|
2002-10-09 04:56:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
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-10-09 04:56:41 +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-10-09 04:56:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-06-04 17:21:26 +00:00
|
|
|
sprintf(buf, "where name='%s'", wu_name);
|
|
|
|
retval = wu.lookup(buf);
|
2002-10-14 23:10:12 +00:00
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "can't find wu %s\n", wu_name);
|
2002-10-14 23:10:12 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2003-09-05 21:26:21 +00:00
|
|
|
strcpy(starting_xml, wu.xml_doc);
|
2003-06-19 22:55:50 +00:00
|
|
|
|
2002-10-14 23:10:12 +00:00
|
|
|
sprintf(keypath, "%s/upload_private", config.key_dir);
|
|
|
|
retval = read_key_file(keypath, key);
|
2002-10-09 04:56:41 +00:00
|
|
|
if (retval) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_CRITICAL, "can't read key\n");
|
2002-10-09 04:56:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-19 22:55:50 +00:00
|
|
|
|
2003-01-07 01:02:08 +00:00
|
|
|
while (1) {
|
2004-05-03 19:30:01 +00:00
|
|
|
check_stop_daemons();
|
2003-06-20 01:31:03 +00:00
|
|
|
|
2003-12-06 01:49:30 +00:00
|
|
|
sprintf(buf,
|
|
|
|
"where appid=%d and server_state=%d",
|
|
|
|
wu.appid, RESULT_SERVER_STATE_UNSENT
|
|
|
|
);
|
|
|
|
int unsent_results = count_results(buf);
|
2003-08-16 01:02:49 +00:00
|
|
|
int total_wus = count_workunits();
|
|
|
|
if (max_wus && total_wus >= max_wus) {
|
2005-09-26 23:28:48 +00:00
|
|
|
log_messages.printf(SCHED_MSG_LOG::MSG_NORMAL, "Reached max_wus = %d\n", max_wus);
|
2003-08-16 01:02:49 +00:00
|
|
|
exit(0);
|
2002-10-09 04:56:41 +00:00
|
|
|
}
|
2004-12-06 22:41:19 +00:00
|
|
|
log_messages.printf(
|
2005-09-26 23:28:48 +00:00
|
|
|
SCHED_MSG_LOG::MSG_DEBUG, "unsent: %d cushion: %d\n",
|
2004-12-06 22:41:19 +00:00
|
|
|
unsent_results, cushion
|
|
|
|
);
|
2003-08-16 01:02:49 +00:00
|
|
|
if (unsent_results > cushion) {
|
2005-01-29 23:29:54 +00:00
|
|
|
sleep(10);
|
2002-10-09 04:56:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
2002-10-14 23:10:12 +00:00
|
|
|
|
2005-02-23 19:44:59 +00:00
|
|
|
// decide how many WUs to create based on cushion
|
|
|
|
// and (if present) max_wus
|
|
|
|
//
|
2005-01-29 23:29:54 +00:00
|
|
|
int nwus = (cushion-unsent_results)/wu.target_nresults+1;
|
2005-02-23 19:44:59 +00:00
|
|
|
if (max_wus) {
|
|
|
|
int mwlimit = max_wus - total_wus;
|
|
|
|
if (nwus > mwlimit) nwus = mwlimit;
|
|
|
|
}
|
|
|
|
|
2005-01-29 23:29:54 +00:00
|
|
|
for (int i=0; i<nwus; i++) {
|
|
|
|
make_new_wu(wu, starting_xml, start_time, seqno, config);
|
2002-10-18 16:52:28 +00:00
|
|
|
}
|
2003-11-08 00:23:06 +00:00
|
|
|
|
2005-07-17 19:52:44 +00:00
|
|
|
if (one_pass) break;
|
2005-02-23 19:44:59 +00:00
|
|
|
// wait a while for the transitioner to make results
|
|
|
|
//
|
2005-01-29 23:29:54 +00:00
|
|
|
sleep(60);
|
2002-10-09 04:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
bool asynch = false;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i=1; i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-asynch")) {
|
|
|
|
asynch = true;
|
2002-10-14 23:10:12 +00:00
|
|
|
} else if (!strcmp(argv[i], "-cushion")) {
|
|
|
|
cushion = atoi(argv[++i]);
|
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-10-14 23:10:12 +00:00
|
|
|
} else if (!strcmp(argv[i], "-wu_name")) {
|
|
|
|
strcpy(wu_name, argv[++i]);
|
2003-08-16 01:02:49 +00:00
|
|
|
} else if (!strcmp(argv[i], "-max_wus")) {
|
|
|
|
max_wus = atoi(argv[++i]);
|
2005-07-17 19:52:44 +00:00
|
|
|
} else if (!strcmp(argv[i], "-one_pass")) {
|
|
|
|
one_pass = true;
|
2005-02-23 19:44:59 +00:00
|
|
|
} else {
|
|
|
|
log_messages.printf(
|
2005-09-26 23:28:48 +00:00
|
|
|
SCHED_MSG_LOG::MSG_CRITICAL, "unknown argument: %s\n", argv[i]
|
2005-02-23 19:44:59 +00:00
|
|
|
);
|
2002-10-09 04:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
2005-07-17 19:52:44 +00:00
|
|
|
check_stop_daemons();
|
2002-10-09 04:56:41 +00:00
|
|
|
|
2003-08-18 22:34:53 +00:00
|
|
|
#define CHKARG(x,m) do { if (!(x)) { fprintf(stderr, "make_work: bad command line: "m"\n"); exit(1); } } while (0)
|
|
|
|
#define CHKARG_STR(v,m) CHKARG(strlen(v),m)
|
|
|
|
CHKARG_STR(wu_name , "need -wu_name");
|
|
|
|
#undef CHKARG
|
|
|
|
#undef CHKARG_STR
|
2002-10-14 23:10:12 +00:00
|
|
|
|
2002-10-09 04:56:41 +00:00
|
|
|
if (asynch) {
|
2003-02-27 19:29:48 +00:00
|
|
|
if (fork()) {
|
|
|
|
exit(0);
|
2002-10-09 04:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
2003-02-27 19:29:48 +00:00
|
|
|
|
2003-09-02 21:16:55 +00:00
|
|
|
log_messages.printf(
|
2005-09-26 23:28:48 +00:00
|
|
|
SCHED_MSG_LOG::MSG_NORMAL,
|
2004-04-13 21:31:55 +00:00
|
|
|
"Starting: cushion %d, max_wus %d\n",
|
|
|
|
cushion, max_wus
|
2003-09-02 21:16:55 +00:00
|
|
|
);
|
2003-12-31 23:09:21 +00:00
|
|
|
install_stop_signal_handler();
|
2003-04-01 07:13:43 +00:00
|
|
|
|
2003-04-01 03:28:37 +00:00
|
|
|
srand48(getpid() + time(0));
|
2003-02-27 19:29:48 +00:00
|
|
|
make_work();
|
2002-10-09 04:56:41 +00:00
|
|
|
}
|
2004-12-08 00:40:19 +00:00
|
|
|
|
2005-01-02 18:29:53 +00:00
|
|
|
const char *BOINC_RCSID_d24265dc7f = "$Id$";
|