Skip to content

Commit

Permalink
feat: enable logging for when iavl storage is performing an upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
p0mvn committed Feb 8, 2022
1 parent 4ad6d82 commit 4fee686
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 36 deletions.
2 changes: 1 addition & 1 deletion baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func NewBaseApp(
logger: logger,
name: name,
db: db,
cms: store.NewCommitMultiStore(db),
cms: store.NewCommitMultiStore(db, logger),
storeLoader: DefaultStoreLoader,
router: NewRouter(),
queryRouter: NewQueryRouter(),
Expand Down
4 changes: 2 additions & 2 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func useDefaultLoader(app *BaseApp) {
}

func initStore(t *testing.T, db dbm.DB, storeKey string, k, v []byte) {
rs := rootmulti.NewStore(db)
rs := rootmulti.NewStore(db, log.NewNopLogger())
rs.SetPruning(store.PruneNothing)
key := sdk.NewKVStoreKey(storeKey)
rs.MountStoreWithDB(key, store.StoreTypeIAVL, nil)
Expand All @@ -275,7 +275,7 @@ func initStore(t *testing.T, db dbm.DB, storeKey string, k, v []byte) {
}

func checkStore(t *testing.T, db dbm.DB, ver int64, storeKey string, k, v []byte) {
rs := rootmulti.NewStore(db)
rs := rootmulti.NewStore(db, log.NewNopLogger())
rs.SetPruning(store.PruneDefault)
key := sdk.NewKVStoreKey(storeKey)
rs.MountStoreWithDB(key, store.StoreTypeIAVL, nil)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ replace github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.7.0

replace github.com/tendermint/tm-db => github.com/osmosis-labs/tm-db v0.6.5-0.20210911033928-ba9154613417

replace github.com/cosmos/iavl => github.com/osmosis-labs/iavl v0.17.3-fast.1.0.20220207021926-5f889f6b44d5
replace github.com/cosmos/iavl => github.com/osmosis-labs/iavl v0.17.3-fast.4
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,8 @@ github.com/ory/dockertest v3.3.5+incompatible h1:iLLK6SQwIhcbrG783Dghaaa3WPzGc+4
github.com/ory/dockertest v3.3.5+incompatible/go.mod h1:1vX4m9wsvi00u5bseYwXaSnhNrne+V0E6LAcBILJdPs=
github.com/osmosis-labs/iavl v0.17.3-fast.1.0.20220207021926-5f889f6b44d5 h1:Yxj5CB5vnE2BSWbfWlDFzmyccDQmaFVGTms5RlD2Rgo=
github.com/osmosis-labs/iavl v0.17.3-fast.1.0.20220207021926-5f889f6b44d5/go.mod h1:lJEOIlsd3sVO0JDyXWIXa9/Ur5FBscP26zJx0KxHjto=
github.com/osmosis-labs/iavl v0.17.3-fast.2 h1:8jCq1xaZq5KiQzo86fOHF0TkiAEdxUKe5MJ/Xmwz4b8=
github.com/osmosis-labs/iavl v0.17.3-fast.2/go.mod h1:lJEOIlsd3sVO0JDyXWIXa9/Ur5FBscP26zJx0KxHjto=
github.com/osmosis-labs/iavl v0.17.3-fast.4 h1:P6872Aq9Q+X2nCQFMpUb+VCP0rNYZPW5OKa4tTnzR+A=
github.com/osmosis-labs/iavl v0.17.3-fast.4/go.mod h1:lJEOIlsd3sVO0JDyXWIXa9/Ur5FBscP26zJx0KxHjto=
github.com/osmosis-labs/tm-db v0.6.5-0.20210911033928-ba9154613417 h1:otchJDd2SjFWfs7Tse3ULblGcVWqMJ50BE02XCaqXOo=
github.com/osmosis-labs/tm-db v0.6.5-0.20210911033928-ba9154613417/go.mod h1:dptYhIpJ2M5kUuenLr+Yyf3zQOv1SgBZcl8/BmWlMBw=
github.com/otiai10/copy v1.6.0 h1:IinKAryFFuPONZ7cm6T6E2QX/vcJwSnlaA5lfoaXIiQ=
Expand Down
23 changes: 20 additions & 3 deletions store/iavl/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package iavl
import (
"errors"
"fmt"
"github.com/tendermint/tendermint/libs/log"
"io"
"time"

Expand Down Expand Up @@ -41,20 +42,36 @@ type Store struct {
// LoadStore returns an IAVL Store as a CommitKVStore. Internally, it will load the
// store's version (id) from the provided DB. An error is returned if the version
// fails to load, or if called with a positive version on an empty tree.
func LoadStore(db dbm.DB, id types.CommitID, lazyLoading bool) (types.CommitKVStore, error) {
return LoadStoreWithInitialVersion(db, id, lazyLoading, 0)
func LoadStore(db dbm.DB, logger log.Logger, id types.CommitID, lazyLoading bool) (types.CommitKVStore, error) {
return LoadStoreWithInitialVersion(db, logger, id, lazyLoading, 0)
}

// LoadStoreWithInitialVersion returns an IAVL Store as a CommitKVStore setting its initialVersion
// to the one given. Internally, it will load the store's version (id) from the
// provided DB. An error is returned if the version fails to load, or if called with a positive
// version on an empty tree.
func LoadStoreWithInitialVersion(db dbm.DB, id types.CommitID, lazyLoading bool, initialVersion uint64) (types.CommitKVStore, error) {
func LoadStoreWithInitialVersion(db dbm.DB, logger log.Logger, id types.CommitID, lazyLoading bool, initialVersion uint64) (types.CommitKVStore, error) {
tree, err := iavl.NewMutableTreeWithOpts(db, defaultIAVLCacheSize, &iavl.Options{InitialVersion: initialVersion})
if err != nil {
return nil, err
}

if tree.IsUpgradeable() && logger != nil {
logger.Info(
"module's storage upgrade is in progress while loading",
"version", initialVersion,
"commit", fmt.Sprintf("%X", id),
"is_lazy", lazyLoading,
)
defer func() {
if err != nil {
logger.Info("failed upgrading while loading storage", "error", err.Error())
} else {
logger.Info("storage upgrade is now complete")
}
}()
}

if lazyLoading {
_, err = tree.LazyLoadVersion(id.Version)
} else {
Expand Down
7 changes: 4 additions & 3 deletions store/iavl/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package iavl
import (
crand "crypto/rand"
"fmt"
"github.com/tendermint/tendermint/libs/log"
"testing"

"github.com/cosmos/cosmos-sdk/store/cachekv"
Expand Down Expand Up @@ -93,17 +94,17 @@ func TestLoadStore(t *testing.T) {
require.Equal(t, string(hcStore.Get([]byte("hello"))), "ciao")

// Querying a new store at some previous non-pruned height H
newHStore, err := LoadStore(db, cIDH, false)
newHStore, err := LoadStore(db, log.NewNopLogger(), cIDH, false)
require.NoError(t, err)
require.Equal(t, string(newHStore.Get([]byte("hello"))), "hallo")

// Querying a new store at some previous pruned height Hp
newHpStore, err := LoadStore(db, cIDHp, false)
newHpStore, err := LoadStore(db, log.NewNopLogger(), cIDHp, false)
require.NoError(t, err)
require.Equal(t, string(newHpStore.Get([]byte("hello"))), "hola")

// Querying a new store at current height H
newHcStore, err := LoadStore(db, cIDHc, false)
newHcStore, err := LoadStore(db, log.NewNopLogger(), cIDHc, false)
require.NoError(t, err)
require.Equal(t, string(newHcStore.Get([]byte("hello"))), "ciao")
}
Expand Down
7 changes: 4 additions & 3 deletions store/rootmulti/proof_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rootmulti

import (
"github.com/tendermint/tendermint/libs/log"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -14,7 +15,7 @@ import (
func TestVerifyIAVLStoreQueryProof(t *testing.T) {
// Create main tree for testing.
db := dbm.NewMemDB()
iStore, err := iavl.LoadStore(db, types.CommitID{}, false)
iStore, err := iavl.LoadStore(db, nil, types.CommitID{}, false)
store := iStore.(*iavl.Store)
require.Nil(t, err)
store.Set([]byte("MYKEY"), []byte("MYVALUE"))
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestVerifyIAVLStoreQueryProof(t *testing.T) {
func TestVerifyMultiStoreQueryProof(t *testing.T) {
// Create main tree for testing.
db := dbm.NewMemDB()
store := NewStore(db)
store := NewStore(db, nil)
iavlStoreKey := types.NewKVStoreKey("iavlStoreKey")

store.MountStoreWithDB(iavlStoreKey, types.StoreTypeIAVL, nil)
Expand Down Expand Up @@ -112,7 +113,7 @@ func TestVerifyMultiStoreQueryProof(t *testing.T) {
func TestVerifyMultiStoreQueryProofAbsence(t *testing.T) {
// Create main tree for testing.
db := dbm.NewMemDB()
store := NewStore(db)
store := NewStore(db, log.NewNopLogger())
iavlStoreKey := types.NewKVStoreKey("iavlStoreKey")

store.MountStoreWithDB(iavlStoreKey, types.StoreTypeIAVL, nil)
Expand Down
9 changes: 6 additions & 3 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"compress/zlib"
"encoding/binary"
"fmt"
"github.com/tendermint/tendermint/libs/log"
"io"
"math"
"sort"
Expand Down Expand Up @@ -46,6 +47,7 @@ const (
// the CommitMultiStore interface.
type Store struct {
db dbm.DB
logger log.Logger
lastCommitInfo *types.CommitInfo
pruningOpts types.PruningOptions
storesParams map[types.StoreKey]storeParams
Expand All @@ -72,9 +74,10 @@ var (
// store will be created with a PruneNothing pruning strategy by default. After
// a store is created, KVStores must be mounted and finally LoadLatestVersion or
// LoadVersion must be called.
func NewStore(db dbm.DB) *Store {
func NewStore(db dbm.DB, logger log.Logger) *Store {
return &Store{
db: db,
logger: logger,
pruningOpts: types.PruneNothing,
storesParams: make(map[types.StoreKey]storeParams),
stores: make(map[types.StoreKey]types.CommitKVStore),
Expand Down Expand Up @@ -876,9 +879,9 @@ func (rs *Store) loadCommitStoreFromParams(key types.StoreKey, id types.CommitID
var err error

if params.initialVersion == 0 {
store, err = iavl.LoadStore(db, id, rs.lazyLoading)
store, err = iavl.LoadStore(db, rs.logger, id, rs.lazyLoading)
} else {
store, err = iavl.LoadStoreWithInitialVersion(db, id, rs.lazyLoading, params.initialVersion)
store, err = iavl.LoadStoreWithInitialVersion(db, rs.logger, id, rs.lazyLoading, params.initialVersion)
}

if err != nil {
Expand Down
17 changes: 9 additions & 8 deletions store/rootmulti/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/tendermint/tendermint/libs/log"
"io"
"io/ioutil"
"math/rand"
Expand All @@ -30,7 +31,7 @@ import (

func TestStoreType(t *testing.T) {
db := dbm.NewMemDB()
store := NewStore(db)
store := NewStore(db, log.NewNopLogger())
store.MountStoreWithDB(types.NewKVStoreKey("store1"), types.StoreTypeIAVL, db)
}

Expand All @@ -53,7 +54,7 @@ func TestGetCommitKVStore(t *testing.T) {

func TestStoreMount(t *testing.T) {
db := dbm.NewMemDB()
store := NewStore(db)
store := NewStore(db, log.NewNopLogger())

key1 := types.NewKVStoreKey("store1")
key2 := types.NewKVStoreKey("store2")
Expand Down Expand Up @@ -831,7 +832,7 @@ func benchmarkMultistoreSnapshot(b *testing.B, stores uint8, storeKeys uint64) {
b.StartTimer()

for i := 0; i < b.N; i++ {
target := NewStore(dbm.NewMemDB())
target := NewStore(dbm.NewMemDB(), log.NewNopLogger())
for key := range source.stores {
target.MountStoreWithDB(key, types.StoreTypeIAVL, nil)
}
Expand Down Expand Up @@ -861,7 +862,7 @@ func benchmarkMultistoreSnapshotRestore(b *testing.B, stores uint8, storeKeys ui
b.StartTimer()

for i := 0; i < b.N; i++ {
target := NewStore(dbm.NewMemDB())
target := NewStore(dbm.NewMemDB(), log.NewNopLogger())
for key := range source.stores {
target.MountStoreWithDB(key, types.StoreTypeIAVL, nil)
}
Expand All @@ -887,7 +888,7 @@ var (
)

func newMultiStoreWithMounts(db dbm.DB, pruningOpts types.PruningOptions) *Store {
store := NewStore(db)
store := NewStore(db, log.NewNopLogger())
store.pruningOpts = pruningOpts

store.MountStoreWithDB(testStoreKey1, types.StoreTypeIAVL, nil)
Expand All @@ -898,7 +899,7 @@ func newMultiStoreWithMounts(db dbm.DB, pruningOpts types.PruningOptions) *Store
}

func newMultiStoreWithMixedMounts(db dbm.DB) *Store {
store := NewStore(db)
store := NewStore(db, log.NewNopLogger())
store.MountStoreWithDB(types.NewKVStoreKey("iavl1"), types.StoreTypeIAVL, nil)
store.MountStoreWithDB(types.NewKVStoreKey("iavl2"), types.StoreTypeIAVL, nil)
store.MountStoreWithDB(types.NewKVStoreKey("iavl3"), types.StoreTypeIAVL, nil)
Expand Down Expand Up @@ -935,7 +936,7 @@ func newMultiStoreWithMixedMountsAndBasicData(db dbm.DB) *Store {
}

func newMultiStoreWithGeneratedData(db dbm.DB, stores uint8, storeKeys uint64) *Store {
multiStore := NewStore(db)
multiStore := NewStore(db, log.NewNopLogger())
r := rand.New(rand.NewSource(49872768940)) // Fixed seed for deterministic tests

keys := []*types.KVStoreKey{}
Expand Down Expand Up @@ -967,7 +968,7 @@ func newMultiStoreWithGeneratedData(db dbm.DB, stores uint8, storeKeys uint64) *
}

func newMultiStoreWithModifiedMounts(db dbm.DB, pruningOpts types.PruningOptions) (*Store, *types.StoreUpgrades) {
store := NewStore(db)
store := NewStore(db, log.NewNopLogger())
store.pruningOpts = pruningOpts

store.MountStoreWithDB(types.NewKVStoreKey("store1"), types.StoreTypeIAVL, nil)
Expand Down
5 changes: 3 additions & 2 deletions store/store.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package store

import (
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"

"github.com/cosmos/cosmos-sdk/store/cache"
"github.com/cosmos/cosmos-sdk/store/rootmulti"
"github.com/cosmos/cosmos-sdk/store/types"
)

func NewCommitMultiStore(db dbm.DB) types.CommitMultiStore {
return rootmulti.NewStore(db)
func NewCommitMultiStore(db dbm.DB, logger log.Logger) types.CommitMultiStore {
return rootmulti.NewStore(db, logger)
}

func NewCommitKVStoreCacheManager() types.MultiStorePersistentCache {
Expand Down
3 changes: 2 additions & 1 deletion store/types/iterator_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types_test

import (
"github.com/tendermint/tendermint/libs/log"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -12,7 +13,7 @@ import (

func newMemTestKVStore(t *testing.T) types.KVStore {
db := dbm.NewMemDB()
store, err := iavl.LoadStore(db, types.CommitID{}, false)
store, err := iavl.LoadStore(db, log.NewNopLogger(), types.CommitID{}, false)
require.NoError(t, err)
return store
}
Expand Down
3 changes: 2 additions & 1 deletion store/types/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types_test

import (
"bytes"
"github.com/tendermint/tendermint/libs/log"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -13,7 +14,7 @@ import (

func initTestStores(t *testing.T) (types.KVStore, types.KVStore) {
db := dbm.NewMemDB()
ms := rootmulti.NewStore(db)
ms := rootmulti.NewStore(db, log.NewNopLogger())

key1 := types.NewKVStoreKey("store1")
key2 := types.NewKVStoreKey("store2")
Expand Down
2 changes: 1 addition & 1 deletion testutil/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// DefaultContext creates a sdk.Context with a fresh MemDB that can be used in tests.
func DefaultContext(key sdk.StoreKey, tkey sdk.StoreKey) sdk.Context {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db)
cms := store.NewCommitMultiStore(db, log.NewNopLogger())
cms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
cms.MountStoreWithDB(tkey, sdk.StoreTypeTransient, db)
err := cms.LoadLatestVersion()
Expand Down
3 changes: 2 additions & 1 deletion types/query/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package query_test
import (
gocontext "context"
"fmt"
"github.com/tendermint/tendermint/libs/log"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -340,7 +341,7 @@ func setupTest() (*simapp.SimApp, sdk.Context, codec.Codec) {
appCodec := app.AppCodec()

db := dbm.NewMemDB()
ms := store.NewCommitMultiStore(db)
ms := store.NewCommitMultiStore(db, log.NewNopLogger())

ms.LoadLatestVersion()

Expand Down
3 changes: 2 additions & 1 deletion types/store_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package types_test

import (
"github.com/tendermint/tendermint/libs/log"
"testing"

"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -108,7 +109,7 @@ func (s *storeTestSuite) TestDiffKVStores() {

func (s *storeTestSuite) initTestStores() (types.KVStore, types.KVStore) {
db := dbm.NewMemDB()
ms := rootmulti.NewStore(db)
ms := rootmulti.NewStore(db, log.NewNopLogger())

key1 := types.NewKVStoreKey("store1")
key2 := types.NewKVStoreKey("store2")
Expand Down
2 changes: 1 addition & 1 deletion x/params/types/subspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type SubspaceTestSuite struct {
func (suite *SubspaceTestSuite) SetupTest() {
db := dbm.NewMemDB()

ms := store.NewCommitMultiStore(db)
ms := store.NewCommitMultiStore(db, log.NewNopLogger())
ms.MountStoreWithDB(key, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkey, sdk.StoreTypeTransient, db)
suite.NoError(ms.LoadLatestVersion())
Expand Down
4 changes: 2 additions & 2 deletions x/upgrade/types/storeloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func defaultLogger() log.Logger {
}

func initStore(t *testing.T, db dbm.DB, storeKey string, k, v []byte) {
rs := rootmulti.NewStore(db)
rs := rootmulti.NewStore(db, log.NewNopLogger())
rs.SetPruning(store.PruneNothing)
key := sdk.NewKVStoreKey(storeKey)
rs.MountStoreWithDB(key, store.StoreTypeIAVL, nil)
Expand All @@ -47,7 +47,7 @@ func initStore(t *testing.T, db dbm.DB, storeKey string, k, v []byte) {
}

func checkStore(t *testing.T, db dbm.DB, ver int64, storeKey string, k, v []byte) {
rs := rootmulti.NewStore(db)
rs := rootmulti.NewStore(db, log.NewNopLogger())
rs.SetPruning(store.PruneNothing)
key := sdk.NewKVStoreKey(storeKey)
rs.MountStoreWithDB(key, store.StoreTypeIAVL, nil)
Expand Down

0 comments on commit 4fee686

Please sign in to comment.