Skip to content

Commit ecdbb40

Browse files
rjl493456442fjl
authored andcommitted
trie: remove node ordering slice in sync batch (#19929)
When we flush a batch of trie nodes into database during the state sync, we should guarantee that all children should be flushed before parent. Actually the trie nodes commit order is strict by: children -> parent. But when we flush all ready nodes into db, we don't need the order anymore since (1) they are all ready nodes (no more dependency) (2) underlying database provides write atomicity
1 parent 9c81387 commit ecdbb40

4 files changed

Lines changed: 63 additions & 46 deletions

File tree

‎core/state/sync_test.go‎

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ func TestEmptyStateSync(t *testing.T) {
136136
func TestIterativeStateSyncIndividual(t *testing.T) { testIterativeStateSync(t, 1) }
137137
func TestIterativeStateSyncBatched(t *testing.T) { testIterativeStateSync(t, 100) }
138138

139-
func testIterativeStateSync(t *testing.T, batch int) {
139+
func testIterativeStateSync(t *testing.T, count int) {
140140
// Create a random state to copy
141141
srcDb, srcRoot, srcAccounts := makeTestState()
142142

143143
// Create a destination state and sync with the scheduler
144144
dstDb := rawdb.NewMemoryDatabase()
145145
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
146146

147-
queue := append([]common.Hash{}, sched.Missing(batch)...)
147+
queue := append([]common.Hash{}, sched.Missing(count)...)
148148
for len(queue) > 0 {
149149
results := make([]trie.SyncResult, len(queue))
150150
for i, hash := range queue {
@@ -157,10 +157,12 @@ func testIterativeStateSync(t *testing.T, batch int) {
157157
if _, index, err := sched.Process(results); err != nil {
158158
t.Fatalf("failed to process result #%d: %v", index, err)
159159
}
160-
if index, err := sched.Commit(dstDb); err != nil {
161-
t.Fatalf("failed to commit data #%d: %v", index, err)
160+
batch := dstDb.NewBatch()
161+
if err := sched.Commit(batch); err != nil {
162+
t.Fatalf("failed to commit data: %v", err)
162163
}
163-
queue = append(queue[:0], sched.Missing(batch)...)
164+
batch.Write()
165+
queue = append(queue[:0], sched.Missing(count)...)
164166
}
165167
// Cross check that the two states are in sync
166168
checkStateAccounts(t, dstDb, srcRoot, srcAccounts)
@@ -190,9 +192,11 @@ func TestIterativeDelayedStateSync(t *testing.T) {
190192
if _, index, err := sched.Process(results); err != nil {
191193
t.Fatalf("failed to process result #%d: %v", index, err)
192194
}
193-
if index, err := sched.Commit(dstDb); err != nil {
194-
t.Fatalf("failed to commit data #%d: %v", index, err)
195+
batch := dstDb.NewBatch()
196+
if err := sched.Commit(batch); err != nil {
197+
t.Fatalf("failed to commit data: %v", err)
195198
}
199+
batch.Write()
196200
queue = append(queue[len(results):], sched.Missing(0)...)
197201
}
198202
// Cross check that the two states are in sync
@@ -205,7 +209,7 @@ func TestIterativeDelayedStateSync(t *testing.T) {
205209
func TestIterativeRandomStateSyncIndividual(t *testing.T) { testIterativeRandomStateSync(t, 1) }
206210
func TestIterativeRandomStateSyncBatched(t *testing.T) { testIterativeRandomStateSync(t, 100) }
207211

208-
func testIterativeRandomStateSync(t *testing.T, batch int) {
212+
func testIterativeRandomStateSync(t *testing.T, count int) {
209213
// Create a random state to copy
210214
srcDb, srcRoot, srcAccounts := makeTestState()
211215

@@ -214,7 +218,7 @@ func testIterativeRandomStateSync(t *testing.T, batch int) {
214218
sched := NewStateSync(srcRoot, dstDb, trie.NewSyncBloom(1, dstDb))
215219

216220
queue := make(map[common.Hash]struct{})
217-
for _, hash := range sched.Missing(batch) {
221+
for _, hash := range sched.Missing(count) {
218222
queue[hash] = struct{}{}
219223
}
220224
for len(queue) > 0 {
@@ -231,11 +235,13 @@ func testIterativeRandomStateSync(t *testing.T, batch int) {
231235
if _, index, err := sched.Process(results); err != nil {
232236
t.Fatalf("failed to process result #%d: %v", index, err)
233237
}
234-
if index, err := sched.Commit(dstDb); err != nil {
235-
t.Fatalf("failed to commit data #%d: %v", index, err)
238+
batch := dstDb.NewBatch()
239+
if err := sched.Commit(batch); err != nil {
240+
t.Fatalf("failed to commit data: %v", err)
236241
}
242+
batch.Write()
237243
queue = make(map[common.Hash]struct{})
238-
for _, hash := range sched.Missing(batch) {
244+
for _, hash := range sched.Missing(count) {
239245
queue[hash] = struct{}{}
240246
}
241247
}
@@ -277,9 +283,11 @@ func TestIterativeRandomDelayedStateSync(t *testing.T) {
277283
if _, index, err := sched.Process(results); err != nil {
278284
t.Fatalf("failed to process result #%d: %v", index, err)
279285
}
280-
if index, err := sched.Commit(dstDb); err != nil {
281-
t.Fatalf("failed to commit data #%d: %v", index, err)
286+
batch := dstDb.NewBatch()
287+
if err := sched.Commit(batch); err != nil {
288+
t.Fatalf("failed to commit data: %v", err)
282289
}
290+
batch.Write()
283291
for _, hash := range sched.Missing(0) {
284292
queue[hash] = struct{}{}
285293
}
@@ -316,9 +324,11 @@ func TestIncompleteStateSync(t *testing.T) {
316324
if _, index, err := sched.Process(results); err != nil {
317325
t.Fatalf("failed to process result #%d: %v", index, err)
318326
}
319-
if index, err := sched.Commit(dstDb); err != nil {
320-
t.Fatalf("failed to commit data #%d: %v", index, err)
327+
batch := dstDb.NewBatch()
328+
if err := sched.Commit(batch); err != nil {
329+
t.Fatalf("failed to commit data: %v", err)
321330
}
331+
batch.Write()
322332
for _, result := range results {
323333
added = append(added, result.Hash)
324334
}

‎eth/downloader/statesync.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ func (s *stateSync) commit(force bool) error {
347347
}
348348
start := time.Now()
349349
b := s.d.stateDB.NewBatch()
350-
if written, err := s.sched.Commit(b); written == 0 || err != nil {
350+
if err := s.sched.Commit(b); err != nil {
351351
return err
352352
}
353353
if err := b.Write(); err != nil {

‎trie/sync.go‎

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,12 @@ type SyncResult struct {
5757
// persisted data items.
5858
type syncMemBatch struct {
5959
batch map[common.Hash][]byte // In-memory membatch of recently completed items
60-
order []common.Hash // Order of completion to prevent out-of-order data loss
6160
}
6261

6362
// newSyncMemBatch allocates a new memory-buffer for not-yet persisted trie nodes.
6463
func newSyncMemBatch() *syncMemBatch {
6564
return &syncMemBatch{
6665
batch: make(map[common.Hash][]byte),
67-
order: make([]common.Hash, 0, 256),
6866
}
6967
}
7068

@@ -223,20 +221,18 @@ func (s *Sync) Process(results []SyncResult) (bool, int, error) {
223221
}
224222

225223
// Commit flushes the data stored in the internal membatch out to persistent
226-
// storage, returning the number of items written and any occurred error.
227-
func (s *Sync) Commit(dbw ethdb.KeyValueWriter) (int, error) {
224+
// storage, returning any occurred error.
225+
func (s *Sync) Commit(dbw ethdb.Batch) error {
228226
// Dump the membatch into a database dbw
229-
for i, key := range s.membatch.order {
230-
if err := dbw.Put(key[:], s.membatch.batch[key]); err != nil {
231-
return i, err
227+
for key, value := range s.membatch.batch {
228+
if err := dbw.Put(key[:], value); err != nil {
229+
return err
232230
}
233231
s.bloom.Add(key[:])
234232
}
235-
written := len(s.membatch.order) // TODO(karalabe): could an order change improve write performance?
236-
237233
// Drop the membatch data and return
238234
s.membatch = newSyncMemBatch()
239-
return written, nil
235+
return nil
240236
}
241237

242238
// Pending returns the number of state entries currently pending for download.
@@ -330,7 +326,6 @@ func (s *Sync) children(req *request, object node) ([]*request, error) {
330326
func (s *Sync) commit(req *request) (err error) {
331327
// Write the node content to the membatch
332328
s.membatch.batch[req.hash] = req.data
333-
s.membatch.order = append(s.membatch.order, req.hash)
334329

335330
delete(s.requests, req.hash)
336331

‎trie/sync_test.go‎

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestEmptySync(t *testing.T) {
105105
func TestIterativeSyncIndividual(t *testing.T) { testIterativeSync(t, 1) }
106106
func TestIterativeSyncBatched(t *testing.T) { testIterativeSync(t, 100) }
107107

108-
func testIterativeSync(t *testing.T, batch int) {
108+
func testIterativeSync(t *testing.T, count int) {
109109
// Create a random trie to copy
110110
srcDb, srcTrie, srcData := makeTestTrie()
111111

@@ -114,7 +114,7 @@ func testIterativeSync(t *testing.T, batch int) {
114114
triedb := NewDatabase(diskdb)
115115
sched := NewSync(srcTrie.Hash(), diskdb, nil, NewSyncBloom(1, diskdb))
116116

117-
queue := append([]common.Hash{}, sched.Missing(batch)...)
117+
queue := append([]common.Hash{}, sched.Missing(count)...)
118118
for len(queue) > 0 {
119119
results := make([]SyncResult, len(queue))
120120
for i, hash := range queue {
@@ -127,10 +127,12 @@ func testIterativeSync(t *testing.T, batch int) {
127127
if _, index, err := sched.Process(results); err != nil {
128128
t.Fatalf("failed to process result #%d: %v", index, err)
129129
}
130-
if index, err := sched.Commit(diskdb); err != nil {
131-
t.Fatalf("failed to commit data #%d: %v", index, err)
130+
batch := diskdb.NewBatch()
131+
if err := sched.Commit(batch); err != nil {
132+
t.Fatalf("failed to commit data: %v", err)
132133
}
133-
queue = append(queue[:0], sched.Missing(batch)...)
134+
batch.Write()
135+
queue = append(queue[:0], sched.Missing(count)...)
134136
}
135137
// Cross check that the two tries are in sync
136138
checkTrieContents(t, triedb, srcTrie.Hash().Bytes(), srcData)
@@ -161,9 +163,11 @@ func TestIterativeDelayedSync(t *testing.T) {
161163
if _, index, err := sched.Process(results); err != nil {
162164
t.Fatalf("failed to process result #%d: %v", index, err)
163165
}
164-
if index, err := sched.Commit(diskdb); err != nil {
165-
t.Fatalf("failed to commit data #%d: %v", index, err)
166+
batch := diskdb.NewBatch()
167+
if err := sched.Commit(batch); err != nil {
168+
t.Fatalf("failed to commit data: %v", err)
166169
}
170+
batch.Write()
167171
queue = append(queue[len(results):], sched.Missing(10000)...)
168172
}
169173
// Cross check that the two tries are in sync
@@ -176,7 +180,7 @@ func TestIterativeDelayedSync(t *testing.T) {
176180
func TestIterativeRandomSyncIndividual(t *testing.T) { testIterativeRandomSync(t, 1) }
177181
func TestIterativeRandomSyncBatched(t *testing.T) { testIterativeRandomSync(t, 100) }
178182

179-
func testIterativeRandomSync(t *testing.T, batch int) {
183+
func testIterativeRandomSync(t *testing.T, count int) {
180184
// Create a random trie to copy
181185
srcDb, srcTrie, srcData := makeTestTrie()
182186

@@ -186,7 +190,7 @@ func testIterativeRandomSync(t *testing.T, batch int) {
186190
sched := NewSync(srcTrie.Hash(), diskdb, nil, NewSyncBloom(1, diskdb))
187191

188192
queue := make(map[common.Hash]struct{})
189-
for _, hash := range sched.Missing(batch) {
193+
for _, hash := range sched.Missing(count) {
190194
queue[hash] = struct{}{}
191195
}
192196
for len(queue) > 0 {
@@ -203,11 +207,13 @@ func testIterativeRandomSync(t *testing.T, batch int) {
203207
if _, index, err := sched.Process(results); err != nil {
204208
t.Fatalf("failed to process result #%d: %v", index, err)
205209
}
206-
if index, err := sched.Commit(diskdb); err != nil {
207-
t.Fatalf("failed to commit data #%d: %v", index, err)
210+
batch := diskdb.NewBatch()
211+
if err := sched.Commit(batch); err != nil {
212+
t.Fatalf("failed to commit data: %v", err)
208213
}
214+
batch.Write()
209215
queue = make(map[common.Hash]struct{})
210-
for _, hash := range sched.Missing(batch) {
216+
for _, hash := range sched.Missing(count) {
211217
queue[hash] = struct{}{}
212218
}
213219
}
@@ -248,9 +254,11 @@ func TestIterativeRandomDelayedSync(t *testing.T) {
248254
if _, index, err := sched.Process(results); err != nil {
249255
t.Fatalf("failed to process result #%d: %v", index, err)
250256
}
251-
if index, err := sched.Commit(diskdb); err != nil {
252-
t.Fatalf("failed to commit data #%d: %v", index, err)
257+
batch := diskdb.NewBatch()
258+
if err := sched.Commit(batch); err != nil {
259+
t.Fatalf("failed to commit data: %v", err)
253260
}
261+
batch.Write()
254262
for _, result := range results {
255263
delete(queue, result.Hash)
256264
}
@@ -293,9 +301,11 @@ func TestDuplicateAvoidanceSync(t *testing.T) {
293301
if _, index, err := sched.Process(results); err != nil {
294302
t.Fatalf("failed to process result #%d: %v", index, err)
295303
}
296-
if index, err := sched.Commit(diskdb); err != nil {
297-
t.Fatalf("failed to commit data #%d: %v", index, err)
304+
batch := diskdb.NewBatch()
305+
if err := sched.Commit(batch); err != nil {
306+
t.Fatalf("failed to commit data: %v", err)
298307
}
308+
batch.Write()
299309
queue = append(queue[:0], sched.Missing(0)...)
300310
}
301311
// Cross check that the two tries are in sync
@@ -329,9 +339,11 @@ func TestIncompleteSync(t *testing.T) {
329339
if _, index, err := sched.Process(results); err != nil {
330340
t.Fatalf("failed to process result #%d: %v", index, err)
331341
}
332-
if index, err := sched.Commit(diskdb); err != nil {
333-
t.Fatalf("failed to commit data #%d: %v", index, err)
342+
batch := diskdb.NewBatch()
343+
if err := sched.Commit(batch); err != nil {
344+
t.Fatalf("failed to commit data: %v", err)
334345
}
346+
batch.Write()
335347
for _, result := range results {
336348
added = append(added, result.Hash)
337349
}

0 commit comments

Comments
 (0)