From 2b709c380eed2f460c2c928bf09503088d63e0b0 Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Wed, 22 May 2024 14:08:29 -0400 Subject: [PATCH 1/2] Grab iterator at previously executed height --- .../snowman/bootstrap/interval/state.go | 10 ++++++ snow/engine/snowman/bootstrap/storage.go | 35 +++++++++++-------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/snow/engine/snowman/bootstrap/interval/state.go b/snow/engine/snowman/bootstrap/interval/state.go index cf2e2bf3a2ef..8ba06824eea2 100644 --- a/snow/engine/snowman/bootstrap/interval/state.go +++ b/snow/engine/snowman/bootstrap/interval/state.go @@ -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)) } diff --git a/snow/engine/snowman/bootstrap/storage.go b/snow/engine/snowman/bootstrap/storage.go index b70330218439..d28b87cba78e 100644 --- a/snow/engine/snowman/bootstrap/storage.go +++ b/snow/engine/snowman/bootstrap/storage.go @@ -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), + ) } } @@ -167,6 +169,22 @@ func execute( ) defer func() { iterator.Release() + + 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", haltable.Halted()), + zap.Duration("duration", time.Since(startTime)), + ) }() log("executing blocks", @@ -208,7 +226,7 @@ func execute( processedSinceIteratorRelease = 0 iterator.Release() - iterator = interval.GetBlockIterator(db) + iterator = interval.GetBlockIteratorWithStart(db, height) } if now := time.Now(); now.After(timeOfNextLog) { @@ -248,16 +266,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() } From 7d9a96664ba8ed2a7eaa1511e6a257430767c37a Mon Sep 17 00:00:00 2001 From: Stephen Buttolph Date: Wed, 22 May 2024 16:31:52 -0400 Subject: [PATCH 2/2] comments --- snow/engine/snowman/bootstrap/storage.go | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/snow/engine/snowman/bootstrap/storage.go b/snow/engine/snowman/bootstrap/storage.go index d28b87cba78e..53c2e735a572 100644 --- a/snow/engine/snowman/bootstrap/storage.go +++ b/snow/engine/snowman/bootstrap/storage.go @@ -170,19 +170,22 @@ func execute( defer func() { iterator.Release() - 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), - ) + 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", haltable.Halted()), + zap.Bool("halted", halted), zap.Duration("duration", time.Since(startTime)), ) }() @@ -226,7 +229,10 @@ func execute( processedSinceIteratorRelease = 0 iterator.Release() - iterator = interval.GetBlockIteratorWithStart(db, height) + // 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) {