Skip to content

Commit

Permalink
do advance at start
Browse files Browse the repository at this point in the history
  • Loading branch information
cristaloleg committed Jan 24, 2025
1 parent 7b5b282 commit ac9eb8c
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,14 +376,17 @@ func (s *Store[H]) Append(ctx context.Context, headers ...H) error {
// (1) Appends not to be blocked on long disk IO writes and underlying DB compactions
// (2) Batching header writes
func (s *Store[H]) flushLoop() {
// advance based on what we have on disk.
s.doAdvanceContiguousHead(context.Background(), s.Height())

defer close(s.writesDn)
ctx := context.Background()
for headers := range s.writes {
// add headers to the pending and ensure they are accessible
s.pending.Append(headers...)
// try to advance contiguousHead if we don't have gaps.
// and notify waiters in heightSub.
s.advanceContiguousHead(ctx, headers...)
s.tryAdvanceContiguousHead(ctx, headers...)
// don't flush and continue if pending batch is not grown enough,
// and Store is not stopping(headers == nil)
if s.pending.Len() < s.Params.WriteBatchSize && headers != nil {
Expand Down Expand Up @@ -497,17 +500,19 @@ func (s *Store[H]) get(ctx context.Context, hash header.Hash) ([]byte, error) {
}

// try advance contiguous head based on already written headers.
func (s *Store[H]) advanceContiguousHead(ctx context.Context, headers ...H) {
func (s *Store[H]) tryAdvanceContiguousHead(ctx context.Context, headers ...H) {
// always inform heightSub about new headers seen
for _, h := range headers {
s.heightSub.UnblockHeight(h.Height())
}

currHead := s.contiguousHead.Load()
if currHead == nil {
return
if currHead != nil {
s.doAdvanceContiguousHead(ctx, (*currHead).Height())
}
currHeight := (*currHead).Height()
}

func (s *Store[H]) doAdvanceContiguousHead(ctx context.Context, currHeight uint64) {
prevHeight := currHeight

// TODO(cristaloleg): benchmark this timeout or make it dynamic.
Expand Down

0 comments on commit ac9eb8c

Please sign in to comment.