Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grab iterator at previously executed height #3045

Merged
merged 2 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions snow/engine/snowman/bootstrap/interval/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ func GetBlockIterator(db database.Iteratee) database.Iterator {
return db.NewIteratorWithPrefix(blockPrefix)
}

// GetBlockIterator returns a block iterator that will produce values
// corresponding to persisted blocks in order of increasing height starting at
// [height].
func GetBlockIteratorWithStart(db database.Iteratee, height uint64) database.Iterator {
return db.NewIteratorWithStartAndPrefix(
makeBlockKey(height),
blockPrefix,
)
}

func GetBlock(db database.KeyValueReader, height uint64) ([]byte, error) {
return db.Get(makeBlockKey(height))
}
Expand Down
41 changes: 27 additions & 14 deletions snow/engine/snowman/bootstrap/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ func execute(
log("compacting database before executing blocks...")
if err := db.Compact(nil, nil); err != nil {
// Not a fatal error, log and move on.
log("failed to compact bootstrap database before executing blocks", zap.Error(err))
log("failed to compact bootstrap database before executing blocks",
zap.Error(err),
)
}
}

Expand Down Expand Up @@ -167,6 +169,25 @@ func execute(
)
defer func() {
iterator.Release()

halted := haltable.Halted()
if !halted {
log("compacting database after executing blocks...")
if err := db.Compact(nil, nil); err != nil {
// Not a fatal error, log and move on.
log("failed to compact bootstrap database after executing blocks",
zap.Error(err),
)
}
}

numProcessed := totalNumberToProcess - tree.Len()
log("executed blocks",
zap.Uint64("numExecuted", numProcessed),
zap.Uint64("numToExecute", totalNumberToProcess),
zap.Bool("halted", halted),
zap.Duration("duration", time.Since(startTime)),
)
}()

log("executing blocks",
Expand Down Expand Up @@ -208,7 +229,10 @@ func execute(

processedSinceIteratorRelease = 0
iterator.Release()
iterator = interval.GetBlockIterator(db)
// We specify the starting key of the iterator so that the
// underlying database doesn't need to scan over the, potentially
// not yet compacted, blocks we just deleted.
iterator = interval.GetBlockIteratorWithStart(db, height+1)
}

if now := time.Now(); now.After(timeOfNextLog) {
Expand Down Expand Up @@ -248,16 +272,5 @@ func execute(
if err := writeBatch(); err != nil {
return err
}
if err := iterator.Error(); err != nil {
return err
}

numProcessed := totalNumberToProcess - tree.Len()
log("executed blocks",
zap.Uint64("numExecuted", numProcessed),
zap.Uint64("numToExecute", totalNumberToProcess),
zap.Bool("halted", haltable.Halted()),
zap.Duration("duration", time.Since(startTime)),
)
return nil
return iterator.Error()
}
Loading