Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `hub`: sources created from a `ForkableHub` at the same time could lose their subscription: they got the hub's current blocks, then never a live block and no error.
- `(*transform.BlockIndexer).Add` no longer swallows a failed index write with a warning and moves on to the next range, which left permanent holes in the index; the error now propagates to the caller and `currentIndex` is left unchanged so a retry does not lose data. `writeIndex` also waits with a backoff between retry attempts instead of retrying back-to-back, and no longer leaks a context per attempt until the whole call returns.
- A live stream starting at a block number with no block (a missed or skipped slot) now starts at the next block instead of waiting for the merged bundle containing it.
- `hub.ForkableHub` no longer breaks when its one-block bootstrap races with the merger deleting the files of a bundle it just merged. Concretely: bootstrap retries a few times when the listing comes back with a gap or a listed file is gone by the time it is opened (both expected during that race), a failed bootstrap no longer leaves the forkable holding a bogus LIB it then can't reconcile with live blocks (the hub now only swaps in the forkable it built once bootstrap actually succeeds, falling back to a fresh one otherwise), and `cannot link block after reconnection` is only fatal once the hub has actually been ready — before that, an unlinkable block is handled like any other, already subject to `WithMaxConsecutiveUnlinkableBlocks`. Previously a consumer starting or restarting during that race could see the hub die or never become ready.
Expand Down
24 changes: 18 additions & 6 deletions hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"io"
"strconv"
"strings"
"sync"
"time"

"github.com/streamingfast/dstore"
Expand All @@ -46,6 +47,7 @@ type ForkableHub struct {

keepFinalBlocks int

subscribersLock sync.Mutex // subscribe only runs under the forkable's read lock
subscribers []*Subscription
sourceChannelSize int

Expand Down Expand Up @@ -129,7 +131,7 @@ func newForkableHub(liveSourceFactory bstream.SourceFactory, keepFinalBlocks int
)

hub.OnTerminating(func(err error) {
for _, sub := range hub.subscribers {
for _, sub := range hub.currentSubscribers() {
sub.Shutdown(err)
}
})
Expand Down Expand Up @@ -230,19 +232,31 @@ func (h *ForkableHub) IsReady() bool {
}
}

// subscribe must be called while hub is locked
// subscribe must be called while the forkable is locked (a read lock is enough)
func (h *ForkableHub) subscribe(handler bstream.Handler, initialBlocks []*bstream.PreprocessedBlock, withPartials bool) *Subscription {
chanSize := h.sourceChannelSize + len(initialBlocks)
sub := NewSubscription(handler, chanSize, withPartials)
for _, ppblk := range initialBlocks {
_ = sub.push(ppblk)
}

h.subscribersLock.Lock()
h.subscribers = append(h.subscribers, sub)
h.subscribersLock.Unlock()
return sub
}

// unsubscribe must be called while hub is locked
// currentSubscribers returns a snapshot, safe to range over without the lock
func (h *ForkableHub) currentSubscribers() []*Subscription {
h.subscribersLock.Lock()
defer h.subscribersLock.Unlock()
return h.subscribers
}

func (h *ForkableHub) unsubscribe(removeSub *Subscription) {
h.subscribersLock.Lock()
defer h.subscribersLock.Unlock()

var newSubscriber []*Subscription
for _, sub := range h.subscribers {
if sub != removeSub {
Expand Down Expand Up @@ -832,9 +846,7 @@ func (h *ForkableHub) broadcastBlock(blk *pbbstream.Block, obj any) error {

preprocBlock := &bstream.PreprocessedBlock{Block: blk, Obj: obj}

subscribers := h.subscribers // we may remove some from the original slice during the loop

for _, sub := range subscribers {
for _, sub := range h.currentSubscribers() {
err := sub.push(preprocBlock)
if err != nil {
h.unsubscribe(sub)
Expand Down
79 changes: 79 additions & 0 deletions hub/subscribe_race_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package hub

import (
"sync"
"testing"
"time"

"github.com/streamingfast/bstream"
pbbstream "github.com/streamingfast/bstream/pb/sf/bstream/v1"
"github.com/streamingfast/dstore"
"github.com/stretchr/testify/require"
)

// Sources created at the same time must all get live blocks.
func TestForkableHub_ConcurrentSubscribe(t *testing.T) {
const clients = 8
const rounds = 200

for round := range rounds {
lsf := bstream.NewTestSourceFactory()
store := dstore.NewMockStore(nil)
fh := NewForkableHub(lsf.NewSource, 0, store)
AddToMockStore(t, store,
bstream.TestBlockWithLIBNum("00000003", "00000002", 2),
bstream.TestBlockWithLIBNum("00000004", "00000003", 2),
bstream.TestBlockWithLIBNum("00000005", "00000004", 2),
bstream.TestBlockWithLIBNum("00000008", "00000005", 3),
)
go fh.Run()
select {
case <-fh.Ready:
case <-time.After(time.Second):
t.Fatal("hub not ready")
}
liveSource := <-lsf.Created

gotLiveBlock := make([]chan struct{}, clients)
sources := make([]bstream.Source, clients)

var wg sync.WaitGroup
start := make(chan struct{})
for i := range clients {
gotLiveBlock[i] = make(chan struct{}, 1)
handler := bstream.HandlerFunc(func(blk *pbbstream.Block, _ any) error {
if blk.Id == "00000009" {
gotLiveBlock[i] <- struct{}{}
}
return nil
})

wg.Add(1)
go func() {
defer wg.Done()
<-start
// as BlockstreamServer.Blocks does for each block request
sources[i] = fh.SourceFromBlockNumWithForks(3, handler, true)
}()
}
close(start)
wg.Wait()

for i, source := range sources {
require.NotNil(t, source, "round %d: client %d", round, i)
go source.Run()
}

require.NoError(t, liveSource.Push(bstream.TestBlockWithLIBNum("00000009", "00000008", 3), nil))

for i := range clients {
select {
case <-gotLiveBlock[i]:
case <-time.After(time.Second):
t.Fatalf("round %d: client %d never got the live block, the hub has %d subscribers for %d clients",
round, i, len(fh.currentSubscribers()), clients)
}
}
fh.Shutdown(nil)
}
}
Loading