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

thread to PushData2Database #125

Open
wants to merge 7 commits into
base: dev
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ replace (
github.com/buger/jsonparser => github.com/buger/jsonparser v1.0.0 // imported by nacos-go-sdk, upgraded to v1.0.0 in case of a known vulnerable bug
github.com/confio/ics23/go => github.com/cosmos/cosmos-sdk/ics23/go v0.8.0
github.com/cosmos/gorocksdb => github.com/okx/grocksdb v1.6.45-okc2
github.com/ethereum/go-ethereum => github.com/okx/go-ethereum v1.10.26-0.20230605041300-9db59c1d91e6
github.com/ethereum/go-ethereum => github.com/okx/go-ethereum v1.10.26-0.20230627015235-d0b4e45610b7
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1
github.com/keybase/go-keychain => github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4
github.com/tendermint/go-amino => github.com/okx/go-amino v0.15.1-okc4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,8 @@ github.com/nxadm/tail v1.4.8/go.mod h1:+ncqLTQzXmGhMZNUePPaPqPvBxHAIsmXswZKocGu+
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/okx/go-amino v0.15.1-okc4 h1:K0xPoVjkz33ySNC4VLStyit4dblMeVlWJQQ6sq+aUBY=
github.com/okx/go-amino v0.15.1-okc4/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
github.com/okx/go-ethereum v1.10.26-0.20230605041300-9db59c1d91e6 h1:pXsdHnWHejHIc1cJZ625U44eBN3x12Sr5H8Qm9EQCtY=
github.com/okx/go-ethereum v1.10.26-0.20230605041300-9db59c1d91e6/go.mod h1:f9/CleoGkiU7NWF9tdpWGfEYmFbN7zUi17jGick31KM=
github.com/okx/go-ethereum v1.10.26-0.20230627015235-d0b4e45610b7 h1:l+OUB+AvdmiYiTOshKjTY9//eBMnffeqRL+odaxpFGM=
github.com/okx/go-ethereum v1.10.26-0.20230627015235-d0b4e45610b7/go.mod h1:f9/CleoGkiU7NWF9tdpWGfEYmFbN7zUi17jGick31KM=
github.com/okx/grocksdb v1.6.45-okc2 h1:Dbmax+uY71YuGTkJftpF55dbGW7hfIVOfNz2mnFqXl0=
github.com/okx/grocksdb v1.6.45-okc2/go.mod h1:+/BHUY+mT0tbaVXwO2wTtD9eytazyw1W5n2O7AGyXZA=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
Expand Down
1 change: 1 addition & 0 deletions libs/cosmos-sdk/server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,5 +362,6 @@ func SetExternalPackageValue(cmd *cobra.Command) {
mptstore.TrieAsyncDBInitCap = viper.GetInt(mptstore.FlagTrieAsyncDBInitCap)
mptstore.TrieAsyncDBAutoPruningOff = viper.GetBool(mptstore.FlagTrieAsyncDBAutoPruningOff)
mptstore.TrieAsyncDBSyncPruning = viper.GetBool(mptstore.FlagTrieAsyncDBSyncPruning)
mptstore.TrieAsyncPushDB = viper.GetBool(mptstore.FlagTrieAsyncPushDB)
mptstore.SetSnapshotJournal(viper.GetBool(mptstore.FlagTrieEnableSnapshotJournal))
}
1 change: 1 addition & 0 deletions libs/cosmos-sdk/server/start_okchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func RegisterServerFlags(cmd *cobra.Command) *cobra.Command {
cmd.Flags().Int(mpt.FlagTrieAsyncDBInitCap, 200_0000, "Init cap of trie async db")
cmd.Flags().Bool(mpt.FlagTrieAsyncDBAutoPruningOff, false, "Disable auto prune of trie async db")
cmd.Flags().Bool(mpt.FlagTrieAsyncDBSyncPruning, false, "if auto pruning is off and this is on, trie async db will be pruned every block in sync mode")
cmd.Flags().Bool(mpt.FlagTrieAsyncPushDB, false, "Enable to Async PushData2DB")
cmd.Flags().Bool(mpt.FlagTrieEnableSnapshotJournal, false, "Enable record snapshot's journal. So that snapshot can be repaired within certain version")

cmd.Flags().Int64(FlagCommitGapHeight, 10, "Block interval to commit cached data into db, affects iavl & mpt")
Expand Down
4 changes: 4 additions & 0 deletions libs/cosmos-sdk/store/mpt/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ const (
FlagTrieAsyncDBAutoPruningOff = "trie.asyncdb.auto-pruning-off"
FlagTrieAsyncDBSyncPruning = "trie.asyncdb.sync-pruning"

FlagTrieAsyncPushDB = "trie.async-push-db"

FlagTrieEnableSnapshotJournal = "trie.enable-snapshot-journal"
)

Expand All @@ -40,6 +42,8 @@ var (
TrieAsyncDBSyncPruning = false

EnableAsyncCommit = false

TrieAsyncPushDB = false
)

var (
Expand Down
2 changes: 2 additions & 0 deletions libs/cosmos-sdk/store/mpt/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ func (ms *MptStore) commitSnap(root common.Hash) {
if ms.snap == nil {
return
}
ms.snapRWLock.Lock()
defer ms.snapRWLock.Unlock()
// Only update if there's a state transition
if parent := ms.snap.Root(); parent != root {
if err := ms.snaps.Update(root, parent, ms.snapDestructs, ms.snapAccounts, ms.snapStorage); err != nil {
Expand Down
14 changes: 8 additions & 6 deletions libs/cosmos-sdk/store/mpt/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ func TestMptStoreSnapshotDeleteAccount(t *testing.T) {
require.NoError(t, err)

mptStore := &MptStore{
trie: trie,
db: stateDb,
logger: tmlog.NewNopLogger(),
originalRoot: trie.Hash(),
retriever: EmptyStateRootRetriever{},
triegc: prque.New(nil),
trie: trie,
db: stateDb,
logger: tmlog.NewNopLogger(),
originalRoot: trie.Hash(),
retriever: EmptyStateRootRetriever{},
triegc: prque.New(nil),
pushDBHeightChan: make(chan int64, 10),
}
go mptStore.threadPushData2DB()
SetSnapshotRebuild(true)
err = mptStore.openSnapshot()
require.NoError(t, err)
Expand Down
21 changes: 18 additions & 3 deletions libs/cosmos-sdk/store/mpt/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ type MptStore struct {
// for time statistics
statisticsBeginTime time.Time

outputDelta *trie.MptDelta
pushDBHeightChan chan int64
outputDelta *trie.MptDelta
}

func (ms *MptStore) CommitterCommitMap(deltaMap iavl.TreeDeltaMap) (_ types.CommitID, _ iavl.TreeDeltaMap) {
Expand Down Expand Up @@ -126,6 +127,7 @@ func generateMptStore(logger tmlog.Logger, id types.CommitID, db ethstate.Databa
logger: logger,
retriever: retriever,
exitSignal: make(chan struct{}),
pushDBHeightChan: make(chan int64, 10),
outputDelta: trie.NewMptDelta(),
}
if mptStore.logger == nil {
Expand All @@ -144,6 +146,10 @@ func generateMptStore(logger tmlog.Logger, id types.CommitID, db ethstate.Databa
mptStore.logger.Info("open snapshot successfully", "snapshot", "ok")
}

if TrieAsyncPushDB {
go mptStore.threadPushData2DB()
}

return mptStore, err
}

Expand Down Expand Up @@ -464,9 +470,12 @@ func (ms *MptStore) CommitterCommit(inputDelta interface{}) (rootHash types.Comm
ms.SetMptRootHash(uint64(ms.version), root)
ms.originalRoot = root

// TODO: use a thread to push data to database
// push data to database
ms.PushData2Database(ms.version)
if TrieAsyncPushDB {
ms.pushDBHeightChan <- ms.version
} else {
ms.PushData2Database(ms.version)
}

ms.sprintDebugLog(ms.version)

Expand All @@ -479,6 +488,12 @@ func (ms *MptStore) CommitterCommit(inputDelta interface{}) (rootHash types.Comm
}, outputDelta
}

func (ms *MptStore) threadPushData2DB() {
for h := range ms.pushDBHeightChan {
ms.PushData2Database(h)
}
}

func (ms *MptStore) LastCommitID() types.CommitID {
return types.CommitID{
Version: ms.version,
Expand Down