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

fix: revise to call Begin/EndRecheck even though mem.Size() is 0 #219

Merged
merged 4 commits into from
Apr 19, 2021
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
12 changes: 2 additions & 10 deletions abci/client/local_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ var _ Client = (*localClient)(nil)
type localClient struct {
service.BaseService

// TODO: remove `mtx` to increase concurrency.
// CONTRACT: The application should protect itself from concurrency as an abci server.
mtx *tmsync.Mutex
types.Application
Callback
Expand Down Expand Up @@ -87,8 +89,6 @@ func (app *localClient) DeliverTxAsync(params types.RequestDeliverTx) *ReqRes {
}

func (app *localClient) CheckTxAsync(req types.RequestCheckTx) *ReqRes {
// CONTRACT: Application should handle concurrent `CheckTx`
// In this abci client layer, we don't protect `CheckTx` with a mutex for concurrency
res := app.Application.CheckTx(req)
return app.callback(
types.ToRequestCheckTx(req),
Expand Down Expand Up @@ -152,9 +152,6 @@ func (app *localClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes {
}

func (app *localClient) BeginRecheckTxAsync(req types.RequestBeginRecheckTx) *ReqRes {
app.mtx.Lock()
defer app.mtx.Unlock()

res := app.Application.BeginRecheckTx(req)
return app.callback(
types.ToRequestBeginRecheckTx(req),
Expand All @@ -163,9 +160,6 @@ func (app *localClient) BeginRecheckTxAsync(req types.RequestBeginRecheckTx) *Re
}

func (app *localClient) EndRecheckTxAsync(req types.RequestEndRecheckTx) *ReqRes {
app.mtx.Lock()
defer app.mtx.Unlock()

res := app.Application.EndRecheckTx(req)
return app.callback(
types.ToRequestEndRecheckTx(req),
Expand Down Expand Up @@ -252,8 +246,6 @@ func (app *localClient) DeliverTxSync(req types.RequestDeliverTx) (*types.Respon
}

func (app *localClient) CheckTxSync(req types.RequestCheckTx) (*types.ResponseCheckTx, error) {
// CONTRACT: Application should handle concurrent `CheckTx`
// In this abci client layer, we don't protect `CheckTx` with a mutex for concurrency
res := app.Application.CheckTx(req)
return &res, nil
}
Expand Down
70 changes: 31 additions & 39 deletions mempool/clist_mempool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"github.com/line/ostracon/p2p"
"github.com/line/ostracon/proxy"
"github.com/line/ostracon/types"
"github.com/pkg/errors"
)

// TxKeySize is the size of the transaction key index
Expand Down Expand Up @@ -586,7 +585,7 @@ func (mem *CListMempool) Update(
block *types.Block,
deliverTxResponses []*abci.ResponseDeliverTx,
preCheck PreCheckFunc,
) error {
) (err error) {
// Set height
mem.height = block.Height
mem.notifiedTxsAvailable = false
Expand Down Expand Up @@ -619,54 +618,47 @@ func (mem *CListMempool) Update(
}
}

// Either recheck non-committed txs to see if they became invalid
// or just notify there're some txs left.
recheckStartTime := time.Now().UnixNano()
if mem.Size() > 0 {
if mem.config.Recheck {
mem.logger.Debug("recheck txs", "numtxs", mem.Size(), "height", block.Height)
res, err := mem.proxyAppConn.BeginRecheckTxSync(abci.RequestBeginRecheckTx{
Header: types.OST2PB.Header(&block.Header),
})
if res.Code == abci.CodeTypeOK && err == nil {
mem.recheckTxs()
res2, err2 := mem.proxyAppConn.EndRecheckTxSync(abci.RequestEndRecheckTx{Height: block.Height})
if res2.Code != abci.CodeTypeOK {
return errors.New("the function EndRecheckTxSync does not respond CodeTypeOK")
}
if err2 != nil {
return errors.Wrap(err2, "the function EndRecheckTxSync returns an error")
}
} else {
if res.Code != abci.CodeTypeOK {
return errors.New("the function BeginRecheckTxSync does not respond CodeTypeOK")
}
if err != nil {
return errors.Wrap(err, "the function BeginRecheckTxSync returns an error")
}
}
if mem.config.Recheck {
// recheck non-committed txs to see if they became invalid
recheckStartTime := time.Now().UnixNano()

// At this point, mem.txs are being rechecked.
// mem.recheckCursor re-scans mem.txs and possibly removes some txs.
// Before mem.Reap(), we should wait for mem.recheckCursor to be nil.
} else {
mem.notifyTxsAvailable()
_, err = mem.proxyAppConn.BeginRecheckTxSync(abci.RequestBeginRecheckTx{
Header: types.OST2PB.Header(&block.Header),
})
if err != nil {
mem.logger.Error("error in proxyAppConn.BeginRecheckTxSync", "err", err)
}
}
recheckEndTime := time.Now().UnixNano()

recheckTimeMs := float64(recheckEndTime-recheckStartTime) / 1000000
mem.metrics.RecheckTime.Set(recheckTimeMs)
mem.logger.Info("recheck txs", "numtxs", mem.Size(), "height", block.Height)
mem.recheckTxs()

_, err = mem.proxyAppConn.EndRecheckTxSync(abci.RequestEndRecheckTx{Height: block.Height})
if err != nil {
mem.logger.Error("error in proxyAppConn.EndRecheckTxSync", "err", err)
}

recheckEndTime := time.Now().UnixNano()

recheckTimeMs := float64(recheckEndTime-recheckStartTime) / 1000000
mem.metrics.RecheckTime.Set(recheckTimeMs)

// At this point, mem.txs are being rechecked.
// mem.recheckCursor re-scans mem.txs and possibly removes some txs.
// Before mem.Reap(), we should wait for mem.recheckCursor to be nil.
} else if mem.Size() > 0 {
// just notify there're some txs left.
mem.notifyTxsAvailable()
}

// Update metrics
mem.metrics.Size.Set(float64(mem.Size()))

return nil
return err
}

func (mem *CListMempool) recheckTxs() {
if mem.Size() == 0 {
panic("recheckTxs is called, but the mempool is empty")
return
}

mem.recheckCursor = mem.txs.Front()
Expand Down