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

perf(state/store): avoid double-saving ABCI responses #13

Merged
merged 3 commits into from
Mar 8, 2024
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- `[state]` avoid double-saving `FinalizeBlockResponse` for performance reasons
([\#2017](https://github.com/cometbft/cometbft/pull/2017))
82 changes: 43 additions & 39 deletions state/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ func calcABCIResponsesKey(height int64) []byte {

//----------------------

var lastABCIResponseKey = []byte("lastABCIResponseKey")
var offlineStateSyncHeight = []byte("offlineStateSyncHeightKey")
var (
lastABCIResponseKey = []byte("lastABCIResponseKey") // DEPRECATED
offlineStateSyncHeight = []byte("offlineStateSyncHeightKey")
)

//go:generate ../scripts/mockery_generate.sh Store

Expand Down Expand Up @@ -399,16 +401,16 @@ func (store dbStore) LoadABCIResponses(height int64) (*cmtstate.ABCIResponses, e
return nil, ErrNoABCIResponsesForHeight{height}
}

abciResponses := new(cmtstate.ABCIResponses)
err = abciResponses.Unmarshal(buf)
resp := new(cmtstate.ABCIResponses)
err = resp.Unmarshal(buf)
if err != nil {
// DATA HAS BEEN CORRUPTED OR THE SPEC HAS CHANGED
cmtos.Exit(fmt.Sprintf(`LoadABCIResponses: Data has been corrupted or its spec has
changed: %v\n`, err))
}
// TODO: ensure that buf is completely read.

return abciResponses, nil
return resp, nil
}

// LoadLastABCIResponses loads the ABCIResponses from the most recent height.
Expand All @@ -418,28 +420,36 @@ func (store dbStore) LoadABCIResponses(height int64) (*cmtstate.ABCIResponses, e
// This method is used for recovering in the case that we called the Commit ABCI
// method on the application but crashed before persisting the results.
func (store dbStore) LoadLastABCIResponse(height int64) (*cmtstate.ABCIResponses, error) {
bz, err := store.db.Get(lastABCIResponseKey)
buf, err := store.db.Get(calcABCIResponsesKey(height))
if err != nil {
return nil, err
}

if len(bz) == 0 {
return nil, errors.New("no last ABCI response has been persisted")
if len(buf) == 0 {
// DEPRECATED lastABCIResponseKey
// It is possible if this is called directly after an upgrade that
// `lastABCIResponseKey` contains the last ABCI responses.
bz, err := store.db.Get(lastABCIResponseKey)
if err == nil && len(bz) > 0 {
info := new(cmtstate.ABCIResponsesInfo)
err = info.Unmarshal(bz)
if err != nil {
cmtos.Exit(fmt.Sprintf(`LoadLastABCIResponses: Data has been corrupted or its spec has changed: %v\n`, err))
}
// Here we validate the result by comparing its height to the expected height.
if height != info.GetHeight() {
return nil, fmt.Errorf("expected height %d but last stored abci responses was at height %d", height, info.GetHeight())
}
return info.AbciResponses, nil
Comment on lines +439 to +442
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left out the following logic branch because I don't think it matters here, but would like an ACK

	if info.FinalizeBlock == nil {
		// sanity check
		if info.LegacyAbciResponses == nil {
			panic("state store contains last abci response but it is empty")
		}
		return responseFinalizeBlockFromLegacy(info.LegacyAbciResponses), nil
	}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for an edge case, but I think this backports it wrong

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will hold off on merging this till we talk next then

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh your right we are fine without this logic branch, sorry about that.

}
// END OF DEPRECATED lastABCIResponseKey
return nil, fmt.Errorf("expected last ABCI responses at height %d, but none are found", height)
}

abciResponse := new(cmtstate.ABCIResponsesInfo)
err = abciResponse.Unmarshal(bz)
resp := new(cmtstate.ABCIResponses)
err = resp.Unmarshal(buf)
if err != nil {
cmtos.Exit(fmt.Sprintf(`LoadLastABCIResponses: Data has been corrupted or its spec has
changed: %v\n`, err))
cmtos.Exit(fmt.Sprintf(`LoadLastABCIResponses: Data has been corrupted or its spec has changed: %v\n`, err))
}

// Here we validate the result by comparing its height to the expected height.
if height != abciResponse.GetHeight() {
return nil, errors.New("expected height %d but last stored abci responses was at height %d")
}

return abciResponse.AbciResponses, nil
return resp, nil
}

// SaveABCIResponses persists the ABCIResponses to the database.
Expand All @@ -458,30 +468,24 @@ func (store dbStore) SaveABCIResponses(height int64, abciResponses *cmtstate.ABC
}
abciResponses.DeliverTxs = dtxs

// If the flag is false then we save the ABCIResponse. This can be used for the /BlockResults
// query or to reindex an event using the command line.
if !store.DiscardABCIResponses {
bz, err := abciResponses.Marshal()
if err != nil {
return err
}
if err := store.db.Set(calcABCIResponsesKey(height), bz); err != nil {
return err
}
bz, err := abciResponses.Marshal()
if err != nil {
return err
}

// Save the ABCI response.
//
// We always save the last ABCI response for crash recovery.
// This overwrites the previous saved ABCI Response.
response := &cmtstate.ABCIResponsesInfo{
AbciResponses: abciResponses,
Height: height,
// If `store.DiscardABCIResponses` is true, then we delete the previous ABCI response.
if store.DiscardABCIResponses && height > 1 {
if err := store.db.Delete(calcABCIResponsesKey(height - 1)); err != nil {
return err
}
}
bz, err := response.Marshal()
if err != nil {
if err := store.db.SetSync(calcABCIResponsesKey(height), bz); err != nil {
return err
}

return store.db.SetSync(lastABCIResponseKey, bz)
return nil
}

//-----------------------------------------------------------------------------
Expand Down
Loading