]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/check_results.py
c1b791c55fcdb37653d736a4e23bb2a8f6b02daa
[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
5 from allmydata.util import base32
6
7 class CheckResults:
8     implements(ICheckResults)
9
10     def __init__(self, uri, storage_index):
11         assert IURI.providedBy(uri), uri
12         self.uri = uri
13         self.storage_index = storage_index
14         self.problems = []
15         self.summary = ""
16         self.report = []
17
18     def set_healthy(self, healthy):
19         self.healthy = bool(healthy)
20         if self.healthy:
21             assert (not hasattr(self, 'recoverable')) or self.recoverable, hasattr(self, 'recoverable') and self.recoverable
22             self.recoverable = True
23             self.summary = "healthy"
24         else:
25             self.summary = "not healthy"
26     def set_recoverable(self, recoverable):
27         self.recoverable = recoverable
28         if not self.recoverable:
29             assert (not hasattr(self, 'healthy')) or not self.healthy
30             self.healthy = False
31     def set_needs_rebalancing(self, needs_rebalancing):
32         self.needs_rebalancing_p = bool(needs_rebalancing)
33     def set_data(self,
34                  count_shares_needed, count_shares_expected,
35                  count_shares_good, count_good_share_hosts,
36                  count_recoverable_versions, count_unrecoverable_versions,
37                  servers_responding, sharemap,
38                  count_wrong_shares, list_corrupt_shares, count_corrupt_shares,
39                  list_incompatible_shares, count_incompatible_shares):
40         data = {"count-shares-needed": count_shares_needed,
41                 "count-shares-expected": count_shares_expected,
42                 "count-shares-good": count_shares_good,
43                 "count-good-share-hosts": count_good_share_hosts,
44                 "count-recoverable-versions": count_recoverable_versions,
45                 "count-unrecoverable-versions": count_unrecoverable_versions,
46                 "servers-responding": servers_responding,
47                 "sharemap": sharemap,
48                 "count-wrong-shares": count_wrong_shares,
49                 "list-corrupt-shares": list_corrupt_shares,
50                 "count-corrupt-shares": count_corrupt_shares,
51                 "list-incompatible-shares": list_incompatible_shares,
52                 "count-incompatible-shares": count_incompatible_shares,
53                 }
54         self._data = data
55     def set_summary(self, summary):
56         assert isinstance(summary, str) # should be a single string
57         self.summary = summary
58     def set_report(self, report):
59         assert not isinstance(report, str) # should be list of strings
60         self.report = report
61
62     def set_servermap(self, smap):
63         # mutable only
64         self.servermap = smap
65
66
67     def get_storage_index(self):
68         return self.storage_index
69     def get_storage_index_string(self):
70         return base32.b2a(self.storage_index)
71     def get_uri(self):
72         return self.uri
73
74     def is_healthy(self):
75         return self.healthy
76     def is_recoverable(self):
77         return self.recoverable
78
79     def needs_rebalancing(self):
80         return self.needs_rebalancing_p
81     def get_data(self):
82         return self._data
83
84     def get_summary(self):
85         return self.summary
86     def get_report(self):
87         return self.report
88     def get_servermap(self):
89         return self.servermap
90
91 class CheckAndRepairResults:
92     implements(ICheckAndRepairResults)
93
94     def __init__(self, storage_index):
95         self.storage_index = storage_index
96         self.repair_attempted = False
97
98     def get_storage_index(self):
99         return self.storage_index
100     def get_storage_index_string(self):
101         return base32.b2a(self.storage_index)
102     def get_repair_attempted(self):
103         return self.repair_attempted
104     def get_repair_successful(self):
105         if not self.repair_attempted:
106             return False
107         return self.repair_successful
108     def get_pre_repair_results(self):
109         return self.pre_repair_results
110     def get_post_repair_results(self):
111         return self.post_repair_results
112
113
114 class DeepResultsBase:
115
116     def __init__(self, root_storage_index):
117         self.root_storage_index = root_storage_index
118         if root_storage_index is None:
119             self.root_storage_index_s = "<none>"  # is this correct?
120         else:
121             self.root_storage_index_s = base32.b2a(root_storage_index)
122
123         self.objects_checked = 0
124         self.objects_healthy = 0
125         self.objects_unhealthy = 0
126         self.objects_unrecoverable = 0
127         self.corrupt_shares = []
128         self.all_results = {}
129         self.all_results_by_storage_index = {}
130         self.stats = {}
131
132     def update_stats(self, new_stats):
133         self.stats.update(new_stats)
134
135     def get_root_storage_index_string(self):
136         return self.root_storage_index_s
137
138     def get_corrupt_shares(self):
139         return self.corrupt_shares
140
141     def get_all_results(self):
142         return self.all_results
143
144     def get_results_for_storage_index(self, storage_index):
145         return self.all_results_by_storage_index[storage_index]
146
147     def get_stats(self):
148         return self.stats
149
150
151 class DeepCheckResults(DeepResultsBase):
152     implements(IDeepCheckResults)
153
154     def add_check(self, r, path):
155         if not r:
156             return # non-distributed object, i.e. LIT file
157         r = ICheckResults(r)
158         assert isinstance(path, (list, tuple))
159         self.objects_checked += 1
160         if r.is_healthy():
161             self.objects_healthy += 1
162         else:
163             self.objects_unhealthy += 1
164         if not r.is_recoverable():
165             self.objects_unrecoverable += 1
166         self.all_results[tuple(path)] = r
167         self.all_results_by_storage_index[r.get_storage_index()] = r
168         self.corrupt_shares.extend(r.get_data()["list-corrupt-shares"])
169
170     def get_counters(self):
171         return {"count-objects-checked": self.objects_checked,
172                 "count-objects-healthy": self.objects_healthy,
173                 "count-objects-unhealthy": self.objects_unhealthy,
174                 "count-objects-unrecoverable": self.objects_unrecoverable,
175                 "count-corrupt-shares": len(self.corrupt_shares),
176                 }
177
178
179 class DeepCheckAndRepairResults(DeepResultsBase):
180     implements(IDeepCheckAndRepairResults)
181
182     def __init__(self, root_storage_index):
183         DeepResultsBase.__init__(self, root_storage_index)
184         self.objects_healthy_post_repair = 0
185         self.objects_unhealthy_post_repair = 0
186         self.objects_unrecoverable_post_repair = 0
187         self.repairs_attempted = 0
188         self.repairs_successful = 0
189         self.repairs_unsuccessful = 0
190         self.corrupt_shares_post_repair = []
191
192     def add_check_and_repair(self, r, path):
193         if not r:
194             return # non-distributed object, i.e. LIT file
195         r = ICheckAndRepairResults(r)
196         assert isinstance(path, (list, tuple))
197         pre_repair = r.get_pre_repair_results()
198         post_repair = r.get_post_repair_results()
199         self.objects_checked += 1
200         if pre_repair.is_healthy():
201             self.objects_healthy += 1
202         else:
203             self.objects_unhealthy += 1
204         if not pre_repair.is_recoverable():
205             self.objects_unrecoverable += 1
206         self.corrupt_shares.extend(pre_repair.get_data()["list-corrupt-shares"])
207         if r.get_repair_attempted():
208             self.repairs_attempted += 1
209             if r.get_repair_successful():
210                 self.repairs_successful += 1
211             else:
212                 self.repairs_unsuccessful += 1
213         if post_repair.is_healthy():
214             self.objects_healthy_post_repair += 1
215         else:
216             self.objects_unhealthy_post_repair += 1
217         if not post_repair.is_recoverable():
218             self.objects_unrecoverable_post_repair += 1
219         self.all_results[tuple(path)] = r
220         self.all_results_by_storage_index[r.get_storage_index()] = r
221         self.corrupt_shares_post_repair.extend(post_repair.get_data()["list-corrupt-shares"])
222
223     def get_counters(self):
224         return {"count-objects-checked": self.objects_checked,
225                 "count-objects-healthy-pre-repair": self.objects_healthy,
226                 "count-objects-unhealthy-pre-repair": self.objects_unhealthy,
227                 "count-objects-unrecoverable-pre-repair": self.objects_unrecoverable,
228                 "count-objects-healthy-post-repair": self.objects_healthy_post_repair,
229                 "count-objects-unhealthy-post-repair": self.objects_unhealthy_post_repair,
230                 "count-objects-unrecoverable-post-repair": self.objects_unrecoverable_post_repair,
231                 "count-repairs-attempted": self.repairs_attempted,
232                 "count-repairs-successful": self.repairs_successful,
233                 "count-repairs-unsuccessful": self.repairs_unsuccessful,
234                 "count-corrupt-shares-pre-repair": len(self.corrupt_shares),
235                 "count-corrupt-shares-post-repair": len(self.corrupt_shares_post_repair),
236                 }
237
238     def get_remaining_corrupt_shares(self):
239         return self.corrupt_shares_post_repair