2011-11-19 00:29:55 +00:00
|
|
|
// This file is part of BOINC.
|
|
|
|
// http://boinc.berkeley.edu
|
|
|
|
// Copyright (C) 2011 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/>.
|
|
|
|
|
2011-11-03 03:29:07 +00:00
|
|
|
// ssim - simulator for distributed storage
|
2011-11-19 00:29:55 +00:00
|
|
|
//
|
|
|
|
// Simulates the storage of files on a dynamic set of hosts.
|
|
|
|
//
|
|
|
|
// The model of the host population is:
|
|
|
|
// - hosts arrival is a Poisson process
|
|
|
|
// - host lifetime is exponentially distributed
|
2011-12-06 04:21:27 +00:00
|
|
|
// - the time needed to download n bytes of data to/from a host is
|
|
|
|
// U()*n/DOWNLOAD_BYTES_SEC
|
|
|
|
// where U() is a uniform random var
|
|
|
|
// TODO: add a factor corresponding to host availability
|
2011-11-19 00:29:55 +00:00
|
|
|
|
|
|
|
#define HOSTS_PER_DAY 10.
|
|
|
|
#define HOST_LIFE_MEAN 100.*86400
|
2011-12-14 01:34:51 +00:00
|
|
|
#define UPLOAD_BYTES_SEC (5./3600)
|
|
|
|
#define DOWNLOAD_BYTES_SEC (5./3600)
|
2011-11-19 00:29:55 +00:00
|
|
|
|
|
|
|
// We simulate policies based on coding and replication.
|
|
|
|
//
|
|
|
|
// Coding means that data is divided into M = N+K units,
|
2011-11-29 04:47:10 +00:00
|
|
|
// of which any N are sufficient to reconstruct the original data.
|
2011-12-14 01:34:51 +00:00
|
|
|
// When we need to reconstruct an encoded unit on the server,
|
|
|
|
// we try to upload N_UPLOAD subunits,
|
|
|
|
// where N <= N_UPLOAD <= M
|
2011-11-19 00:29:55 +00:00
|
|
|
|
2011-11-20 09:18:12 +00:00
|
|
|
#define ENCODING_N 4
|
|
|
|
#define ENCODING_K 2
|
|
|
|
#define ENCODING_M 6
|
2011-12-14 01:34:51 +00:00
|
|
|
#define N_UPLOAD 5
|
|
|
|
|
|
|
|
// The units in an encoding can themselves be encoded.
|
|
|
|
// There are LEVELS levels of encoding.
|
|
|
|
|
2011-11-19 00:29:55 +00:00
|
|
|
#define ENCODING_LEVELS 1
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
// The bottom-level data units ("chunks") are stored on hosts,
|
|
|
|
// possibly with replication
|
2011-11-19 00:29:55 +00:00
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
#define REPLICATION_LEVEL 2
|
2011-11-19 00:29:55 +00:00
|
|
|
|
|
|
|
// Terminology:
|
|
|
|
//
|
2011-12-10 22:03:43 +00:00
|
|
|
// A data unit is "recoverable" if it can be recovered on the server
|
|
|
|
// by uploading data from hosts.
|
|
|
|
// A chunk is recoverable if it's present on the server or on at least 1 host.
|
|
|
|
// (note: if it's downloading, it's still present on the server)
|
|
|
|
// An encoded data unit is recoverable if at least N
|
2011-11-19 00:29:55 +00:00
|
|
|
// of its subunits are recoverable.
|
|
|
|
|
2011-12-06 04:21:27 +00:00
|
|
|
// Figures of merit
|
|
|
|
//
|
|
|
|
// for each file, we compute:
|
|
|
|
// - the average and peak server network rate, up and down
|
|
|
|
// - the average and peak disk usage
|
|
|
|
// - the average and min fault tolerance level
|
|
|
|
// (i.e. number of simultaneous host failures needed to lose the file)
|
|
|
|
//
|
|
|
|
// These are measured starting from the time when the file's
|
|
|
|
// initial downloads have all succeeded or failed
|
|
|
|
|
|
|
|
#include <math.h>
|
2011-12-16 19:45:31 +00:00
|
|
|
#include <limits.h>
|
2011-11-03 03:29:07 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <set>
|
|
|
|
|
|
|
|
#include "des.h"
|
|
|
|
|
|
|
|
using std::set;
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
#define EVENT_DEBUG
|
|
|
|
#define SAMPLE_DEBUG
|
|
|
|
//#define RECOVERY_DEBUG
|
2011-11-03 03:29:07 +00:00
|
|
|
|
|
|
|
SIMULATOR sim;
|
2011-12-14 01:34:51 +00:00
|
|
|
int next_file_id=0;
|
|
|
|
int next_host_id=0;
|
2011-11-03 03:29:07 +00:00
|
|
|
|
|
|
|
inline double drand() {
|
|
|
|
return (double)rand()/(double)RAND_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
double ran_exp(double mean) {
|
2011-12-06 04:21:27 +00:00
|
|
|
return -log(drand())*mean;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* time_str(double t) {
|
|
|
|
static char buf[256];
|
|
|
|
struct tm;
|
|
|
|
int n = (int)t;
|
|
|
|
int nsec = n % 60;
|
|
|
|
n /= 60;
|
|
|
|
int nmin = n % 60;
|
|
|
|
n /= 60;
|
|
|
|
int nhour = n % 24;
|
|
|
|
n /= 24;
|
|
|
|
sprintf(buf, "%4d days %02d:%02d:%02d", n, nhour, nmin, nsec);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* now_str() {
|
|
|
|
return time_str(sim.now);
|
2011-11-03 03:29:07 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 00:29:55 +00:00
|
|
|
struct CHUNK;
|
2011-12-14 01:34:51 +00:00
|
|
|
struct CHUNK_ON_HOST;
|
2011-11-19 00:29:55 +00:00
|
|
|
struct META_CHUNK;
|
|
|
|
struct DFILE;
|
2011-11-03 03:29:07 +00:00
|
|
|
struct HOST;
|
2011-11-20 09:18:12 +00:00
|
|
|
set<HOST*> hosts;
|
2011-11-03 03:29:07 +00:00
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
// Represents a host.
|
|
|
|
// The associated EVENT is the disappearance of the host
|
|
|
|
//
|
2011-11-03 03:29:07 +00:00
|
|
|
struct HOST : public EVENT {
|
2011-11-20 09:18:12 +00:00
|
|
|
int id;
|
2011-12-14 01:34:51 +00:00
|
|
|
set<CHUNK_ON_HOST*> chunks; // chunks present or downloading
|
2011-11-19 00:29:55 +00:00
|
|
|
virtual void handle();
|
2011-11-20 09:18:12 +00:00
|
|
|
HOST() {
|
|
|
|
t = sim.now + ran_exp(HOST_LIFE_MEAN);
|
|
|
|
id = next_host_id++;
|
|
|
|
hosts.insert(this);
|
|
|
|
}
|
2011-11-03 03:29:07 +00:00
|
|
|
};
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
// The host arrival process.
|
|
|
|
// The associated EVENT is the arrival of a host
|
|
|
|
//
|
2011-11-03 03:29:07 +00:00
|
|
|
struct HOST_ARRIVAL : public EVENT {
|
|
|
|
virtual void handle() {
|
2011-11-20 09:18:12 +00:00
|
|
|
sim.insert(new HOST);
|
2011-11-03 03:29:07 +00:00
|
|
|
t += ran_exp(86400./HOSTS_PER_DAY);
|
|
|
|
sim.insert(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-20 09:18:12 +00:00
|
|
|
void die(const char* msg) {
|
2011-12-06 04:21:27 +00:00
|
|
|
printf("%s: %s\n", now_str(), msg);
|
2011-11-20 09:18:12 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
// The status of a chunk on a particular host.
|
|
|
|
// The associated event is the completion of an upload or download
|
|
|
|
//
|
|
|
|
struct CHUNK_ON_HOST : public EVENT {
|
|
|
|
HOST* host;
|
|
|
|
CHUNK* chunk;
|
|
|
|
char name[256];
|
|
|
|
bool present_on_host;
|
|
|
|
bool transfer_in_progress; // upload if present_on_host, else download
|
|
|
|
virtual void handle();
|
|
|
|
inline bool download_in_progress() {
|
|
|
|
return (transfer_in_progress && !present_on_host);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define PRESENT 0
|
|
|
|
#define RECOVERABLE 1
|
|
|
|
#define UNRECOVERABLE 2
|
|
|
|
|
2011-11-19 00:29:55 +00:00
|
|
|
// base class for chunks and meta-chunks
|
2011-11-11 17:18:04 +00:00
|
|
|
//
|
2011-11-19 00:29:55 +00:00
|
|
|
struct DATA_UNIT {
|
2011-12-12 22:07:37 +00:00
|
|
|
virtual void recovery_plan(){};
|
|
|
|
virtual void recovery_action(){};
|
2011-12-14 01:34:51 +00:00
|
|
|
int status;
|
2011-12-12 22:07:37 +00:00
|
|
|
bool in_recovery_set;
|
|
|
|
bool data_now_present;
|
|
|
|
bool data_needed;
|
|
|
|
double cost;
|
2011-12-16 19:45:31 +00:00
|
|
|
int fault_tolerance;
|
|
|
|
// min # of host failures that would make this unrecoverable
|
2011-11-20 09:18:12 +00:00
|
|
|
char name[64];
|
2011-11-19 00:29:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct CHUNK : DATA_UNIT {
|
|
|
|
set<CHUNK_ON_HOST*> hosts;
|
|
|
|
META_CHUNK* parent;
|
|
|
|
double size;
|
2011-12-12 22:07:37 +00:00
|
|
|
bool present_on_server;
|
2011-11-19 00:29:55 +00:00
|
|
|
|
2011-11-20 09:18:12 +00:00
|
|
|
CHUNK(META_CHUNK* mc, double s, int index);
|
2011-11-19 00:29:55 +00:00
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
void start_upload();
|
2011-12-12 22:07:37 +00:00
|
|
|
void host_failed(CHUNK_ON_HOST* p);
|
2011-12-14 01:34:51 +00:00
|
|
|
bool download_in_progress();
|
2011-12-12 22:07:37 +00:00
|
|
|
void upload_complete();
|
2011-12-06 04:21:27 +00:00
|
|
|
void download_complete();
|
2011-12-12 22:07:37 +00:00
|
|
|
void assign();
|
|
|
|
virtual void recovery_plan();
|
|
|
|
virtual void recovery_action();
|
2011-11-19 00:29:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct META_CHUNK : DATA_UNIT {
|
|
|
|
vector<DATA_UNIT*> children;
|
|
|
|
META_CHUNK* parent;
|
|
|
|
int n_children_present;
|
2011-12-12 22:07:37 +00:00
|
|
|
bool have_unrecoverable_children;
|
2011-11-14 06:27:36 +00:00
|
|
|
DFILE* dfile;
|
2011-11-19 00:29:55 +00:00
|
|
|
bool uploading;
|
|
|
|
|
2011-11-20 09:18:12 +00:00
|
|
|
META_CHUNK(
|
|
|
|
DFILE* d, META_CHUNK* par, double size, int encoding_level, int index
|
|
|
|
);
|
2011-11-27 07:42:00 +00:00
|
|
|
|
2011-12-12 22:07:37 +00:00
|
|
|
virtual void recovery_plan();
|
|
|
|
virtual void recovery_action();
|
2011-11-03 03:29:07 +00:00
|
|
|
};
|
|
|
|
|
2011-12-06 04:21:27 +00:00
|
|
|
// keeps track of a time-varying property of a file
|
2011-12-14 01:34:51 +00:00
|
|
|
// (server disk usage, up/download rate, fault tolerance level)
|
2011-12-06 04:21:27 +00:00
|
|
|
//
|
|
|
|
struct STATS_ITEM {
|
|
|
|
double value;
|
|
|
|
double integral;
|
|
|
|
double max_val;
|
|
|
|
double max_val_time;
|
|
|
|
double prev_t;
|
|
|
|
double start_time;
|
|
|
|
bool first;
|
|
|
|
|
|
|
|
STATS_ITEM() {
|
|
|
|
value = 0;
|
|
|
|
integral = 0;
|
|
|
|
max_val = 0;
|
|
|
|
max_val_time = 0;
|
|
|
|
first = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void sample(double v, bool collecting_stats) {
|
|
|
|
double old_val = value;
|
|
|
|
value = v;
|
|
|
|
if (!collecting_stats) return;
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
prev_t = sim.now;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
double dt = sim.now - prev_t;
|
|
|
|
prev_t = sim.now;
|
|
|
|
integral += dt*old_val;
|
|
|
|
if (v > max_val) {
|
|
|
|
max_val = v;
|
|
|
|
max_val_time = sim.now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sample_inc(double inc, bool collecting_stats) {
|
|
|
|
sample(value+inc, collecting_stats);
|
2011-12-14 01:34:51 +00:00
|
|
|
#ifdef SAMPLE_DEBUG
|
|
|
|
printf("%s: sample_inc: %f %f\n", now_str(), inc, value);
|
|
|
|
#endif
|
2011-12-06 04:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print() {
|
|
|
|
sample_inc(0, true);
|
|
|
|
double dt = sim.now - start_time;
|
|
|
|
printf(" mean: %f\n", integral/dt);
|
|
|
|
printf(" max: %f\n", max_val);
|
|
|
|
printf(" time of max: %s\n", time_str(max_val_time));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
// represents a file to be stored.
|
|
|
|
// The associated EVENT is the arrival of the file
|
|
|
|
//
|
2011-11-03 03:29:07 +00:00
|
|
|
struct DFILE : EVENT {
|
2011-11-19 00:29:55 +00:00
|
|
|
META_CHUNK* meta_chunk;
|
|
|
|
double size;
|
2011-11-20 09:18:12 +00:00
|
|
|
int id;
|
2011-11-03 03:29:07 +00:00
|
|
|
set<HOST*> unused_hosts;
|
2011-12-14 01:34:51 +00:00
|
|
|
// hosts that don't have any chunks of this file
|
2011-12-06 04:21:27 +00:00
|
|
|
int pending_init_downloads;
|
|
|
|
// # of initial downloads pending.
|
|
|
|
// When this is zero, we start collecting stats for the file
|
|
|
|
double accounting_start_time;
|
|
|
|
STATS_ITEM disk_usage;
|
|
|
|
STATS_ITEM upload_rate;
|
|
|
|
STATS_ITEM download_rate;
|
|
|
|
STATS_ITEM fault_tolerance;
|
2011-11-19 00:29:55 +00:00
|
|
|
|
2011-11-20 09:18:12 +00:00
|
|
|
DFILE(double s) {
|
|
|
|
id = next_file_id++;
|
|
|
|
unused_hosts = hosts;
|
|
|
|
size = s;
|
|
|
|
}
|
|
|
|
|
2011-11-19 00:29:55 +00:00
|
|
|
// the creation of a file
|
|
|
|
//
|
2011-11-03 03:29:07 +00:00
|
|
|
virtual void handle() {
|
2011-11-20 09:18:12 +00:00
|
|
|
meta_chunk = new META_CHUNK(this, NULL, size, ENCODING_LEVELS, id);
|
2011-12-14 01:34:51 +00:00
|
|
|
#ifdef EVENT_DEBUG
|
|
|
|
printf("created file %d: size %f encoded size %f\n",
|
2011-12-07 22:04:03 +00:00
|
|
|
id, size, disk_usage.value
|
|
|
|
);
|
2011-12-14 01:34:51 +00:00
|
|
|
#endif
|
2011-12-12 22:07:37 +00:00
|
|
|
meta_chunk->recovery_plan();
|
|
|
|
meta_chunk->recovery_action();
|
2011-11-03 03:29:07 +00:00
|
|
|
}
|
2011-12-06 04:21:27 +00:00
|
|
|
|
2011-12-16 19:45:31 +00:00
|
|
|
inline bool collecting_stats() {
|
|
|
|
return (pending_init_downloads == 0);
|
|
|
|
}
|
|
|
|
|
2011-12-12 22:07:37 +00:00
|
|
|
void recover() {
|
|
|
|
meta_chunk->recovery_plan();
|
|
|
|
meta_chunk->recovery_action();
|
2011-12-16 19:45:31 +00:00
|
|
|
fault_tolerance.sample(meta_chunk->fault_tolerance, collecting_stats());
|
2011-12-06 04:21:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void print_stats() {
|
|
|
|
printf("Statistics for file %d\n", id);
|
|
|
|
printf(" Server disk usage:\n");
|
|
|
|
disk_usage.print();
|
|
|
|
printf(" Upload rate:\n");
|
|
|
|
upload_rate.print();
|
|
|
|
printf(" Download rate:\n");
|
|
|
|
download_rate.print();
|
|
|
|
printf(" Fault tolerance level:\n");
|
|
|
|
fault_tolerance.print();
|
|
|
|
}
|
2011-11-03 03:29:07 +00:00
|
|
|
};
|
|
|
|
|
2011-11-19 00:29:55 +00:00
|
|
|
//////////////////// method defs ////////////////////
|
|
|
|
|
|
|
|
// transfer has finished
|
|
|
|
//
|
|
|
|
void CHUNK_ON_HOST::handle() {
|
|
|
|
transfer_in_progress = false;
|
|
|
|
if (present_on_host) {
|
|
|
|
// it was an upload
|
2011-12-14 01:34:51 +00:00
|
|
|
#ifdef EVENT_DEBUG
|
2011-12-06 04:21:27 +00:00
|
|
|
printf("%s: upload of %s completed\n", now_str(), name);
|
2011-12-14 01:34:51 +00:00
|
|
|
#endif
|
2011-11-27 07:42:00 +00:00
|
|
|
chunk->upload_complete(); // create new replicas if needed
|
2011-11-19 00:29:55 +00:00
|
|
|
} else {
|
|
|
|
present_on_host = true;
|
2011-12-14 01:34:51 +00:00
|
|
|
#ifdef EVENT_DEBUG
|
2011-12-06 04:21:27 +00:00
|
|
|
printf("%s: download of %s completed\n", now_str(), name);
|
2011-12-14 01:34:51 +00:00
|
|
|
#endif
|
2011-11-20 09:18:12 +00:00
|
|
|
chunk->download_complete();
|
2011-11-19 00:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-06 04:21:27 +00:00
|
|
|
// the host has failed
|
2011-11-19 00:29:55 +00:00
|
|
|
//
|
2011-11-20 09:18:12 +00:00
|
|
|
void HOST::handle() {
|
2011-11-19 00:29:55 +00:00
|
|
|
set<HOST*>::iterator i = hosts.find(this);
|
|
|
|
hosts.erase(i);
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
#ifdef EVENT_DEBUG
|
2011-12-06 04:21:27 +00:00
|
|
|
printf("%s: host %d failed\n", now_str(), id);
|
2011-12-14 01:34:51 +00:00
|
|
|
#endif
|
2011-11-19 00:29:55 +00:00
|
|
|
set<CHUNK_ON_HOST*>::iterator p;
|
|
|
|
for (p = chunks.begin(); p != chunks.end(); p++) {
|
|
|
|
CHUNK_ON_HOST* c = *p;
|
|
|
|
c->chunk->host_failed(c);
|
2011-11-24 21:16:32 +00:00
|
|
|
if (c->transfer_in_progress) {
|
|
|
|
sim.remove(c);
|
|
|
|
}
|
2011-11-19 00:29:55 +00:00
|
|
|
delete c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-20 09:18:12 +00:00
|
|
|
CHUNK::CHUNK(META_CHUNK* mc, double s, int index) {
|
|
|
|
parent = mc;
|
2011-12-12 22:07:37 +00:00
|
|
|
present_on_server = true;
|
2011-11-20 09:18:12 +00:00
|
|
|
size = s;
|
|
|
|
sprintf(name, "%s.%d", parent->name, index);
|
2011-12-06 04:21:27 +00:00
|
|
|
parent->dfile->pending_init_downloads += REPLICATION_LEVEL;
|
|
|
|
parent->dfile->disk_usage.sample_inc(size, false);
|
2011-11-20 09:18:12 +00:00
|
|
|
}
|
|
|
|
|
2011-12-10 22:03:43 +00:00
|
|
|
// if there aren't enough replicas of this chunk,
|
|
|
|
// pick new hosts and start downloads
|
|
|
|
//
|
2011-11-19 00:29:55 +00:00
|
|
|
void CHUNK::assign() {
|
2011-12-12 22:07:37 +00:00
|
|
|
if (!present_on_server) return;
|
2011-11-19 00:29:55 +00:00
|
|
|
while (hosts.size() < REPLICATION_LEVEL) {
|
2011-11-20 09:18:12 +00:00
|
|
|
if (parent->dfile->unused_hosts.size() == 0) {
|
|
|
|
die("no more hosts!\n");
|
|
|
|
}
|
2011-11-19 00:29:55 +00:00
|
|
|
set<HOST*>::iterator i = parent->dfile->unused_hosts.begin();
|
|
|
|
HOST* h = *i;
|
|
|
|
parent->dfile->unused_hosts.erase(i);
|
|
|
|
CHUNK_ON_HOST *c = new CHUNK_ON_HOST();
|
2011-11-20 09:18:12 +00:00
|
|
|
sprintf(c->name, "chunk %s on host %d", name, h->id);
|
2011-12-14 01:34:51 +00:00
|
|
|
#ifdef EVENT_DEBUG
|
2011-12-06 04:21:27 +00:00
|
|
|
printf("%s: assigning chunk %s to host %d\n", now_str(), name, h->id);
|
2011-12-14 01:34:51 +00:00
|
|
|
#endif
|
2011-11-19 00:29:55 +00:00
|
|
|
c->host = h;
|
|
|
|
c->chunk = this;
|
2011-11-24 21:16:32 +00:00
|
|
|
c->t = sim.now + (drand()+.5)*size/DOWNLOAD_BYTES_SEC;
|
2011-11-20 09:18:12 +00:00
|
|
|
hosts.insert(c);
|
2011-12-16 19:45:31 +00:00
|
|
|
parent->dfile->download_rate.sample_inc(
|
|
|
|
DOWNLOAD_BYTES_SEC,
|
|
|
|
parent->dfile->collecting_stats()
|
|
|
|
);
|
2011-11-20 09:18:12 +00:00
|
|
|
h->chunks.insert(c);
|
2011-11-27 07:42:00 +00:00
|
|
|
c->transfer_in_progress = true;
|
2011-11-19 00:29:55 +00:00
|
|
|
sim.insert(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
bool CHUNK::download_in_progress() {
|
2011-12-07 22:04:03 +00:00
|
|
|
set<CHUNK_ON_HOST*>::iterator i;
|
|
|
|
for (i=hosts.begin(); i!=hosts.end(); i++) {
|
|
|
|
CHUNK_ON_HOST* c = *i;
|
2011-12-14 01:34:51 +00:00
|
|
|
if (c->download_in_progress()) return true;
|
2011-12-07 22:04:03 +00:00
|
|
|
}
|
2011-12-14 01:34:51 +00:00
|
|
|
return false;
|
2011-12-07 22:04:03 +00:00
|
|
|
}
|
2011-12-14 01:34:51 +00:00
|
|
|
|
|
|
|
void 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
|
|
|
|
//
|
|
|
|
CHUNK_ON_HOST* c;
|
|
|
|
set<CHUNK_ON_HOST*>::iterator i;
|
|
|
|
for (i=hosts.begin(); i!=hosts.end(); i++) {
|
|
|
|
c = *i;
|
|
|
|
if (c->transfer_in_progress) return;
|
|
|
|
}
|
|
|
|
c = *(hosts.begin());
|
|
|
|
c->transfer_in_progress = true;
|
|
|
|
c->t = sim.now + (drand()+.5)*size/UPLOAD_BYTES_SEC;
|
2011-12-16 19:45:31 +00:00
|
|
|
parent->dfile->upload_rate.sample_inc(
|
|
|
|
UPLOAD_BYTES_SEC,
|
|
|
|
parent->dfile->collecting_stats()
|
|
|
|
);
|
2011-12-14 01:34:51 +00:00
|
|
|
#ifdef EVENT_DEBUG
|
|
|
|
printf("%s: starting upload of %s\n", now_str(), c->name);
|
2011-12-12 22:07:37 +00:00
|
|
|
#endif
|
2011-12-14 01:34:51 +00:00
|
|
|
sim.insert(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHUNK::host_failed(CHUNK_ON_HOST* p) {
|
|
|
|
set<CHUNK_ON_HOST*>::iterator i = hosts.find(p);
|
|
|
|
hosts.erase(i);
|
|
|
|
#ifdef EVENT_DEBUG
|
|
|
|
printf("%s: handling loss of %s\n", now_str(), p->name);
|
|
|
|
#endif
|
|
|
|
parent->dfile->recover();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHUNK::upload_complete() {
|
|
|
|
if (!present_on_server) {
|
|
|
|
present_on_server = true;
|
|
|
|
parent->dfile->disk_usage.sample_inc(
|
|
|
|
size,
|
|
|
|
parent->dfile->collecting_stats()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
parent->dfile->recover();
|
2011-12-16 19:45:31 +00:00
|
|
|
parent->dfile->upload_rate.sample_inc(
|
|
|
|
-UPLOAD_BYTES_SEC,
|
|
|
|
parent->dfile->collecting_stats()
|
|
|
|
);
|
2011-12-14 01:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CHUNK::download_complete() {
|
|
|
|
if (parent->dfile->pending_init_downloads) {
|
|
|
|
parent->dfile->pending_init_downloads--;
|
|
|
|
}
|
|
|
|
parent->dfile->recover();
|
2011-12-16 19:45:31 +00:00
|
|
|
parent->dfile->download_rate.sample_inc(
|
|
|
|
-DOWNLOAD_BYTES_SEC,
|
|
|
|
parent->dfile->collecting_stats()
|
|
|
|
);
|
2011-12-14 01:34:51 +00:00
|
|
|
}
|
2011-12-07 22:04:03 +00:00
|
|
|
|
2011-11-20 09:18:12 +00:00
|
|
|
META_CHUNK::META_CHUNK(
|
|
|
|
DFILE* d, META_CHUNK* par, double size, int encoding_level, int index
|
|
|
|
) {
|
|
|
|
dfile = d;
|
|
|
|
parent = par;
|
|
|
|
if (parent) {
|
|
|
|
sprintf(name, "%s.%d", parent->name, index);
|
|
|
|
} else {
|
|
|
|
sprintf(name, "%d", index);
|
|
|
|
}
|
|
|
|
if (encoding_level) {
|
|
|
|
for (int j=0; j<ENCODING_M; j++) {
|
|
|
|
children.push_back(new META_CHUNK(
|
|
|
|
d,
|
|
|
|
this,
|
|
|
|
size/ENCODING_N,
|
|
|
|
encoding_level-1,
|
|
|
|
j
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (int j=0; j<ENCODING_M; j++) {
|
|
|
|
children.push_back(new CHUNK(this, size/ENCODING_N, j));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-16 19:45:31 +00:00
|
|
|
// sort by increasing cost
|
|
|
|
//
|
|
|
|
bool compare_cost(const DATA_UNIT* d1, const DATA_UNIT* d2) {
|
|
|
|
return d1->cost < d2->cost;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sort by decreasing fault tolerance
|
|
|
|
//
|
|
|
|
bool compare_fault_tolerance(const DATA_UNIT* d1, const DATA_UNIT* d2) {
|
|
|
|
return d1->fault_tolerance > d2->fault_tolerance;
|
|
|
|
}
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
// Recovery logic: decide what to do in response to
|
|
|
|
// host failures and upload/download completions.
|
|
|
|
//
|
|
|
|
// One way to do this would be to store a bunch of state info
|
|
|
|
// with each node in the file's tree,
|
|
|
|
// and do things by local tree traversal.
|
|
|
|
//
|
|
|
|
// However, it's a lot simpler (for me, the programmer)
|
|
|
|
// to store minimal state info,
|
|
|
|
// and to reconstruct state info using
|
|
|
|
// a top-down tree traversal in response to each event.
|
|
|
|
// Actually we do 2 traversals:
|
|
|
|
// 1) plan phase:
|
|
|
|
// We see whether every node recoverable,
|
|
|
|
// and if so compute its "recovery set":
|
|
|
|
// the set of children from which it can be recovered
|
|
|
|
// with minimal cost (i.e. network traffic).
|
|
|
|
// Decide whether each chunk currently on the server needs to remain.
|
|
|
|
// 2) action phase
|
|
|
|
// Based on the results of phase 1,
|
|
|
|
// decide whether to start upload/download of chunks,
|
|
|
|
// and whether to delete data currently on server
|
2011-12-10 22:03:43 +00:00
|
|
|
//
|
2011-12-12 22:07:37 +00:00
|
|
|
void META_CHUNK::recovery_plan() {
|
|
|
|
vector<DATA_UNIT*> recoverable;
|
|
|
|
vector<DATA_UNIT*> present;
|
|
|
|
|
|
|
|
unsigned int i;
|
|
|
|
have_unrecoverable_children = false;
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
// make lists of children in various states
|
|
|
|
//
|
2011-12-12 22:07:37 +00:00
|
|
|
for (i=0; i<children.size(); i++) {
|
|
|
|
DATA_UNIT* c = children[i];
|
|
|
|
c->in_recovery_set = false;
|
2011-12-14 01:34:51 +00:00
|
|
|
c->data_needed = false;
|
|
|
|
c->data_now_present = false;
|
2011-12-12 22:07:37 +00:00
|
|
|
c->recovery_plan();
|
|
|
|
switch (c->status) {
|
|
|
|
case PRESENT:
|
|
|
|
present.push_back(c);
|
|
|
|
break;
|
|
|
|
case RECOVERABLE:
|
|
|
|
recoverable.push_back(c);
|
|
|
|
break;
|
|
|
|
case UNRECOVERABLE:
|
|
|
|
have_unrecoverable_children = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
// based on states of children, decide what state we're in
|
|
|
|
//
|
2011-12-12 22:07:37 +00:00
|
|
|
if (present.size() >= ENCODING_N) {
|
|
|
|
status = PRESENT;
|
2011-12-16 19:45:31 +00:00
|
|
|
sort(present.begin(), present.end(), compare_cost);
|
2011-12-12 22:07:37 +00:00
|
|
|
present.resize(ENCODING_N);
|
|
|
|
cost = 0;
|
2011-12-16 19:45:31 +00:00
|
|
|
fault_tolerance = INT_MAX;
|
2011-12-12 22:07:37 +00:00
|
|
|
for (i=0; i<present.size(); i++) {
|
|
|
|
DATA_UNIT* c= present[i];
|
|
|
|
cost += c->cost;
|
|
|
|
c->in_recovery_set = true;
|
|
|
|
}
|
|
|
|
} else if (present.size() + recoverable.size() >= ENCODING_N) {
|
|
|
|
status = RECOVERABLE;
|
2011-12-16 19:45:31 +00:00
|
|
|
unsigned int j = ENCODING_N - present.size();
|
|
|
|
sort(recoverable.begin(), recoverable.end(), compare_cost);
|
2011-12-12 22:07:37 +00:00
|
|
|
cost = 0;
|
|
|
|
for (i=0; i<present.size(); i++) {
|
|
|
|
DATA_UNIT* c= present[i];
|
|
|
|
c->in_recovery_set = true;
|
|
|
|
}
|
2011-12-16 19:45:31 +00:00
|
|
|
for (i=0; i<j; i++) {
|
2011-12-12 22:07:37 +00:00
|
|
|
DATA_UNIT* c= recoverable[i];
|
|
|
|
c->in_recovery_set = true;
|
|
|
|
cost += c->cost;
|
2011-12-10 22:03:43 +00:00
|
|
|
}
|
2011-12-16 19:45:31 +00:00
|
|
|
|
|
|
|
// compute our fault tolerance
|
|
|
|
//
|
|
|
|
sort(recoverable.begin(), recoverable.end(), compare_fault_tolerance);
|
|
|
|
fault_tolerance = 0;
|
|
|
|
for (i=0; i<j; i++) {
|
|
|
|
DATA_UNIT* c= recoverable[i];
|
|
|
|
fault_tolerance += c->fault_tolerance;
|
|
|
|
}
|
2011-12-12 22:07:37 +00:00
|
|
|
} else {
|
|
|
|
status = UNRECOVERABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
const char* status_str(int status) {
|
|
|
|
switch (status) {
|
|
|
|
case PRESENT: return "present";
|
|
|
|
case RECOVERABLE: return "recoverable";
|
|
|
|
case UNRECOVERABLE: return "unrecoverable";
|
|
|
|
}
|
|
|
|
return "unknown";
|
|
|
|
}
|
|
|
|
|
2011-12-12 22:07:37 +00:00
|
|
|
void CHUNK::recovery_plan() {
|
|
|
|
if (present_on_server) {
|
|
|
|
status = PRESENT;
|
|
|
|
cost = 0;
|
2011-12-16 19:45:31 +00:00
|
|
|
fault_tolerance = INT_MAX;
|
2011-12-12 22:07:37 +00:00
|
|
|
} else if (hosts.size() > 0) {
|
|
|
|
status = RECOVERABLE;
|
|
|
|
cost = size;
|
|
|
|
if (hosts.size() < REPLICATION_LEVEL) {
|
|
|
|
data_needed = true;
|
|
|
|
}
|
2011-12-16 19:45:31 +00:00
|
|
|
fault_tolerance = hosts.size();
|
2011-12-12 22:07:37 +00:00
|
|
|
} else {
|
|
|
|
status = UNRECOVERABLE;
|
2011-12-16 19:45:31 +00:00
|
|
|
fault_tolerance = 0;
|
2011-12-10 22:03:43 +00:00
|
|
|
}
|
2011-12-14 01:34:51 +00:00
|
|
|
#ifdef DEBUG_RECOVERY
|
|
|
|
printf("chunk plan %s: status %s\n", name, status_str(status));
|
|
|
|
#endif
|
2011-12-12 22:07:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void META_CHUNK::recovery_action() {
|
|
|
|
if (data_now_present) {
|
|
|
|
status = PRESENT;
|
|
|
|
}
|
2011-12-14 01:34:51 +00:00
|
|
|
#ifdef DEBUG_RECOVERY
|
|
|
|
printf("meta chunk action %s state %s unrec children %d\n",
|
|
|
|
name, status_str(status), have_unrecoverable_children
|
|
|
|
);
|
|
|
|
#endif
|
2011-12-12 22:07:37 +00:00
|
|
|
for (unsigned i=0; i<children.size(); i++) {
|
|
|
|
DATA_UNIT* c = children[i];
|
2011-12-14 01:34:51 +00:00
|
|
|
#ifdef DEBUG_RECOVERY
|
|
|
|
printf(" child %s status %s in rec set %d\n",
|
|
|
|
c->name, status_str(c->status), c->in_recovery_set
|
|
|
|
);
|
|
|
|
#endif
|
|
|
|
switch (status) {
|
|
|
|
case PRESENT:
|
|
|
|
if (c->status == UNRECOVERABLE) {
|
|
|
|
c->data_now_present = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case RECOVERABLE:
|
2011-12-12 22:07:37 +00:00
|
|
|
if (c->in_recovery_set && have_unrecoverable_children) {
|
|
|
|
c->data_needed = true;
|
|
|
|
}
|
2011-12-14 01:34:51 +00:00
|
|
|
break;
|
|
|
|
case UNRECOVERABLE:
|
|
|
|
break;
|
2011-12-12 22:07:37 +00:00
|
|
|
}
|
|
|
|
c->recovery_action();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CHUNK::recovery_action() {
|
|
|
|
if (data_now_present) {
|
|
|
|
present_on_server = true;
|
2011-12-10 22:03:43 +00:00
|
|
|
parent->dfile->disk_usage.sample_inc(
|
2011-12-12 22:07:37 +00:00
|
|
|
size,
|
2011-12-10 22:03:43 +00:00
|
|
|
parent->dfile->collecting_stats()
|
|
|
|
);
|
2011-12-12 22:07:37 +00:00
|
|
|
status = PRESENT;
|
2011-12-10 22:03:43 +00:00
|
|
|
}
|
2011-12-12 22:07:37 +00:00
|
|
|
if (status == PRESENT && hosts.size() < REPLICATION_LEVEL) {
|
|
|
|
assign();
|
|
|
|
}
|
2011-12-14 01:34:51 +00:00
|
|
|
if (download_in_progress()) {
|
|
|
|
data_needed = true;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_RECOVERY
|
|
|
|
printf("chunk action: %s data_needed %d present_on_server %d\n",
|
|
|
|
name, data_needed, present_on_server
|
|
|
|
);
|
|
|
|
#endif
|
2011-12-12 22:07:37 +00:00
|
|
|
if (data_needed) {
|
|
|
|
if (!present_on_server) {
|
|
|
|
start_upload();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (present_on_server) {
|
|
|
|
present_on_server = false;
|
2011-12-14 01:34:51 +00:00
|
|
|
#ifdef EVENT_DEBUG
|
2011-12-12 22:07:37 +00:00
|
|
|
printf("%s: %s replicated, removing from server\n", now_str(), name);
|
2011-12-14 01:34:51 +00:00
|
|
|
#endif
|
2011-12-12 22:07:37 +00:00
|
|
|
parent->dfile->disk_usage.sample_inc(
|
|
|
|
-size,
|
|
|
|
parent->dfile->collecting_stats()
|
|
|
|
);
|
|
|
|
}
|
2011-12-10 22:03:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-03 03:29:07 +00:00
|
|
|
set<DFILE*> dfiles;
|
|
|
|
|
|
|
|
int main() {
|
2011-11-20 09:18:12 +00:00
|
|
|
#if 0
|
2011-11-03 03:29:07 +00:00
|
|
|
HOST_ARRIVAL *h = new HOST_ARRIVAL;
|
|
|
|
h->t = 0;
|
|
|
|
sim.insert(h);
|
2011-11-20 09:18:12 +00:00
|
|
|
#endif
|
|
|
|
|
2011-12-14 01:34:51 +00:00
|
|
|
for (int i=0; i<500; i++) {
|
2011-11-20 09:18:12 +00:00
|
|
|
sim.insert(new HOST);
|
|
|
|
}
|
2011-12-06 04:21:27 +00:00
|
|
|
DFILE* dfile = new DFILE(1e2);
|
|
|
|
sim.insert(dfile);
|
2011-11-03 03:29:07 +00:00
|
|
|
|
|
|
|
sim.simulate(200*86400);
|
2011-12-06 04:21:27 +00:00
|
|
|
|
|
|
|
printf("%s: simulation finished\n", now_str());
|
|
|
|
dfile->print_stats();
|
2011-11-03 03:29:07 +00:00
|
|
|
}
|