-
Notifications
You must be signed in to change notification settings - Fork 16
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
fix(store): respect context in flushLoop #214
Open
cristaloleg
wants to merge
1
commit into
main
Choose a base branch
from
store/sleep-with-context
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -408,9 +408,12 @@ func (s *Store[H]) flushLoop() { | |
log.Errorw("writing header batch", "try", i+1, "from", from, "to", to, "err", err) | ||
s.metrics.flush(ctx, time.Since(startTime), s.pending.Len(), true) | ||
|
||
const maxRetrySleep = time.Second | ||
sleep := min(10*time.Duration(i+1)*time.Millisecond, maxRetrySleep) | ||
time.Sleep(sleep) | ||
const maxRetrySleep = 100 * time.Millisecond | ||
sleepDur := min(10*time.Duration(i+1)*time.Millisecond, maxRetrySleep) | ||
|
||
if err := sleep(ctx, sleepDur); err != nil { | ||
break | ||
} | ||
} | ||
|
||
s.metrics.flush(ctx, time.Since(startTime), s.pending.Len(), false) | ||
|
@@ -511,3 +514,16 @@ func indexTo[H header.Header[H]](ctx context.Context, batch datastore.Batch, hea | |
} | ||
return nil | ||
} | ||
|
||
// sleep with cancellation, returns nil when timer has fired, error otherwise. | ||
func sleep(ctx context.Context, duration time.Duration) error { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadly this func isn't presented in stdlib |
||
timer := time.NewTimer(duration) | ||
defer timer.Stop() | ||
|
||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-timer.C: | ||
return nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package store | |
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -89,6 +90,45 @@ func TestStore(t *testing.T) { | |
assert.Len(t, out, 12) | ||
} | ||
|
||
func TestStore_BadFlush(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
t.Cleanup(cancel) | ||
|
||
suite := headertest.NewTestSuite(t) | ||
|
||
var callCount atomic.Int32 | ||
|
||
rootDS := sync.MutexWrap(datastore.NewMapDatastore()) | ||
ds := &badBatchDatastore{ | ||
Datastore: rootDS, | ||
BatchFn: func(ctx context.Context) (datastore.Batch, error) { | ||
count := callCount.Add(1) | ||
// do not fail on 1st call due to store.Init and stop failing after 5 call. | ||
if count > 1 && count < 5 { | ||
return nil, context.Canceled | ||
} | ||
return rootDS.Batch(ctx) | ||
}, | ||
} | ||
store := NewTestStore(t, ctx, ds, suite.Head()) | ||
|
||
head, err := store.Head(ctx) | ||
require.NoError(t, err) | ||
assert.EqualValues(t, suite.Head().Hash(), head.Hash()) | ||
|
||
in := suite.GenDummyHeaders(10) | ||
err = store.Append(ctx, in...) | ||
require.NoError(t, err) | ||
|
||
assert.Eventually(t, func() bool { | ||
// accessing store.ds directly because inside store we use wrappedStore, | ||
// accessing ds or rootDS directly will result in constant error due to key prefix. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cost me 30min of my life :\ |
||
ok, err := store.ds.Has(ctx, headKey) | ||
require.NoError(t, err) | ||
return ok | ||
}, 3*time.Second, 50*time.Millisecond) | ||
} | ||
|
||
// TestStore_GetRangeByHeight_ExpectedRange | ||
func TestStore_GetRangeByHeight_ExpectedRange(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) | ||
|
@@ -279,3 +319,18 @@ func TestStoreInit(t *testing.T) { | |
err = store.Init(ctx, headers[len(headers)-1]) // init should work with any height, not only 1 | ||
require.NoError(t, err) | ||
} | ||
|
||
var _ datastore.Batching = &badBatchDatastore{} | ||
|
||
type badBatchDatastore struct { | ||
datastore.Datastore | ||
|
||
BatchFn func(ctx context.Context) (datastore.Batch, error) | ||
} | ||
|
||
func (s *badBatchDatastore) Batch(ctx context.Context) (datastore.Batch, error) { | ||
if s.BatchFn != nil { | ||
return s.BatchFn(ctx) | ||
} | ||
return nil, nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't that the case that this context is never canceled in practice?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Step 1: make to respect context.
Step 2: change context to something real.