// This file is part of BOINC.
// http://boinc.berkeley.edu
// Copyright (C) 2008 University of California
//
// BOINC 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 3 of the License, or (at your option) any later version.
//
// BOINC 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.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see .
#include "config.h"
#ifdef _USING_FCGI_
#include "boinc_fcgi.h"
#else
#include
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "boinc_db.h"
#include "crypt.h"
#include "error_numbers.h"
#include "md5_file.h"
#include "parse.h"
#include "str_util.h"
#include "str_replace.h"
#include "common_defs.h"
#include "filesys.h"
#include "sched_util.h"
#include "util.h"
#include "process_input_template.h"
#include "backend_lib.h"
using std::string;
static struct random_init {
random_init() {
srand48(getpid() + time(0));
}
} random_init;
int read_file(FILE* f, char* buf, int len) {
int n = fread(buf, 1, len, f);
buf[n] = 0;
return 0;
}
int read_filename(const char* path, char* buf, int len) {
int retval;
#ifndef _USING_FCGI_
FILE* f = fopen(path, "r");
#else
FCGI_FILE *f=FCGI::fopen(path, "r");
#endif
if (!f) return -1;
retval = read_file(f, buf, len);
fclose(f);
return retval;
}
// initialize an about-to-be-created result, given its WU
//
static void initialize_result(DB_RESULT& result, WORKUNIT& wu) {
result.id = 0;
result.create_time = time(0);
result.workunitid = wu.id;
result.server_state = RESULT_SERVER_STATE_UNSENT;
result.hostid = 0;
result.report_deadline = 0;
result.sent_time = 0;
result.received_time = 0;
result.client_state = 0;
result.cpu_time = 0;
strcpy(result.xml_doc_out, "");
strcpy(result.stderr_out, "");
result.outcome = RESULT_OUTCOME_INIT;
result.file_delete_state = ASSIMILATE_INIT;
result.validate_state = VALIDATE_STATE_INIT;
result.claimed_credit = 0;
result.granted_credit = 0;
result.appid = wu.appid;
result.priority = wu.priority;
result.batch = wu.batch;
}
int create_result_ti(
TRANSITIONER_ITEM& ti,
char* result_template_filename,
char* result_name_suffix,
R_RSA_PRIVATE_KEY& key,
SCHED_CONFIG& config_loc,
char* query_string,
// if nonzero, write value list here; else do insert
int priority_increase
) {
WORKUNIT wu;
// copy relevant fields from TRANSITIONER_ITEM to WORKUNIT
//
strcpy(wu.name, ti.name);
wu.id = ti.id;
wu.appid = ti.appid;
wu.priority = ti.priority;
wu.batch = ti.batch;
return create_result(
wu,
result_template_filename,
result_name_suffix,
key,
config_loc,
query_string,
priority_increase
);
}
// Create a new result for the given WU.
// This is called from:
// - the transitioner
// - the scheduler (for assigned jobs)
//
int create_result(
WORKUNIT& wu,
char* result_template_filename,
char* result_name_suffix,
R_RSA_PRIVATE_KEY& key,
SCHED_CONFIG& config_loc,
char* query_string,
// if nonzero, write value list here; else do insert
int priority_increase
) {
DB_RESULT result;
char base_outfile_name[256];
char result_template[BLOB_SIZE];
int retval;
result.clear();
initialize_result(result, wu);
result.priority += priority_increase;
sprintf(result.name, "%s_%s", wu.name, result_name_suffix);
sprintf(base_outfile_name, "%s_", result.name);
retval = read_filename(
result_template_filename, result_template, sizeof(result_template)
);
if (retval) {
fprintf(stderr,
"Failed to read result template file '%s': %s\n",
result_template_filename, boincerror(retval)
);
return retval;
}
retval = process_result_template(
result_template, key, base_outfile_name, config_loc
);
if (retval) {
fprintf(stderr,
"process_result_template() error: %s\n", boincerror(retval)
);
}
if (strlen(result_template) > sizeof(result.xml_doc_in)-1) {
fprintf(stderr,
"result XML doc is too long: %d bytes, max is %d\n",
(int)strlen(result_template), (int)sizeof(result.xml_doc_in)-1
);
return ERR_BUFFER_OVERFLOW;
}
strlcpy(result.xml_doc_in, result_template, sizeof(result.xml_doc_in));
result.random = lrand48();
if (query_string) {
result.db_print_values(query_string);
} else {
retval = result.insert();
if (retval) {
fprintf(stderr, "result.insert(): %s\n", boincerror(retval));
return retval;
}
}
return 0;
}
// make sure a WU's input files are actually there
//
int check_files(char** infiles, int ninfiles, SCHED_CONFIG& config_loc) {
int i;
char path[MAXPATHLEN];
for (i=0; i sizeof(wu.result_template_file)-1) {
fprintf(stderr,
"result template filename is too big: %d bytes, max is %d\n",
(int)strlen(result_template_filename),
(int)sizeof(wu.result_template_file)-1
);
return ERR_BUFFER_OVERFLOW;
}
strlcpy(wu.result_template_file, result_template_filename, sizeof(wu.result_template_file));
if (wu.rsc_fpops_est == 0) {
fprintf(stderr, "no rsc_fpops_est given; can't create job\n");
return ERR_NO_OPTION;
}
if (wu.rsc_fpops_bound == 0) {
fprintf(stderr, "no rsc_fpops_bound given; can't create job\n");
return ERR_NO_OPTION;
}
if (wu.rsc_disk_bound == 0) {
fprintf(stderr, "no rsc_disk_bound given; can't create job\n");
return ERR_NO_OPTION;
}
if (wu.target_nresults == 0) {
fprintf(stderr, "no target_nresults given; can't create job\n");
return ERR_NO_OPTION;
}
if (wu.max_error_results == 0) {
fprintf(stderr, "no max_error_results given; can't create job\n");
return ERR_NO_OPTION;
}
if (wu.max_total_results == 0) {
fprintf(stderr, "no max_total_results given; can't create job\n");
return ERR_NO_OPTION;
}
if (wu.max_success_results == 0) {
fprintf(stderr, "no max_success_results given; can't create job\n");
return ERR_NO_OPTION;
}
if (wu.max_success_results > wu.max_total_results) {
fprintf(stderr, "max_success_results > max_total_results; can't create job\n");
return ERR_INVALID_PARAM;
}
if (wu.max_error_results > wu.max_total_results) {
fprintf(stderr, "max_error_results > max_total_results; can't create job\n");
return ERR_INVALID_PARAM;
}
if (wu.target_nresults > wu.max_success_results) {
fprintf(stderr, "target_nresults > max_success_results; can't create job\n");
return ERR_INVALID_PARAM;
}
if (wu.transitioner_flags & TRANSITION_NONE) {
wu.transition_time = INT_MAX;
} else {
wu.transition_time = time(0);
}
if (wu.id) {
retval = wu.update();
if (retval) {
fprintf(stderr,
"create_work: workunit.update() %s\n", boincerror(retval)
);
return retval;
}
} else {
retval = wu.insert();
if (retval) {
fprintf(stderr,
"create_work: workunit.insert() %s\n", boincerror(retval)
);
return retval;
}
wu.id = boinc_db.insert_id();
}
return 0;
}
// STUFF RELATED TO FILE UPLOAD/DOWNLOAD
int get_file_xml(
const char* file_name, vector urls,
double max_nbytes,
double report_deadline,
bool generate_upload_certificate,
R_RSA_PRIVATE_KEY& key,
char* out
) {
char buf[8192];
sprintf(out,
"\n"
" file_xfer\n"
"\n"
"\n"
" file_xfer\n"
" 0\n"
"\n"
"\n"
" %s\n"
" %.0f\n",
file_name,
max_nbytes
);
for (unsigned int i=0; i%s\n", urls[i]);
strcat(out, buf);
}
sprintf(buf,
"\n"
"\n"
" upload_%s\n"
" file_xfer\n"
"\n"
"\n"
" upload_%s\n"
" upload_%s\n"
" \n"
" %s\n"
" \n"
" %f\n"
"\n",
file_name,
file_name,
file_name,
file_name,
report_deadline
);
strcat(out, buf);
if (generate_upload_certificate) {
add_signatures(out, key);
}
return 0;
}
int create_get_file_msg(
int host_id, const char* file_name, vector urls,
double max_nbytes,
double report_deadline,
bool generate_upload_certificate,
R_RSA_PRIVATE_KEY& key
) {
DB_MSG_TO_HOST mth;
int retval;
mth.clear();
mth.create_time = time(0);
mth.hostid = host_id;
strcpy(mth.variety, "file_xfer");
mth.handled = false;
get_file_xml(
file_name, urls, max_nbytes, report_deadline,
generate_upload_certificate, key,
mth.xml
);
retval = mth.insert();
if (retval) {
fprintf(stderr, "msg_to_host.insert(): %s\n", boincerror(retval));
return retval;
}
return 0;
}
int put_file_xml(
const char* file_name,
vector urls, const char* md5, double nbytes,
double report_deadline,
char* out
) {
char buf[8192];
sprintf(out,
"\n"
" file_xfer\n"
"\n"
"\n"
" file_xfer\n"
" 0\n"
"\n"
"\n"
" %s\n",
file_name
);
for (unsigned int i=0; i%s\n", urls[i]);
strcat(out, buf);
}
sprintf(buf,
" %s\n"
" %.0f\n"
" \n"
"\n"
"\n"
" download_%s\n"
" file_xfer\n"
" \n"
" %s\n"
" \n"
"\n"
"\n"
" download_%s\n"
" download_%s\n"
" %f\n"
"\n",
md5,
nbytes,
file_name,
file_name,
file_name,
file_name,
report_deadline
);
strcat(out, buf);
return 0;
}
int create_put_file_msg(
int host_id, const char* file_name,
vector urls, const char* md5, double nbytes,
double report_deadline
) {
DB_MSG_TO_HOST mth;
int retval;
mth.clear();
mth.create_time = time(0);
mth.hostid = host_id;
strcpy(mth.variety, "file_xfer");
mth.handled = false;
put_file_xml(file_name, urls, md5, nbytes, report_deadline, mth.xml);
retval = mth.insert();
if (retval) {
fprintf(stderr, "msg_to_host.insert(): %s\n", boincerror(retval));
return retval;
}
return 0;
}
int delete_file_xml(const char* file_name, char* out) {
sprintf(out, "%s\n", file_name);
return 0;
}
int create_delete_file_msg(int host_id, const char* file_name) {
DB_MSG_TO_HOST mth;
int retval;
mth.clear();
mth.create_time = time(0);
mth.hostid = host_id;
mth.handled = false;
delete_file_xml(file_name, mth.xml);
sprintf(mth.variety, "delete_file");
retval = mth.insert();
if (retval) {
fprintf(stderr, "msg_to_host.insert(): %s\n", boincerror(retval));
return retval;
}
return 0;
}
// cancel jobs in a range of workunit IDs
//
int cancel_jobs(int min_id, int max_id) {
DB_WORKUNIT wu;
DB_RESULT result;
char set_clause[256], where_clause[256];
int retval;
sprintf(set_clause, "server_state=%d, outcome=%d",
RESULT_SERVER_STATE_OVER, RESULT_OUTCOME_DIDNT_NEED
);
sprintf(where_clause, "server_state=%d and workunitid >=%d and workunitid<= %d",
RESULT_SERVER_STATE_UNSENT, min_id, max_id
);
retval = result.update_fields_noid(set_clause, where_clause);
if (retval) return retval;
sprintf(set_clause, "error_mask=error_mask|%d, transition_time=%d",
WU_ERROR_CANCELLED, (int)(time(0))
);
sprintf(where_clause, "id>=%d and id<=%d", min_id, max_id);
retval = wu.update_fields_noid(set_clause, where_clause);
if (retval) return retval;
return 0;
}
// cancel a particular job
//
int cancel_job(DB_WORKUNIT& wu) {
DB_RESULT result;
char set_clause[256], where_clause[256];
int retval;
// cancel unsent results
//
sprintf(set_clause, "server_state=%d, outcome=%d",
RESULT_SERVER_STATE_OVER, RESULT_OUTCOME_DIDNT_NEED
);
sprintf(where_clause, "server_state=%d and workunitid=%d",
RESULT_SERVER_STATE_UNSENT, wu.id
);
retval = result.update_fields_noid(set_clause, where_clause);
if (retval) return retval;
// cancel the workunit
//
sprintf(set_clause, "error_mask=error_mask|%d, transition_time=%d",
WU_ERROR_CANCELLED, (int)(time(0))
);
retval = wu.update_field(set_clause);
if (retval) return retval;
return 0;
}
// return the sum of user quotas
//
int get_total_quota(double& total) {
DB_USER_SUBMIT us;
total = 0;
while (1) {
int retval = us.enumerate("");
if (retval == ERR_DB_NOT_FOUND) break;
if (retval) {
return retval;
}
total += us.quota;
}
return 0;
}
// return total project FLOPS (based on recent credit)
//
int get_project_flops(double& total) {
DB_APP_VERSION av;
char buf[256];
sprintf(buf, "expavg_credit_time > %f", dtime() - 30*86400);
total = 0;
while (1) {
int retval = av.enumerate(buf);
if (retval == ERR_DB_NOT_FOUND) break;
if (retval) {
return retval;
}
total += av.expavg_credit;
}
total /= COBBLESTONE_SCALE;
return 0;
}
// update user (job submitter) priority fields given the assumption
// that they did flop_count FLOPS of computing
//
int adjust_user_priority(
DB_USER_SUBMIT& us,
double flop_count,
// this should be wu.rsc_fpops_est * app.min_avg_pfc
// to account for systematic errors in rsc_fpops_est
double total_quota,
double project_flops
) {
char set_clause[256], where_clause[256];
double runtime = flop_count / project_flops;
double share = us.quota / total_quota;
runtime /= share;
us.logical_start_time += runtime;
sprintf(set_clause, "logical_start_time=%f", us.logical_start_time);
sprintf(where_clause, "user_id=%d", us.user_id);
return us.update_fields_noid(set_clause, where_clause);
}
const char *BOINC_RCSID_b5f8b10eb5 = "$Id$";