// The contents of this file are subject to the BOINC 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 // http://boinc.berkeley.edu/license_1.0.txt // // 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 // under the License. // // The Original Code is the Berkeley Open Infrastructure for Network Computing. // // The Initial Developer of the Original Code is the SETI@home project. // Portions created by the SETI@home project are Copyright (C) 2002 // University of California at Berkeley. All Rights Reserved. // // Contributor(s): // // TO DO: // 1) Need to add support for compressed zip of archived files that may contain max X number // of items (similiar to db_dump.C). // 2) Need to escape XML output // db_purge: purges workunit and result records that are no longer needed from // the database #include #include #include #include #include #include #include #include "boinc_db.h" #include "util.h" #include "parse.h" #include "sched_config.h" #include "sched_util.h" SCHED_CONFIG config; FILE *wu_stream; FILE *re_stream; int openDBconnection() { int retval=0; //open DB connection retval = boinc_db.open(config.db_name, config.db_host, config.db_user, config.db_passwd); return retval; } int closeFile(FILE *stream) { return fclose(stream); } //open archive files for purged workunits with the name- archive_workunitTIMESTAMP.xml //open archive files for purged results with the name- archive_resultTIMESTAMP.xml FILE* open_archive(char* filename){ int retval=0; char path[256]; FILE* f; sprintf(path,"../archives/%s_%d.xml", filename, time(0)); if ((f = fopen( wu_archive_file_name,"a")) == NULL) { fprintf(stdout,"DB_PURGE: Can't open archive file %s\n", wu_archive_file_name); return 0; } fprintf(f, "\n" ); return f; } int archive_result(DB_RESULT result) { fprintf(re_stream, "\n" " %d\n", result.id ); escape_xml(stderr_out); fprintf( re_stream, " %d\n" " %d\n" " %d\n" " %d\n" " %d\n" " %d\n" " %d\n" " %d\n" " %d\n" " %d\n" " %s\n" " %.15e\n" " %s\n" " %s\n" " %s\n" " %d\n" " %d\n" " %d\n" " %.15e\n" " %.15e\n" " %f\n" " %d\n" " %d\n" " %d\n" " %d\n" " %d\n", result.create_time, result.workunitid, result.server_state, result.outcome, result.client_state, result.hostid, result.userid, result.report_deadline, result.sent_time, result.received_time, result.name, result.cpu_time, result.xml_doc_in, result.xml_doc_out, result.stderr_out, result.batch, result.file_delete_state, result.validate_state, result.claimed_credit, result.granted_credit, result.opaque, result.random, result.app_version_num, result.appid, result.exit_status, result.teamid ); fprintf(re_stream, "\n" ); return 0; } int archive_wu(DB_WORKUNIT wu) { fprintf(wu_stream, "\n" " %d\n", wu.id ); fprintf(wu_stream, " %d\n" " %d\n" " %s\n" " %s\n" " %d\n" " %.15e\n" " %.15e\n" " %.15e\n" " %.15e\n" " %d\n" " %d\n" " %.15e\n" " %d\n" " %d\n" " %d\n" " %d\n" " %d\n" " %d\n" " %f\n" " %d\n" " %d\n" " %d\n" " %d\n" " %d\n" " %s\n", wu.create_time, wu.appid, wu.name, wu.xml_doc, wu.batch, wu.rsc_fpops_est, wu.rsc_fpops_bound, wu.rsc_memory_bound, wu.rsc_disk_bound, wu.need_validate, wu.canonical_resultid, wu.canonical_credit, wu.transition_time, wu.delay_bound, wu.error_mask, wu.file_delete_state, wu.assimilate_state, wu.workseq_next, wu.opaque, wu.min_quorum, wu.target_nresults, wu.max_error_results, wu.max_total_results, wu.max_success_results, wu.result_template_file ); fprintf(wu_stream, "\n" ); return 0; } void cleanUpResources() { //close archive files closeFile(wu_stream); closeFile(re_stream); //close DB connection boinc_db.close(); } int purge_and_archive_results(DB_WORKUNIT& wu, int& number_results) { int retval= 0; DB_RESULT result; char buf[256]; number_results=0; sprintf(buf, "where workunitid=%d", wu.id); while (!result.enumerate(buf)) { retval= archive_result(result); if (retval) return retval; retval= result.delete_from_db(); if (retval) return retval; number_results++; } return 0; } int main(int argc, char** argv) { int purged_workunits= 0, purged_results= 0; int retval= 0; retval= config.parse_file(".."); if (retval) { fprintf(stdout, "DB_PURGE: Can't parse config file\n"); exit(1); } fprintf(stdout, "DB_PURGE: Starting DB Purger\n"); retval= openDBconnection(); if (retval) { fprintf(stdout, "DB_PURGE: Can't open DB\n"); exit(1); } fprintf(stdout, "DB_PURGE: Opened DB\n"); mkdir("../archives", 0777); wu_stream = open_archive("wuarchive"); re_stream = open_archive("resultarchive"); if (!wu_stream || !re_stream) { fprintf(stdout, "Can't open archives\n"); exit(1); } bool did_something = false; DB_WORKUNIT wu; char buf[256]; // select all workunits with file_delete_state='DONE' // sprintf(buf, "where file_delete_state=%d limit 1000", FILE_DELETE_DONE); while (!wu.enumerate(buf)) { did_something = true; retval = purge_and_archive_results(wu, n); purged_results += n; retval= archive_wu(wu); if (retval) { fprintf(stdout,"DB_PURGE: Failed to write to XML file workunit:%d\n", wu.id); exit(1); } fprintf(stdout,"DB_PURGE: Archived workunit [%d] to a file\n", wu.id); //purge workunit from DB retval= wu.delete_from_db(); if (retval) { fprintf(stdout,"DB_PURGE: Can't delete workunit [%d] from database:%d\n", wu.id, retval); exit(1); } fprintf(stdout,"DB_PURGE: Purged workunit [%d] from database\n", wu.id); purged_workunits++; } if (!did_something) { fprintf(stdout, "DB_PURGE: Did not do anything\n"); exit(1); } fprintf(stdout, "DB_PURGE: Archived %d workunits and %d results\n",purged_workunits,purged_results); }