Skip to content

Commit

Permalink
Revert "feat: Add skipFastStorageUpgrade to MutableTree to contro…
Browse files Browse the repository at this point in the history
…l fast storage upgrade (cosmos#547)"

This reverts commit fcab556.
  • Loading branch information
faddat committed Nov 25, 2022
1 parent da766a3 commit 20f64ef
Show file tree
Hide file tree
Showing 16 changed files with 202 additions and 423 deletions.
6 changes: 3 additions & 3 deletions basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,15 +470,15 @@ func TestPersistence(t *testing.T) {
}

// Construct some tree and save it
t1, err := NewMutableTree(db, 0, false)
t1, err := NewMutableTree(db, 0)
require.NoError(t, err)
for key, value := range records {
t1.Set([]byte(key), []byte(value))
}
t1.SaveVersion()

// Load a tree
t2, err := NewMutableTree(db, 0, false)
t2, err := NewMutableTree(db, 0)
require.NoError(t, err)
t2.Load()
for key, value := range records {
Expand Down Expand Up @@ -528,7 +528,7 @@ func TestProof(t *testing.T) {

func TestTreeProof(t *testing.T) {
db := db.NewMemDB()
tree, err := NewMutableTree(db, 100, false)
tree, err := NewMutableTree(db, 100)
require.NoError(t, err)
hash, err := tree.Hash()
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func randBytes(length int) []byte {
}

func prepareTree(b *testing.B, db db.DB, size, keyLen, dataLen int) (*iavl.MutableTree, [][]byte) {
t, err := iavl.NewMutableTreeWithOpts(db, size, nil, false)
t, err := iavl.NewMutableTreeWithOpts(db, size, nil)
require.NoError(b, err)
keys := make([][]byte, size)

Expand Down
6 changes: 3 additions & 3 deletions benchmarks/cosmos-exim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) {
if err != nil {
return 0, nil, err
}
tree, err := iavl.NewMutableTree(tmdb.NewPrefixDB(ldb, []byte("s/k:main/")), 0, false)
tree, err := iavl.NewMutableTree(tmdb.NewPrefixDB(ldb, []byte("s/k:main/")), 0)
if err != nil {
return 0, nil, err
}
Expand All @@ -105,7 +105,7 @@ func runExport(dbPath string) (int64, map[string][]*iavl.ExportNode, error) {
totalStats := Stats{}
for _, name := range stores {
db := tmdb.NewPrefixDB(ldb, []byte("s/k:"+name+"/"))
tree, err := iavl.NewMutableTree(db, 0, false)
tree, err := iavl.NewMutableTree(db, 0)
if err != nil {
return 0, nil, err
}
Expand Down Expand Up @@ -170,7 +170,7 @@ func runImport(version int64, exports map[string][]*iavl.ExportNode) error {
if err != nil {
return err
}
newTree, err := iavl.NewMutableTree(newDB, 0, false)
newTree, err := iavl.NewMutableTree(newDB, 0)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/iaviewer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func ReadTree(dir string, version int, prefix []byte) (*iavl.MutableTree, error)
db = dbm.NewPrefixDB(db, prefix)
}

tree, err := iavl.NewMutableTree(db, DefaultCacheSize, false)
tree, err := iavl.NewMutableTree(db, DefaultCacheSize)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// setupExportTreeBasic sets up a basic tree with a handful of
// create/update/delete operations over a few versions.
func setupExportTreeBasic(t require.TestingT) *ImmutableTree {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)

tree.Set([]byte("x"), []byte{255})
Expand Down Expand Up @@ -59,7 +59,7 @@ func setupExportTreeRandom(t *testing.T) *ImmutableTree {
)

r := rand.New(rand.NewSource(randSeed))
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)

var version int64
Expand Down Expand Up @@ -119,7 +119,7 @@ func setupExportTreeSized(t require.TestingT, treeSize int) *ImmutableTree {
)

r := rand.New(rand.NewSource(randSeed))
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)

for i := 0; i < treeSize; i++ {
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestExporter(t *testing.T) {

func TestExporter_Import(t *testing.T) {
testcases := map[string]*ImmutableTree{
"empty tree": NewImmutableTree(db.NewMemDB(), 0, false),
"empty tree": NewImmutableTree(db.NewMemDB(), 0),
"basic tree": setupExportTreeBasic(t),
}
if !testing.Short() {
Expand All @@ -192,7 +192,7 @@ func TestExporter_Import(t *testing.T) {
exporter := tree.Export()
defer exporter.Close()

newTree, err := NewMutableTree(db.NewMemDB(), 0, false)
newTree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)
importer, err := newTree.Import(tree.Version())
require.NoError(t, err)
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestExporter_Close(t *testing.T) {
}

func TestExporter_DeleteVersionErrors(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)

tree.Set([]byte("a"), []byte{1})
Expand Down
75 changes: 33 additions & 42 deletions immutable_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,28 @@ import (
// Returned key/value byte slices must not be modified, since they may point to data located inside
// IAVL which would also be modified.
type ImmutableTree struct {
root *Node
ndb *nodeDB
version int64
skipFastStorageUpgrade bool
root *Node
ndb *nodeDB
version int64
}

// NewImmutableTree creates both in-memory and persistent instances
func NewImmutableTree(db dbm.DB, cacheSize int, skipFastStorageUpgrade bool) *ImmutableTree {
func NewImmutableTree(db dbm.DB, cacheSize int) *ImmutableTree {
if db == nil {
// In-memory Tree.
return &ImmutableTree{}
}
return &ImmutableTree{
// NodeDB-backed Tree.
ndb: newNodeDB(db, cacheSize, nil),
skipFastStorageUpgrade: skipFastStorageUpgrade,
ndb: newNodeDB(db, cacheSize, nil),
}
}

// NewImmutableTreeWithOpts creates an ImmutableTree with the given options.
func NewImmutableTreeWithOpts(db dbm.DB, cacheSize int, opts *Options, skipFastStorageUpgrade bool) *ImmutableTree {
func NewImmutableTreeWithOpts(db dbm.DB, cacheSize int, opts *Options) *ImmutableTree {
return &ImmutableTree{
// NodeDB-backed Tree.
ndb: newNodeDB(db, cacheSize, opts),
skipFastStorageUpgrade: skipFastStorageUpgrade,
ndb: newNodeDB(db, cacheSize, opts),
}
}

Expand Down Expand Up @@ -175,40 +172,36 @@ func (t *ImmutableTree) GetWithIndex(key []byte) (int64, []byte, error) {
// Get returns the value of the specified key if it exists, or nil.
// The returned value must not be modified, since it may point to data stored within IAVL.
// Get potentially employs a more performant strategy than GetWithIndex for retrieving the value.
// If tree.skipFastStorageUpgrade is true, this will work almost the same as GetWithIndex.
func (t *ImmutableTree) Get(key []byte) ([]byte, error) {
if t.root == nil {
return nil, nil
}

if !t.skipFastStorageUpgrade {
// attempt to get a FastNode directly from db/cache.
// if call fails, fall back to the original IAVL logic in place.
fastNode, err := t.ndb.GetFastNode(key)
if err != nil {
_, result, err := t.root.get(t, key)
return result, err
}

if fastNode == nil {
// If the tree is of the latest version and fast node is not in the tree
// then the regular node is not in the tree either because fast node
// represents live state.
if t.version == t.ndb.latestVersion {
return nil, nil
}
// attempt to get a FastNode directly from db/cache.
// if call fails, fall back to the original IAVL logic in place.
fastNode, err := t.ndb.GetFastNode(key)
if err != nil {
_, result, err := t.root.get(t, key)
return result, err
}

_, result, err := t.root.get(t, key)
return result, err
if fastNode == nil {
// If the tree is of the latest version and fast node is not in the tree
// then the regular node is not in the tree either because fast node
// represents live state.
if t.version == t.ndb.latestVersion {
return nil, nil
}

if fastNode.GetVersionLastUpdatedAt() <= t.version {
return fastNode.GetValue(), nil
}
_, result, err := t.root.get(t, key)
return result, err
}

if fastNode.GetVersionLastUpdatedAt() <= t.version {
return fastNode.GetValue(), nil
}

// otherwise skipFastStorageUpgrade is true or
// the cached node was updated later than the current tree. In this case,
// Otherwise the cached node was updated later than the current tree. In this case,
// we need to use the regular stategy for reading from the current tree to avoid staleness.
_, result, err := t.root.get(t, key)
return result, err
Expand Down Expand Up @@ -246,15 +239,13 @@ func (t *ImmutableTree) Iterate(fn func(key []byte, value []byte) bool) (bool, e

// Iterator returns an iterator over the immutable tree.
func (t *ImmutableTree) Iterator(start, end []byte, ascending bool) (dbm.Iterator, error) {
if !t.skipFastStorageUpgrade {
isFastCacheEnabled, err := t.IsFastCacheEnabled()
if err != nil {
return nil, err
}
isFastCacheEnabled, err := t.IsFastCacheEnabled()
if err != nil {
return nil, err
}

if isFastCacheEnabled {
return NewFastIterator(start, end, ascending, t.ndb), nil
}
if isFastCacheEnabled {
return NewFastIterator(start, end, ascending, t.ndb), nil
}
return NewIterator(start, end, ascending, t), nil
}
Expand Down
28 changes: 14 additions & 14 deletions import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

func ExampleImporter() {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
if err != nil {
// handle err
}
Expand Down Expand Up @@ -41,7 +41,7 @@ func ExampleImporter() {
exported = append(exported, node)
}

newTree, err := NewMutableTree(db.NewMemDB(), 0, false)
newTree, err := NewMutableTree(db.NewMemDB(), 0)
if err != nil {
// handle err
}
Expand All @@ -63,14 +63,14 @@ func ExampleImporter() {
}

func TestImporter_NegativeVersion(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)
_, err = tree.Import(-1)
require.Error(t, err)
}

func TestImporter_NotEmpty(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)
tree.Set([]byte("a"), []byte{1})
_, _, err = tree.SaveVersion()
Expand All @@ -83,13 +83,13 @@ func TestImporter_NotEmpty(t *testing.T) {
func TestImporter_NotEmptyDatabase(t *testing.T) {
db := db.NewMemDB()

tree, err := NewMutableTree(db, 0, false)
tree, err := NewMutableTree(db, 0)
require.NoError(t, err)
tree.Set([]byte("a"), []byte{1})
_, _, err = tree.SaveVersion()
require.NoError(t, err)

tree, err = NewMutableTree(db, 0, false)
tree, err = NewMutableTree(db, 0)
require.NoError(t, err)
_, err = tree.Load()
require.NoError(t, err)
Expand All @@ -99,7 +99,7 @@ func TestImporter_NotEmptyDatabase(t *testing.T) {
}

func TestImporter_NotEmptyUnsaved(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)
tree.Set([]byte("a"), []byte{1})

Expand All @@ -126,7 +126,7 @@ func TestImporter_Add(t *testing.T) {
for desc, tc := range testcases {
tc := tc // appease scopelint
t.Run(desc, func(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)
importer, err := tree.Import(1)
require.NoError(t, err)
Expand All @@ -143,7 +143,7 @@ func TestImporter_Add(t *testing.T) {
}

func TestImporter_Add_Closed(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)
importer, err := tree.Import(1)
require.NoError(t, err)
Expand All @@ -155,7 +155,7 @@ func TestImporter_Add_Closed(t *testing.T) {
}

func TestImporter_Close(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)
importer, err := tree.Import(1)
require.NoError(t, err)
Expand All @@ -172,7 +172,7 @@ func TestImporter_Close(t *testing.T) {
}

func TestImporter_Commit(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)
importer, err := tree.Import(1)
require.NoError(t, err)
Expand All @@ -188,7 +188,7 @@ func TestImporter_Commit(t *testing.T) {
}

func TestImporter_Commit_Closed(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)
importer, err := tree.Import(1)
require.NoError(t, err)
Expand All @@ -203,7 +203,7 @@ func TestImporter_Commit_Closed(t *testing.T) {
}

func TestImporter_Commit_Empty(t *testing.T) {
tree, err := NewMutableTree(db.NewMemDB(), 0, false)
tree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(t, err)
importer, err := tree.Import(3)
require.NoError(t, err)
Expand Down Expand Up @@ -232,7 +232,7 @@ func BenchmarkImport(b *testing.B) {
b.StartTimer()

for n := 0; n < b.N; n++ {
newTree, err := NewMutableTree(db.NewMemDB(), 0, false)
newTree, err := NewMutableTree(db.NewMemDB(), 0)
require.NoError(b, err)
importer, err := newTree.Import(tree.Version())
require.NoError(b, err)
Expand Down
Loading

0 comments on commit 20f64ef

Please sign in to comment.