forked from erigontech/erigon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage_log_index.go
457 lines (403 loc) · 11.3 KB
/
stage_log_index.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
package stagedsync
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"runtime"
"time"
"github.com/RoaringBitmap/roaring"
"github.com/c2h5oh/datasize"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/etl"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/ethdb/bitmapdb"
"github.com/ledgerwatch/erigon/ethdb/cbor"
"github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/ledgerwatch/log/v3"
"golang.org/x/exp/slices"
)
const (
bitmapsBufLimit = 256 * datasize.MB // limit how much memory can use bitmaps before flushing to DB
bitmapsFlushEvery = 10 * time.Second
)
type LogIndexCfg struct {
tmpdir string
db kv.RwDB
prune prune.Mode
bufLimit datasize.ByteSize
flushEvery time.Duration
}
func StageLogIndexCfg(db kv.RwDB, prune prune.Mode, tmpDir string) LogIndexCfg {
return LogIndexCfg{
db: db,
prune: prune,
bufLimit: bitmapsBufLimit,
flushEvery: bitmapsFlushEvery,
tmpdir: tmpDir,
}
}
func SpawnLogIndex(s *StageState, tx kv.RwTx, cfg LogIndexCfg, 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()
}
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
}
startBlock := s.BlockNumber
pruneTo := cfg.prune.Receipts.PruneTo(endBlock)
if startBlock < pruneTo {
startBlock = pruneTo
}
if startBlock > 0 {
startBlock++
}
if err = promoteLogIndex(logPrefix, tx, startBlock, cfg, ctx); 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 promoteLogIndex(logPrefix string, tx kv.RwTx, start uint64, cfg LogIndexCfg, ctx context.Context) error {
quit := ctx.Done()
logEvery := time.NewTicker(30 * time.Second)
defer logEvery.Stop()
topics := map[string]*roaring.Bitmap{}
addresses := map[string]*roaring.Bitmap{}
logs, err := tx.Cursor(kv.Log)
if err != nil {
return err
}
defer logs.Close()
checkFlushEvery := time.NewTicker(cfg.flushEvery)
defer checkFlushEvery.Stop()
collectorTopics := etl.NewCollector(logPrefix, cfg.tmpdir, etl.NewSortableBuffer(etl.BufferOptimalSize))
defer collectorTopics.Close()
collectorAddrs := etl.NewCollector(logPrefix, cfg.tmpdir, etl.NewSortableBuffer(etl.BufferOptimalSize))
defer collectorAddrs.Close()
reader := bytes.NewReader(nil)
for k, v, err := logs.Seek(dbutils.LogKey(start, 0)); k != nil; k, v, err = logs.Next() {
if err != nil {
return err
}
if err := libcommon.Stopped(quit); err != nil {
return err
}
blockNum := binary.BigEndian.Uint64(k[:8])
select {
default:
case <-logEvery.C:
var m runtime.MemStats
libcommon.ReadMemStats(&m)
log.Info(fmt.Sprintf("[%s] Progress", logPrefix), "number", blockNum, "alloc", libcommon.ByteCount(m.Alloc), "sys", libcommon.ByteCount(m.Sys))
case <-checkFlushEvery.C:
if needFlush(topics, cfg.bufLimit) {
if err := flushBitmaps(collectorTopics, topics); err != nil {
return err
}
topics = map[string]*roaring.Bitmap{}
}
if needFlush(addresses, cfg.bufLimit) {
if err := flushBitmaps(collectorAddrs, addresses); err != nil {
return err
}
addresses = map[string]*roaring.Bitmap{}
}
}
var ll types.Logs
reader.Reset(v)
if err := cbor.Unmarshal(&ll, reader); err != nil {
return fmt.Errorf("receipt unmarshal failed: %w, blocl=%d", err, blockNum)
}
for _, l := range ll {
for _, topic := range l.Topics {
topicStr := string(topic.Bytes())
m, ok := topics[topicStr]
if !ok {
m = roaring.New()
topics[topicStr] = m
}
m.Add(uint32(blockNum))
}
accStr := string(l.Address.Bytes())
m, ok := addresses[accStr]
if !ok {
m = roaring.New()
addresses[accStr] = m
}
m.Add(uint32(blockNum))
}
}
if err := flushBitmaps(collectorTopics, topics); err != nil {
return err
}
if err := flushBitmaps(collectorAddrs, addresses); err != nil {
return err
}
var currentBitmap = roaring.New()
var buf = bytes.NewBuffer(nil)
lastChunkKey := make([]byte, 128)
var loaderFunc = func(k []byte, v []byte, table etl.CurrentTableReader, next etl.LoadNextFunc) error {
lastChunkKey = lastChunkKey[:len(k)+4]
copy(lastChunkKey, k)
binary.BigEndian.PutUint32(lastChunkKey[len(k):], ^uint32(0))
lastChunkBytes, err := table.Get(lastChunkKey)
if err != nil {
return fmt.Errorf("find last chunk: %w", err)
}
lastChunk := roaring.New()
if len(lastChunkBytes) > 0 {
_, err = lastChunk.FromBuffer(lastChunkBytes)
if err != nil {
return fmt.Errorf("couldn't read last log index chunk: %w, len(lastChunkBytes)=%d", err, len(lastChunkBytes))
}
}
if _, err := currentBitmap.FromBuffer(v); err != nil {
return err
}
currentBitmap.Or(lastChunk) // merge last existing chunk from db - next loop will overwrite it
return bitmapdb.WalkChunkWithKeys(k, currentBitmap, bitmapdb.ChunkLimit, func(chunkKey []byte, chunk *roaring.Bitmap) error {
buf.Reset()
if _, err := chunk.WriteTo(buf); err != nil {
return err
}
return next(k, chunkKey, buf.Bytes())
})
}
if err := collectorTopics.Load(tx, kv.LogTopicIndex, loaderFunc, etl.TransformArgs{Quit: quit}); err != nil {
return err
}
if err := collectorAddrs.Load(tx, kv.LogAddressIndex, loaderFunc, etl.TransformArgs{Quit: quit}); err != nil {
return err
}
return nil
}
func UnwindLogIndex(u *UnwindState, s *StageState, tx kv.RwTx, cfg LogIndexCfg, ctx context.Context) (err error) {
quitCh := ctx.Done()
useExternalTx := tx != nil
if !useExternalTx {
tx, err = cfg.db.BeginRw(ctx)
if err != nil {
return err
}
defer tx.Rollback()
}
logPrefix := s.LogPrefix()
if err := unwindLogIndex(logPrefix, tx, u.UnwindPoint, cfg, quitCh); err != nil {
return err
}
if err := u.Done(tx); err != nil {
return fmt.Errorf("%w", err)
}
if !useExternalTx {
if err := tx.Commit(); err != nil {
return err
}
}
return nil
}
func unwindLogIndex(logPrefix string, db kv.RwTx, to uint64, cfg LogIndexCfg, quitCh <-chan struct{}) error {
topics := map[string]struct{}{}
addrs := map[string]struct{}{}
reader := bytes.NewReader(nil)
c, err := db.Cursor(kv.Log)
if err != nil {
return err
}
defer c.Close()
for k, v, err := c.Seek(dbutils.EncodeBlockNumber(to + 1)); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
if err := libcommon.Stopped(quitCh); err != nil {
return err
}
var logs types.Logs
reader.Reset(v)
if err := cbor.Unmarshal(&logs, reader); err != nil {
return fmt.Errorf("receipt unmarshal: %w, block=%d", err, binary.BigEndian.Uint64(k))
}
for _, l := range logs {
for _, topic := range l.Topics {
topics[string(topic.Bytes())] = struct{}{}
}
addrs[string(l.Address.Bytes())] = struct{}{}
}
}
if err := truncateBitmaps(db, kv.LogTopicIndex, topics, to); err != nil {
return err
}
if err := truncateBitmaps(db, kv.LogAddressIndex, addrs, to); err != nil {
return err
}
return nil
}
func needFlush(bitmaps map[string]*roaring.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 flushBitmaps(c *etl.Collector, inMem map[string]*roaring.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 truncateBitmaps(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)
}
slices.Sort(keys)
for _, k := range keys {
if err := bitmapdb.TruncateRange(tx, bucket, []byte(k), uint32(to+1)); err != nil {
return fmt.Errorf("fail TruncateRange: bucket=%s, %w", bucket, err)
}
}
return nil
}
func pruneOldLogChunks(tx kv.RwTx, bucket string, inMem map[string]struct{}, pruneTo uint64, logPrefix string, ctx context.Context) error {
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
keys := make([]string, 0, len(inMem))
for k := range inMem {
keys = append(keys, k)
}
slices.Sort(keys)
c, err := tx.RwCursor(bucket)
if err != nil {
return err
}
defer c.Close()
for _, kS := range keys {
seek := []byte(kS)
for k, _, err := c.Seek(seek); k != nil; k, _, err = c.Next() {
if err != nil {
return err
}
blockNum := uint64(binary.BigEndian.Uint32(k[len(seek):]))
if !bytes.HasPrefix(k, seek) || blockNum >= pruneTo {
break
}
select {
case <-logEvery.C:
log.Info(fmt.Sprintf("[%s]", logPrefix), "table", kv.AccountsHistory, "block", blockNum)
case <-ctx.Done():
return libcommon.ErrStopped
default:
}
if err = c.DeleteCurrent(); err != nil {
return fmt.Errorf("failed delete, block=%d: %w", blockNum, err)
}
}
}
return nil
}
func PruneLogIndex(s *PruneState, tx kv.RwTx, cfg LogIndexCfg, ctx context.Context) (err error) {
if !cfg.prune.Receipts.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.Receipts.PruneTo(s.ForwardProgress)
if err = pruneLogIndex(logPrefix, tx, 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 pruneLogIndex(logPrefix string, tx kv.RwTx, tmpDir string, pruneTo uint64, ctx context.Context) error {
logEvery := time.NewTicker(logInterval)
defer logEvery.Stop()
topics := map[string]struct{}{}
addrs := map[string]struct{}{}
reader := bytes.NewReader(nil)
{
c, err := tx.Cursor(kv.Log)
if err != nil {
return err
}
defer c.Close()
for k, v, err := c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
return err
}
blockNum := binary.BigEndian.Uint64(k)
if blockNum >= pruneTo {
break
}
select {
case <-logEvery.C:
log.Info(fmt.Sprintf("[%s]", logPrefix), "table", kv.Log, "block", blockNum)
case <-ctx.Done():
return libcommon.ErrStopped
default:
}
var logs types.Logs
reader.Reset(v)
if err := cbor.Unmarshal(&logs, reader); err != nil {
return fmt.Errorf("receipt unmarshal failed: %w, block=%d", err, binary.BigEndian.Uint64(k))
}
for _, l := range logs {
for _, topic := range l.Topics {
topics[string(topic.Bytes())] = struct{}{}
}
addrs[string(l.Address.Bytes())] = struct{}{}
}
}
}
if err := pruneOldLogChunks(tx, kv.LogTopicIndex, topics, pruneTo, logPrefix, ctx); err != nil {
return err
}
if err := pruneOldLogChunks(tx, kv.LogAddressIndex, addrs, pruneTo, logPrefix, ctx); err != nil {
return err
}
return nil
}