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

fix(store): respect context in flushLoop #214

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
22 changes: 19 additions & 3 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Member

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?

Copy link
Contributor Author

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.

break
}
}

s.metrics.flush(ctx, time.Since(startTime), s.pending.Len(), false)
Expand Down Expand Up @@ -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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sadly this func isn't presented in stdlib sync package :(

CC: https://github.com/cristalhq/synx

timer := time.NewTimer(duration)
defer timer.Stop()

select {
case <-ctx.Done():
return ctx.Err()
case <-timer.C:
return nil
}
}
55 changes: 55 additions & 0 deletions store/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package store

import (
"context"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -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.
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)
Expand Down Expand Up @@ -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
}
Loading