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

refactor(pruner): reset maxHeadersPerLoop properly #3552

Merged
merged 3 commits into from
Jul 8, 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
8 changes: 4 additions & 4 deletions pruner/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// maxHeadersPerLoop is the maximum number of headers to fetch
// for a prune loop (prevents fetching too many headers at a
// time for nodes that have a large number of pruneable headers).
var maxHeadersPerLoop = uint64(512)
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
var maxHeadersPerLoop = 512

// findPruneableHeaders returns all headers that are eligible for pruning
// (outside the sampling window).
Expand Down Expand Up @@ -56,7 +56,7 @@ func (s *Service) findPruneableHeaders(
// loop we could increase by a range every iteration
headerCount := len(headers)
for {
if headerCount > int(maxHeadersPerLoop) {
if headerCount > maxHeadersPerLoop {
headers = headers[:maxHeadersPerLoop]
break
}
Expand Down Expand Up @@ -106,8 +106,8 @@ func (s *Service) calculateEstimatedCutoff(
estimatedCutoffHeight = head.Height()
}

if estimatedCutoffHeight-lastPruned.Height() > maxHeadersPerLoop {
estimatedCutoffHeight = lastPruned.Height() + maxHeadersPerLoop
if estimatedCutoffHeight-lastPruned.Height() > uint64(maxHeadersPerLoop) {
estimatedCutoffHeight = lastPruned.Height() + uint64(maxHeadersPerLoop)
}

return estimatedCutoffHeight, nil
Expand Down
2 changes: 1 addition & 1 deletion pruner/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (s *Service) prune(
return lastPrunedHeader
}

if uint64(len(headers)) < maxHeadersPerLoop {
if len(headers) < maxHeadersPerLoop {
// we've pruned all the blocks we can
return lastPrunedHeader
}
Expand Down
11 changes: 5 additions & 6 deletions pruner/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,18 +159,17 @@ func TestPrune_LargeNumberOfBlocks(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)

maxHeadersPerLoop = 10
t.Cleanup(func() {
maxHeadersPerLoop = 1024
})
var maxHeadersPerLoopOld int
maxHeadersPerLoopOld, maxHeadersPerLoop = maxHeadersPerLoop, 10
t.Cleanup(func() { maxHeadersPerLoop = maxHeadersPerLoopOld })

blockTime := time.Nanosecond
availabilityWindow := AvailabilityWindow(blockTime * 10)

// all headers generated in suite are timestamped to time.Now(), so
// they will all be considered "pruneable" within the availability window
suite := headertest.NewTestSuite(t, 1, blockTime)
store := headertest.NewCustomStore(t, suite, int(maxHeadersPerLoop*6)) // add small buffer
store := headertest.NewCustomStore(t, suite, maxHeadersPerLoop*6) // add small buffer

mp := &mockPruner{failHeight: make(map[uint64]int, 0)}

Expand All @@ -196,7 +195,7 @@ func TestPrune_LargeNumberOfBlocks(t *testing.T) {
_ = serv.prune(ctx, lastPruned)

// ensure all headers have been pruned
assert.Equal(t, maxHeadersPerLoop*5, serv.checkpoint.LastPrunedHeight)
assert.Equal(t, uint64(maxHeadersPerLoop*5), serv.checkpoint.LastPrunedHeight)
assert.Len(t, serv.checkpoint.FailedHeaders, 0)
}

Expand Down
Loading