/usr/share/pyshared/allmydata/reliability.py is in tahoe-lafs 1.9.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | #! /usr/bin/python
import math
from allmydata.util import statistics
from numpy import array, matrix, dot
DAY=24*60*60
MONTH=31*DAY
YEAR=365*DAY
class ReliabilityModel:
"""Generate a model of system-wide reliability, given several input
parameters.
This runs a simulation in which time is quantized down to 'delta' seconds
(default is one month): a smaller delta will result in a more accurate
simulation, but will take longer to run. 'report_span' simulated seconds
will be run.
The encoding parameters are provided as 'k' (minimum number of shares
needed to recover the file) and 'N' (total number of shares generated).
The default parameters are 3-of-10.
The first step is to build a probability of individual drive loss during
any given delta. This uses a simple exponential model, in which the
average drive lifetime is specified by the 'drive_lifetime' parameter
(default is 8 years).
The second step is to calculate a 'transition matrix': a table of
probabilities that shows, given A shares at the start of the delta, what
the chances are of having B shares left at the end of the delta. The
current code optimistically assumes all drives are independent. A
subclass could override that assumption.
An additional 'repair matrix' is created to show what happens when the
Checker/Repairer is run. In the simulation, the Checker will be run every
'check_period' seconds (default is one month), and the Repairer will be
run if it sees fewer than 'R' shares (default 7).
The third step is to finally run the simulation. An initial probability
vector is created (with a 100% chance of N shares and a 0% chance of
fewer than N shares), then it is multiplied by the transition matrix for
every delta of time. Each time the Checker is to be run, the repair
matrix is multiplied in, and some additional stats are accumulated
(average number of repairs that occur, average number of shares
regenerated per repair).
The output is a ReliabilityReport instance, which contains a table that
samples the state of the simulation once each 'report_period' seconds
(defaults to 3 months). Each row of this table will contain the
probability vector for one sample period (chance of having X shares, from
0 to N, at the end of the period). The report will also contain other
information.
"""
@classmethod
def run(klass,
drive_lifetime=8*YEAR,
k=3, R=7, N=10,
delta=1*MONTH,
check_period=1*MONTH,
report_period=3*MONTH,
report_span=5*YEAR,
):
self = klass()
check_period = check_period-1
P = self.p_in_period(drive_lifetime, delta)
decay = self.build_decay_matrix(N, P)
repair = self.build_repair_matrix(k, N, R)
#print "DECAY:", decay
#print "OLD-POST-REPAIR:", old_post_repair
#print "NEW-POST-REPAIR:", decay * repair
#print "REPAIR:", repair
#print "DIFF:", (old_post_repair - decay * repair)
START = array([0]*N + [1])
DEAD = array([1]*k + [0]*(1+N-k))
REPAIRp = array([0]*k + [1]*(R-k) + [0]*(1+N-R))
REPAIR_newshares = array([0]*k +
[N-i for i in range(k, R)] +
[0]*(1+N-R))
assert REPAIR_newshares.shape[0] == N+1
#print "START", START
#print "REPAIRp", REPAIRp
#print "REPAIR_newshares", REPAIR_newshares
unmaintained_state = START
maintained_state = START
last_check = 0
last_report = 0
P_repaired_last_check_period = 0.0
needed_repairs = []
needed_new_shares = []
report = ReliabilityReport()
for t in range(0, report_span+delta, delta):
# the .A[0] turns the one-row matrix back into an array
unmaintained_state = (unmaintained_state * decay).A[0]
maintained_state = (maintained_state * decay).A[0]
if (t-last_check) > check_period:
last_check = t
# we do a check-and-repair this frequently
need_repair = dot(maintained_state, REPAIRp)
P_repaired_last_check_period = need_repair
new_shares = dot(maintained_state, REPAIR_newshares)
needed_repairs.append(need_repair)
needed_new_shares.append(new_shares)
maintained_state = (maintained_state * repair).A[0]
if (t-last_report) > report_period:
last_report = t
P_dead_unmaintained = dot(unmaintained_state, DEAD)
P_dead_maintained = dot(maintained_state, DEAD)
cumulative_number_of_repairs = sum(needed_repairs)
cumulative_number_of_new_shares = sum(needed_new_shares)
report.add_sample(t, unmaintained_state, maintained_state,
P_repaired_last_check_period,
cumulative_number_of_repairs,
cumulative_number_of_new_shares,
P_dead_unmaintained, P_dead_maintained)
# record one more sample at the end of the run
P_dead_unmaintained = dot(unmaintained_state, DEAD)
P_dead_maintained = dot(maintained_state, DEAD)
cumulative_number_of_repairs = sum(needed_repairs)
cumulative_number_of_new_shares = sum(needed_new_shares)
report.add_sample(t, unmaintained_state, maintained_state,
P_repaired_last_check_period,
cumulative_number_of_repairs,
cumulative_number_of_new_shares,
P_dead_unmaintained, P_dead_maintained)
#def yandm(seconds):
# return "%dy.%dm" % (int(seconds/YEAR), int( (seconds%YEAR)/MONTH))
#needed_repairs_total = sum(needed_repairs)
#needed_new_shares_total = sum(needed_new_shares)
#print "at 2y:"
#print " unmaintained", unmaintained_state
#print " maintained", maintained_state
#print " number of repairs", needed_repairs_total
#print " new shares generated", needed_new_shares_total
#repair_rate_inv = report_span / needed_repairs_total
#print " avg repair rate: once every %s" % yandm(repair_rate_inv)
#print " avg repair download: one share every %s" % yandm(repair_rate_inv/k)
#print " avg repair upload: one share every %s" % yandm(report_span / needed_new_shares_total)
return report
def p_in_period(self, avg_lifetime, period):
"""Given an average lifetime of a disk (using an exponential model),
what is the chance that a live disk will survive the next 'period'
seconds?"""
# eg p_in_period(8*YEAR, MONTH) = 98.94%
return math.exp(-1.0*period/avg_lifetime)
def build_decay_matrix(self, N, P):
"""Return a decay matrix. decay[start_shares][end_shares] is the
conditional probability of finishing with end_shares, given that we
started with start_shares."""
decay_rows = []
decay_rows.append( [0.0]*(N+1) )
for start_shares in range(1, (N+1)):
end_shares = self.build_decay_row(start_shares, P)
decay_row = end_shares + [0.0] * (N-start_shares)
assert len(decay_row) == (N+1), len(decay_row)
decay_rows.append(decay_row)
decay = matrix(decay_rows)
return decay
def build_decay_row(self, start_shares, P):
"""Return a decay row 'end_shares'. end_shares[i] is the chance that
we finish with i shares, given that we started with start_shares, for
all i between 0 and start_shares, inclusive. This implementation
assumes that all shares are independent (IID), but a more complex
model could incorporate inter-share failure correlations like having
two shares on the same server."""
end_shares = statistics.binomial_distribution_pmf(start_shares, P)
return end_shares
def build_repair_matrix(self, k, N, R):
"""Return a repair matrix. repair[start][end]: is the conditional
probability of the repairer finishing with 'end' shares, given that
it began with 'start' shares (repair if fewer than R shares). The
repairer's behavior is deterministic, so all values in this matrix
are either 0 or 1. This matrix should be applied *after* the decay
matrix."""
new_repair_rows = []
for start_shares in range(0, N+1):
new_repair_row = [0] * (N+1)
if start_shares < k:
new_repair_row[start_shares] = 1
elif start_shares < R:
new_repair_row[N] = 1
else:
new_repair_row[start_shares] = 1
new_repair_rows.append(new_repair_row)
repair = matrix(new_repair_rows)
return repair
class ReliabilityReport:
def __init__(self):
self.samples = []
def add_sample(self, when, unmaintained_shareprobs, maintained_shareprobs,
P_repaired_last_check_period,
cumulative_number_of_repairs,
cumulative_number_of_new_shares,
P_dead_unmaintained, P_dead_maintained):
"""
when: the timestamp at the end of the report period
unmaintained_shareprobs: a vector of probabilities, element[S]
is the chance that there are S shares
left at the end of the report period.
This tracks what happens if no repair
is ever done.
maintained_shareprobs: same, but for 'maintained' grids, where
check and repair is done at the end
of each check period
P_repaired_last_check_period: a float, with the probability
that a repair was performed
at the end of the most recent
check period.
cumulative_number_of_repairs: a float, with the average number
of repairs that will have been
performed by the end of the
report period
cumulative_number_of_new_shares: a float, with the average number
of new shares that repair proceses
generated by the end of the report
period
P_dead_unmaintained: a float, with the chance that the file will
be unrecoverable at the end of the period
P_dead_maintained: same, but for maintained grids
"""
row = (when, unmaintained_shareprobs, maintained_shareprobs,
P_repaired_last_check_period,
cumulative_number_of_repairs,
cumulative_number_of_new_shares,
P_dead_unmaintained, P_dead_maintained)
self.samples.append(row)
|