-
Notifications
You must be signed in to change notification settings - Fork 858
[giga] pipelining skeleton #2631
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
codchen
wants to merge
2
commits into
main
Choose a base branch
from
tony/executor-tracks
base: main
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.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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,52 @@ | ||
| package executor | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/sei-protocol/sei-chain/giga/executor/tracks" | ||
| ) | ||
|
|
||
| type Config struct { | ||
| StatelessWorkerCount int | ||
| ReceiptSinkWorkerCount int | ||
| blocksBuffer int | ||
| receiptsBuffer int | ||
| changeSetsBuffer int | ||
| } | ||
|
|
||
| func RunExecutor[RawBlock, Block tracks.Identifiable, Receipt, ChangeSet any]( | ||
| ctx context.Context, | ||
| config Config, | ||
| rawBlocks <-chan RawBlock, // channel where the consensus layer produces to | ||
| statelessFn func(RawBlock) Block, // TODO: function that checks sig, nonce, etc. | ||
| schedulerFn func(Block, chan<- Receipt) ChangeSet, // TODO: main processing logic | ||
| commitFn func(ChangeSet), // TODO: commit to working set after a block is fully done | ||
| receiptSinkFn func(Receipt), // TODO: persist receipts to disk | ||
| historicalStateSinkFn func(ChangeSet), // TODO: persist historical state to disk | ||
| prevBlock uint64, // TODO: the last executed block id, | ||
| ) { | ||
| blocks := make(chan Block, config.blocksBuffer) | ||
| receipts := make(chan Receipt, config.receiptsBuffer) | ||
| changeSets := make(chan ChangeSet, config.changeSetsBuffer) | ||
|
|
||
| // spins off `StatelessWorkerCount` goroutines. | ||
| statelessTrack := tracks.NewStatelessTrack(rawBlocks, blocks, statelessFn, config.StatelessWorkerCount, prevBlock) | ||
| // spins off 1 goroutine. | ||
| executionTrack := tracks.NewExecutionTrack(blocks, receipts, changeSets, schedulerFn, commitFn) | ||
| // spins off `ReceiptSinkWorkerCount` goroutines. | ||
| receiptSinkTrack := tracks.NewReceiptSinkTrack(receipts, receiptSinkFn, config.ReceiptSinkWorkerCount) | ||
| // spins off 1 goroutine. | ||
| historicalStateSinkTrack := tracks.NewHistoricalStateSinkTrack(changeSets, historicalStateSinkFn) | ||
|
|
||
| statelessTrack.Start() | ||
| executionTrack.Start() | ||
| receiptSinkTrack.Start() | ||
| historicalStateSinkTrack.Start() | ||
|
|
||
| <-ctx.Done() | ||
|
|
||
| statelessTrack.Stop() | ||
| executionTrack.Stop() | ||
| receiptSinkTrack.Stop() | ||
| historicalStateSinkTrack.Stop() | ||
| } |
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,34 @@ | ||
| package tracks | ||
|
|
||
| type ExecutionTrack[Block, Receipt, ChangeSet any] struct { | ||
| blocks <-chan Block | ||
| receipts chan<- Receipt | ||
| changeSets chan<- ChangeSet | ||
| schedulerFn func(Block, chan<- Receipt) ChangeSet | ||
| commitFn func(ChangeSet) | ||
| } | ||
|
|
||
| func NewExecutionTrack[Block, Receipt, ChangeSet any]( | ||
| blocks <-chan Block, | ||
| receipts chan<- Receipt, | ||
| changeSets chan<- ChangeSet, | ||
| schedulerFn func(Block, chan<- Receipt) ChangeSet, | ||
| commitFn func(ChangeSet), | ||
| ) *ExecutionTrack[Block, Receipt, ChangeSet] { | ||
| return &ExecutionTrack[Block, Receipt, ChangeSet]{blocks, receipts, changeSets, schedulerFn, commitFn} | ||
| } | ||
|
|
||
| func (t *ExecutionTrack[Block, Receipt, ChangeSet]) Start() { | ||
| go func() { | ||
| for block := range t.blocks { | ||
| rcs := t.schedulerFn(block, t.receipts) | ||
| t.commitFn(rcs) | ||
| t.changeSets <- rcs | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| func (t *ExecutionTrack[Block, Receipt, ChangeSet]) Stop() { | ||
| close(t.receipts) | ||
| close(t.changeSets) | ||
| } | ||
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,47 @@ | ||
| package tracks | ||
|
|
||
| type ReceiptSinkTrack[Receipt any] struct { | ||
| receipts <-chan Receipt | ||
| sinkFn func(Receipt) | ||
| workerCount int | ||
| } | ||
|
|
||
| func NewReceiptSinkTrack[Receipt any]( | ||
| receipts <-chan Receipt, | ||
| sinkFn func(Receipt), | ||
| workerCount int, | ||
| ) *ReceiptSinkTrack[Receipt] { | ||
| return &ReceiptSinkTrack[Receipt]{receipts, sinkFn, workerCount} | ||
| } | ||
|
|
||
| func (t *ReceiptSinkTrack[Receipt]) Start() { | ||
| go func() { | ||
| for receipt := range t.receipts { | ||
| t.sinkFn(receipt) | ||
| } | ||
| }() | ||
|
Comment on lines
+18
to
+22
Check noticeCode scanning / CodeQL Spawning a Go routine Note
Spawning a Go routine may be a possible source of non-determinism
|
||
| } | ||
|
|
||
| func (t *ReceiptSinkTrack[Receipt]) Stop() {} | ||
|
|
||
| type HistoricalStateSinkTrack[ChangeSet any] struct { | ||
| changeSets <-chan ChangeSet | ||
| sinkFn func(ChangeSet) | ||
| } | ||
|
|
||
| func NewHistoricalStateSinkTrack[ChangeSet any]( | ||
| changeSets <-chan ChangeSet, | ||
| sinkFn func(ChangeSet), | ||
| ) *HistoricalStateSinkTrack[ChangeSet] { | ||
| return &HistoricalStateSinkTrack[ChangeSet]{changeSets, sinkFn} | ||
| } | ||
|
|
||
| func (t *HistoricalStateSinkTrack[ChangeSet]) Start() { | ||
| go func() { | ||
| for changeSet := range t.changeSets { | ||
| t.sinkFn(changeSet) | ||
| } | ||
| }() | ||
|
Comment on lines
+40
to
+44
Check noticeCode scanning / CodeQL Spawning a Go routine Note
Spawning a Go routine may be a possible source of non-determinism
|
||
| } | ||
|
|
||
| func (t *HistoricalStateSinkTrack[ChangeSet]) Stop() {} | ||
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,59 @@ | ||
| package tracks | ||
|
|
||
| import ( | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| type Identifiable interface { | ||
| GetID() uint64 | ||
| } | ||
|
|
||
| type StatelessTrack[RawBlock, Block Identifiable] struct { | ||
| inputs <-chan RawBlock | ||
| outputs chan<- Block | ||
| processFn func(RawBlock) Block | ||
| workerCount int | ||
| prevBlock uint64 | ||
| } | ||
|
|
||
| func NewStatelessTrack[RawBlock, Block Identifiable]( | ||
| inputs <-chan RawBlock, | ||
| outputs chan<- Block, | ||
| processFn func(RawBlock) Block, | ||
| workerCount int, | ||
| prevBlock uint64, | ||
| ) *StatelessTrack[RawBlock, Block] { | ||
| return &StatelessTrack[RawBlock, Block]{inputs, outputs, processFn, workerCount, prevBlock} | ||
| } | ||
|
|
||
| func (t *StatelessTrack[RawBlock, Block]) Start() { | ||
| // completion signals are used to ensure outputs are in order. | ||
| completionSignals := sync.Map{} | ||
| lastBlockSignal := make(chan struct{}, 1) | ||
| lastBlockSignal <- struct{}{} | ||
| completionSignals.Store(t.prevBlock, lastBlockSignal) | ||
| for range t.workerCount { | ||
| go func() { | ||
| for input := range t.inputs { | ||
| completionSignal := make(chan struct{}, 1) | ||
| completionSignals.Store(input.GetID(), completionSignal) | ||
| output := t.processFn(input) | ||
| prevCompletionSignal, ok := completionSignals.Load(input.GetID() - 1) | ||
| // in practice it's almost impossible for ok == false, but we'll handle it anyway | ||
| for !ok { | ||
| time.Sleep(10 * time.Millisecond) | ||
| prevCompletionSignal, ok = completionSignals.Load(input.GetID() - 1) | ||
| } | ||
| <-prevCompletionSignal.(chan struct{}) | ||
| t.outputs <- output | ||
| completionSignal <- struct{}{} | ||
| completionSignals.Delete(input.GetID() - 1) | ||
| } | ||
| }() | ||
|
Comment on lines
+37
to
+53
Check noticeCode scanning / CodeQL Spawning a Go routine Note
Spawning a Go routine may be a possible source of non-determinism
|
||
| } | ||
| } | ||
|
|
||
| func (t *StatelessTrack[RawBlock, Block]) Stop() { | ||
| close(t.outputs) | ||
| } | ||
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,36 @@ | ||
| package tracks_test | ||
|
|
||
| import ( | ||
| "math/rand" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/sei-protocol/sei-chain/giga/executor/tracks" | ||
| "github.com/tendermint/tendermint/libs/utils/require" | ||
| ) | ||
|
|
||
| type testBlock struct{ id uint64 } | ||
|
|
||
| func (b *testBlock) GetID() uint64 { return b.id } | ||
|
|
||
| func TestStatelessTrack(t *testing.T) { | ||
| inputs := make(chan *testBlock, 100) | ||
| outputs := make(chan *testBlock, 100) | ||
| processFn := func(input *testBlock) *testBlock { | ||
| sleepDuration := time.Duration(rand.Intn(100)) * time.Millisecond | ||
| time.Sleep(sleepDuration) | ||
| return &testBlock{id: input.GetID()} | ||
| } | ||
| workerCount := 10 | ||
| lastBlock := uint64(100) | ||
| statelessTrack := tracks.NewStatelessTrack(inputs, outputs, processFn, workerCount, lastBlock) | ||
| statelessTrack.Start() | ||
| defer statelessTrack.Stop() | ||
| for i := range 100 { | ||
| inputs <- &testBlock{id: uint64(i) + 1 + lastBlock} | ||
| } | ||
| for i := range 100 { | ||
| output := <-outputs | ||
| require.Equal(t, uint64(i)+1+lastBlock, output.GetID()) | ||
| } | ||
| } |
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.
Check notice
Code scanning / CodeQL
Spawning a Go routine Note