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

Problem: async fireEvents could overlap #6

Merged
merged 3 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 13 additions & 1 deletion consensus/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ var (
errPubKeyIsNotSet = errors.New("pubkey is not set. Look for \"Can't get private validator pubkey\" errors")
)

var msgQueueSize = 1000
var (
msgQueueSize = 1000
taskQueueSize = 128
)

// msgs from the reactor which may update the state
type msgInfo struct {
Expand Down Expand Up @@ -145,6 +148,9 @@ type State struct {

// offline state sync height indicating to which height the node synced offline
offlineStateSyncHeight int64

// run tasks asynchronously to avoid blocking block executor, currently mainly for firing tx/block events.
taskRunner *TaskRunner
}

// StateOption sets an optional parameter on the State.
Expand All @@ -160,6 +166,8 @@ func NewState(
evpool evidencePool,
options ...StateOption,
) *State {
taskRunner := StartTaskRunner(taskQueueSize)
blockExec.SetTaskRunner(taskRunner.RunTask)
cs := &State{
config: config,
blockExec: blockExec,
Expand All @@ -175,6 +183,7 @@ func NewState(
evpool: evpool,
evsw: cmtevents.NewEventSwitch(),
metrics: NopMetrics(),
taskRunner: taskRunner,
}
for _, option := range options {
option(cs)
Expand Down Expand Up @@ -438,6 +447,9 @@ func (cs *State) OnStop() {
cs.Logger.Error("failed trying to stop timeoutTicket", "error", err)
}
// WAL is stopped in receiveRoutine.

// Stop the task runner
cs.taskRunner.StopAndWait()
}

// Wait waits for the the main routine to return.
Expand Down
31 changes: 31 additions & 0 deletions consensus/task.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package consensus

import "sync"

type TaskRunner struct {
wg sync.WaitGroup
taskCh chan func()
}

func StartTaskRunner(buf int) *TaskRunner {
tr := &TaskRunner{
taskCh: make(chan func(), buf),
}
tr.wg.Add(1)
go func() {
defer tr.wg.Done()
for f := range tr.taskCh {
f()
yihuang marked this conversation as resolved.
Show resolved Hide resolved
}
}()
return tr
}

func (tr *TaskRunner) StopAndWait() {
close(tr.taskCh)
tr.wg.Wait()
}

func (tr *TaskRunner) RunTask(f func()) {
tr.taskCh <- f
}
22 changes: 21 additions & 1 deletion state/execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type BlockExecutor struct {
logger log.Logger

metrics *Metrics

asyncRunner func(func())
}

type BlockExecutorOption func(executor *BlockExecutor)
Expand All @@ -53,6 +55,12 @@ func BlockExecutorWithMetrics(metrics *Metrics) BlockExecutorOption {
}
}

func BlockExecutorWithAsyncRunner(runner func(func())) BlockExecutorOption {
return func(blockExec *BlockExecutor) {
blockExec.asyncRunner = runner
}
}

// NewBlockExecutor returns a new BlockExecutor with a NopEventBus.
// Call SetEventBus to provide one.
func NewBlockExecutor(
Expand Down Expand Up @@ -92,6 +100,10 @@ func (blockExec *BlockExecutor) SetEventBus(eventBus types.BlockEventPublisher)
blockExec.eventBus = eventBus
}

func (blockExec *BlockExecutor) SetTaskRunner(runner func(func())) {
blockExec.asyncRunner = runner
}

// CreateProposalBlock calls state.MakeBlock with evidence from the evpool
// and txs from the mempool. The max bytes must be big enough to fit the commit.
// The block space is first allocated to outstanding evidence.
Expand Down Expand Up @@ -318,7 +330,15 @@ func (blockExec *BlockExecutor) applyBlock(state State, blockID types.BlockID, b
if _, ok := blockExec.eventBus.(types.NopEventBus); !ok {
// Events are fired after everything else.
// NOTE: if we crash between Commit and Save, events wont be fired during replay
go fireEvents(blockExec.logger, blockExec.eventBus, block, blockID, abciResponse, validatorUpdates)
task := func() {
fireEvents(blockExec.logger, blockExec.eventBus, block, blockID, abciResponse, validatorUpdates)
}

if blockExec.asyncRunner != nil {
blockExec.asyncRunner(task)
} else {
task()
}
}

return state, nil
Expand Down
Loading