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): properly update store head #207

Merged
merged 23 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
33 changes: 18 additions & 15 deletions store/heightsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,35 @@ func newHeightSub[H header.Header[H]]() *heightSub[H] {
}
}

// Height reports current height.
func (hs *heightSub[H]) Height() uint64 {
return hs.height.Load()
func (hs *heightSub[H]) isInited() bool {
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
return hs.height.Load() != 0
}

// SetHeight sets the new head height for heightSub.
func (hs *heightSub[H]) SetHeight(height uint64) {
hs.height.Store(height)
// setHeight sets the new head height for heightSub.
// Only higher then current height will be set.
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
func (hs *heightSub[H]) setHeight(height uint64) {
for {
curr := hs.height.Load()
if curr >= height {
return
}
if hs.height.CompareAndSwap(curr, height) {
return
}
}
}

// Sub subscribes for a header of a given height.
// It can return errElapsedHeight, which means a requested header was already provided
// and caller should get it elsewhere.
func (hs *heightSub[H]) Sub(ctx context.Context, height uint64) (H, error) {
var zero H
if hs.Height() >= height {
if hs.height.Load() >= height {
return zero, errElapsedHeight
}

hs.heightReqsLk.Lock()
if hs.Height() >= height {
if hs.height.Load() >= height {
// This is a rare case we have to account for.
// The lock above can park a goroutine long enough for hs.height to change for a requested height,
// leaving the request never fulfilled and the goroutine deadlocked.
Expand Down Expand Up @@ -81,21 +89,16 @@ func (hs *heightSub[H]) Sub(ctx context.Context, height uint64) (H, error) {

// Pub processes all the outstanding subscriptions matching the given headers.
// Pub is only safe when called from one goroutine.
// For Pub to work correctly, heightSub has to be initialized with SetHeight
// For Pub to work correctly, heightSub has to be initialized with setHeight
// so that given headers are contiguous to the height on heightSub.
func (hs *heightSub[H]) Pub(headers ...H) {
ln := len(headers)
if ln == 0 {
return
}

height := hs.Height()
from, to := headers[0].Height(), headers[ln-1].Height()
if height+1 != from && height != 0 { // height != 0 is needed to enable init from any height and not only 1
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
log.Fatalf("PLEASE FILE A BUG REPORT: headers given to the heightSub are in the wrong order: expected %d, got %d", height+1, from)
return
}
hs.SetHeight(to)
hs.setHeight(to)

hs.heightReqsLk.Lock()
defer hs.heightReqsLk.Unlock()
Expand Down
63 changes: 62 additions & 1 deletion store/heightsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestHeightSub(t *testing.T) {
{
h := headertest.RandDummyHeader(t)
h.HeightI = 100
hs.SetHeight(99)
hs.setHeight(99)
hs.Pub(h)

h, err := hs.Sub(ctx, 10)
Expand All @@ -47,6 +47,67 @@ func TestHeightSub(t *testing.T) {
}
}

func TestHeightSubNonAdjacement(t *testing.T) {
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()

hs := newHeightSub[*headertest.DummyHeader]()

{
h := headertest.RandDummyHeader(t)
h.HeightI = 100
hs.setHeight(99)
hs.Pub(h)
}

{
go func() {
// fixes flakiness on CI
time.Sleep(time.Millisecond)

h1 := headertest.RandDummyHeader(t)
h1.HeightI = 200
h2 := headertest.RandDummyHeader(t)
h2.HeightI = 300
hs.Pub(h1, h2)
}()

h, err := hs.Sub(ctx, 200)
assert.NoError(t, err)
assert.NotNil(t, h)
}
}

func TestHeightSub_monotonicHeight(t *testing.T) {
hs := newHeightSub[*headertest.DummyHeader]()

{
h := headertest.RandDummyHeader(t)
h.HeightI = 100
hs.setHeight(99)
hs.Pub(h)
}

{
h1 := headertest.RandDummyHeader(t)
h1.HeightI = 200
h2 := headertest.RandDummyHeader(t)
h2.HeightI = 300
hs.Pub(h1, h2)
}

{

h1 := headertest.RandDummyHeader(t)
h1.HeightI = 120
h2 := headertest.RandDummyHeader(t)
h2.HeightI = 130
hs.Pub(h1, h2)
}

assert.Equal(t, hs.height.Load(), uint64(300))
}

func TestHeightSubCancellation(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
Expand Down
96 changes: 76 additions & 20 deletions store/store.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package store

import (
"cmp"
"context"
"errors"
"fmt"
"slices"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -51,6 +54,12 @@ type Store[H header.Header[H]] struct {
writesDn chan struct{}
// writeHead maintains the current write head
writeHead atomic.Pointer[H]

knownHeadersLk sync.Mutex
// knownHeaders tracks all processed headers
// to advance writeHead only over continuous headers.
knownHeaders map[uint64]H

// pending keeps headers pending to be written in one batch
pending *batch[H]

Expand Down Expand Up @@ -108,11 +117,13 @@ func newStore[H header.Header[H]](ds datastore.Batching, opts ...Option) (*Store
writesDn: make(chan struct{}),
pending: newBatch[H](params.WriteBatchSize),
Params: params,

knownHeaders: make(map[uint64]H),
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
}, nil
}

func (s *Store[H]) Init(ctx context.Context, initial H) error {
if s.heightSub.Height() != 0 {
if s.heightSub.isInited() {
return errors.New("store already initialized")
}
// trust the given header as the initial head
Expand Down Expand Up @@ -164,27 +175,30 @@ func (s *Store[H]) Stop(ctx context.Context) error {
}

func (s *Store[H]) Height() uint64 {
return s.heightSub.Height()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

head, err := s.Head(ctx)
if err != nil {
// TODO(cristaloleg): log? panic? retry?
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
return 0
}
return head.Height()
}

// Head returns the highest contiguous header written to the store.
func (s *Store[H]) Head(ctx context.Context, _ ...header.HeadOption[H]) (H, error) {
head, err := s.GetByHeight(ctx, s.heightSub.Height())
if err == nil {
return head, nil
headPtr := s.writeHead.Load()
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
if headPtr != nil {
return *headPtr, nil
}

var zero H
head, err = s.readHead(ctx)
switch {
default:
head, err := s.readHead(ctx)
if err != nil {
var zero H
return zero, err
case errors.Is(err, datastore.ErrNotFound), errors.Is(err, header.ErrNotFound):
return zero, header.ErrNoHead
case err == nil:
s.heightSub.SetHeight(head.Height())
log.Infow("loaded head", "height", head.Height(), "hash", head.Hash())
return head, nil
}
return head, nil
}

func (s *Store[H]) Get(ctx context.Context, hash header.Hash) (H, error) {
Expand Down Expand Up @@ -300,6 +314,9 @@ func (s *Store[H]) HasAt(_ context.Context, height uint64) bool {
return height != uint64(0) && s.Height() >= height
}

// Append the given headers to the store. Real write to the disk happens
// asynchronously and might fail without reporting error (just logging).
// TODO(cristaloleg): add retries to the flush worker.
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
func (s *Store[H]) Append(ctx context.Context, headers ...H) error {
lh := len(headers)
if lh == 0 {
Expand All @@ -309,20 +326,25 @@ func (s *Store[H]) Append(ctx context.Context, headers ...H) error {
var err error
// take current write head to verify headers against
var head H
headPtr := s.writeHead.Load()
if headPtr == nil {
if headPtr := s.writeHead.Load(); headPtr == nil {
head, err = s.Head(ctx)
if err != nil {
return err
}
// store header from the disk.
gotHead := head
s.writeHead.CompareAndSwap(nil, &gotHead)
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
} else {
head = *headPtr
}

slices.SortFunc(headers, func(a, b H) int {
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
return cmp.Compare(a.Height(), b.Height())
})

// collect valid headers
verified := make([]H, 0, lh)
for i, h := range headers {

err = head.Verify(h)
if err != nil {
var verErr *header.VerifyError
Expand All @@ -344,11 +366,16 @@ func (s *Store[H]) Append(ctx context.Context, headers ...H) error {
}
verified = append(verified, h)
head = h

{
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
s.knownHeadersLk.Lock()
s.knownHeaders[head.Height()] = head
s.knownHeadersLk.Unlock()
}
}

onWrite := func() {
newHead := verified[len(verified)-1]
s.writeHead.Store(&newHead)
newHead := s.tryAdvanceHead()
log.Infow("new head", "height", newHead.Height(), "hash", newHead.Hash())
s.metrics.newHead(newHead.Height())
}
Expand Down Expand Up @@ -493,6 +520,35 @@ func (s *Store[H]) get(ctx context.Context, hash header.Hash) ([]byte, error) {
return data, nil
}

// try advance heighest header if we saw a higher continuous before.
func (s *Store[H]) tryAdvanceHead() H {
s.knownHeadersLk.Lock()
defer s.knownHeadersLk.Unlock()

head := *s.writeHead.Load()
height := head.Height()
currHead := head

// try to move to the next height.
for len(s.knownHeaders) > 0 {
h, ok := s.knownHeaders[height+1]
if !ok {
break
}
head = h
delete(s.knownHeaders, height+1)
height++
}

// if writeHead not set OR it's height is less then we found then update.
cristaloleg marked this conversation as resolved.
Show resolved Hide resolved
if currHead.Height() < head.Height() {
// we don't need CAS here because that's the only place
// where writeHead is updated, knownHeadersLk ensures 1 goroutine.
s.writeHead.Store(&head)
}
return head
}

// indexTo saves mapping between header Height and Hash to the given batch.
func indexTo[H header.Header[H]](ctx context.Context, batch datastore.Batch, headers ...H) error {
for _, h := range headers {
Expand Down
Loading
Loading