forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.go
395 lines (341 loc) · 8.59 KB
/
scan.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright 2016 Stellar Development Foundation and contributors. Licensed
// under the Apache License, Version 2.0. See the COPYING file at the root
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0
package archivist
import (
"errors"
"fmt"
"log"
"strings"
"sync"
"sync/atomic"
)
type scanCheckpointFastReq struct {
category string
pathprefix string
}
type scanCheckpointSlowReq struct {
category string
checkpoint uint32
}
func (arch *Archive) ScanCheckpoints(opts *CommandOptions) error {
state, e := arch.GetRootHAS()
if e != nil {
return e
}
opts.Range = opts.Range.Clamp(state.Range())
log.Printf("Scanning checkpoint files in range: %s", opts.Range)
if arch.backend.CanListFiles() {
return arch.ScanCheckpointsFast(opts)
} else {
return arch.ScanCheckpointsSlow(opts)
}
}
func (arch *Archive) ScanCheckpointsSlow(opts *CommandOptions) error {
if opts.Concurrency == 0 {
return errors.New("Zero concurrency")
}
var errs uint32
tick := makeTicker(func(_ uint) {
arch.ReportCheckpointStats()
})
var wg sync.WaitGroup
wg.Add(opts.Concurrency)
req := make(chan scanCheckpointSlowReq)
cats := Categories()
go func() {
for _, cat := range cats {
for chk := range opts.Range.Checkpoints() {
req <- scanCheckpointSlowReq{category: cat, checkpoint: chk}
}
}
close(req)
}()
for i := 0; i < opts.Concurrency; i++ {
go func() {
for {
r, ok := <-req
if !ok {
break
}
exists := arch.CategoryCheckpointExists(r.category, r.checkpoint)
tick <- true
arch.NoteCheckpointFile(r.category, r.checkpoint, exists)
if exists && opts.Verify {
atomic.AddUint32(&errs,
noteError(arch.VerifyCategoryCheckpoint(r.category,
r.checkpoint)))
}
}
wg.Done()
}()
}
wg.Wait()
close(tick)
log.Printf("Checkpoint files scanned with %d errors", errs)
arch.ReportCheckpointStats()
if errs != 0 {
return fmt.Errorf("%d errors scanning checkpoints", errs)
}
return nil
}
func (arch *Archive) ScanCheckpointsFast(opts *CommandOptions) error {
if opts.Concurrency == 0 {
return errors.New("Zero concurrency")
}
var errs uint32
tick := makeTicker(func(_ uint) {
arch.ReportCheckpointStats()
})
var wg sync.WaitGroup
wg.Add(opts.Concurrency)
req := make(chan scanCheckpointFastReq)
cats := Categories()
go func() {
for _, cat := range cats {
for _, pth := range RangePaths(opts.Range) {
req <- scanCheckpointFastReq{category: cat, pathprefix: pth}
}
}
close(req)
}()
for i := 0; i < opts.Concurrency; i++ {
go func() {
for {
r, ok := <-req
if !ok {
break
}
ch, es := arch.ListCategoryCheckpoints(r.category, r.pathprefix)
for n := range ch {
tick <- true
arch.NoteCheckpointFile(r.category, n, true)
if opts.Verify {
atomic.AddUint32(&errs,
noteError(arch.VerifyCategoryCheckpoint(r.category, n)))
}
}
atomic.AddUint32(&errs, drainErrors(es))
}
wg.Done()
}()
}
wg.Wait()
close(tick)
log.Printf("Checkpoint files scanned with %d errors", errs)
arch.ReportCheckpointStats()
if errs != 0 {
return fmt.Errorf("%d errors scanning checkpoints", errs)
}
return nil
}
func (arch *Archive) Scan(opts *CommandOptions) error {
e1 := arch.ScanCheckpoints(opts)
e2 := arch.ScanBuckets(opts)
if e1 != nil {
return e1
}
if e2 != nil {
return e2
}
return nil
}
func (arch *Archive) ScanAllBuckets() error {
log.Printf("Scanning all buckets, and those referenced by range")
tick := makeTicker(func(_ uint) {
arch.ReportBucketStats()
})
allBuckets, ech := arch.ListAllBucketHashes()
for b := range allBuckets {
arch.NoteExistingBucket(b)
tick <- true
}
errs := drainErrors(ech)
if errs != 0 {
return fmt.Errorf("%d errors while scanning all buckets", errs)
}
return nil
}
func (arch *Archive) ScanBuckets(opts *CommandOptions) error {
if opts.Concurrency == 0 {
return errors.New("Zero concurrency")
}
var errs uint32
// First scan _all_ buckets if we can; if not, we'll do an exists-check
// on each bucket as we go. But this is faster when we can do it.
doList := arch.backend.CanListFiles()
if doList {
errs += noteError(arch.ScanAllBuckets())
}
// Grab the set of checkpoints we have HASs for, to read references.
arch.mutex.Lock()
hists := arch.checkpointFiles["history"]
seqs := make([]uint32, 0, len(hists))
for k, present := range hists {
if present {
seqs = append(seqs, k)
}
}
arch.mutex.Unlock()
var wg sync.WaitGroup
wg.Add(opts.Concurrency)
tick := makeTicker(func(_ uint) {
arch.ReportBucketStats()
})
// Make a bunch of goroutines that pull each HAS and enumerate
// its buckets into a channel. These are the _referenced_ buckets.
req := make(chan uint32)
go func() {
for _, seq := range seqs {
req <- seq
}
close(req)
}()
for i := 0; i < opts.Concurrency; i++ {
go func() {
for {
ix, ok := <-req
if !ok {
break
}
has, e := arch.GetCheckpointHAS(ix)
atomic.AddUint32(&errs, noteError(e))
for _, bucket := range has.Buckets() {
new := arch.NoteReferencedBucket(bucket)
if !new {
continue
}
if !doList || opts.Verify {
if arch.BucketExists(bucket) {
if !doList {
arch.NoteExistingBucket(bucket)
}
if opts.Verify {
n := uint32(0)
if opts.Thorough {
n = noteError(arch.VerifyBucketEntries(bucket))
} else {
n = noteError(arch.VerifyBucketHash(bucket))
}
atomic.AddUint32(&errs, n)
if n != 0 {
arch.mutex.Lock()
arch.invalidBuckets++
arch.mutex.Unlock()
}
}
}
}
}
tick <- true
}
wg.Done()
}()
}
wg.Wait()
arch.ReportBucketStats()
close(tick)
if errs != 0 {
return fmt.Errorf("%d errors while scanning buckets", errs)
}
return nil
}
func (arch *Archive) ClearCachedInfo() {
arch.mutex.Lock()
defer arch.mutex.Unlock()
for _, cat := range Categories() {
arch.checkpointFiles[cat] = make(map[uint32]bool)
}
arch.allBuckets = make(map[Hash]bool)
arch.referencedBuckets = make(map[Hash]bool)
}
func (arch *Archive) ReportCheckpointStats() {
arch.mutex.Lock()
defer arch.mutex.Unlock()
s := make([]string, 0)
for _, cat := range Categories() {
tab := arch.checkpointFiles[cat]
s = append(s, fmt.Sprintf("%d %s", len(tab), cat))
}
log.Printf("Archive: %s", strings.Join(s, ", "))
}
func (arch *Archive) ReportBucketStats() {
arch.mutex.Lock()
defer arch.mutex.Unlock()
log.Printf("Archive: %d buckets total, %d referenced",
len(arch.allBuckets), len(arch.referencedBuckets))
}
func (arch *Archive) NoteCheckpointFile(cat string, chk uint32, present bool) {
arch.mutex.Lock()
defer arch.mutex.Unlock()
arch.checkpointFiles[cat][chk] = present
}
func (arch *Archive) NoteExistingBucket(bucket Hash) {
arch.mutex.Lock()
defer arch.mutex.Unlock()
arch.allBuckets[bucket] = true
}
func (arch *Archive) NoteReferencedBucket(bucket Hash) bool {
arch.mutex.Lock()
defer arch.mutex.Unlock()
_, exists := arch.referencedBuckets[bucket]
if exists {
return false
}
arch.referencedBuckets[bucket] = true
return true
}
func (arch *Archive) CheckCheckpointFilesMissing(opts *CommandOptions) map[string][]uint32 {
arch.mutex.Lock()
defer arch.mutex.Unlock()
missing := make(map[string][]uint32)
for _, cat := range Categories() {
missing[cat] = make([]uint32, 0)
for ix := range opts.Range.Checkpoints() {
_, ok := arch.checkpointFiles[cat][ix]
if !ok {
missing[cat] = append(missing[cat], ix)
}
}
}
return missing
}
func (arch *Archive) CheckBucketsMissing() map[Hash]bool {
arch.mutex.Lock()
defer arch.mutex.Unlock()
missing := make(map[Hash]bool)
for k, _ := range arch.referencedBuckets {
_, ok := arch.allBuckets[k]
if !ok {
missing[k] = true
}
}
return missing
}
func (arch *Archive) ReportMissing(opts *CommandOptions) error {
log.Printf("Examining checkpoint files for gaps")
missingCheckpointFiles := arch.CheckCheckpointFilesMissing(opts)
log.Printf("Examining buckets referenced by checkpoints")
missingBuckets := arch.CheckBucketsMissing()
missingCheckpoints := false
for cat, missing := range missingCheckpointFiles {
if !categoryRequired(cat) {
continue
}
if len(missing) != 0 {
s := fmtRangeList(missing)
missingCheckpoints = true
log.Printf("Missing %s (%d): %s", cat, len(missing), s)
}
}
if !missingCheckpoints {
log.Printf("No checkpoint files missing in range %s", opts.Range)
}
for bucket, _ := range missingBuckets {
log.Printf("Missing bucket: %s", bucket)
}
if len(missingBuckets) == 0 {
log.Printf("No missing buckets referenced in range %s", opts.Range)
}
return nil
}