forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_test.go
207 lines (176 loc) · 5.64 KB
/
migrate_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package iavl
import (
"bytes"
"fmt"
"math/rand"
"os"
"os/exec"
"path"
"testing"
"cosmossdk.io/log"
"github.com/stretchr/testify/require"
dbm "github.com/cosmos/iavl/db"
)
const (
dbType = "goleveldb"
)
func createLegacyTree(t *testing.T, dbType, dbDir string, version int) (string, error) {
relateDir := path.Join(t.TempDir(), dbDir)
if _, err := os.Stat(relateDir); err == nil {
err := os.RemoveAll(relateDir)
if err != nil {
t.Errorf("%+v\n", err)
}
}
cmd := exec.Command("sh", "-c", fmt.Sprintf("./cmd/legacydump/legacydump %s %s random %d %d", dbType, relateDir, version, version/2)) //nolint:gosec
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil || stderr.Len() > 0 {
t.Log(fmt.Sprint(err) + ": " + stderr.String())
if err == nil {
err = fmt.Errorf("stderr: %s", stderr.String())
}
}
t.Log("Result: " + out.String())
return relateDir, err
}
func TestLazySet(t *testing.T) {
legacyVersion := 1000
dbDir := fmt.Sprintf("legacy-%s-%d", dbType, legacyVersion)
relateDir, err := createLegacyTree(t, dbType, dbDir, legacyVersion)
require.NoError(t, err)
db, err := dbm.NewDB("test", "goleveldb", relateDir)
require.NoError(t, err)
defer func() {
if err := db.Close(); err != nil {
t.Errorf("DB close error: %v\n", err)
}
if err := os.RemoveAll(relateDir); err != nil {
t.Errorf("%+v\n", err)
}
}()
tree := NewMutableTree(db, 1000, false, log.NewNopLogger())
// Load the latest legacy version
_, err = tree.LoadVersion(int64(legacyVersion))
require.NoError(t, err)
// Commit new versions
postVersions := 1000
for i := 0; i < postVersions; i++ {
leafCount := rand.Intn(50)
for j := 0; j < leafCount; j++ {
_, err = tree.Set([]byte(fmt.Sprintf("key-%d-%d", i, j)), []byte(fmt.Sprintf("value-%d-%d", i, j)))
require.NoError(t, err)
}
_, _, err = tree.SaveVersion()
require.NoError(t, err)
}
tree = NewMutableTree(db, 1000, false, log.NewNopLogger())
// Verify that the latest legacy version can still be loaded
_, err = tree.LoadVersion(int64(legacyVersion))
require.NoError(t, err)
}
func TestLegacyReferenceNode(t *testing.T) {
legacyVersion := 20
dbDir := fmt.Sprintf("./legacy-%s-%d", dbType, legacyVersion)
relateDir, err := createLegacyTree(t, dbType, dbDir, legacyVersion)
require.NoError(t, err)
db, err := dbm.NewDB("test", "goleveldb", relateDir)
require.NoError(t, err)
defer func() {
if err := db.Close(); err != nil {
t.Errorf("DB close error: %v\n", err)
}
if err := os.RemoveAll(relateDir); err != nil {
t.Errorf("%+v\n", err)
}
}()
tree := NewMutableTree(db, 1000, false, log.NewNopLogger())
// Load the latest legacy version
_, err = tree.LoadVersion(int64(legacyVersion))
require.NoError(t, err)
// Commit new versions without updates
_, _, err = tree.SaveVersion()
require.NoError(t, err)
_, version, err := tree.SaveVersion()
require.NoError(t, err)
// Load the previous version
newTree := NewMutableTree(db, 1000, false, log.NewNopLogger())
_, err = newTree.LoadVersion(version - 1)
require.NoError(t, err)
// Check if the reference node is refactored
require.Equal(t, newTree.root.nodeKey.nonce, uint32(1))
require.Equal(t, newTree.root.nodeKey.version, int64(legacyVersion))
}
func TestDeleteVersions(t *testing.T) {
legacyVersion := 100
dbDir := fmt.Sprintf("./legacy-%s-%d", dbType, legacyVersion)
relateDir, err := createLegacyTree(t, dbType, dbDir, legacyVersion)
require.NoError(t, err)
db, err := dbm.NewDB("test", "goleveldb", relateDir)
require.NoError(t, err)
defer func() {
if err := db.Close(); err != nil {
t.Errorf("DB close error: %v\n", err)
}
if err := os.RemoveAll(relateDir); err != nil {
t.Errorf("%+v\n", err)
}
}()
tree := NewMutableTree(db, 1000, false, log.NewNopLogger())
// Load the latest legacy version
_, err = tree.LoadVersion(int64(legacyVersion))
require.NoError(t, err)
// Commit new versions
postVersions := 100
for i := 0; i < postVersions; i++ {
leafCount := rand.Intn(10)
for j := 0; j < leafCount; j++ {
_, err = tree.Set([]byte(fmt.Sprintf("key-%d-%d", i, j)), []byte(fmt.Sprintf("value-%d-%d", i, j)))
require.NoError(t, err)
}
_, _, err = tree.SaveVersion()
require.NoError(t, err)
}
// Check the available versions
versions := tree.AvailableVersions()
targetVersion := 0
for i := len(versions) - 1; i >= 0; i-- {
if versions[i] < legacyVersion {
targetVersion = versions[i]
break
}
}
// Test LoadVersionForOverwriting for the legacy version
err = tree.LoadVersionForOverwriting(int64(targetVersion))
require.NoError(t, err)
latestVersion, err := tree.ndb.getLatestVersion()
require.NoError(t, err)
require.Equal(t, int64(targetVersion), latestVersion)
legacyLatestVersion, err := tree.ndb.getLegacyLatestVersion()
require.NoError(t, err)
require.Equal(t, int64(targetVersion), legacyLatestVersion)
// Test DeleteVersionsTo for the legacy version
for i := 0; i < postVersions; i++ {
leafCount := rand.Intn(20)
for j := 0; j < leafCount; j++ {
_, err = tree.Set([]byte(fmt.Sprintf("key-%d-%d", i, j)), []byte(fmt.Sprintf("value-%d-%d", i, j)))
require.NoError(t, err)
}
_, _, err = tree.SaveVersion()
require.NoError(t, err)
}
// Check if the legacy versions are deleted at once
versions = tree.AvailableVersions()
err = tree.DeleteVersionsTo(legacyLatestVersion - 1)
require.NoError(t, err)
pVersions := tree.AvailableVersions()
require.Equal(t, len(versions), len(pVersions))
toVersion := legacyLatestVersion + int64(postVersions)/2
err = tree.DeleteVersionsTo(toVersion)
require.NoError(t, err)
pVersions = tree.AvailableVersions()
require.Equal(t, postVersions/2, len(pVersions))
}