Skip to content

Commit 72c7b74

Browse files
pyselHuangYi
andauthored
Problem: memiavl changes are not persisted in real time (#946)
* wal: writing to * wal * IndexBlockChangesBytes * add some tests * fix tests * pull changeset * refactor logic to use new changeset logic * fix wal test * cleanup comments * replay wal * fix conflict * choosable wal paths for trees * cleanup * wip * replay wal * refactor wal * dead code rm * add functionality to replay everything from wal * root hash once * go mod tidy * chores: remove unused code, better test for ReplayWAL, fix bugs caught with better test * gomod2nix * make wal optional, and fix unit tests * fix lint * tidy * use go 1.20 in CI * fix lint and unit test * fix build * fix go-version * fix lint&test * fix tests * replayWAL * gofumpt --------- Co-authored-by: HuangYi <huang@crypto.com>
1 parent ea68e79 commit 72c7b74

23 files changed

+520
-61
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ jobs:
6666
access-tokens = github.com=${{ secrets.GITHUB_TOKEN }}
6767
- uses: actions/setup-go@v3
6868
with:
69-
go-version: 1.20.0
69+
go-version: '^1.20.0'
7070
- id: changed-files
7171
uses: tj-actions/changed-files@v34
7272
with:

.github/workflows/codeql-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
uses: actions/checkout@v3
4545
- uses: actions/setup-go@v3
4646
with:
47-
go-version: 1.20.0
47+
go-version: '^1.20.0'
4848
# Initializes the CodeQL tools for scanning.
4949
- name: Initialize CodeQL
5050
uses: github/codeql-action/init@v2

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
steps:
2323
- uses: actions/setup-go@v3
2424
with:
25-
go-version: 1.19.4
25+
go-version: '^1.20.0'
2626
- uses: actions/checkout@v3
2727
- uses: cachix/install-nix-action@v19
2828
with:

.github/workflows/sims.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ jobs:
110110
if: github.event_name == 'push' || github.event_name == 'pull_request'
111111
- uses: actions/setup-go@v3
112112
with:
113-
go-version: 1.20.0
113+
go-version: '^1.20.0'
114114
- name: Display go version
115115
run: go version
116116
- run: make build
@@ -131,7 +131,7 @@ jobs:
131131
steps:
132132
- uses: actions/setup-go@v3
133133
with:
134-
go-version: 1.20.0
134+
go-version: '^1.20.0'
135135
- name: Display go version
136136
run: go version
137137
- name: Install runsim
@@ -157,7 +157,7 @@ jobs:
157157
steps:
158158
- uses: actions/setup-go@v3
159159
with:
160-
go-version: 1.20.0
160+
go-version: '^1.20.0'
161161
- name: Checkout Comment PR Branch
162162
uses: actions/checkout@v3
163163
if: github.event_name == 'issue_comment'
@@ -212,7 +212,7 @@ jobs:
212212
steps:
213213
- uses: actions/setup-go@v3
214214
with:
215-
go-version: 1.20.0
215+
go-version: '^1.20.0'
216216
- name: Checkout Comment PR Branch
217217
uses: actions/checkout@v3
218218
if: github.event_name == 'issue_comment'
@@ -267,7 +267,7 @@ jobs:
267267
steps:
268268
- uses: actions/setup-go@v3
269269
with:
270-
go-version: 1.20.0
270+
go-version: '^1.20.0'
271271
- name: Checkout Comment PR Branch
272272
uses: actions/checkout@v3
273273
if: github.event_name == 'issue_comment'
@@ -322,7 +322,7 @@ jobs:
322322
steps:
323323
- uses: actions/setup-go@v3
324324
with:
325-
go-version: 1.20.0
325+
go-version: '^1.20.0'
326326
- name: Checkout Comment PR Branch
327327
uses: actions/checkout@v3
328328
if: github.event_name == 'issue_comment'

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,11 @@ require (
174174
github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // indirect
175175
github.com/tendermint/go-amino v0.16.0 // indirect
176176
github.com/tidwall/btree v1.5.0 // indirect
177+
github.com/tidwall/gjson v1.14.0 // indirect
178+
github.com/tidwall/match v1.1.1 // indirect
179+
github.com/tidwall/pretty v1.2.0 // indirect
180+
github.com/tidwall/tinylru v1.1.0 // indirect
181+
github.com/tidwall/wal v1.1.7 // indirect
177182
github.com/tklauser/go-sysconf v0.3.10 // indirect
178183
github.com/tklauser/numcpus v0.4.0 // indirect
179184
github.com/torquem-ch/mdbx-go v0.27.5 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,11 +1155,19 @@ github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2l
11551155
github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
11561156
github.com/tidwall/btree v1.5.0 h1:iV0yVY/frd7r6qGBXfEYs7DH0gTDgrKTrDjS7xt/IyQ=
11571157
github.com/tidwall/btree v1.5.0/go.mod h1:LGm8L/DZjPLmeWGjv5kFrY8dL4uVhMmzmmLYmsObdKE=
1158+
github.com/tidwall/gjson v1.10.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
11581159
github.com/tidwall/gjson v1.12.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
1160+
github.com/tidwall/gjson v1.14.0 h1:6aeJ0bzojgWLa82gDQHcx3S0Lr/O51I9bJ5nv6JFx5w=
11591161
github.com/tidwall/gjson v1.14.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
1162+
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
11601163
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
1164+
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
11611165
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
11621166
github.com/tidwall/sjson v1.2.4/go.mod h1:098SZ494YoMWPmMO6ct4dcFnqxwj9r/gF0Etp19pSNM=
1167+
github.com/tidwall/tinylru v1.1.0 h1:XY6IUfzVTU9rpwdhKUF6nQdChgCdGjkMfLzbWyiau6I=
1168+
github.com/tidwall/tinylru v1.1.0/go.mod h1:3+bX+TJ2baOLMWTnlyNWHh4QMnFyARg2TLTQ6OFbzw8=
1169+
github.com/tidwall/wal v1.1.7 h1:emc1TRjIVsdKKSnpwGBAcsAGg0767SvUk8+ygx7Bb+4=
1170+
github.com/tidwall/wal v1.1.7/go.mod h1:r6lR1j27W9EPalgHiB7zLJDYu3mzW5BQP5KrzBpYY/E=
11631171
github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE=
11641172
github.com/tinylib/msgp v1.1.5/go.mod h1:eQsjooMTnV42mHu917E26IogZ2930nFyBQdofk10Udg=
11651173
github.com/tklauser/go-sysconf v0.3.5/go.mod h1:MkWzOF4RMCshBAMXuhXJs64Rte09mITnppBXY/rYEFI=

gomod2nix.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,21 @@ schema = 3
494494
[mod."github.com/tidwall/btree"]
495495
version = "v1.5.0"
496496
hash = "sha256-iWll4/+ADLVse3VAHxXYLprILugX/+3u0ZIk0YlLv/Q="
497+
[mod."github.com/tidwall/gjson"]
498+
version = "v1.14.0"
499+
hash = "sha256-TiMGdAn840a2tOsi5mQ6fAK62ohvY6zuoVaUHCTyIwA="
500+
[mod."github.com/tidwall/match"]
501+
version = "v1.1.1"
502+
hash = "sha256-M2klhPId3Q3T3VGkSbOkYl/2nLHnsG+yMbXkPkyrRdg="
503+
[mod."github.com/tidwall/pretty"]
504+
version = "v1.2.0"
505+
hash = "sha256-esRQGsn2Ee/CiySlwyuOICSLdqUkH4P7u8qXszos8Yc="
506+
[mod."github.com/tidwall/tinylru"]
507+
version = "v1.1.0"
508+
hash = "sha256-yjhttcm2IcJn6Rf9KicGqxm3LAOD3oheJel3QTi9znE="
509+
[mod."github.com/tidwall/wal"]
510+
version = "v1.1.7"
511+
hash = "sha256-HLLFUEw8hWSSSSPSWZHt2KjoZftbje3Xxd9gROM2I+U="
497512
[mod."github.com/tklauser/go-sysconf"]
498513
version = "v0.3.10"
499514
hash = "sha256-Zf2NsgM9+HeM949vCce4HQtSbfUiFpeiQ716yKcFyx4="

memiavl/benchmark_test.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,28 @@ func BenchmarkByteCompare(b *testing.B) {
1919
}
2020

2121
func BenchmarkRandomGet(b *testing.B) {
22+
b.Cleanup(removeDefaultWal)
23+
2224
amount := 1000000
2325
items := genRandItems(amount)
2426
targetKey := items[500].key
2527
targetValue := items[500].value
2628
targetItem := itemT{key: targetKey}
2729

28-
tree := New()
30+
tree, err := New(DefaultPathToWAL)
31+
require.NoError(b, err)
2932
for _, item := range items {
3033
tree.set(item.key, item.value)
3134
}
3235

3336
snapshotDir := b.TempDir()
34-
err := tree.WriteSnapshot(snapshotDir, true)
37+
err = tree.WriteSnapshot(snapshotDir, true)
3538
require.NoError(b, err)
3639
snapshot, err := OpenSnapshot(snapshotDir)
3740
require.NoError(b, err)
3841
defer snapshot.Close()
39-
diskTree := NewFromSnapshot(snapshot)
42+
diskTree, err := NewFromSnapshot(snapshot, DefaultPathToWAL)
43+
require.NoError(b, err)
4044

4145
require.Equal(b, targetValue, tree.Get(targetKey))
4246
require.Equal(b, targetValue, diskTree.Get(targetKey))
@@ -109,22 +113,24 @@ func BenchmarkRandomGet(b *testing.B) {
109113
for _, item := range items {
110114
m[string(item.key)] = item.value
111115
}
112-
v, _ := m[string(targetItem.key)]
116+
v := m[string(targetItem.key)]
113117
require.Equal(b, targetValue, v)
114118

115119
b.ResetTimer()
116120
for i := 0; i < b.N; i++ {
117-
_, _ = m[string(targetKey)]
121+
_ = m[string(targetKey)]
118122
}
119123
})
120124
}
121125

122126
func BenchmarkRandomSet(b *testing.B) {
127+
b.Cleanup(removeDefaultWal)
123128
items := genRandItems(1000000)
124129
b.ResetTimer()
125130
b.Run("memiavl", func(b *testing.B) {
126131
for i := 0; i < b.N; i++ {
127-
tree := New()
132+
tree, err := New(DefaultPathToWAL)
133+
require.NoError(b, err)
128134
for _, item := range items {
129135
tree.set(item.key, item.value)
130136
}

memiavl/changeset.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ import (
2222
//
2323
// ]
2424
// ```
25-
func MarshalChangeSet(cs *iavl.ChangeSet) ([]byte, error) {
25+
func MarshalChangeSet(cs iavl.ChangeSet) ([]byte, error) {
2626
buf := bytes.NewBuffer(nil)
2727
if err := MarshalChangeSetTo(cs, buf); err != nil {
2828
return nil, err
2929
}
3030
return buf.Bytes(), nil
3131
}
3232

33-
func MarshalChangeSetTo(cs *iavl.ChangeSet, w io.Writer) error {
33+
func MarshalChangeSetTo(cs iavl.ChangeSet, w io.Writer) error {
3434
for _, pair := range cs.Pairs {
3535
if _, err := w.Write([]byte{bool2byte(pair.Delete)}); err != nil {
3636
return err
@@ -49,18 +49,18 @@ func MarshalChangeSetTo(cs *iavl.ChangeSet, w io.Writer) error {
4949
return nil
5050
}
5151

52-
func UnmarshalChangeSet(data []byte) (*iavl.ChangeSet, error) {
52+
func UnmarshalChangeSet(data []byte) (iavl.ChangeSet, error) {
5353
var offset int
5454
var cs iavl.ChangeSet
5555
for offset < len(data) {
5656
pair, n := readKVPair(data[offset:])
5757
if n <= 0 {
58-
return nil, fmt.Errorf("decode kv pair failed: %d", n)
58+
return iavl.ChangeSet{}, fmt.Errorf("decode kv pair failed: %d", n)
5959
}
6060
offset += n
6161
cs.Pairs = append(cs.Pairs, pair)
6262
}
63-
return &cs, nil
63+
return cs, nil
6464
}
6565

6666
func bool2byte(b bool) byte {

memiavl/changeset_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ import (
88

99
func TestChangeSetMarshal(t *testing.T) {
1010
for _, changes := range ChangeSets {
11-
bz, err := MarshalChangeSet(&changes)
11+
bz, err := MarshalChangeSet(changes)
1212
require.NoError(t, err)
1313

1414
cs, err := UnmarshalChangeSet(bz)
1515
require.NoError(t, err)
16-
require.Equal(t, changes, *cs)
16+
require.Equal(t, changes, cs)
1717
}
1818
}

0 commit comments

Comments
 (0)