2012-01-20 20:21:29 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2012 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-02-15 04:40:50 +00:00
|
|
|
// vdad - volunteer data archival daemon
|
|
|
|
//
|
|
|
|
// Enumerates files needing updating from the DB.
|
|
|
|
// Creates the corresponding tree of META_CHUNKs, CHUNKs,
|
|
|
|
// and VDA_CHUNK_HOSTs.
|
|
|
|
// Calls the recovery routines to initiate transfers,
|
|
|
|
// update the DB, etc.
|
|
|
|
|
2012-01-25 03:35:50 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2012-02-15 04:40:50 +00:00
|
|
|
#include <vector>
|
2012-02-24 22:55:11 +00:00
|
|
|
#include <set>
|
2012-02-15 04:40:50 +00:00
|
|
|
|
|
|
|
using std::vector;
|
2012-02-24 22:55:11 +00:00
|
|
|
using std::set;
|
2012-01-25 03:35:50 +00:00
|
|
|
|
2012-01-23 05:03:52 +00:00
|
|
|
#include "boinc_db.h"
|
2012-02-21 20:55:09 +00:00
|
|
|
#include "sched_config.h"
|
2012-02-29 07:22:59 +00:00
|
|
|
#include "sched_util.h"
|
|
|
|
#include "md5_file.h"
|
2012-01-20 20:21:29 +00:00
|
|
|
|
2012-02-15 04:40:50 +00:00
|
|
|
#include "error_numbers.h"
|
2012-01-23 08:04:41 +00:00
|
|
|
#include "util.h"
|
2012-02-15 04:40:50 +00:00
|
|
|
#include "filesys.h"
|
2012-01-23 08:04:41 +00:00
|
|
|
|
2012-01-24 18:23:18 +00:00
|
|
|
#include "vda_lib.h"
|
|
|
|
|
|
|
|
// return the name of a file created by Jerasure's encoder
|
|
|
|
//
|
|
|
|
// encoder creates files with names of the form
|
|
|
|
// Coding/fname_k01.ext
|
|
|
|
// Coding/fname_m01.ext
|
|
|
|
//
|
2012-02-24 21:13:00 +00:00
|
|
|
// Assume there's no extension
|
|
|
|
//
|
2012-01-24 18:23:18 +00:00
|
|
|
void encoder_filename(
|
|
|
|
const char* base, const char* ext, CODING& c, int i, char* buf
|
|
|
|
) {
|
|
|
|
int ndigits = 1;
|
|
|
|
if (c.m > 9) ndigits = 2;
|
|
|
|
else if (c.m > 99) ndigits = 3;
|
|
|
|
else if (c.m > 999) ndigits = 4;
|
|
|
|
int j;
|
|
|
|
char ch;
|
|
|
|
if (i >= c.n) {
|
|
|
|
j = i-c.n + 1;
|
2012-01-25 03:35:50 +00:00
|
|
|
ch = 'm';
|
2012-01-24 18:23:18 +00:00
|
|
|
} else {
|
|
|
|
j = i+1;
|
2012-01-25 03:35:50 +00:00
|
|
|
ch = 'k';
|
2012-01-24 18:23:18 +00:00
|
|
|
}
|
2012-02-24 21:13:00 +00:00
|
|
|
sprintf(buf, "%s_%c%0*d.%s", base, ch, ndigits, j, ext);
|
2012-01-24 18:23:18 +00:00
|
|
|
}
|
|
|
|
|
2012-02-29 07:22:59 +00:00
|
|
|
#define DATA_FILENAME "data.vda"
|
2012-02-24 21:13:00 +00:00
|
|
|
|
2012-01-23 21:59:12 +00:00
|
|
|
// encode a meta-chunk.
|
2012-02-29 07:22:59 +00:00
|
|
|
// precondition: "dir" contains a file "data.vda".
|
2012-01-23 21:59:12 +00:00
|
|
|
// postcondition: dir contains
|
|
|
|
// a subdir Coding with encoded chunks
|
2012-02-29 07:22:59 +00:00
|
|
|
// subdirs 0/ .. m/
|
|
|
|
// each containing a symbolic link "data.vda" to the corresponding chunk
|
2012-01-23 21:59:12 +00:00
|
|
|
//
|
2012-02-15 04:40:50 +00:00
|
|
|
// The size of these chunks is returned in "size"
|
|
|
|
//
|
2012-03-11 01:51:07 +00:00
|
|
|
int META_CHUNK::encode() {
|
2012-01-24 18:23:18 +00:00
|
|
|
char cmd[1024];
|
|
|
|
sprintf(cmd,
|
2012-02-29 07:22:59 +00:00
|
|
|
"cd %s; /mydisks/b/users/boincadm/vda_test/encoder %s %d %d cauchy_good 32 1024 500000",
|
2012-03-11 01:51:07 +00:00
|
|
|
dir, DATA_FILENAME, coding.n, coding.k
|
2012-01-24 18:23:18 +00:00
|
|
|
);
|
2012-01-26 08:39:51 +00:00
|
|
|
printf("%s\n", cmd);
|
2012-01-24 18:23:18 +00:00
|
|
|
int s = system(cmd);
|
2012-01-25 03:35:50 +00:00
|
|
|
if (WIFEXITED(s)) {
|
2012-03-11 01:51:07 +00:00
|
|
|
int st = WEXITSTATUS(s);
|
|
|
|
if (st != 32) return -1; // encoder returns 32 for some reason
|
2012-01-25 03:35:50 +00:00
|
|
|
}
|
2012-01-24 18:23:18 +00:00
|
|
|
|
2012-02-24 21:13:00 +00:00
|
|
|
// make symlinks
|
|
|
|
//
|
2012-03-11 01:51:07 +00:00
|
|
|
for (int i=0; i<coding.m; i++) {
|
2012-02-29 07:22:59 +00:00
|
|
|
char enc_filename[1024], target_path[1024];
|
2012-01-25 03:35:50 +00:00
|
|
|
char dir_name[1024], link_name[1024];
|
2012-03-11 01:51:07 +00:00
|
|
|
encoder_filename("data", "vda", coding, i, enc_filename);
|
2012-01-25 03:35:50 +00:00
|
|
|
sprintf(target_path, "%s/Coding/%s", dir, enc_filename);
|
2012-02-29 07:22:59 +00:00
|
|
|
sprintf(dir_name, "%s/%d", dir, i);
|
2012-01-25 03:35:50 +00:00
|
|
|
int retval = mkdir(dir_name, 0777);
|
|
|
|
if (retval) {
|
|
|
|
perror("mkdir");
|
|
|
|
return retval;
|
|
|
|
}
|
2012-02-29 07:22:59 +00:00
|
|
|
sprintf(link_name, "%s/%s", dir_name, DATA_FILENAME);
|
2012-01-25 03:35:50 +00:00
|
|
|
retval = symlink(target_path, link_name);
|
|
|
|
if (retval) {
|
2012-02-29 07:22:59 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL,
|
|
|
|
"encode(): symlink %s %s failed\n", target_path, link_name
|
|
|
|
);
|
2012-01-25 03:35:50 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2012-02-15 04:40:50 +00:00
|
|
|
if (i == 0) {
|
2012-03-11 01:51:07 +00:00
|
|
|
file_size(target_path, child_size);
|
2012-02-15 04:40:50 +00:00
|
|
|
}
|
2012-01-24 18:23:18 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-11 01:51:07 +00:00
|
|
|
int META_CHUNK::decode() {
|
|
|
|
char cmd[1024];
|
|
|
|
sprintf(cmd,
|
|
|
|
"cd %s; /mydisks/b/users/boincadm/vda_test/decoder %s",
|
|
|
|
dir, DATA_FILENAME
|
|
|
|
);
|
|
|
|
printf("%s\n", cmd);
|
|
|
|
int s = system(cmd);
|
|
|
|
if (WIFEXITED(s)) {
|
|
|
|
int st = WEXITSTATUS(s);
|
|
|
|
if (st != 32) return -1; // encoder returns 32 for some reason
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int DATA_UNIT::delete_file() {
|
|
|
|
char path[1024], buf[1024];
|
|
|
|
sprintf(path, "%s/data.vda", dir);
|
|
|
|
size_t n = readlink(path, buf, 1024);
|
|
|
|
buf[n] = 0;
|
|
|
|
return unlink(buf);
|
|
|
|
}
|
|
|
|
|
2012-02-24 22:55:11 +00:00
|
|
|
CHUNK::CHUNK(META_CHUNK* mc, double s, int index) {
|
|
|
|
parent = mc;
|
|
|
|
present_on_server = true;
|
|
|
|
size = s;
|
|
|
|
if (strlen(parent->name)) {
|
|
|
|
sprintf(name, "%s.%d", parent->name, index);
|
|
|
|
} else {
|
|
|
|
sprintf(name, "%d", index);
|
|
|
|
}
|
2012-03-11 01:51:07 +00:00
|
|
|
sprintf(dir, "%s/%d", mc->dir, index);
|
2012-02-24 22:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assign this chunk to a host
|
|
|
|
//
|
|
|
|
int CHUNK::assign() {
|
|
|
|
int host_id = parent->dfile->choose_host();
|
|
|
|
if (!host_id) {
|
|
|
|
return ERR_NOT_FOUND;
|
|
|
|
}
|
|
|
|
DB_VDA_CHUNK_HOST ch;
|
|
|
|
ch.create_time = dtime();
|
|
|
|
ch.vda_file_id = parent->dfile->id;
|
|
|
|
ch.host_id = host_id;
|
2012-02-29 07:22:59 +00:00
|
|
|
strcpy(ch.name, name);
|
|
|
|
ch.size = parent->dfile->policy.chunk_size();
|
2012-02-24 22:55:11 +00:00
|
|
|
ch.present_on_host = 0;
|
|
|
|
ch.transfer_in_progress = true;
|
|
|
|
ch.transfer_wait = true;
|
|
|
|
ch.transfer_request_time = ch.create_time;
|
|
|
|
ch.transfer_send_time = 0;
|
|
|
|
int retval = ch.insert();
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL, "ch.insert() failed\n");
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CHUNK::start_upload() {
|
|
|
|
// if no upload of this chunk is in progress, start one.
|
|
|
|
// NOTE: all instances are inherently present_on_host,
|
|
|
|
// since this is only called if chunk is not present on server
|
|
|
|
//
|
|
|
|
VDA_CHUNK_HOST* chp;
|
|
|
|
set<VDA_CHUNK_HOST*>::iterator i;
|
|
|
|
for (i=hosts.begin(); i!=hosts.end(); i++) {
|
|
|
|
chp = *i;
|
|
|
|
if (chp->transfer_in_progress) return 0;
|
|
|
|
}
|
|
|
|
chp = *(hosts.begin());
|
|
|
|
DB_VDA_CHUNK_HOST dch;
|
|
|
|
char set_clause[256], where_clause[256];
|
|
|
|
sprintf(set_clause,
|
|
|
|
"transfer_in_progress=1, transfer_wait=1, transfer_request_time=%f",
|
|
|
|
dtime()
|
|
|
|
);
|
|
|
|
sprintf(where_clause,
|
|
|
|
"where vda_file_id=%d and host_id=%d and name='%s'",
|
|
|
|
chp->vda_file_id,
|
|
|
|
chp->host_id,
|
|
|
|
name
|
|
|
|
);
|
|
|
|
int retval = dch.update_fields_noid(set_clause, where_clause);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline bool alive(DB_HOST& h) {
|
|
|
|
return (h.rpc_time > dtime()-VDA_HOST_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* host_alive_clause() {
|
|
|
|
static char buf[256];
|
|
|
|
sprintf(buf, "rpc_time > %f", dtime() - VDA_HOST_TIMEOUT);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pick a host to send a chunk of this file to.
|
|
|
|
// The host must:
|
|
|
|
// 1) be alive (recent RPC time)
|
|
|
|
// 2) not have any chunks of this file
|
|
|
|
//
|
|
|
|
// We maintain a cache of such hosts
|
|
|
|
// The policy is:
|
|
|
|
//
|
|
|
|
// - scan the cache, removing hosts that are no longer alive;
|
|
|
|
// return if find a live host
|
|
|
|
// - pick a random starting point in host ID space,
|
|
|
|
// and enumerate 100 live hosts; wrap around if needed.
|
|
|
|
// Return one and put the rest in cache
|
|
|
|
//
|
|
|
|
int VDA_FILE_AUX::choose_host() {
|
|
|
|
int retval;
|
|
|
|
DB_HOST host;
|
|
|
|
|
2012-02-28 06:57:28 +00:00
|
|
|
return 467;
|
|
|
|
|
2012-02-24 22:55:11 +00:00
|
|
|
// replenish cache if needed
|
|
|
|
//
|
|
|
|
if (!available_hosts.size()) {
|
|
|
|
int nhosts_scanned = 0;
|
|
|
|
int rand_id;
|
|
|
|
for (int i=0; i<2; i++) {
|
|
|
|
char buf[256];
|
|
|
|
if (i == 0) {
|
|
|
|
retval = host.max_id(rand_id, "");
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL, "host.max_id() failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
rand_id = (int)(((double)id)*drand());
|
|
|
|
sprintf(buf,
|
|
|
|
"where %s and id>=%d order by id limit 100",
|
|
|
|
host_alive_clause(), rand_id
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
sprintf(buf,
|
|
|
|
"where %s and id<%d order by id limit %d",
|
|
|
|
host_alive_clause(), rand_id, 100-nhosts_scanned
|
|
|
|
);
|
|
|
|
}
|
|
|
|
while (1) {
|
|
|
|
retval = host.enumerate(buf);
|
|
|
|
if (retval == ERR_DB_NOT_FOUND) break;
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL, "host enum failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
nhosts_scanned++;
|
|
|
|
DB_VDA_CHUNK_HOST ch;
|
|
|
|
char buf2[256];
|
|
|
|
int count;
|
|
|
|
sprintf(buf2, "where vda_file_id=%d and host_id=%d", id, host.id);
|
|
|
|
#if 0
|
|
|
|
retval = ch.count(count, buf2);
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL, "ch.count failed\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
count = 0;
|
|
|
|
#endif
|
|
|
|
if (count == 0) {
|
|
|
|
available_hosts.push_back(host.id);
|
|
|
|
}
|
|
|
|
if (nhosts_scanned == 100) break;
|
|
|
|
}
|
|
|
|
if (nhosts_scanned == 100) break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
while (available_hosts.size()) {
|
|
|
|
int hostid = available_hosts.back();
|
|
|
|
available_hosts.pop_back();
|
|
|
|
retval = host.lookup_id(hostid);
|
|
|
|
if (retval || !alive(host)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return hostid;
|
|
|
|
}
|
|
|
|
|
|
|
|
log_messages.printf(MSG_CRITICAL, "No hosts available\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-24 21:13:00 +00:00
|
|
|
META_CHUNK::META_CHUNK(VDA_FILE_AUX* d, META_CHUNK* p, int index) {
|
|
|
|
dfile = d;
|
|
|
|
parent = p;
|
|
|
|
if (parent) {
|
|
|
|
if (strlen(parent->name)) {
|
|
|
|
sprintf(name, "%s.%d", parent->name, index);
|
|
|
|
} else {
|
|
|
|
sprintf(name, "%d", index);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
strcpy(name, "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// initialize a meta-chunk:
|
|
|
|
// encode it, then recursively initialize its meta-chunk children
|
2012-02-15 04:40:50 +00:00
|
|
|
//
|
2012-03-11 01:51:07 +00:00
|
|
|
int META_CHUNK::init(const char* _dir, POLICY& p, int level) {
|
2012-02-15 04:40:50 +00:00
|
|
|
double size;
|
2012-02-29 07:22:59 +00:00
|
|
|
char child_dir[1024];
|
2012-02-24 21:13:00 +00:00
|
|
|
|
2012-03-11 01:51:07 +00:00
|
|
|
strcpy(dir, _dir);
|
|
|
|
coding = p.codings[level];
|
|
|
|
int retval = encode();
|
2012-01-25 03:35:50 +00:00
|
|
|
if (retval) return retval;
|
2012-03-11 01:51:07 +00:00
|
|
|
p.chunk_sizes[level] = child_size;
|
2012-02-24 21:13:00 +00:00
|
|
|
|
2012-01-25 03:35:50 +00:00
|
|
|
if (level+1 < p.coding_levels) {
|
2012-03-11 01:51:07 +00:00
|
|
|
for (int i=0; i<coding.m; i++) {
|
2012-02-29 07:22:59 +00:00
|
|
|
sprintf(child_dir, "%s/%d", dir, i);
|
2012-02-24 21:13:00 +00:00
|
|
|
META_CHUNK* mc = new META_CHUNK(dfile, parent, i);
|
|
|
|
retval = mc->init(child_dir, p, level+1);
|
2012-01-25 03:35:50 +00:00
|
|
|
if (retval) return retval;
|
2012-02-15 04:40:50 +00:00
|
|
|
children.push_back(mc);
|
|
|
|
}
|
|
|
|
} else {
|
2012-03-11 01:51:07 +00:00
|
|
|
for (int i=0; i<coding.m; i++) {
|
2012-02-15 04:40:50 +00:00
|
|
|
CHUNK* cp = new CHUNK(this, p.chunk_sizes[level], i);
|
|
|
|
children.push_back(cp);
|
2012-02-29 07:22:59 +00:00
|
|
|
|
|
|
|
// write the chunk's MD5 to a file
|
|
|
|
//
|
|
|
|
char file_path[1024], md5_file_path[1024];
|
|
|
|
sprintf(file_path, "%s/%d/%s", dir, i, DATA_FILENAME);
|
|
|
|
sprintf(md5_file_path, "%s/%d/md5.txt", dir, i);
|
|
|
|
char md5[64];
|
|
|
|
md5_file(file_path, md5, size);
|
|
|
|
FILE* f = fopen(md5_file_path, "w");
|
|
|
|
fprintf(f, "%s\n", md5);
|
|
|
|
fclose(f);
|
2012-01-25 03:35:50 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-24 18:23:18 +00:00
|
|
|
return 0;
|
2012-01-23 21:59:12 +00:00
|
|
|
}
|
|
|
|
|
2012-02-15 04:40:50 +00:00
|
|
|
// initialize a file: create its directory hierarchy
|
|
|
|
// and expand out its encoding tree,
|
|
|
|
// leaving only the bottom-level chunks
|
|
|
|
//
|
|
|
|
int VDA_FILE_AUX::init() {
|
2012-02-24 21:13:00 +00:00
|
|
|
char buf[1024], buf2[1024];
|
2012-02-29 07:22:59 +00:00
|
|
|
sprintf(buf, "%s/%s", dir, DATA_FILENAME);
|
2012-02-24 21:13:00 +00:00
|
|
|
sprintf(buf2, "%s/%s", dir, name);
|
|
|
|
int retval = symlink(buf2, buf);
|
|
|
|
if (retval) {
|
2012-02-29 07:22:59 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL, "symlink %s %s failed\n", buf2, buf);
|
2012-02-24 21:13:00 +00:00
|
|
|
return ERR_SYMLINK;
|
|
|
|
}
|
2012-02-29 07:22:59 +00:00
|
|
|
|
2012-02-24 21:13:00 +00:00
|
|
|
meta_chunk = new META_CHUNK(this, NULL, 0);
|
|
|
|
retval = meta_chunk->init(dir, policy, 0);
|
2012-02-15 04:40:50 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
sprintf(buf, "%s/chunk_sizes.txt", dir);
|
|
|
|
FILE* f = fopen(buf, "w");
|
|
|
|
for (int i=0; i<policy.coding_levels; i++) {
|
|
|
|
fprintf(f, "%.0f\n", policy.chunk_sizes[i]);
|
|
|
|
}
|
|
|
|
fclose(f);
|
2012-02-29 07:22:59 +00:00
|
|
|
|
|
|
|
// create symlink from download dir
|
|
|
|
//
|
|
|
|
dir_hier_path(name, config.download_dir, config.uldl_dir_fanout, buf);
|
|
|
|
retval = symlink(buf2, buf);
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL, "symlink %s %s failed\n", buf2, buf);
|
|
|
|
return ERR_SYMLINK;
|
|
|
|
}
|
|
|
|
|
2012-02-15 04:40:50 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-11 01:51:07 +00:00
|
|
|
int META_CHUNK::get_state(const char* _dir, POLICY& p, int level) {
|
2012-01-24 18:23:18 +00:00
|
|
|
int retval;
|
|
|
|
|
2012-03-11 01:51:07 +00:00
|
|
|
strcpy(dir, _dir);
|
|
|
|
coding = p.codings[level];
|
2012-02-15 04:40:50 +00:00
|
|
|
if (level+1 < p.coding_levels) {
|
2012-03-11 01:51:07 +00:00
|
|
|
for (int i=0; i<coding.m; i++) {
|
2012-02-24 21:13:00 +00:00
|
|
|
char child_dir[1024];
|
|
|
|
sprintf(child_dir, "%s/%s.%d", dir, DATA_FILENAME, i);
|
|
|
|
META_CHUNK* mc = new META_CHUNK(dfile, this, i);
|
|
|
|
retval = mc->get_state(child_dir, p, level+1);
|
2012-02-15 04:40:50 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
children.push_back(mc);
|
|
|
|
}
|
|
|
|
} else {
|
2012-03-11 01:51:07 +00:00
|
|
|
for (int i=0; i<coding.m; i++) {
|
2012-02-15 04:40:50 +00:00
|
|
|
CHUNK* ch = new CHUNK(this, p.chunk_sizes[level], i);
|
|
|
|
children.push_back(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_chunk_numbers(VDA_CHUNK_HOST& vch, vector<int>& chunk_numbers) {
|
2012-02-24 21:13:00 +00:00
|
|
|
char* p = vch.name;
|
2012-02-15 04:40:50 +00:00
|
|
|
while (1) {
|
2012-02-24 21:13:00 +00:00
|
|
|
p = strchr(p, '.');
|
|
|
|
if (!p) break;
|
|
|
|
p++;
|
2012-02-15 04:40:50 +00:00
|
|
|
int i = atoi(p);
|
|
|
|
chunk_numbers.push_back(i);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the state of an already-initialized file:
|
|
|
|
// expand the encoding tree,
|
|
|
|
// enumerate the VDA_HOST_CHUNKs from the DB
|
|
|
|
// and put them in the appropriate lists
|
|
|
|
//
|
|
|
|
int VDA_FILE_AUX::get_state() {
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
sprintf(buf, "%s/chunk_sizes.txt", dir);
|
|
|
|
FILE* f = fopen(buf, "r");
|
|
|
|
if (!f) return -1;
|
|
|
|
for (int i=0; i<policy.coding_levels; i++) {
|
|
|
|
int n = fscanf(f, "%lf\n", &(policy.chunk_sizes[i]));
|
|
|
|
if (n != 1) return -1;
|
|
|
|
}
|
|
|
|
fclose(f);
|
2012-02-24 21:13:00 +00:00
|
|
|
meta_chunk = new META_CHUNK(this, NULL, 0);
|
|
|
|
int retval = meta_chunk->get_state(dir, policy, 0);
|
2012-02-15 04:40:50 +00:00
|
|
|
if (retval) return retval;
|
|
|
|
DB_VDA_CHUNK_HOST vch;
|
|
|
|
sprintf(buf, "where vda_file_id=%d", id);
|
|
|
|
while (1) {
|
|
|
|
retval = vch.enumerate(buf);
|
|
|
|
if (retval == ERR_DB_NOT_FOUND) break;
|
|
|
|
if (retval) return retval;
|
|
|
|
vector<int> chunk_numbers;
|
|
|
|
retval = get_chunk_numbers(vch, chunk_numbers);
|
2012-02-24 03:09:56 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL,
|
|
|
|
"get_chunk_numbers(): %d\n", retval
|
|
|
|
);
|
|
|
|
return retval;
|
|
|
|
}
|
2012-02-15 04:40:50 +00:00
|
|
|
if ((int)(chunk_numbers.size()) != policy.coding_levels) {
|
2012-02-24 21:13:00 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL,
|
|
|
|
"wrong get_chunk_numbers: got %d, expected %d\n",
|
|
|
|
(int)(chunk_numbers.size()), policy.coding_levels
|
|
|
|
);
|
2012-02-15 04:40:50 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
META_CHUNK* mc = meta_chunk;
|
|
|
|
for (int i=0; i<policy.coding_levels; i++) {
|
|
|
|
if (i == policy.coding_levels-1) {
|
|
|
|
CHUNK* c = (CHUNK*)(mc->children[chunk_numbers[i]]);
|
|
|
|
VDA_CHUNK_HOST* vchp = new VDA_CHUNK_HOST();
|
|
|
|
*vchp = vch;
|
|
|
|
c->hosts.insert(vchp);
|
|
|
|
} else {
|
|
|
|
mc = (META_CHUNK*)(mc->children[chunk_numbers[i]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-24 03:09:56 +00:00
|
|
|
int handle_file(VDA_FILE_AUX& vf, DB_VDA_FILE& dvf) {
|
2012-02-15 04:40:50 +00:00
|
|
|
int retval;
|
|
|
|
char buf[1024];
|
|
|
|
|
2012-02-24 21:13:00 +00:00
|
|
|
log_messages.printf(MSG_NORMAL, "processing file %s\n", vf.name);
|
2012-02-24 03:09:56 +00:00
|
|
|
|
2012-01-24 18:23:18 +00:00
|
|
|
// read the policy file
|
|
|
|
//
|
|
|
|
sprintf(buf, "%s/boinc_meta.txt", vf.dir);
|
2012-02-15 04:40:50 +00:00
|
|
|
retval = vf.policy.parse(buf);
|
2012-01-25 03:35:50 +00:00
|
|
|
if (retval) {
|
2012-02-24 03:09:56 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL, "Can't parse policy file %s\n", buf);
|
2012-01-25 03:35:50 +00:00
|
|
|
return retval;
|
|
|
|
}
|
2012-02-24 03:09:56 +00:00
|
|
|
if (vf.initialized) {
|
2012-02-15 04:40:50 +00:00
|
|
|
retval = vf.get_state();
|
2012-02-24 03:09:56 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL, "vf.get_state failed %d\n", retval);
|
|
|
|
return retval;
|
|
|
|
}
|
2012-02-15 04:40:50 +00:00
|
|
|
} else {
|
|
|
|
retval = vf.init();
|
2012-02-24 03:09:56 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL, "vf.init failed %d\n", retval);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
dvf.update_field("initialized=1");
|
2012-01-23 21:59:12 +00:00
|
|
|
}
|
2012-02-21 20:55:09 +00:00
|
|
|
retval = vf.meta_chunk->recovery_plan();
|
2012-02-24 03:09:56 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL, "vf.recovery_plan failed %d\n", retval);
|
|
|
|
return retval;
|
|
|
|
}
|
2012-02-21 20:55:09 +00:00
|
|
|
retval = vf.meta_chunk->recovery_action(dtime());
|
2012-02-24 03:09:56 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL, "vf.recovery_action failed %d\n", retval);
|
|
|
|
return retval;
|
|
|
|
}
|
2012-01-24 18:23:18 +00:00
|
|
|
return 0;
|
2012-01-23 05:03:52 +00:00
|
|
|
}
|
|
|
|
|
2012-02-13 08:41:48 +00:00
|
|
|
// handle files
|
|
|
|
//
|
2012-01-23 05:03:52 +00:00
|
|
|
bool scan_files() {
|
|
|
|
DB_VDA_FILE vf;
|
|
|
|
bool found = false;
|
2012-01-25 03:35:50 +00:00
|
|
|
int retval;
|
2012-01-23 05:03:52 +00:00
|
|
|
|
2012-02-22 19:51:09 +00:00
|
|
|
while (1) {
|
|
|
|
retval = vf.enumerate("where need_update<>0");
|
|
|
|
if (retval == ERR_DB_NOT_FOUND) break;
|
|
|
|
if (retval) {
|
2012-02-24 03:09:56 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL, "VDA_FILE enumerate failed\n");
|
2012-02-22 19:51:09 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-02-15 04:40:50 +00:00
|
|
|
VDA_FILE_AUX vfa(vf);
|
2012-01-23 05:03:52 +00:00
|
|
|
found = true;
|
2012-02-24 03:09:56 +00:00
|
|
|
retval = handle_file(vfa, vf);
|
2012-01-25 03:35:50 +00:00
|
|
|
if (retval) {
|
2012-02-24 03:09:56 +00:00
|
|
|
log_messages.printf(
|
|
|
|
MSG_CRITICAL, "handle_file() failed: %d\n", retval
|
|
|
|
);
|
2012-02-21 20:55:09 +00:00
|
|
|
exit(1);
|
2012-01-25 03:35:50 +00:00
|
|
|
} else {
|
|
|
|
vf.need_update = 0;
|
|
|
|
vf.update();
|
|
|
|
}
|
2012-01-23 05:03:52 +00:00
|
|
|
}
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2012-02-22 19:51:09 +00:00
|
|
|
// this host is declared dead; deal with the loss of data
|
|
|
|
//
|
|
|
|
int handle_host(DB_HOST& h) {
|
|
|
|
DB_VDA_CHUNK_HOST ch;
|
|
|
|
char buf[256];
|
|
|
|
int retval;
|
|
|
|
|
2012-02-24 03:09:56 +00:00
|
|
|
log_messages.printf(MSG_NORMAL, "processing dead host %d\n", h.id);
|
|
|
|
|
2012-02-22 19:51:09 +00:00
|
|
|
sprintf(buf, "where host_id=%d", h.id);
|
|
|
|
while (1) {
|
|
|
|
retval = ch.enumerate(buf);
|
|
|
|
if (retval == ERR_DB_NOT_FOUND) break;
|
|
|
|
if (retval) return retval;
|
2012-02-24 03:09:56 +00:00
|
|
|
log_messages.printf(MSG_NORMAL, " updating file%d\n", ch.vda_file_id);
|
2012-02-22 19:51:09 +00:00
|
|
|
DB_VDA_FILE vf;
|
|
|
|
retval = vf.lookup_id(ch.vda_file_id);
|
2012-02-24 03:09:56 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL,
|
|
|
|
" file lookup failed%d\n", ch.vda_file_id
|
|
|
|
);
|
|
|
|
return retval;
|
|
|
|
}
|
2012-02-22 19:51:09 +00:00
|
|
|
retval = vf.update_field("need_update=1");
|
2012-02-24 03:09:56 +00:00
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL,
|
|
|
|
" file update failed%d\n", ch.vda_file_id
|
|
|
|
);
|
|
|
|
return retval;
|
|
|
|
}
|
2012-02-22 19:51:09 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2012-01-23 05:03:52 +00:00
|
|
|
}
|
|
|
|
|
2012-02-22 19:51:09 +00:00
|
|
|
// handle timed-out hosts
|
2012-02-13 08:41:48 +00:00
|
|
|
//
|
2012-02-22 19:51:09 +00:00
|
|
|
bool scan_hosts() {
|
|
|
|
DB_HOST h;
|
2012-01-23 08:04:41 +00:00
|
|
|
char buf[256];
|
2012-02-22 19:51:09 +00:00
|
|
|
int retval;
|
2012-01-23 08:04:41 +00:00
|
|
|
bool found = false;
|
2012-01-20 20:21:29 +00:00
|
|
|
|
2012-02-22 19:51:09 +00:00
|
|
|
sprintf(buf, "where cpu_efficiency < %f", dtime());
|
|
|
|
while (1) {
|
|
|
|
retval = h.enumerate(buf);
|
|
|
|
if (retval == ERR_DB_NOT_FOUND) break;
|
|
|
|
if (retval) {
|
2012-02-24 03:09:56 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL, "host.enumerate() failed\n");
|
2012-02-22 19:51:09 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-01-23 08:04:41 +00:00
|
|
|
found = true;
|
2012-02-22 19:51:09 +00:00
|
|
|
retval = handle_host(h);
|
|
|
|
if (retval) {
|
2012-02-24 03:09:56 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL, "handle_host() failed: %d\n", retval);
|
2012-02-22 19:51:09 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2012-02-24 03:09:56 +00:00
|
|
|
retval = h.update_field("cpu_efficiency=1e12");
|
|
|
|
if (retval) {
|
|
|
|
log_messages.printf(MSG_CRITICAL, "h.update_field() failed: %d\n", retval);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2012-01-23 05:03:52 +00:00
|
|
|
}
|
2012-01-23 08:04:41 +00:00
|
|
|
return found;
|
2012-01-23 05:03:52 +00:00
|
|
|
}
|
2012-01-20 20:21:29 +00:00
|
|
|
|
2012-01-23 05:03:52 +00:00
|
|
|
int main(int argc, char** argv) {
|
2012-02-24 03:09:56 +00:00
|
|
|
for (int i=1; i<argc; i++) {
|
|
|
|
if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--debug_level")) {
|
|
|
|
int dl = atoi(argv[++i]);
|
|
|
|
log_messages.set_debug_level(dl);
|
|
|
|
if (dl == 4) g_print_queries = true;
|
|
|
|
}
|
|
|
|
}
|
2012-02-21 20:55:09 +00:00
|
|
|
int retval = config.parse_file();
|
|
|
|
if (retval) {
|
2012-02-24 03:09:56 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL, "can't parse config file\n");
|
2012-02-21 20:55:09 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
retval = boinc_db.open(
|
|
|
|
config.db_name, config.db_host, config.db_user, config.db_passwd
|
|
|
|
);
|
|
|
|
if (retval) {
|
2012-02-24 03:09:56 +00:00
|
|
|
log_messages.printf(MSG_CRITICAL, "can't open DB\n");
|
2012-02-21 20:55:09 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
2012-02-18 23:19:06 +00:00
|
|
|
VDA_FILE_AUX vf;
|
|
|
|
memset(&vf, 0, sizeof(vf));
|
2012-01-25 03:35:50 +00:00
|
|
|
strcpy(vf.dir, "/mydisks/b/users/boincadm/vda_test");
|
|
|
|
strcpy(vf.name, "file.ext");
|
2012-02-15 04:40:50 +00:00
|
|
|
handle_file(vf);
|
2012-01-25 03:35:50 +00:00
|
|
|
exit(0);
|
2012-02-15 04:40:50 +00:00
|
|
|
#endif
|
2012-01-23 05:03:52 +00:00
|
|
|
while(1) {
|
|
|
|
bool action = scan_files();
|
2012-02-22 19:51:09 +00:00
|
|
|
action |= scan_hosts();
|
2012-01-23 05:03:52 +00:00
|
|
|
if (!action) boinc_sleep(5.);
|
|
|
|
}
|
|
|
|
}
|