-
Notifications
You must be signed in to change notification settings - Fork 672
/
height_indexer.go
207 lines (177 loc) · 5.73 KB
/
height_indexer.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package indexer
import (
"context"
"fmt"
"time"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/database"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/vms/proposervm/state"
)
// default number of heights to index before committing
const (
defaultCommitFrequency = 1024
// Sleep [sleepDurationMultiplier]x (5x) the amount of time we spend processing the block
// to ensure the async indexing does not bottleneck the node.
sleepDurationMultiplier = 5
)
var _ HeightIndexer = &heightIndexer{}
type HeightIndexer interface {
// Returns whether the height index is fully repaired.
IsRepaired() bool
// MarkRepaired atomically sets the indexing repaired state.
MarkRepaired(isRepaired bool)
// Resumes repairing of the height index from the checkpoint.
RepairHeightIndex(context.Context) error
}
func NewHeightIndexer(
server BlockServer,
log logging.Logger,
indexState state.State,
) HeightIndexer {
return newHeightIndexer(server, log, indexState)
}
func newHeightIndexer(
server BlockServer,
log logging.Logger,
indexState state.State,
) *heightIndexer {
return &heightIndexer{
server: server,
log: log,
state: indexState,
commitFrequency: defaultCommitFrequency,
}
}
type heightIndexer struct {
server BlockServer
log logging.Logger
jobDone utils.AtomicBool
state state.State
commitFrequency int
}
func (hi *heightIndexer) IsRepaired() bool {
return hi.jobDone.GetValue()
}
func (hi *heightIndexer) MarkRepaired(repaired bool) {
hi.jobDone.SetValue(repaired)
}
// RepairHeightIndex ensures the height -> proBlkID height block index is well formed.
// Starting from the checkpoint, it will go back to snowman++ activation fork
// or genesis. PreFork blocks will be handled by innerVM height index.
// RepairHeightIndex can take a non-trivial time to complete; hence we make sure
// the process has limited memory footprint, can be resumed from periodic checkpoints
// and works asynchronously without blocking the VM.
func (hi *heightIndexer) RepairHeightIndex(ctx context.Context) error {
startBlkID, err := hi.state.GetCheckpoint()
if err == database.ErrNotFound {
hi.MarkRepaired(true)
return nil // nothing to do
}
if err != nil {
return err
}
// retrieve checkpoint height. We explicitly track block height
// in doRepair to avoid heavier DB reads.
startBlk, err := hi.server.GetFullPostForkBlock(startBlkID)
if err != nil {
return err
}
startHeight := startBlk.Height()
if err := hi.doRepair(ctx, startBlkID, startHeight); err != nil {
return fmt.Errorf("could not repair height index: %w", err)
}
if err := hi.flush(); err != nil {
return fmt.Errorf("could not write final height index update: %w", err)
}
return nil
}
// if height index needs repairing, doRepair would do that. It
// iterates back via parents, checking and rebuilding height indexing.
// Note: batch commit is deferred to doRepair caller
func (hi *heightIndexer) doRepair(ctx context.Context, currentProBlkID ids.ID, lastIndexedHeight uint64) error {
var (
start = time.Now()
lastLogTime = start
indexedBlks int
lastIndexedBlks int
)
for {
if err := ctx.Err(); err != nil {
return err
}
processingStart := time.Now()
currentAcceptedBlk, _, err := hi.state.GetBlock(currentProBlkID)
if err == database.ErrNotFound {
// We have visited all the proposerVM blocks. Because we previously
// verified that we needed to perform a repair, we know that this
// will not happen on the first iteration. This guarantees that
// forkHeight will be correctly initialized.
forkHeight := lastIndexedHeight + 1
if err := hi.state.SetForkHeight(forkHeight); err != nil {
return err
}
if err := hi.state.DeleteCheckpoint(); err != nil {
return err
}
hi.MarkRepaired(true)
// it will commit on exit
hi.log.Info("indexing finished",
zap.Int("numIndexedBlocks", indexedBlks),
zap.Duration("duration", time.Since(start)),
zap.Uint64("forkHeight", forkHeight),
)
return nil
}
if err != nil {
return err
}
// Keep memory footprint under control by committing when a size threshold is reached
if indexedBlks-lastIndexedBlks > hi.commitFrequency {
// Note: checkpoint must be the lowest block in the batch. This ensures that
// checkpoint is the highest un-indexed block from which process would restart.
if err := hi.state.SetCheckpoint(currentProBlkID); err != nil {
return err
}
if err := hi.flush(); err != nil {
return err
}
hi.log.Debug("indexed blocks",
zap.Int("numIndexBlocks", indexedBlks),
)
lastIndexedBlks = indexedBlks
}
// Rebuild height block index.
if err := hi.state.SetBlockIDAtHeight(lastIndexedHeight, currentProBlkID); err != nil {
return err
}
// Periodically log progress
indexedBlks++
now := time.Now()
if now.Sub(lastLogTime) > 15*time.Second {
lastLogTime = now
hi.log.Info("indexed blocks",
zap.Int("numIndexBlocks", indexedBlks),
zap.Uint64("lastIndexedHeight", lastIndexedHeight),
)
}
// keep checking the parent
currentProBlkID = currentAcceptedBlk.ParentID()
lastIndexedHeight--
processingDuration := time.Since(processingStart)
// Sleep [sleepDurationMultiplier]x (5x) the amount of time we spend processing the block
// to ensure the indexing does not bottleneck the node.
time.Sleep(processingDuration * sleepDurationMultiplier)
}
}
// flush writes the commits to the underlying DB
func (hi *heightIndexer) flush() error {
if err := hi.state.Commit(); err != nil {
return err
}
return hi.server.Commit()
}