]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/check_results.py
e9de13caea037d5ba9baab6ff565055281196291
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / check_results.py
1
2 from zope.interface import implements
3 from allmydata.interfaces import ICheckResults, ICheckAndRepairResults, \
4      IDeepCheckResults, IDeepCheckAndRepairResults, IURI, IDisplayableServer
5 from allmydata.util import base32
6
7 class CheckResults:
8     implements(ICheckResults)
9
10     def __init__(self, uri, storage_index,
11                  healthy, recoverable, needs_rebalancing,
12                  count_shares_needed, count_shares_expected,
13                  count_shares_good, count_good_share_hosts,
14                  count_recoverable_versions, count_unrecoverable_versions,
15                  servers_responding, sharemap,
16                  count_wrong_shares, list_corrupt_shares, count_corrupt_shares,
17                  list_incompatible_shares, count_incompatible_shares,
18                  summary, report, share_problems, servermap):
19         assert IURI.providedBy(uri), uri
20         self._uri = uri
21         self._storage_index = storage_index
22         self._summary = ""
23         self._healthy = bool(healthy)
24         if self._healthy:
25             assert recoverable
26             if not summary:
27                 summary = "healthy"
28         else:
29             if not summary:
30                 summary = "not healthy"
31         self._recoverable = recoverable
32         if not self._recoverable:
33             assert not self._healthy
34         self._needs_rebalancing_p = bool(needs_rebalancing)
35
36         self._count_shares_needed = count_shares_needed
37         self._count_shares_expected = count_shares_expected
38         self._count_shares_good = count_shares_good
39         self._count_good_share_hosts = count_good_share_hosts
40         self._count_recoverable_versions = count_recoverable_versions
41         self._count_unrecoverable_versions = count_unrecoverable_versions
42         for server in servers_responding:
43             assert IDisplayableServer.providedBy(server), server
44         self._servers_responding = servers_responding
45         for shnum, servers in sharemap.items():
46             for server in servers:
47                 assert IDisplayableServer.providedBy(server), server
48         self._sharemap = sharemap
49         self._count_wrong_shares = count_wrong_shares
50         for (server, SI, shnum) in list_corrupt_shares:
51             assert IDisplayableServer.providedBy(server), server
52         self._list_corrupt_shares = list_corrupt_shares
53         self._count_corrupt_shares = count_corrupt_shares
54         for (server, SI, shnum) in list_incompatible_shares:
55             assert IDisplayableServer.providedBy(server), server
56         self._list_incompatible_shares = list_incompatible_shares
57         self._count_incompatible_shares = count_incompatible_shares
58
59         assert isinstance(summary, str) # should be a single string
60         self._summary = summary
61         assert not isinstance(report, str) # should be list of strings
62         self._report = report
63         if servermap:
64             from allmydata.mutable.servermap import ServerMap
65             assert isinstance(servermap, ServerMap), servermap
66         self._servermap = servermap # mutable only
67         self._share_problems = share_problems
68
69     def get_storage_index(self):
70         return self._storage_index
71     def get_storage_index_string(self):
72         return base32.b2a(self._storage_index)
73     def get_uri(self):
74         return self._uri
75
76     def is_healthy(self):
77         return self._healthy
78     def is_recoverable(self):
79         return self._recoverable
80
81     def needs_rebalancing(self):
82         return self._needs_rebalancing_p
83
84     def get_encoding_needed(self):
85         return self._count_shares_needed
86     def get_encoding_expected(self):
87         return self._count_shares_expected
88
89     def get_share_counter_good(self):
90         return self._count_shares_good
91     def get_share_counter_wrong(self):
92         return self._count_wrong_shares
93
94     def get_new_corrupt_shares(self):
95         return self._list_corrupt_shares
96     def get_corrupt_shares(self):
97         return [(s.get_serverid(), SI, shnum)
98                 for (s, SI, shnum) in self._list_corrupt_shares]
99
100     def get_new_incompatible_shares(self):
101         return self._list_incompatible_shares
102     def get_incompatible_shares(self):
103         return [(s.get_serverid(), SI, shnum)
104                 for (s, SI, shnum) in self._list_incompatible_shares]
105
106     def get_new_servers_responding(self):
107         return self._servers_responding
108     def get_servers_responding(self):
109         return [s.get_serverid() for s in self._servers_responding]
110
111     def get_host_counter_good_shares(self):
112         return self._count_good_share_hosts
113
114     def get_version_counter_recoverable(self):
115         return self._count_recoverable_versions
116     def get_version_counter_unrecoverable(self):
117         return self._count_unrecoverable_versions
118
119     def get_sharemap(self):
120         return self._sharemap
121
122     def as_dict(self):
123         sharemap = {}
124         for shnum, servers in self._sharemap.items():
125             sharemap[shnum] = sorted([s.get_serverid() for s in servers])
126         responding = [s.get_serverid() for s in self._servers_responding]
127         corrupt = [(s.get_serverid(), SI, shnum)
128                    for (s, SI, shnum) in self._list_corrupt_shares]
129         incompatible = [(s.get_serverid(), SI, shnum)
130                         for (s, SI, shnum) in self._list_incompatible_shares]
131         d = {"count-shares-needed": self._count_shares_needed,
132              "count-shares-expected": self._count_shares_expected,
133              "count-shares-good": self._count_shares_good,
134              "count-good-share-hosts": self._count_good_share_hosts,
135              "count-recoverable-versions": self._count_recoverable_versions,
136              "count-unrecoverable-versions": self._count_unrecoverable_versions,
137              "servers-responding": responding,
138              "sharemap": sharemap,
139              "count-wrong-shares": self._count_wrong_shares,
140              "list-corrupt-shares": corrupt,
141              "count-corrupt-shares": self._count_corrupt_shares,
142              "list-incompatible-shares": incompatible,
143              "count-incompatible-shares": self._count_incompatible_shares,
144              }
145         return d
146
147     def get_summary(self):
148         return self._summary
149     def get_report(self):
150         return self._report
151     def get_share_problems(self):
152         return self._share_problems
153     def get_servermap(self):
154         return self._servermap
155
156 class CheckAndRepairResults:
157     implements(ICheckAndRepairResults)
158
159     def __init__(self, storage_index):
160         self.storage_index = storage_index
161         self.repair_attempted = False
162
163     def get_storage_index(self):
164         return self.storage_index
165     def get_storage_index_string(self):
166         return base32.b2a(self.storage_index)
167     def get_repair_attempted(self):
168         return self.repair_attempted
169     def get_repair_successful(self):
170         if not self.repair_attempted:
171             return False
172         return self.repair_successful
173     def get_pre_repair_results(self):
174         return self.pre_repair_results
175     def get_post_repair_results(self):
176         return self.post_repair_results
177
178
179 class DeepResultsBase:
180
181     def __init__(self, root_storage_index):
182         self.root_storage_index = root_storage_index
183         if root_storage_index is None:
184             self.root_storage_index_s = "<none>"  # is this correct?
185         else:
186             self.root_storage_index_s = base32.b2a(root_storage_index)
187
188         self.objects_checked = 0
189         self.objects_healthy = 0
190         self.objects_unhealthy = 0
191         self.objects_unrecoverable = 0
192         self.corrupt_shares = []
193         self.all_results = {}
194         self.all_results_by_storage_index = {}
195         self.stats = {}
196
197     def update_stats(self, new_stats):
198         self.stats.update(new_stats)
199
200     def get_root_storage_index_string(self):
201         return self.root_storage_index_s
202
203     def get_corrupt_shares(self):
204         return self.corrupt_shares
205
206     def get_all_results(self):
207         return self.all_results
208
209     def get_results_for_storage_index(self, storage_index):
210         return self.all_results_by_storage_index[storage_index]
211
212     def get_stats(self):
213         return self.stats
214
215
216 class DeepCheckResults(DeepResultsBase):
217     implements(IDeepCheckResults)
218
219     def add_check(self, r, path):
220         if not r:
221             return # non-distributed object, i.e. LIT file
222         r = ICheckResults(r)
223         assert isinstance(path, (list, tuple))
224         self.objects_checked += 1
225         if r.is_healthy():
226             self.objects_healthy += 1
227         else:
228             self.objects_unhealthy += 1
229         if not r.is_recoverable():
230             self.objects_unrecoverable += 1
231         self.all_results[tuple(path)] = r
232         self.all_results_by_storage_index[r.get_storage_index()] = r
233         self.corrupt_shares.extend(r.get_corrupt_shares())
234
235     def get_counters(self):
236         return {"count-objects-checked": self.objects_checked,
237                 "count-objects-healthy": self.objects_healthy,
238                 "count-objects-unhealthy": self.objects_unhealthy,
239                 "count-objects-unrecoverable": self.objects_unrecoverable,
240                 "count-corrupt-shares": len(self.corrupt_shares),
241                 }
242
243
244 class DeepCheckAndRepairResults(DeepResultsBase):
245     implements(IDeepCheckAndRepairResults)
246
247     def __init__(self, root_storage_index):
248         DeepResultsBase.__init__(self, root_storage_index)
249         self.objects_healthy_post_repair = 0
250         self.objects_unhealthy_post_repair = 0
251         self.objects_unrecoverable_post_repair = 0
252         self.repairs_attempted = 0
253         self.repairs_successful = 0
254         self.repairs_unsuccessful = 0
255         self.corrupt_shares_post_repair = []
256
257     def add_check_and_repair(self, r, path):
258         if not r:
259             return # non-distributed object, i.e. LIT file
260         r = ICheckAndRepairResults(r)
261         assert isinstance(path, (list, tuple))
262         pre_repair = r.get_pre_repair_results()
263         post_repair = r.get_post_repair_results()
264         self.objects_checked += 1
265         if pre_repair.is_healthy():
266             self.objects_healthy += 1
267         else:
268             self.objects_unhealthy += 1
269         if not pre_repair.is_recoverable():
270             self.objects_unrecoverable += 1
271         self.corrupt_shares.extend(pre_repair.get_corrupt_shares())
272         if r.get_repair_attempted():
273             self.repairs_attempted += 1
274             if r.get_repair_successful():
275                 self.repairs_successful += 1
276             else:
277                 self.repairs_unsuccessful += 1
278         if post_repair.is_healthy():
279             self.objects_healthy_post_repair += 1
280         else:
281             self.objects_unhealthy_post_repair += 1
282         if not post_repair.is_recoverable():
283             self.objects_unrecoverable_post_repair += 1
284         self.all_results[tuple(path)] = r
285         self.all_results_by_storage_index[r.get_storage_index()] = r
286         self.corrupt_shares_post_repair.extend(post_repair.get_corrupt_shares())
287
288     def get_counters(self):
289         return {"count-objects-checked": self.objects_checked,
290                 "count-objects-healthy-pre-repair": self.objects_healthy,
291                 "count-objects-unhealthy-pre-repair": self.objects_unhealthy,
292                 "count-objects-unrecoverable-pre-repair": self.objects_unrecoverable,
293                 "count-objects-healthy-post-repair": self.objects_healthy_post_repair,
294                 "count-objects-unhealthy-post-repair": self.objects_unhealthy_post_repair,
295                 "count-objects-unrecoverable-post-repair": self.objects_unrecoverable_post_repair,
296                 "count-repairs-attempted": self.repairs_attempted,
297                 "count-repairs-successful": self.repairs_successful,
298                 "count-repairs-unsuccessful": self.repairs_unsuccessful,
299                 "count-corrupt-shares-pre-repair": len(self.corrupt_shares),
300                 "count-corrupt-shares-post-repair": len(self.corrupt_shares_post_repair),
301                 }
302
303     def get_remaining_corrupt_shares(self):
304         return self.corrupt_shares_post_repair