forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_indexes.go
471 lines (413 loc) · 11.8 KB
/
stage_indexes.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
package stagedsync
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"runtime"
"sort"
"time"
"github.com/RoaringBitmap/roaring/roaring64"
"github.com/c2h5oh/datasize"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon-lib/etl"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common/changeset"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/ethdb"
"github.com/ledgerwatch/erigon/ethdb/bitmapdb"
"github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/ledgerwatch/log/v3"
)
type HistoryCfg struct {
db kv.RwDB
bufLimit datasize.ByteSize
prune prune.Mode
flushEvery time.Duration
tmpdir string
}
func StageHistoryCfg(db kv.RwDB, prune prune.Mode, tmpDir string) HistoryCfg {
return HistoryCfg{
db: db,
prune: prune,
bufLimit: bitmapsBufLimit,
flushEvery: bitmapsFlushEvery,
tmpdir: tmpDir,
}
}
func SpawnAccountHistoryIndex(s *StageState, tx kv.RwTx, cfg HistoryCfg, ctx context.Context) error {
useExternalTx := tx != nil
if !useExternalTx {
var err error
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
quitCh := ctx.Done()
endBlock, err := s.ExecutionAt(tx)
logPrefix := s.LogPrefix()
if err != nil {
return fmt.Errorf(" getting last executed block: %w", err)
}
if endBlock <= s.BlockNumber {
return nil
}
var startBlock uint64
if s.BlockNumber > 0 {
startBlock = s.BlockNumber + 1
}
stopChangeSetsLookupAt := endBlock + 1
pruneTo := cfg.prune.History.PruneTo(endBlock)
if startBlock < pruneTo {
startBlock = pruneTo
}
if err := promoteHistory(logPrefix, tx, kv.AccountChangeSet, startBlock, stopChangeSetsLookupAt, cfg, quitCh); err != nil {
return err
}
if err := s.Update(tx, endBlock); err != nil {
return err
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func SpawnStorageHistoryIndex(s *StageState, tx kv.RwTx, cfg HistoryCfg, ctx context.Context) error {
useExternalTx := tx != nil
if !useExternalTx {
var err error
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
quitCh := ctx.Done()
executionAt, err := s.ExecutionAt(tx)
logPrefix := s.LogPrefix()
if err != nil {
return fmt.Errorf("getting last executed block: %w", err)
}
if executionAt <= s.BlockNumber {
return nil
}
var startChangeSetsLookupAt uint64
if s.BlockNumber > 0 {
startChangeSetsLookupAt = s.BlockNumber + 1
}
stopChangeSetsLookupAt := executionAt + 1
if err := promoteHistory(logPrefix, tx, kv.StorageChangeSet, startChangeSetsLookupAt, stopChangeSetsLookupAt, cfg, quitCh); err != nil {
return err
}
if err := s.Update(tx, executionAt); err != nil {
return err
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func promoteHistory(logPrefix string, tx kv.RwTx, changesetBucket string, start, stop uint64, cfg HistoryCfg, quit <-chan struct{}) error {
logEvery := time.NewTicker(30 * time.Second)
defer logEvery.Stop()
updates := map[string]*roaring64.Bitmap{}
checkFlushEvery := time.NewTicker(cfg.flushEvery)
defer checkFlushEvery.Stop()
collectorUpdates := etl.NewCollector(logPrefix, cfg.tmpdir, etl.NewSortableBuffer(etl.BufferOptimalSize))
defer collectorUpdates.Close()
if err := changeset.ForRange(tx, changesetBucket, start, stop, func(blockN uint64, k, v []byte) error {
if err := libcommon.Stopped(quit); err != nil {
return err
}
k = dbutils.CompositeKeyWithoutIncarnation(k)
select {
default:
case <-logEvery.C:
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Info(fmt.Sprintf("[%s] Progress", logPrefix), "number", blockN, "alloc", libcommon.ByteCount(m.Alloc), "sys", libcommon.ByteCount(m.Sys))
case <-checkFlushEvery.C:
if needFlush64(updates, cfg.bufLimit) {
if err := flushBitmaps64(collectorUpdates, updates); err != nil {
return err
}
updates = map[string]*roaring64.Bitmap{}
}
}
m, ok := updates[string(k)]
if !ok {
m = roaring64.New()
updates[string(k)] = m
}
m.Add(blockN)
return nil
}); err != nil {
return err
}
if err := flushBitmaps64(collectorUpdates, updates); err != nil {
return err
}
var currentBitmap = roaring64.New()
var buf = bytes.NewBuffer(nil)
lastChunkKey := make([]byte, 128)
var loaderFunc = func(k []byte, v []byte, table etl.CurrentTableReader, next etl.LoadNextFunc) error {
if _, err := currentBitmap.ReadFrom(bytes.NewReader(v)); err != nil {
return err
}
lastChunkKey = lastChunkKey[:len(k)+8]
copy(lastChunkKey, k)
binary.BigEndian.PutUint64(lastChunkKey[len(k):], ^uint64(0))
lastChunkBytes, err := table.Get(lastChunkKey)
if err != nil && !errors.Is(err, ethdb.ErrKeyNotFound) {
return fmt.Errorf("find last chunk failed: %w", err)
}
if len(lastChunkBytes) > 0 {
lastChunk := roaring64.New()
_, err = lastChunk.ReadFrom(bytes.NewReader(lastChunkBytes))
if err != nil {
return fmt.Errorf("couldn't read last log index chunk: %w, len(lastChunkBytes)=%d", err, len(lastChunkBytes))
}
currentBitmap.Or(lastChunk) // merge last existing chunk from tx - next loop will overwrite it
}
if err = bitmapdb.WalkChunkWithKeys64(k, currentBitmap, bitmapdb.ChunkLimit, func(chunkKey []byte, chunk *roaring64.Bitmap) error {
buf.Reset()
if _, err = chunk.WriteTo(buf); err != nil {
return err
}
return next(k, chunkKey, buf.Bytes())
}); err != nil {
return err
}
currentBitmap.Clear()
return nil
}
if err := collectorUpdates.Load(tx, changeset.Mapper[changesetBucket].IndexBucket, loaderFunc, etl.TransformArgs{Quit: quit}); err != nil {
return err
}
return nil
}
func UnwindAccountHistoryIndex(u *UnwindState, s *StageState, tx kv.RwTx, cfg HistoryCfg, ctx context.Context) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
quitCh := ctx.Done()
logPrefix := s.LogPrefix()
if err := unwindHistory(logPrefix, tx, kv.AccountChangeSet, u.UnwindPoint, cfg, quitCh); err != nil {
return err
}
if err := u.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func UnwindStorageHistoryIndex(u *UnwindState, s *StageState, tx kv.RwTx, cfg HistoryCfg, ctx context.Context) (err error) {
useExternalTx := tx != nil
if !useExternalTx {
var err error
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
quitCh := ctx.Done()
logPrefix := s.LogPrefix()
if err := unwindHistory(logPrefix, tx, kv.StorageChangeSet, u.UnwindPoint, cfg, quitCh); err != nil {
return err
}
if err := u.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func unwindHistory(logPrefix string, db kv.RwTx, csBucket string, to uint64, cfg HistoryCfg, quitCh <-chan struct{}) error {
logEvery := time.NewTicker(30 * time.Second)
defer logEvery.Stop()
updates := map[string]struct{}{}
if err := changeset.ForEach(db, csBucket, dbutils.EncodeBlockNumber(to), func(blockN uint64, k, v []byte) error {
select {
case <-logEvery.C:
var m runtime.MemStats
runtime.ReadMemStats(&m)
log.Info(fmt.Sprintf("[%s] Progress", logPrefix), "number", blockN, "alloc", libcommon.ByteCount(m.Alloc), "sys", libcommon.ByteCount(m.Sys))
case <-quitCh:
return libcommon.ErrStopped
default:
}
k = dbutils.CompositeKeyWithoutIncarnation(k)
updates[string(k)] = struct{}{}
return nil
}); err != nil {
return err
}
if err := truncateBitmaps64(db, changeset.Mapper[csBucket].IndexBucket, updates, to); err != nil {
return err
}
return nil
}
func needFlush64(bitmaps map[string]*roaring64.Bitmap, memLimit datasize.ByteSize) bool {
sz := uint64(0)
for _, m := range bitmaps {
sz += m.GetSizeInBytes() * 2 // for golang's overhead
}
const memoryNeedsForKey = 32 * 2 * 2 // len(key) * (string and bytes) overhead * go's map overhead
return uint64(len(bitmaps)*memoryNeedsForKey)+sz > uint64(memLimit)
}
func flushBitmaps64(c *etl.Collector, inMem map[string]*roaring64.Bitmap) error {
for k, v := range inMem {
v.RunOptimize()
if v.GetCardinality() == 0 {
continue
}
newV := bytes.NewBuffer(make([]byte, 0, v.GetSerializedSizeInBytes()))
if _, err := v.WriteTo(newV); err != nil {
return err
}
if err := c.Collect([]byte(k), newV.Bytes()); err != nil {
return err
}
}
return nil
}
func truncateBitmaps64(tx kv.RwTx, bucket string, inMem map[string]struct{}, to uint64) error {
keys := make([]string, 0, len(inMem))
for k := range inMem {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
if err := bitmapdb.TruncateRange64(tx, bucket, []byte(k), to+1); err != nil {
return fmt.Errorf("fail TruncateRange: bucket=%s, %w", bucket, err)
}
}
return nil
}
func PruneAccountHistoryIndex(s *PruneState, tx kv.RwTx, cfg HistoryCfg, ctx context.Context) (err error) {
if !cfg.prune.History.Enabled() {
return nil
}
logPrefix := s.LogPrefix()
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
pruneTo := cfg.prune.History.PruneTo(s.ForwardProgress)
if err = pruneHistoryIndex(tx, kv.AccountChangeSet, logPrefix, cfg.tmpdir, pruneTo, ctx); err != nil {
return err
}
if err = s.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
func PruneStorageHistoryIndex(s *PruneState, tx kv.RwTx, cfg HistoryCfg, ctx context.Context) (err error) {
if !cfg.prune.History.Enabled() {
return nil
}
logPrefix := s.LogPrefix()
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
pruneTo := cfg.prune.History.PruneTo(s.ForwardProgress)
if err = pruneHistoryIndex(tx, kv.StorageChangeSet, logPrefix, cfg.tmpdir, pruneTo, ctx); err != nil {
return err
}
if err = s.Done(tx); err != nil {
return err
}
if !useExternalTx {
if err = tx.Commit(); err != nil {
return err
}
}
return nil
}
func pruneHistoryIndex(tx kv.RwTx, csTable, logPrefix, tmpDir string, pruneTo uint64, ctx context.Context) error {
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
collector := etl.NewCollector(logPrefix, tmpDir, etl.NewOldestEntryBuffer(etl.BufferOptimalSize))
defer collector.Close()
if err := changeset.ForRange(tx, csTable, 0, pruneTo, func(blockNum uint64, k, _ []byte) error {
select {
case <-logEvery.C:
log.Info(fmt.Sprintf("[%s]", logPrefix), "table", csTable, "block_num", blockNum)
case <-ctx.Done():
return libcommon.ErrStopped
default:
}
return collector.Collect(k, nil)
}); err != nil {
return err
}
c, err := tx.RwCursor(changeset.Mapper[csTable].IndexBucket)
if err != nil {
return fmt.Errorf("failed to create cursor for pruning %w", err)
}
defer c.Close()
prefixLen := length.Addr
if csTable == kv.StorageChangeSet {
prefixLen = length.Hash
}
if err := collector.Load(tx, "", func(addr, _ []byte, table etl.CurrentTableReader, next etl.LoadNextFunc) error {
select {
case <-logEvery.C:
log.Info(fmt.Sprintf("[%s]", logPrefix), "table", changeset.Mapper[csTable].IndexBucket, "key", fmt.Sprintf("%x", addr))
case <-ctx.Done():
return libcommon.ErrStopped
default:
}
for k, _, err := c.Seek(addr); k != nil; k, _, err = c.Next() {
if err != nil {
return err
}
blockNum := binary.BigEndian.Uint64(k[prefixLen:])
if !bytes.HasPrefix(k, addr) || blockNum >= pruneTo {
break
}
if err = c.DeleteCurrent(); err != nil {
return fmt.Errorf("failed to remove for block %d: %w", blockNum, err)
}
}
return nil
}, etl.TransformArgs{}); err != nil {
return err
}
return nil
}