Skip to content

Commit cdf3f01

Browse files
holimankaralabe
authored andcommitted
snapshot: iteration and buffering optimizations
1 parent d7d81d7 commit cdf3f01

5 files changed

Lines changed: 96 additions & 17 deletions

File tree

‎core/state/snapshot/difflayer.go‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,9 +227,6 @@ func (dl *diffLayer) flatten() snapshot {
227227
// This is meant to be used during shutdown to persist the snapshot without
228228
// flattening everything down (bad for reorgs).
229229
func (dl *diffLayer) Journal() error {
230-
dl.lock.RLock()
231-
defer dl.lock.RUnlock()
232-
233230
writer, err := dl.journal()
234231
if err != nil {
235232
return err

‎core/state/snapshot/difflayer_journal.go‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package snapshot
1818

1919
import (
20+
"bufio"
2021
"fmt"
2122
"io"
2223
"os"
@@ -105,20 +106,31 @@ func (dl *diffLayer) journal() (io.WriteCloser, error) {
105106
}
106107
writer = file
107108
}
109+
dl.lock.RLock()
110+
defer dl.lock.RUnlock()
111+
112+
if dl.stale {
113+
writer.Close()
114+
return nil, ErrSnapshotStale
115+
}
116+
buf := bufio.NewWriter(writer)
108117
// Everything below was journalled, persist this layer too
109-
if err := rlp.Encode(writer, dl.number); err != nil {
118+
if err := rlp.Encode(buf, dl.number); err != nil {
119+
buf.Flush()
110120
writer.Close()
111121
return nil, err
112122
}
113-
if err := rlp.Encode(writer, dl.root); err != nil {
123+
if err := rlp.Encode(buf, dl.root); err != nil {
124+
buf.Flush()
114125
writer.Close()
115126
return nil, err
116127
}
117128
accounts := make([]journalAccount, 0, len(dl.accountData))
118129
for hash, blob := range dl.accountData {
119130
accounts = append(accounts, journalAccount{Hash: hash, Blob: blob})
120131
}
121-
if err := rlp.Encode(writer, accounts); err != nil {
132+
if err := rlp.Encode(buf, accounts); err != nil {
133+
buf.Flush()
122134
writer.Close()
123135
return nil, err
124136
}
@@ -132,9 +144,11 @@ func (dl *diffLayer) journal() (io.WriteCloser, error) {
132144
}
133145
storage = append(storage, journalStorage{Hash: hash, Keys: keys, Vals: vals})
134146
}
135-
if err := rlp.Encode(writer, storage); err != nil {
147+
if err := rlp.Encode(buf, storage); err != nil {
148+
buf.Flush()
136149
writer.Close()
137150
return nil, err
138151
}
152+
buf.Flush()
139153
return writer, nil
140154
}

‎core/state/snapshot/difflayer_test.go‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"bytes"
2121
"math/big"
2222
"math/rand"
23+
"os"
24+
"path"
2325
"testing"
2426

2527
"github.com/ethereum/go-ethereum/common"
@@ -340,3 +342,46 @@ func BenchmarkFlatten(b *testing.B) {
340342
b.StopTimer()
341343
}
342344
}
345+
346+
// This test writes ~324M of diff layers to disk, spread over
347+
// - 128 individual layers,
348+
// - each with 200 accounts
349+
// - containing 200 slots
350+
//
351+
// BenchmarkJournal-6 1 1471373923 ns/ops
352+
// BenchmarkJournal-6 1 1208083335 ns/op // bufio writer
353+
func BenchmarkJournal(b *testing.B) {
354+
fill := func(parent snapshot, blocknum int) *diffLayer {
355+
accounts := make(map[common.Hash][]byte)
356+
storage := make(map[common.Hash]map[common.Hash][]byte)
357+
358+
for i := 0; i < 200; i++ {
359+
accountKey := randomHash()
360+
accounts[accountKey] = randomAccount()
361+
362+
accStorage := make(map[common.Hash][]byte)
363+
for i := 0; i < 200; i++ {
364+
value := make([]byte, 32)
365+
rand.Read(value)
366+
accStorage[randomHash()] = value
367+
368+
}
369+
storage[accountKey] = accStorage
370+
}
371+
return newDiffLayer(parent, uint64(blocknum), common.Hash{}, accounts, storage)
372+
}
373+
374+
var layer snapshot
375+
layer = &diskLayer{
376+
journal: path.Join(os.TempDir(), "difflayer_journal.tmp"),
377+
}
378+
for i := 1; i < 128; i++ {
379+
layer = fill(layer, i)
380+
}
381+
b.ResetTimer()
382+
383+
for i := 0; i < b.N; i++ {
384+
f, _ := layer.(*diffLayer).journal()
385+
f.Close()
386+
}
387+
}

‎core/state/snapshot/snapshot.go‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -160,13 +160,15 @@ func (st *SnapshotTree) Update(blockRoot common.Hash, parentRoot common.Hash, ac
160160
// are flattened downwards.
161161
func (st *SnapshotTree) Cap(blockRoot common.Hash, layers int, memory uint64) error {
162162
// Retrieve the head snapshot to cap from
163-
snap := st.Snapshot(blockRoot).(snapshot)
164-
if snap == nil {
163+
var snap snapshot
164+
if s := st.Snapshot(blockRoot); s == nil {
165165
return fmt.Errorf("snapshot [%#x] missing", blockRoot)
166+
} else {
167+
snap = s.(snapshot)
166168
}
167169
diff, ok := snap.(*diffLayer)
168170
if !ok {
169-
return fmt.Errorf("snapshot [%#x] is base layer", blockRoot)
171+
return fmt.Errorf("snapshot [%#x] is disk layer", blockRoot)
170172
}
171173
// Run the internal capping and discard all stale layers
172174
st.lock.Lock()
@@ -228,13 +230,14 @@ func (st *SnapshotTree) Cap(blockRoot common.Hash, layers int, memory uint64) er
228230
// block numbers for the disk layer and first diff layer are returned for GC.
229231
func (st *SnapshotTree) cap(diff *diffLayer, layers int, memory uint64) (uint64, uint64) {
230232
// Dive until we run out of layers or reach the persistent database
231-
if layers > 2 {
232-
// If we still have diff layers below, recurse
233+
for ; layers > 2; layers-- {
234+
// If we still have diff layers below, continue down
233235
if parent, ok := diff.parent.(*diffLayer); ok {
234-
return st.cap(parent, layers-1, memory)
236+
diff = parent
237+
} else {
238+
// Diff stack too shallow, return block numbers without modifications
239+
return diff.parent.(*diskLayer).number, diff.number
235240
}
236-
// Diff stack too shallow, return block numbers without modifications
237-
return diff.parent.(*diskLayer).number, diff.number
238241
}
239242
// We're out of layers, flatten anything below, stopping if it's the disk or if
240243
// the memory limit is not yet exceeded.
@@ -356,9 +359,11 @@ func diffToDisk(bottom *diffLayer) *diskLayer {
356359
// flattening everything down (bad for reorgs).
357360
func (st *SnapshotTree) Journal(blockRoot common.Hash) error {
358361
// Retrieve the head snapshot to journal from
359-
snap := st.Snapshot(blockRoot).(snapshot)
360-
if snap == nil {
362+
var snap snapshot
363+
if s := st.Snapshot(blockRoot); s == nil {
361364
return fmt.Errorf("snapshot [%#x] missing", blockRoot)
365+
} else {
366+
snap = s.(snapshot)
362367
}
363368
// Run the journaling
364369
st.lock.Lock()

‎core/state/snapshot/snapshot_test.go‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,15 @@ func TestDiffLayerExternalInvalidationPartialFlatten(t *testing.T) {
205205
}
206206
ref := snaps.Snapshot(common.HexToHash("0x02"))
207207

208+
// Doing a Cap operation with many allowed layers should be a no-op
209+
exp := len(snaps.layers)
210+
if err := snaps.Cap(common.HexToHash("0x04"), 2000, 1024*1024); err != nil {
211+
t.Fatalf("failed to flatten diff layer into accumulator: %v", err)
212+
}
213+
if got := len(snaps.layers); got != exp {
214+
t.Errorf("layers modified, got %d exp %d", got, exp)
215+
}
216+
208217
// Flatten the diff layer into the bottom accumulator
209218
if err := snaps.Cap(common.HexToHash("0x04"), 2, 1024*1024); err != nil {
210219
t.Fatalf("failed to flatten diff layer into accumulator: %v", err)
@@ -277,6 +286,10 @@ func TestPostCapBasicDataAccess(t *testing.T) {
277286
if err := checkExist(snap, "0xb3"); err != nil {
278287
t.Error(err)
279288
}
289+
// Cap to a bad root should fail
290+
if err := snaps.Cap(common.HexToHash("0x1337"), 0, 1024); err == nil {
291+
t.Errorf("expected error, got none")
292+
}
280293
// Now, merge the a-chain
281294
snaps.Cap(common.HexToHash("0xa3"), 0, 1024)
282295

@@ -300,4 +313,9 @@ func TestPostCapBasicDataAccess(t *testing.T) {
300313
if err := shouldErr(snap, "0xa3"); err != nil {
301314
t.Error(err)
302315
}
316+
// Now, merge it again, just for fun. It should now error, since a3
317+
// is a disk layer
318+
if err := snaps.Cap(common.HexToHash("0xa3"), 0, 1024); err == nil {
319+
t.Error("expected error capping the disk layer, got none")
320+
}
303321
}

0 commit comments

Comments
 (0)