forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 35
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: commit info data race #155
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9f447e5
bugfix: fix race condition related to last commmit info and flush pru…
p0mvn 5926ca9
fix log
p0mvn 931b36e
flush pruning heights
p0mvn be5950a
WriteSync on flush, fix flush tests
p0mvn 6f859d3
typo
p0mvn 0b829c8
fix rebase conflict
p0mvn 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
This file contains 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
This file contains 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
This file contains 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 | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -8,6 +8,7 @@ import ( | |||||||
"math" | ||||||||
"sort" | ||||||||
"strings" | ||||||||
"sync" | ||||||||
|
||||||||
"github.com/cosmos/cosmos-sdk/pruning" | ||||||||
pruningTypes "github.com/cosmos/cosmos-sdk/pruning/types" | ||||||||
|
@@ -45,13 +46,21 @@ const ( | |||||||
snapshotMaxItemSize = int(64e6) // SDK has no key/value size limit, so we set an arbitrary limit | ||||||||
) | ||||||||
|
||||||||
type storeParams struct { | ||||||||
key types.StoreKey | ||||||||
db dbm.DB | ||||||||
typ types.StoreType | ||||||||
initialVersion uint64 | ||||||||
} | ||||||||
|
||||||||
// Store is composed of many CommitStores. Name contrasts with | ||||||||
// cacheMultiStore which is used for branching other MultiStores. It implements | ||||||||
// the CommitMultiStore interface. | ||||||||
type Store struct { | ||||||||
db dbm.DB | ||||||||
logger log.Logger | ||||||||
lastCommitInfo *types.CommitInfo | ||||||||
mx *sync.RWMutex // mutex to sync access to lastCommitInfo | ||||||||
pruningManager *pruning.Manager | ||||||||
iavlCacheSize int | ||||||||
storesParams map[types.StoreKey]storeParams | ||||||||
|
@@ -86,7 +95,8 @@ func NewStore(db dbm.DB, logger log.Logger) *Store { | |||||||
stores: make(map[types.StoreKey]types.CommitKVStore), | ||||||||
keysByName: make(map[string]types.StoreKey), | ||||||||
listeners: make(map[types.StoreKey][]types.WriteListener), | ||||||||
pruningManager: pruning.NewManager(logger), | ||||||||
pruningManager: pruning.NewManager(logger, db), | ||||||||
mx: &sync.RWMutex{}, | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
|
@@ -197,7 +207,7 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error { | |||||||
// load old data if we are not version 0 | ||||||||
if ver != 0 { | ||||||||
var err error | ||||||||
cInfo, err = getCommitInfo(rs.db, ver) | ||||||||
cInfo, err = rs.getCommitInfoFromDb(ver) | ||||||||
if err != nil { | ||||||||
return err | ||||||||
} | ||||||||
|
@@ -378,6 +388,8 @@ func (rs *Store) ListeningEnabled(key types.StoreKey) bool { | |||||||
|
||||||||
// LastCommitID implements Committer/CommitStore. | ||||||||
func (rs *Store) LastCommitID() types.CommitID { | ||||||||
rs.mx.RLock() | ||||||||
defer rs.mx.RUnlock() | ||||||||
if rs.lastCommitInfo == nil { | ||||||||
return types.CommitID{ | ||||||||
Version: getLatestVersion(rs.db), | ||||||||
|
@@ -405,20 +417,18 @@ func (rs *Store) Commit() types.CommitID { | |||||||
version = previousHeight + 1 | ||||||||
} | ||||||||
|
||||||||
rs.lastCommitInfo = rs.commitStores(version, rs.stores) | ||||||||
newCommitInfo := rs.commitStores(version, rs.stores) | ||||||||
rs.updateLatestCommitInfo(newCommitInfo, version) | ||||||||
|
||||||||
var pruneErr error | ||||||||
defer func() { | ||||||||
rs.flushMetadata(rs.db, version, rs.lastCommitInfo) | ||||||||
if pruneErr != nil { | ||||||||
panic(pruneErr) | ||||||||
} | ||||||||
}() | ||||||||
|
||||||||
pruneErr = rs.handlePruning(version) | ||||||||
err := rs.handlePruning(version) | ||||||||
if err != nil { | ||||||||
panic(err) | ||||||||
} | ||||||||
|
||||||||
rs.mx.RLock() | ||||||||
hash, keys := rs.lastCommitInfo.Hash() | ||||||||
rs.logger.Info("calculated commit hash", "height", version, "commit_hash", hash, "keys", keys) | ||||||||
defer rs.mx.RUnlock() | ||||||||
rs.logger.Info("calculated commit hash", "height", version, "commit_hash", fmt.Sprintf("%X", hash), "keys", keys) | ||||||||
|
||||||||
return types.CommitID{ | ||||||||
Version: version, | ||||||||
|
@@ -520,6 +530,8 @@ func (rs *Store) GetKVStore(key types.StoreKey) types.KVStore { | |||||||
} | ||||||||
|
||||||||
func (rs *Store) handlePruning(version int64) error { | ||||||||
defer rs.pruningManager.FlushPruningHeights() | ||||||||
|
||||||||
rs.pruningManager.HandleHeight(version - 1) // we should never prune the current version. | ||||||||
if rs.pruningManager.ShouldPruneAtHeight(version) { | ||||||||
rs.logger.Info("prune start", "height", version) | ||||||||
|
@@ -603,18 +615,10 @@ func (rs *Store) Query(req abci.RequestQuery) abci.ResponseQuery { | |||||||
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "proof is unexpectedly empty; ensure height has not been pruned")) | ||||||||
} | ||||||||
|
||||||||
// If the request's height is the latest height we've committed, then utilize | ||||||||
// the store's lastCommitInfo as this commit info may not be flushed to disk. | ||||||||
// Otherwise, we query for the commit info from disk. | ||||||||
var commitInfo *types.CommitInfo | ||||||||
|
||||||||
if res.Height == rs.lastCommitInfo.Version { | ||||||||
commitInfo = rs.lastCommitInfo | ||||||||
} else { | ||||||||
commitInfo, err = getCommitInfo(rs.db, res.Height) | ||||||||
if err != nil { | ||||||||
return sdkerrors.QueryResult(err) | ||||||||
} | ||||||||
commitInfo, err := rs.getCommitInfoFromDb(res.Height) | ||||||||
if err != nil { | ||||||||
return sdkerrors.QueryResult(err) | ||||||||
} | ||||||||
|
||||||||
// Restore origin path and append proof op. | ||||||||
|
@@ -887,7 +891,8 @@ func (rs *Store) Restore( | |||||||
importer.Close() | ||||||||
} | ||||||||
|
||||||||
rs.flushMetadata(rs.db, int64(height), rs.buildCommitInfo(int64(height))) | ||||||||
rs.flushLastCommitInfo(rs.buildCommitInfo(int64(height))) | ||||||||
rs.pruningManager.FlushPruningHeights() | ||||||||
return rs.LoadLatestVersion() | ||||||||
} | ||||||||
|
||||||||
|
@@ -992,30 +997,46 @@ func (rs *Store) commitStores(version int64, storeMap map[types.StoreKey]types.C | |||||||
} | ||||||||
} | ||||||||
|
||||||||
func (rs *Store) flushMetadata(db dbm.DB, version int64, cInfo *types.CommitInfo) { | ||||||||
rs.logger.Debug("flushing metadata", "height", version) | ||||||||
batch := db.NewBatch() | ||||||||
func (rs *Store) updateLatestCommitInfo(newCommitInfo *types.CommitInfo, version int64) { | ||||||||
rs.mx.Lock() | ||||||||
defer rs.mx.Unlock() | ||||||||
rs.lastCommitInfo = newCommitInfo | ||||||||
rs.flushLastCommitInfo(newCommitInfo) | ||||||||
} | ||||||||
|
||||||||
func (rs *Store) flushLastCommitInfo(cInfo *types.CommitInfo) { | ||||||||
batch := rs.db.NewBatch() | ||||||||
defer batch.Close() | ||||||||
|
||||||||
flushCommitInfo(batch, version, cInfo) | ||||||||
flushLatestVersion(batch, version) | ||||||||
rs.pruningManager.FlushPruningHeights(batch) | ||||||||
setCommitInfo(batch, cInfo) | ||||||||
setLatestVersion(batch, cInfo.Version) | ||||||||
|
||||||||
if err := batch.Write(); err != nil { | ||||||||
if err := batch.WriteSync(); err != nil { | ||||||||
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.
Suggested change
|
||||||||
panic(fmt.Errorf("error on batch write %w", err)) | ||||||||
} | ||||||||
rs.logger.Debug("flushing metadata finished", "height", version) | ||||||||
} | ||||||||
|
||||||||
type storeParams struct { | ||||||||
key types.StoreKey | ||||||||
db dbm.DB | ||||||||
typ types.StoreType | ||||||||
initialVersion uint64 | ||||||||
// Gets commitInfo from disk. | ||||||||
func (rs *Store) getCommitInfoFromDb(ver int64) (*types.CommitInfo, error) { | ||||||||
cInfoKey := fmt.Sprintf(commitInfoKeyFmt, ver) | ||||||||
|
||||||||
bz, err := rs.db.Get([]byte(cInfoKey)) | ||||||||
if err != nil { | ||||||||
return nil, errors.Wrap(err, "failed to get commit info") | ||||||||
} else if bz == nil { | ||||||||
return nil, errors.New("no commit info found") | ||||||||
} | ||||||||
|
||||||||
cInfo := &types.CommitInfo{} | ||||||||
if err = cInfo.Unmarshal(bz); err != nil { | ||||||||
return nil, errors.Wrap(err, "failed unmarshal commit info") | ||||||||
} | ||||||||
|
||||||||
return cInfo, nil | ||||||||
} | ||||||||
|
||||||||
func (rs *Store) doProofsQuery(req abci.RequestQuery) abci.ResponseQuery { | ||||||||
commitInfo, err := getCommitInfo(rs.db, req.Height) | ||||||||
commitInfo, err := rs.getCommitInfoFromDb(req.Height) | ||||||||
if err != nil { | ||||||||
return sdkerrors.QueryResult(err) | ||||||||
} | ||||||||
|
@@ -1049,36 +1070,17 @@ func getLatestVersion(db dbm.DB) int64 { | |||||||
return latestVersion | ||||||||
} | ||||||||
|
||||||||
// Gets commitInfo from disk. | ||||||||
func getCommitInfo(db dbm.DB, ver int64) (*types.CommitInfo, error) { | ||||||||
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. Made a method and moved up |
||||||||
cInfoKey := fmt.Sprintf(commitInfoKeyFmt, ver) | ||||||||
|
||||||||
bz, err := db.Get([]byte(cInfoKey)) | ||||||||
if err != nil { | ||||||||
return nil, errors.Wrap(err, "failed to get commit info") | ||||||||
} else if bz == nil { | ||||||||
return nil, errors.New("no commit info found") | ||||||||
} | ||||||||
|
||||||||
cInfo := &types.CommitInfo{} | ||||||||
if err = cInfo.Unmarshal(bz); err != nil { | ||||||||
return nil, errors.Wrap(err, "failed unmarshal commit info") | ||||||||
} | ||||||||
|
||||||||
return cInfo, nil | ||||||||
} | ||||||||
|
||||||||
func flushCommitInfo(batch dbm.Batch, version int64, cInfo *types.CommitInfo) { | ||||||||
func setCommitInfo(batch dbm.Batch, cInfo *types.CommitInfo) { | ||||||||
bz, err := cInfo.Marshal() | ||||||||
if err != nil { | ||||||||
panic(err) | ||||||||
} | ||||||||
|
||||||||
cInfoKey := fmt.Sprintf(commitInfoKeyFmt, version) | ||||||||
cInfoKey := fmt.Sprintf(commitInfoKeyFmt, cInfo.Version) | ||||||||
batch.Set([]byte(cInfoKey), bz) | ||||||||
} | ||||||||
|
||||||||
func flushLatestVersion(batch dbm.Batch, version int64) { | ||||||||
func setLatestVersion(batch dbm.Batch, version int64) { | ||||||||
bz, err := gogotypes.StdInt64Marshal(version) | ||||||||
if err != nil { | ||||||||
panic(err) | ||||||||
|
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.