forked from cosmos/iavl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prune_test.go
69 lines (59 loc) · 1.77 KB
/
prune_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
package iavl
import (
"fmt"
"testing"
"time"
"cosmossdk.io/log"
dbm "github.com/cosmos/iavl/db"
"github.com/stretchr/testify/require"
)
func TestAsyncPruning(t *testing.T) {
db, err := dbm.NewDB("test", "goleveldb", t.TempDir())
require.NoError(t, err)
defer db.Close()
tree := NewMutableTree(db, 0, false, log.NewNopLogger(), AsyncPruningOption(true), FlushThresholdOption(1000))
toVersion := 10000
keyCount := 10
pruneInterval := int64(100)
keepRecent := int64(300)
for i := 0; i < toVersion; i++ {
for j := 0; j < keyCount; j++ {
_, err := tree.Set([]byte(fmt.Sprintf("key-%d-%d", i, j)), []byte(fmt.Sprintf("value-%d-%d", i, j)))
require.NoError(t, err)
}
tree.SetCommitting()
_, v, err := tree.SaveVersion()
require.NoError(t, err)
tree.UnsetCommitting()
if v%pruneInterval == 0 && v > keepRecent {
ti := time.Now()
require.NoError(t, tree.DeleteVersionsTo(v-keepRecent))
t.Logf("Pruning %d versions took %v\n", keepRecent, time.Since(ti))
}
}
// wait for async pruning to finish
for i := 0; i < 100; i++ {
tree.SetCommitting()
_, _, err := tree.SaveVersion()
require.NoError(t, err)
tree.UnsetCommitting()
firstVersion, err := tree.ndb.getFirstVersion()
require.NoError(t, err)
t.Logf("Iteration: %d First version: %d\n", i, firstVersion)
if firstVersion == int64(toVersion)-keepRecent+1 {
break
}
// simulate the consensus process
time.Sleep(500 * time.Millisecond)
}
// Reload the tree
tree = NewMutableTree(db, 0, false, log.NewNopLogger())
_, err = tree.LoadVersion(int64(toVersion) - keepRecent)
require.Error(t, err)
versions := tree.AvailableVersions()
require.Equal(t, versions[0], toVersion-int(keepRecent)+1)
for _, v := range versions {
_, err := tree.LoadVersion(int64(v))
require.NoError(t, err)
}
}