forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkpoint_change_reader.go
569 lines (500 loc) · 15.8 KB
/
checkpoint_change_reader.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
package ingest
import (
"context"
"io"
"sync"
"time"
"github.com/pownieh/stellar_go/historyarchive"
"github.com/pownieh/stellar_go/support/errors"
"github.com/pownieh/stellar_go/xdr"
)
// readResult is the result of reading a bucket value
type readResult struct {
entryChange xdr.LedgerEntryChange
e error
}
// CheckpointChangeReader is a ChangeReader which returns Changes from a history archive
// snapshot. The Changes produced by a CheckpointChangeReader reflect the state of the Stellar
// network at a particular checkpoint ledger sequence.
type CheckpointChangeReader struct {
ctx context.Context
has *historyarchive.HistoryArchiveState
archive historyarchive.ArchiveInterface
tempStore tempSet
sequence uint32
readChan chan readResult
streamOnce sync.Once
closeOnce sync.Once
done chan bool
readBytesMutex sync.RWMutex
totalRead int64
totalSize int64
encodingBuffer *xdr.EncodingBuffer
// This should be set to true in tests only
disableBucketListHashValidation bool
sleep func(time.Duration)
}
// Ensure CheckpointChangeReader implements ChangeReader
var _ ChangeReader = &CheckpointChangeReader{}
// tempSet is an interface that must be implemented by stores that
// hold temporary set of objects for state reader. The implementation
// does not need to be thread-safe.
type tempSet interface {
Open() error
// Preload batch-loads keys into internal cache (if a store has any) to
// improve execution time by removing many round-trips.
Preload(keys []string) error
// Add adds key to the store.
Add(key string) error
// Exist returns value true if the value is found in the store.
// If the value has not been set, it should return false.
Exist(key string) (bool, error)
Close() error
}
const (
// maxStreamRetries defines how many times should we retry when there are errors in
// the xdr stream returned by GetXdrStreamForHash().
maxStreamRetries = 3
msrBufferSize = 50000
// preloadedEntries defines a number of bucket entries to preload from a
// bucket in a single run. This is done to allow preloading keys from
// temp set.
preloadedEntries = 20000
sleepDuration = time.Second
)
// NewCheckpointChangeReader constructs a new CheckpointChangeReader instance.
//
// The ledger sequence must be a checkpoint ledger. By default (see
// `historyarchive.ConnectOptions.CheckpointFrequency` for configuring this),
// its next sequence number would have to be a multiple of 64, e.g.
// sequence=100031 is a checkpoint ledger, since: (100031+1) mod 64 == 0
func NewCheckpointChangeReader(
ctx context.Context,
archive historyarchive.ArchiveInterface,
sequence uint32,
) (*CheckpointChangeReader, error) {
manager := archive.GetCheckpointManager()
// The nth ledger is a checkpoint ledger iff: n+1 mod f == 0, where f is the
// checkpoint frequency (64 by default).
if !manager.IsCheckpoint(sequence) {
return nil, errors.Errorf(
"%d is not a checkpoint ledger, try %d or %d "+
"(in general, try n where n+1 mod %d == 0)",
sequence, manager.PrevCheckpoint(sequence),
manager.NextCheckpoint(sequence),
manager.GetCheckpointFrequency())
}
has, err := archive.GetCheckpointHAS(sequence)
if err != nil {
return nil, errors.Wrapf(err, "unable to get checkpoint HAS at ledger sequence %d", sequence)
}
tempStore := &memoryTempSet{}
err = tempStore.Open()
if err != nil {
return nil, errors.Wrap(err, "unable to get open temp store")
}
return &CheckpointChangeReader{
ctx: ctx,
has: &has,
archive: archive,
tempStore: tempStore,
sequence: sequence,
readChan: make(chan readResult, msrBufferSize),
streamOnce: sync.Once{},
closeOnce: sync.Once{},
done: make(chan bool),
encodingBuffer: xdr.NewEncodingBuffer(),
sleep: time.Sleep,
}, nil
}
func (r *CheckpointChangeReader) bucketExists(hash historyarchive.Hash) (bool, error) {
duration := sleepDuration
var exists bool
var err error
for attempts := 0; ; attempts++ {
exists, err = r.archive.BucketExists(hash)
if err == nil {
return exists, nil
}
if attempts >= maxStreamRetries {
break
}
r.sleep(duration)
duration *= 2
}
return exists, err
}
// streamBuckets is internal method that streams buckets from the given HAS.
//
// Buckets should be processed from oldest to newest, `snap` and then `curr` at
// each level. The correct value of ledger entry is the latest seen
// `INITENTRY`/`LIVEENTRY` except the case when there's a `DEADENTRY` later
// which removes the entry.
//
// We can implement trivial algorithm (processing from oldest to newest buckets)
// but it requires to keep map of all entries in memory and stream what's left
// when all buckets are processed.
//
// However, we can modify this algorithm to work from newest to oldest ledgers:
//
// 1. For each `INITENTRY`/`LIVEENTRY` we check if we've seen the key before
// (stored in `tempStore`). If the key hasn't been seen, we write that bucket
// entry to the stream and add it to the `tempStore` (we don't mark `INITENTRY`,
// see the inline comment or CAP-20).
// 2. For each `DEADENTRY` we keep track of removed bucket entries in
// `tempStore` map.
//
// In such algorithm we just need to store a set of keys that require much less space.
// The memory requirements will be lowered when CAP-0020 is live and older buckets are
// rewritten. Then, we will only need to keep track of `DEADENTRY`.
func (r *CheckpointChangeReader) streamBuckets() {
defer func() {
err := r.tempStore.Close()
if err != nil {
r.readChan <- r.error(errors.New("Error closing tempStore"))
}
r.closeOnce.Do(r.close)
close(r.readChan)
}()
var buckets []historyarchive.Hash
for i := 0; i < len(r.has.CurrentBuckets); i++ {
b := r.has.CurrentBuckets[i]
for _, hashString := range []string{b.Curr, b.Snap} {
hash, err := historyarchive.DecodeHash(hashString)
if err != nil {
r.readChan <- r.error(errors.Wrap(err, "Error decoding bucket hash"))
return
}
if hash.IsZero() {
continue
}
buckets = append(buckets, hash)
}
}
for _, hash := range buckets {
exists, err := r.bucketExists(hash)
if err != nil {
r.readChan <- r.error(
errors.Wrapf(err, "error checking if bucket exists: %s", hash),
)
return
}
if !exists {
r.readChan <- r.error(
errors.Errorf("bucket hash does not exist: %s", hash),
)
return
}
size, err := r.archive.BucketSize(hash)
if err != nil {
r.readChan <- r.error(
errors.Wrapf(err, "error checking bucket size: %s", hash),
)
return
}
r.readBytesMutex.Lock()
r.totalSize += size
r.readBytesMutex.Unlock()
}
for i, hash := range buckets {
oldestBucket := i == len(buckets)-1
if shouldContinue := r.streamBucketContents(hash, oldestBucket); !shouldContinue {
break
}
}
}
// readBucketEntry will attempt to read a bucket entry from `stream`.
// If any errors are encountered while reading from `stream`, readBucketEntry will
// retry the operation using a new *historyarchive.XdrStream.
// The total number of retries will not exceed `maxStreamRetries`.
func (r *CheckpointChangeReader) readBucketEntry(stream *historyarchive.XdrStream, hash historyarchive.Hash) (
xdr.BucketEntry,
error,
) {
var entry xdr.BucketEntry
var err error
currentPosition := stream.BytesRead()
gzipCurrentPosition := stream.GzipBytesRead()
for attempts := 0; ; attempts++ {
if r.ctx.Err() != nil {
err = r.ctx.Err()
break
}
if err == nil {
err = stream.ReadOne(&entry)
if err == nil || err == io.EOF {
r.readBytesMutex.Lock()
r.totalRead += stream.GzipBytesRead() - gzipCurrentPosition
r.readBytesMutex.Unlock()
break
}
}
if attempts >= maxStreamRetries {
break
}
stream.Close()
var retryStream *historyarchive.XdrStream
retryStream, err = r.newXDRStream(hash)
if err != nil {
err = errors.Wrap(err, "Error creating new xdr stream")
continue
}
*stream = *retryStream
_, err = stream.Discard(currentPosition)
if err != nil {
err = errors.Wrap(err, "Error discarding from xdr stream")
continue
}
}
return entry, err
}
func (r *CheckpointChangeReader) newXDRStream(hash historyarchive.Hash) (
*historyarchive.XdrStream,
error,
) {
rdr, e := r.archive.GetXdrStreamForHash(hash)
if e == nil && !r.disableBucketListHashValidation {
// Calling SetExpectedHash will enable validation of the stream hash. If hashes
// don't match, rdr.Close() will return an error.
rdr.SetExpectedHash(hash)
}
return rdr, e
}
// streamBucketContents pushes value onto the read channel, returning false when the channel needs to be closed otherwise true
func (r *CheckpointChangeReader) streamBucketContents(hash historyarchive.Hash, oldestBucket bool) bool {
rdr, e := r.newXDRStream(hash)
if e != nil {
r.readChan <- r.error(
errors.Wrapf(e, "cannot get xdr stream for hash '%s'", hash.String()),
)
return false
}
defer func() {
err := rdr.Close()
if err != nil {
r.readChan <- r.error(errors.Wrap(err, "Error closing xdr stream"))
// Stop streaming from the rest of the files.
r.Close()
}
}()
// bucketProtocolVersion is a protocol version read from METAENTRY or 0 when no METAENTRY.
// No METAENTRY means that bucket originates from before protocol version 11.
bucketProtocolVersion := uint32(0)
n := -1
var batch []xdr.BucketEntry
lastBatch := false
LoopBucketEntry:
for {
// Preload entries for faster retrieve from temp store.
if len(batch) == 0 {
if lastBatch {
return true
}
preloadKeys := []string{}
for i := 0; i < preloadedEntries; i++ {
var entry xdr.BucketEntry
entry, e = r.readBucketEntry(rdr, hash)
if e != nil {
if e == io.EOF {
if len(batch) == 0 {
// No entries loaded for this batch, nothing more to process
return true
}
lastBatch = true
break
}
r.readChan <- r.error(
errors.Wrapf(e, "Error on XDR record %d of hash '%s'", n, hash.String()),
)
return false
}
batch = append(batch, entry)
// Generate a key
var key xdr.LedgerKey
var err error
switch entry.Type {
case xdr.BucketEntryTypeLiveentry, xdr.BucketEntryTypeInitentry:
liveEntry := entry.MustLiveEntry()
key, err = liveEntry.LedgerKey()
if err != nil {
r.readChan <- r.error(
errors.Wrapf(err, "Error generating ledger key for XDR record %d of hash '%s'", n, hash.String()),
)
return false
}
case xdr.BucketEntryTypeDeadentry:
key = entry.MustDeadEntry()
default:
// No ledger key associated with this entry, continue to the next one.
continue
}
// We're using compressed keys here
// safe, since we are converting to string right away
keyBytes, e := r.encodingBuffer.LedgerKeyUnsafeMarshalBinaryCompress(key)
if e != nil {
r.readChan <- r.error(
errors.Wrapf(e, "Error marshaling XDR record %d of hash '%s'", n, hash.String()),
)
return false
}
h := string(keyBytes)
preloadKeys = append(preloadKeys, h)
}
err := r.tempStore.Preload(preloadKeys)
if err != nil {
r.readChan <- r.error(errors.Wrap(err, "Error preloading keys"))
return false
}
}
var entry xdr.BucketEntry
entry, batch = batch[0], batch[1:]
n++
var key xdr.LedgerKey
var err error
switch entry.Type {
case xdr.BucketEntryTypeMetaentry:
if n != 0 {
r.readChan <- r.error(
errors.Errorf(
"METAENTRY not the first entry (n=%d) in the bucket hash '%s'",
n, hash.String(),
),
)
return false
}
// We can't use MustMetaEntry() here. Check:
// https://github.com/golang/go/issues/32560
bucketProtocolVersion = uint32(entry.MetaEntry.LedgerVersion)
continue LoopBucketEntry
case xdr.BucketEntryTypeLiveentry, xdr.BucketEntryTypeInitentry:
liveEntry := entry.MustLiveEntry()
key, err = liveEntry.LedgerKey()
if err != nil {
r.readChan <- r.error(
errors.Wrapf(err, "Error generating ledger key for XDR record %d of hash '%s'", n, hash.String()),
)
return false
}
case xdr.BucketEntryTypeDeadentry:
key = entry.MustDeadEntry()
default:
r.readChan <- r.error(
errors.Errorf("Unknown BucketEntryType=%d: %d@%s", entry.Type, n, hash.String()),
)
return false
}
// We're using compressed keys here
// Safe, since we are converting to string right away
keyBytes, e := r.encodingBuffer.LedgerKeyUnsafeMarshalBinaryCompress(key)
if e != nil {
r.readChan <- r.error(
errors.Wrapf(
e, "Error marshaling XDR record %d of hash '%s'", n, hash.String(),
),
)
return false
}
h := string(keyBytes)
switch entry.Type {
case xdr.BucketEntryTypeLiveentry, xdr.BucketEntryTypeInitentry:
if entry.Type == xdr.BucketEntryTypeInitentry && bucketProtocolVersion < 11 {
r.readChan <- r.error(
errors.Errorf("Read INITENTRY from version <11 bucket: %d@%s", n, hash.String()),
)
return false
}
seen, err := r.tempStore.Exist(h)
if err != nil {
r.readChan <- r.error(errors.Wrap(err, "Error reading from tempStore"))
return false
}
if !seen {
// Return LEDGER_ENTRY_STATE changes only now.
liveEntry := entry.MustLiveEntry()
entryChange := xdr.LedgerEntryChange{
Type: xdr.LedgerEntryChangeTypeLedgerEntryState,
State: &liveEntry,
}
r.readChan <- readResult{entryChange, nil}
// We don't update `tempStore` for INITENTRY because CAP-20 says:
// > a bucket entry marked INITENTRY implies that either no entry
// > with the same ledger key exists in an older bucket, or else
// > that the (chronologically) preceding entry with the same ledger
// > key was DEADENTRY.
if entry.Type == xdr.BucketEntryTypeLiveentry {
// We skip adding entries from the last bucket to tempStore because:
// 1. Ledger keys are unique within a single bucket.
// 2. This is the last bucket we process so there's no need to track
// seen last entries in this bucket.
if oldestBucket {
continue
}
err := r.tempStore.Add(h)
if err != nil {
r.readChan <- r.error(errors.Wrap(err, "Error updating to tempStore"))
return false
}
}
}
case xdr.BucketEntryTypeDeadentry:
err := r.tempStore.Add(h)
if err != nil {
r.readChan <- r.error(errors.Wrap(err, "Error writing to tempStore"))
return false
}
default:
r.readChan <- r.error(
errors.Errorf("Unexpected entry type %d: %d@%s", entry.Type, n, hash.String()),
)
return false
}
select {
case <-r.done:
// Close() called: stop processing buckets.
return false
default:
continue
}
}
panic("Shouldn't happen")
}
// Read returns a new ledger entry change on each call, returning io.EOF when the stream ends.
func (r *CheckpointChangeReader) Read() (Change, error) {
r.streamOnce.Do(func() {
go r.streamBuckets()
})
// blocking call. anytime we consume from this channel, the background goroutine will stream in the next value
result, ok := <-r.readChan
if !ok {
// when channel is closed then return io.EOF
return Change{}, io.EOF
}
if result.e != nil {
return Change{}, errors.Wrap(result.e, "Error while reading from buckets")
}
entryType, err := result.entryChange.EntryType()
if err != nil {
return Change{}, errors.Wrap(err, "Error getting entry type")
}
return Change{
Type: entryType,
Post: result.entryChange.State,
}, nil
}
func (r *CheckpointChangeReader) error(err error) readResult {
return readResult{xdr.LedgerEntryChange{}, err}
}
func (r *CheckpointChangeReader) close() {
close(r.done)
}
// Progress returns progress reading all buckets in percents.
func (r *CheckpointChangeReader) Progress() float64 {
r.readBytesMutex.RLock()
defer r.readBytesMutex.RUnlock()
return float64(r.totalRead) / float64(r.totalSize) * 100
}
// Close should be called when reading is finished.
func (r *CheckpointChangeReader) Close() error {
r.closeOnce.Do(r.close)
return nil
}