forked from troubling/hummingbird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispersion.go
364 lines (352 loc) · 10.8 KB
/
dispersion.go
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
package tools
import (
"flag"
"fmt"
"runtime"
"sort"
"strings"
"sync"
"time"
"github.com/troubling/hummingbird/common/conf"
"github.com/troubling/hummingbird/common/ring"
)
var partitionListCap = 4
type dispersionReport struct {
Name string
Time time.Time
Pass bool
Errors []string
IssueCount int
ContainerReport *innerDispersionReport
ObjectReports map[int]*innerDispersionReport
}
func (r *dispersionReport) Passed() bool {
return r.Pass
}
func (r *dispersionReport) String() string {
s := fmt.Sprintf(
"[%s] %s\n",
r.Time.Format("2006-01-02 15:04:05"),
r.Name,
)
for _, e := range r.Errors {
s += fmt.Sprintf("!! %s\n", e)
}
if r.ContainerReport != nil {
s += fmt.Sprintf("\nContainer Dispersion Report\n%s", r.ContainerReport.String())
}
var policies []int
for policy := range r.ObjectReports {
policies = append(policies, policy)
}
sort.Ints(policies)
for policy := range policies {
or := r.ObjectReports[policy]
s += fmt.Sprintf("\nObject Dispersion Report for Policy: %d %s\n%s", policy, or.PolicyName, or.String())
}
return s
}
type innerDispersionReport struct {
PolicyName string
Start time.Time
Complete time.Time
TotalPartitions int
ReplicaCount int
Partitions map[int][]*dispersionMissing
ScanFailures map[int][]*scanFailure
}
type scanFailure struct {
Time time.Time
Service string
DeviceID int
}
func (r *innerDispersionReport) String() string {
addPartitionListCap := func(s []string, p int) []string {
if len(s) < partitionListCap {
return append(s, fmt.Sprintf("%d", p))
}
if len(s) == partitionListCap {
s[partitionListCap-1] = "..."
}
return s
}
var s string
if r.Start.IsZero() {
s += " No known dispersion scan has been run.\n"
} else if r.Complete.IsZero() {
s += fmt.Sprintf(" Last dispersion scan started %s and has yet to complete after %s.\n", r.Start.Format("2006-01-02 15:04"), time.Since(r.Start).Truncate(time.Second))
} else {
s += fmt.Sprintf(" Last dispersion scan ran from %s to %s (%s ago for %s).\n", r.Start.Format("2006-01-02 15:04"), r.Complete.Format("2006-01-02 15:04"), time.Since(r.Complete).Truncate(time.Second), r.Complete.Sub(r.Start).Truncate(time.Second))
}
s += fmt.Sprintf(" There are %d partitions configured for %d copies.\n", r.TotalPartitions, r.ReplicaCount)
if !r.Start.IsZero() && len(r.Partitions) == 0 {
s += " All partition copies were in place when last checked.\n"
} else {
var noCopies []string
var oneCopies []string
counts := map[int]int{}
for partition, missings := range r.Partitions {
switch r.ReplicaCount - len(missings) {
case 0:
noCopies = addPartitionListCap(noCopies, partition)
case 1:
oneCopies = addPartitionListCap(oneCopies, partition)
}
counts[len(missings)]++
}
var keys []int
for key := range counts {
keys = append(keys, key)
}
sort.Ints(keys)
for _, key := range keys {
if counts[key] == 1 {
s += " ! 1 partition was missing "
} else {
s += fmt.Sprintf(" ! %d partitions were missing ", counts[key])
}
if key == 1 {
s += "1 copy"
} else {
s += fmt.Sprintf("%d copies", key)
}
if r.ReplicaCount-key == 0 {
s += fmt.Sprintf("; NO COPIES AVAILABLE! %v\n", noCopies)
} else if r.ReplicaCount-key == 1 {
s += fmt.Sprintf("; DOWN TO ONE COPY! %v\n", oneCopies)
} else {
s += fmt.Sprintf("; %d copies found.\n", r.ReplicaCount-key)
}
}
}
failureCount := 0
for _, sfs := range r.ScanFailures {
failureCount += len(sfs)
}
if failureCount == 1 {
s += " !! 1 scan failure which may skew the report.\n"
} else if failureCount > 1 {
s += fmt.Sprintf(" !! %d scan failures which may skew the report.\n", failureCount)
}
if failureCount > 0 {
s += fmt.Sprintf(" Assuming any scan failure is equivalent to a missing copy:\n")
var noCopies []string
var oneCopies []string
counts := map[int]int{}
for partition, missings := range r.Partitions {
c := len(missings) + len(r.ScanFailures[partition])
if c > r.ReplicaCount {
c = r.ReplicaCount
}
switch r.ReplicaCount - c {
case 0:
if len(noCopies) <= partitionListCap {
noCopies = append(noCopies, fmt.Sprintf("%d", partition))
}
case 1:
if len(oneCopies) <= partitionListCap {
oneCopies = append(oneCopies, fmt.Sprintf("%d", partition))
}
}
counts[c]++
}
for partition, sfs := range r.ScanFailures {
// Did we already count this in loop above?
if r.Partitions[partition] == nil {
c := len(sfs)
if c > r.ReplicaCount {
c = r.ReplicaCount
}
switch r.ReplicaCount - c {
case 0:
noCopies = addPartitionListCap(noCopies, partition)
case 1:
oneCopies = addPartitionListCap(oneCopies, partition)
}
counts[c]++
}
}
var keys []int
for key := range counts {
keys = append(keys, key)
}
sort.Ints(keys)
for _, key := range keys {
if counts[key] == 1 {
s += " ! 1 partition was missing "
} else {
s += fmt.Sprintf(" ! %d partitions were missing ", counts[key])
}
if key == 1 {
s += "1 copy"
} else {
s += fmt.Sprintf("%d copies", key)
}
if r.ReplicaCount-key == 0 {
s += fmt.Sprintf("; NO COPIES AVAILABLE! %v\n", noCopies)
} else if r.ReplicaCount-key == 1 {
s += fmt.Sprintf("; DOWN TO ONE COPY! %v\n", oneCopies)
} else {
s += fmt.Sprintf("; %d copies found.\n", r.ReplicaCount-key)
}
}
}
return s
}
type dispersionMissing struct {
Time time.Time
Service string
Device string
}
func getDispersionReport(flags *flag.FlagSet) *dispersionReport {
report := &dispersionReport{
Name: "Dispersion Report",
Time: time.Now().UTC(),
Pass: true,
ObjectReports: map[int]*innerDispersionReport{},
}
serverconf, err := getAndrewdConf(flags)
if err != nil {
report.Errors = append(report.Errors, err.Error())
return report
}
db, err := newDB(serverconf, "")
if err != nil {
report.Errors = append(report.Errors, err.Error())
return report
}
ring, _ := getRing("", "container", 0)
report.ContainerReport = &innerDispersionReport{TotalPartitions: int(ring.PartitionCount()), ReplicaCount: int(ring.ReplicaCount()), Partitions: map[int][]*dispersionMissing{}, ScanFailures: map[int][]*scanFailure{}}
var progress string
report.ContainerReport.Start, _, progress, report.ContainerReport.Complete, err = db.processPass("dispersion scan", "container", 0)
if err != nil {
report.Errors = append(report.Errors, err.Error())
}
if strings.HasSuffix(progress, "-init does not yet exist") {
report.ContainerReport.Start = time.Time{}
report.ContainerReport.Complete = time.Time{}
}
if dsfs, err := db.dispersionScanFailures("container", 0); err != nil {
report.Errors = append(report.Errors, err.Error())
} else {
for _, dsf := range dsfs {
report.ContainerReport.ScanFailures[dsf.partition] = append(report.ContainerReport.ScanFailures[dsf.partition], &scanFailure{Time: dsf.time, Service: dsf.service, DeviceID: dsf.deviceID})
}
}
if qrs, err := db.queuedReplications("container", 0, "dispersion"); err != nil {
report.Errors = append(report.Errors, err.Error())
} else {
for _, qr := range qrs {
dev := ring.AllDevices()[qr.toDeviceID]
if !dev.Active() {
continue
}
report.ContainerReport.Partitions[qr.partition] = append(report.ContainerReport.Partitions[qr.partition], &dispersionMissing{
Time: qr.created,
Service: fmt.Sprintf("%s:%d", dev.Ip, dev.Port),
Device: dev.Device,
})
report.Pass = false
}
}
if policies, err := conf.GetPolicies(); err != nil {
report.Errors = append(report.Errors, err.Error())
} else {
for _, policy := range policies {
ring, _ := getRing("", "object", policy.Index)
objectReport := &innerDispersionReport{TotalPartitions: int(ring.PartitionCount()), ReplicaCount: int(ring.ReplicaCount()), Partitions: map[int][]*dispersionMissing{}, ScanFailures: map[int][]*scanFailure{}}
var progress string
objectReport.Start, _, progress, objectReport.Complete, err = db.processPass("dispersion scan", "object", policy.Index)
if err != nil {
report.Errors = append(report.Errors, err.Error())
continue
}
if strings.HasSuffix(progress, "-init does not yet exist") {
objectReport.Start = time.Time{}
objectReport.Complete = time.Time{}
}
if dsfs, err := db.dispersionScanFailures("object", policy.Index); err != nil {
report.Errors = append(report.Errors, err.Error())
} else {
for _, dsf := range dsfs {
objectReport.ScanFailures[dsf.partition] = append(objectReport.ScanFailures[dsf.partition], &scanFailure{Time: dsf.time, Service: dsf.service, DeviceID: dsf.deviceID})
}
}
objectReport.PolicyName = policy.Name
report.ObjectReports[policy.Index] = objectReport
qrs, err := db.queuedReplications("object", policy.Index, "dispersion")
if err != nil {
report.Errors = append(report.Errors, err.Error())
continue
}
for _, qr := range qrs {
dev := ring.AllDevices()[qr.toDeviceID]
if !dev.Active() {
continue
}
objectReport.Partitions[qr.partition] = append(objectReport.Partitions[qr.partition], &dispersionMissing{
Time: qr.created,
Service: fmt.Sprintf("%s:%d", dev.Ip, dev.Port),
Device: dev.Device,
})
report.Pass = false
}
}
}
if len(report.Errors) > 0 {
report.Pass = false
}
return report
}
// generateDispersionNames will feed a name per partition into the names
// channel. The container should be an empty string if you wish to have
// container names generated. The prefix will be the start of every name. The
// cancel channel can be closed to abort the name generation. Usually, the
// container name generation is called with
// generateDispersionNames("", "disp-conts-", containerRing, containerNames, cancel)
// and the object name generation is called with
// generateDispersionNames("disp-objs-<POLICYINDEX>", "", objectRing, objectNames, cancel)
func generateDispersionNames(container, prefix string, r ring.Ring, names chan string, cancel chan struct{}) {
// if looking for container names, send container=""
defer close(names)
var wg sync.WaitGroup
numCPU := uint64(runtime.NumCPU())
for cpu := uint64(0); cpu < numCPU; cpu++ {
wg.Add(1)
go func(funcCPU uint64) {
defer wg.Done()
for partition := funcCPU; true; partition += numCPU {
select {
case <-cancel:
break
default:
}
devs := r.GetNodes(partition)
if devs == nil {
break
}
for i := uint64(0); true; i++ {
select {
case <-cancel:
break
default:
}
c := container
o := ""
n := fmt.Sprintf("%s%d-%d", prefix, partition, i)
if c == "" {
c = n
} else {
o = n
}
genPart := r.GetPartition(AdminAccount, c, o)
if genPart == partition {
names <- n
break
}
}
}
}(cpu)
}
wg.Wait()
}