2003-07-01 20:37:09 +00:00
|
|
|
// The contents of this file are subject to the BOINC Public License
|
2002-12-03 18:57:40 +00:00
|
|
|
// 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-07-01 20:37:09 +00:00
|
|
|
// http://boinc.berkeley.edu/license_1.0.txt
|
2003-07-02 20:57:59 +00:00
|
|
|
//
|
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-07-02 20:57:59 +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-07-01 20:37:09 +00:00
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
2003-07-02 20:57:59 +00:00
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
2002-12-03 18:57:40 +00:00
|
|
|
// Contributor(s):
|
2003-07-01 20:37:09 +00:00
|
|
|
//
|
2002-12-03 18:57:40 +00:00
|
|
|
|
2002-10-18 16:52:28 +00:00
|
|
|
// make_work
|
|
|
|
// -wu_name name
|
|
|
|
// -result_template filename
|
2003-09-02 21:16:55 +00:00
|
|
|
// [ -cushion n ] // make work if fewer than this many unsent results
|
|
|
|
// [ -max_wus n ] // don't make work if more than this many WUs
|
|
|
|
// [ -min_quorum n ]
|
|
|
|
// [ -target_nresults n ]
|
|
|
|
// [ -max_error_results n ]
|
|
|
|
// [ -max_total_results n ]
|
|
|
|
// [ -max_success_results n ]
|
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
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
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"
|
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;
|
|
|
|
int min_quorum = 2;
|
|
|
|
int target_nresults = 5;
|
|
|
|
int max_error_results = 10;
|
|
|
|
int max_total_results = 20;
|
|
|
|
int max_success_results = 10;
|
2003-08-15 23:54:50 +00:00
|
|
|
|
2002-10-14 23:10:12 +00:00
|
|
|
char wu_name[256], result_template_file[256];
|
|
|
|
|
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
|
|
|
|
) {
|
2003-12-07 18:58:08 +00:00
|
|
|
char buf[MEDIUM_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)) {
|
|
|
|
replace_element(xml_doc + (p - buf),"<name>","</name>",new_filename);
|
|
|
|
}
|
|
|
|
} else if (parse_str(p, "<file_name>", temp, sizeof(temp))) {
|
|
|
|
if(!strcmp(filename, temp)) {
|
|
|
|
replace_element(xml_doc + (p - buf),"<file_name>","</file_name>",new_filename);
|
|
|
|
}
|
|
|
|
} else if (parse_str(p, "<url>", temp, sizeof(temp))) {
|
|
|
|
if(!strcmp(temp, download_path)) {
|
|
|
|
replace_element(xml_doc + (p - buf),"<url>","</url>",new_download_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
log_messages.printf(SchedMessages::CRITICAL, "can't count results\n");
|
|
|
|
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) {
|
|
|
|
log_messages.printf(SchedMessages::CRITICAL, "can't count workunits\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2002-10-09 04:56:41 +00:00
|
|
|
void make_work() {
|
2003-09-02 21:16:55 +00:00
|
|
|
SCHED_CONFIG config;
|
2002-12-20 02:12:27 +00:00
|
|
|
char * p;
|
2003-08-16 01:02:49 +00:00
|
|
|
int retval, start_time=time(0);
|
2003-12-07 18:58:08 +00:00
|
|
|
char keypath[256], result_template[MEDIUM_BLOB_SIZE];
|
|
|
|
char file_name[256], buf[MEDIUM_BLOB_SIZE], pathname[256];
|
2003-01-02 23:12:05 +00:00
|
|
|
char new_file_name[256], new_pathname[256], command[256];
|
2003-12-07 18:58:08 +00:00
|
|
|
char starting_xml[MEDIUM_BLOB_SIZE], new_buf[MEDIUM_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) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::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) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::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) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::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) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::CRITICAL, "can't read key\n");
|
2002-10-09 04:56:41 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-06-19 22:55:50 +00:00
|
|
|
|
2002-11-21 00:48:50 +00:00
|
|
|
retval = read_filename(result_template_file, result_template);
|
|
|
|
if (retval) {
|
2003-07-02 02:02:18 +00:00
|
|
|
log_messages.printf(SchedMessages::CRITICAL, "can't open result template\n");
|
2002-11-21 00:48:50 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2003-01-07 01:02:08 +00:00
|
|
|
while (1) {
|
2003-06-20 01:31:03 +00:00
|
|
|
check_stop_trigger();
|
|
|
|
|
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) {
|
|
|
|
log_messages.printf(SchedMessages::NORMAL, "Reached max_wus = %d\n", max_wus);
|
|
|
|
exit(0);
|
2002-10-09 04:56:41 +00:00
|
|
|
}
|
2003-08-16 01:02:49 +00:00
|
|
|
if (unsent_results > cushion) {
|
2002-10-09 04:56:41 +00:00
|
|
|
sleep(1);
|
|
|
|
continue;
|
|
|
|
}
|
2002-10-14 23:10:12 +00:00
|
|
|
|
2003-08-15 23:54:50 +00:00
|
|
|
strcpy(buf, starting_xml);
|
|
|
|
p = strtok(buf, "\n");
|
|
|
|
strcpy(file_name, "");
|
|
|
|
|
|
|
|
// make new copies of all the WU's input files
|
2003-01-02 23:12:05 +00:00
|
|
|
//
|
2003-08-15 23:54:50 +00:00
|
|
|
while (p) {
|
|
|
|
if (parse_str(p, "<name>", file_name, sizeof(file_name))) {
|
|
|
|
sprintf(
|
|
|
|
new_file_name, "%s__%d_%d", file_name, start_time, seqno++
|
|
|
|
);
|
|
|
|
sprintf(pathname, "%s/%s", config.download_dir, file_name);
|
|
|
|
sprintf(
|
|
|
|
new_pathname, "%s/%s",config.download_dir, new_file_name
|
|
|
|
);
|
|
|
|
sprintf(command,"ln %s %s", pathname, new_pathname);
|
|
|
|
log_messages.printf(SchedMessages::DEBUG, "executing command: %s\n", command);
|
2003-09-05 21:26:21 +00:00
|
|
|
retval = system(command);
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(
|
|
|
|
SchedMessages::CRITICAL, "system() error %d\n", retval
|
|
|
|
);
|
2003-08-15 23:54:50 +00:00
|
|
|
perror(command);
|
|
|
|
exit(1);
|
2003-01-02 23:12:05 +00:00
|
|
|
}
|
2003-08-15 23:54:50 +00:00
|
|
|
strcpy(new_buf, starting_xml);
|
|
|
|
replace_file_name(
|
|
|
|
new_buf, file_name, new_file_name, config.download_url
|
|
|
|
);
|
|
|
|
strcpy(wu.xml_doc, new_buf);
|
2003-01-02 23:12:05 +00:00
|
|
|
}
|
2003-08-15 23:54:50 +00:00
|
|
|
p = strtok(0, "\n");
|
2002-10-18 16:52:28 +00:00
|
|
|
}
|
2003-11-08 00:23:06 +00:00
|
|
|
|
|
|
|
// set various fields for new WU (all others are copied)
|
|
|
|
//
|
2003-08-15 23:54:50 +00:00
|
|
|
wu.id = 0;
|
|
|
|
wu.create_time = time(0);
|
2003-11-08 00:23:06 +00:00
|
|
|
sprintf(wu.name, "wu_%d_%d", start_time, seqno++);
|
2003-09-02 21:16:55 +00:00
|
|
|
wu.min_quorum = min_quorum;
|
|
|
|
wu.target_nresults = target_nresults;
|
|
|
|
wu.max_error_results = max_error_results;
|
|
|
|
wu.max_total_results = max_total_results;
|
|
|
|
wu.max_success_results = max_success_results;
|
2003-11-08 00:23:06 +00:00
|
|
|
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;
|
2003-08-15 23:54:50 +00:00
|
|
|
strcpy(wu.result_template, result_template);
|
|
|
|
process_result_template_upload_url_only(wu.result_template, config.upload_url);
|
|
|
|
retval = wu.insert();
|
2003-11-08 00:23:06 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(SchedMessages::CRITICAL,
|
|
|
|
"Failed to created WU, error %d; exiting\n", retval
|
|
|
|
);
|
|
|
|
exit(retval);
|
|
|
|
}
|
2003-09-05 21:26:21 +00:00
|
|
|
wu.id = boinc_db.insert_id();
|
2003-08-15 23:54:50 +00:00
|
|
|
log_messages.printf(SchedMessages::DEBUG, "[%s] Created new WU\n", wu.name);
|
2002-10-09 04:56:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv) {
|
|
|
|
bool asynch = false;
|
|
|
|
int i;
|
|
|
|
|
2003-03-08 00:09:40 +00:00
|
|
|
check_stop_trigger();
|
2002-10-09 04:56:41 +00:00
|
|
|
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]);
|
|
|
|
} else if (!strcmp(argv[i], "-result_template")) {
|
|
|
|
strcpy(result_template_file, 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]);
|
2003-09-02 21:16:55 +00:00
|
|
|
} else if (!strcmp(argv[i], "-min_quorum")) {
|
|
|
|
min_quorum = atoi(argv[++i]);
|
|
|
|
} else if (!strcmp(argv[i], "-target_nresults")) {
|
|
|
|
target_nresults = atoi(argv[++i]);
|
|
|
|
} else if (!strcmp(argv[i], "-max_error_results")) {
|
|
|
|
max_error_results = atoi(argv[++i]);
|
|
|
|
} else if (!strcmp(argv[i], "-max_total_results")) {
|
|
|
|
max_total_results = atoi(argv[++i]);
|
|
|
|
} else if (!strcmp(argv[i], "-max_success_results")) {
|
|
|
|
max_success_results = atoi(argv[++i]);
|
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(result_template_file , "need -result_template_file");
|
|
|
|
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-07-08 21:30:47 +00:00
|
|
|
// // Call lock_file after fork(), because file locks are not always inherited
|
|
|
|
// if (lock_file(LOCKFILE)) {
|
|
|
|
// log_messages.printf(SchedMessages::NORMAL, "Another copy of make_work is already running\n");
|
|
|
|
// exit(1);
|
|
|
|
// }
|
|
|
|
// write_pid_file(PIDFILE);
|
2003-09-02 21:16:55 +00:00
|
|
|
log_messages.printf(
|
|
|
|
SchedMessages::NORMAL,
|
|
|
|
"Starting: min_quorum=%d target_nresults=%d max_error_results=%d max_total_results=%d max_success_results=%d\n",
|
|
|
|
min_quorum, target_nresults, max_error_results, max_total_results, max_success_results
|
|
|
|
);
|
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
|
|
|
}
|