-
Notifications
You must be signed in to change notification settings - Fork 5
Read concurrency #437
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
Open
Alrighttt
wants to merge
12
commits into
master
Choose a base branch
from
matt/read-concurrency
base: master
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.
+473
−53
Open
Read concurrency #437
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ac89ac9
use sync.RWMutex and bbolt View() for read-only methods
Alrighttt db7a3a3
golangci-lint fix
Alrighttt 21a3ef3
add changeset
Alrighttt d4981a6
Merge branch 'master' into matt/read-concurrency
Alrighttt f0f5400
whitespace
Alrighttt 6cdb243
add writeUnlock helper to pair store flush with lock release
Alrighttt ea2e279
Merge branch 'master' into matt/read-concurrency
Alrighttt 98a7da2
Merge branch 'master' into matt/read-concurrency
Alrighttt bcad357
add ReadonlyStore
Alrighttt 1661443
add concurrency_test.go
Alrighttt 94d628e
Remove panic
Alrighttt 38d9717
add ReadonlyDBBucket
Alrighttt 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
5 changes: 5 additions & 0 deletions
5
.changeset/change_chain_manager_mutex_to_rwmutex_to_enable_parallel_reads.md
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| default: patch | ||
| --- | ||
|
|
||
| # Change Chain Manager Mutex to RWMutex to enable parallel reads | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| package chain_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "go.sia.tech/core/types" | ||
| "go.sia.tech/coreutils" | ||
| "go.sia.tech/coreutils/chain" | ||
| "go.sia.tech/coreutils/testutil" | ||
| ) | ||
|
|
||
| // TestManagerConcurrentReadsWrites hammers the Manager's read methods from many | ||
| // goroutines while a writer continuously appends blocks and periodically forces | ||
| // reorgs. It exercises the RWMutex + per-reader snapshot design and, in | ||
| // particular, the flush-before-unlock invariant: once a tip is observable, the | ||
| // block and state behind it must be committed, so any later snapshot can read | ||
| // them. If a writer ever released the lock without flushing (m.mu.Unlock | ||
| // instead of m.writeUnlock), a reader could observe an advanced tip whose block | ||
| // is missing from the snapshot it reads, and the assertions below would fail. | ||
| // | ||
| // It runs against both store backends. The bbolt backend is the one that | ||
| // actually exercises the invariant: its snapshots are isolated read | ||
| // transactions that do not observe uncommitted writes, whereas MemDB has no | ||
| // MVCC and reads pending writes regardless of flushing. Run with -race to also | ||
| // catch any cross-goroutine sharing of store state. | ||
| func TestManagerConcurrentReadsWrites(t *testing.T) { | ||
| for _, tc := range []struct { | ||
| name string | ||
| makeDB func(testing.TB) chain.DB | ||
| }{ | ||
| {"MemDB", func(testing.TB) chain.DB { return chain.NewMemDB() }}, | ||
| {"BoltDB", func(tb testing.TB) chain.DB { | ||
| db, err := coreutils.OpenBoltChainDB(filepath.Join(tb.TempDir(), "consensus.db")) | ||
| if err != nil { | ||
| tb.Fatal(err) | ||
| } | ||
| tb.Cleanup(func() { db.Close() }) | ||
| return db | ||
| }}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| runConcurrentReadsWrites(t, tc.makeDB(t)) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func runConcurrentReadsWrites(t *testing.T, db chain.DB) { | ||
|
Member
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. Should we add a reader that calls |
||
| n, genesis := testutil.V2Network() | ||
| store, tipState, err := chain.NewDBStore(db, n, genesis, nil) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| cm := chain.NewManager(store, tipState) | ||
|
|
||
| // give readers some history to traverse | ||
| for i := 0; i < 20; i++ { | ||
| b, ok := coreutils.MineBlock(cm, types.VoidAddress, time.Second) | ||
| if !ok { | ||
| t.Fatal("PoW failed") | ||
| } else if err := cm.AddBlocks([]types.Block{b}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| } | ||
|
|
||
| const ( | ||
| totalBlocks = 200 | ||
| readers = 8 | ||
| reorgInterval = 20 // force a reorg roughly every N writer iterations | ||
| reorgDepth = 3 // blocks reverted per forced reorg | ||
| ) | ||
|
|
||
| done := make(chan struct{}) | ||
| var doneOnce sync.Once | ||
| closeDone := func() { doneOnce.Do(func() { close(done) }) } | ||
|
|
||
| var failOnce sync.Once | ||
| var failure error | ||
| fail := func(e error) { | ||
| failOnce.Do(func() { failure = e }) | ||
| closeDone() | ||
| } | ||
|
|
||
| // forkReorg builds a competing chain reorgDepth+2 blocks long from an | ||
| // ancestor reorgDepth back and submits it, forcing the Manager to revert | ||
| // and reapply. The fork is mined on an independent store, so building it | ||
| // never touches cm's store; only the final AddBlocks does. | ||
| forkReorg := func() bool { | ||
| tip := cm.Tip() | ||
| if tip.Height <= reorgDepth { | ||
| return false | ||
| } | ||
| aidx, ok := cm.BestIndex(tip.Height - reorgDepth) | ||
| if !ok { | ||
| return false | ||
| } | ||
| ab, ok := cm.Block(aidx.ID) | ||
| if !ok { | ||
| return false | ||
| } | ||
| parentState, ok := cm.State(ab.ParentID) | ||
| if !ok { | ||
| return false | ||
| } | ||
| fdb, ftip, err := chain.NewDBStoreAtCheckpoint(chain.NewMemDB(), parentState, ab, nil) | ||
| if err != nil { | ||
| fail(fmt.Errorf("fork: checkpoint init: %w", err)) | ||
| return false | ||
| } | ||
| fcm := chain.NewManager(fdb, ftip) | ||
| fork := make([]types.Block, 0, reorgDepth+2) | ||
| for j := 0; j < reorgDepth+2; j++ { | ||
| b, ok := coreutils.MineBlock(fcm, types.VoidAddress, 5*time.Second) | ||
| if !ok { | ||
| fail(fmt.Errorf("fork: PoW failed")) | ||
| return false | ||
| } else if err := fcm.AddBlocks([]types.Block{b}); err != nil { | ||
| fail(fmt.Errorf("fork: AddBlocks: %w", err)) | ||
| return false | ||
| } | ||
| fork = append(fork, b) | ||
| } | ||
| // the fork is strictly heavier (cm cannot advance while this single | ||
| // writer builds it), so this triggers a reorg. | ||
| if err := cm.AddBlocks(fork); err != nil { | ||
| fail(fmt.Errorf("writer: reorg AddBlocks: %w", err)) | ||
| return false | ||
| } | ||
| return true | ||
| } | ||
|
|
||
| var wg sync.WaitGroup | ||
|
|
||
| // writer: extend the chain, periodically forcing a reorg. | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| defer closeDone() | ||
| for i := 0; i < totalBlocks; i++ { | ||
| if i > 0 && i%reorgInterval == 0 { | ||
| if forkReorg() { | ||
| continue | ||
| } | ||
| select { | ||
| case <-done: | ||
| return // forkReorg failed | ||
| default: | ||
| } | ||
| } | ||
| b, ok := coreutils.MineBlock(cm, types.VoidAddress, 5*time.Second) | ||
| if !ok { | ||
| fail(fmt.Errorf("writer: PoW failed at block %d", i)) | ||
| return | ||
| } else if err := cm.AddBlocks([]types.Block{b}); err != nil { | ||
| fail(fmt.Errorf("writer: AddBlocks failed at block %d: %w", i, err)) | ||
| return | ||
| } | ||
| } | ||
| }() | ||
|
|
||
| genesisIndex := types.ChainIndex{Height: 0, ID: genesis.ID()} | ||
| for r := 0; r < readers; r++ { | ||
| wg.Add(1) | ||
| go func() { | ||
| defer wg.Done() | ||
| for { | ||
| select { | ||
| case <-done: | ||
| return | ||
| default: | ||
| } | ||
|
|
||
| // The core invariant, which survives reorgs: whatever tip the | ||
| // Manager reports, that tip's block and state must be committed, | ||
| // so a fresh snapshot can read them. This is exactly what | ||
| // flush-before-unlock guarantees. (We can't assert | ||
| // BestIndex(tip.Height) == tip here, because a reorg may land | ||
| // between observing the tip and the lookup.) | ||
| tip := cm.Tip() | ||
| if b, ok := cm.Block(tip.ID); !ok { | ||
| fail(fmt.Errorf("observed tip %v but its block is absent from the read snapshot", tip)) | ||
| return | ||
| } else if b.ID() != tip.ID { | ||
| fail(fmt.Errorf("Block(%v) returned block %v", tip.ID, b.ID())) | ||
| return | ||
| } | ||
| if _, ok := cm.State(tip.ID); !ok { | ||
| fail(fmt.Errorf("observed tip %v but its state is absent from the read snapshot", tip)) | ||
| return | ||
| } | ||
|
|
||
| // multi-lookup reads (each runs in a single snapshot): must | ||
| // remain internally consistent and never error on a live best | ||
| // chain, even while reorgs are happening. | ||
| hist, err := cm.History() | ||
| if err != nil { | ||
| fail(fmt.Errorf("History: %w", err)) | ||
| return | ||
| } | ||
| if _, _, err := cm.BlocksForHistory(hist[:], 10); err != nil { | ||
| fail(fmt.Errorf("BlocksForHistory: %w", err)) | ||
| return | ||
| } | ||
| if _, _, err := cm.Headers(genesisIndex, 100); err != nil { | ||
| fail(fmt.Errorf("Headers: %w", err)) | ||
| return | ||
| } | ||
| if _, _, err := cm.UpdatesSince(types.ChainIndex{}, 10); err != nil { | ||
| fail(fmt.Errorf("UpdatesSince: %w", err)) | ||
| return | ||
| } | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| wg.Wait() | ||
| if failure != nil { | ||
| t.Fatal(failure) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
This should probably be
minorconsidering we're extending the exportedStoreandDBinterfaces? Anyone implementing those will have to add Snapshot for it to compile.