Skip to content

Commit 432633f

Browse files
committed
triedb/pathdb: address comments from martin
1 parent 20b4ffd commit 432633f

File tree

5 files changed

+19
-10
lines changed

5 files changed

+19
-10
lines changed

triedb/pathdb/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ func (db *Database) Close() error {
442442
// Release the memory held by clean cache.
443443
disk := db.tree.bottom()
444444
if disk.frozen != nil {
445-
if err := disk.frozen.flushed(); err != nil {
445+
if err := disk.frozen.waitFlush(); err != nil {
446446
return err
447447
}
448448
}

triedb/pathdb/disklayer.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co
140140
} else {
141141
blob = rawdb.ReadStorageTrieNode(dl.db.diskdb, owner, path)
142142
}
143+
// Store the resolved data in the clean cache. The background buffer flusher
144+
// may also write to the clean cache concurrently, but two writers cannot
145+
// write the same item with different content. If the item already exists,
146+
// it will be found in the frozen buffer, eliminating the need to check the
147+
// database.
143148
if dl.cleans != nil && len(blob) > 0 {
144149
dl.cleans.Set(key, blob)
145150
cleanWriteMeter.Mark(int64(len(blob)))
@@ -208,7 +213,7 @@ func (dl *diskLayer) commit(bottom *diffLayer, force bool) (*diskLayer, error) {
208213
if combined.full() || force {
209214
// Wait until the previous frozen buffer is fully flushed
210215
if dl.frozen != nil {
211-
if err := dl.frozen.flushed(); err != nil {
216+
if err := dl.frozen.waitFlush(); err != nil {
212217
return nil, err
213218
}
214219
}
@@ -221,7 +226,7 @@ func (dl *diskLayer) commit(bottom *diffLayer, force bool) (*diskLayer, error) {
221226
// Block until the frozen buffer is fully flushed out if the oldest history
222227
// surpasses the persisted state ID.
223228
if persistedID < oldest {
224-
if err := dl.frozen.flushed(); err != nil {
229+
if err := dl.frozen.waitFlush(); err != nil {
225230
return nil, err
226231
}
227232
}
@@ -278,10 +283,12 @@ func (dl *diskLayer) revert(h *history) (*diskLayer, error) {
278283
} else {
279284
// Block until the frozen buffer is fully flushed
280285
if dl.frozen != nil {
281-
if err := dl.frozen.flushed(); err != nil {
286+
if err := dl.frozen.waitFlush(); err != nil {
282287
return nil, err
283288
}
284-
dl.frozen = nil // unset the frozen buffer
289+
// Unset the frozen buffer if it exists, otherwise these "reverted"
290+
// states will still be accessible after revert in frozen buffer.
291+
dl.frozen = nil
285292
}
286293
batch := dl.db.diskdb.NewBatch()
287294
writeNodes(batch, nodes, dl.cleans)

triedb/pathdb/journal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func (db *Database) Journal(root common.Hash) error {
343343
}
344344
disk := db.tree.bottom()
345345
if disk.frozen != nil {
346-
if err := disk.frozen.flushed(); err != nil {
346+
if err := disk.frozen.waitFlush(); err != nil {
347347
return err
348348
}
349349
}

triedb/pathdb/layertree.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func (tree *layerTree) cap(root common.Hash, layers int) error {
133133
}
134134
// Block until the frozen buffer is fully flushed
135135
if base.frozen != nil {
136-
if err := base.frozen.flushed(); err != nil {
136+
if err := base.frozen.waitFlush(); err != nil {
137137
return err
138138
}
139139
}

triedb/pathdb/nodebuffer.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,14 @@ func (b *nodebuffer) flush(db ethdb.KeyValueStore, clean *fastcache.Cache, id ui
245245
commitNodesMeter.Mark(int64(nodes))
246246
commitTimeTimer.UpdateSince(start)
247247
log.Info("Persisted pathdb nodes", "nodes", nodes, "bytes", common.StorageSize(size), "elapsed", common.PrettyDuration(time.Since(start)))
248+
249+
// The content in the frozen buffer is kept for consequent state access
248250
}()
249251
}
250252

251-
// flushed blocks until the buffer is fully flushed and also returns the memorized
252-
// error which occurs within the flushing.
253-
func (b *nodebuffer) flushed() error {
253+
// waitFlush blocks until the buffer has been fully flushed and returns any
254+
// stored errors that occurred during the process.
255+
func (b *nodebuffer) waitFlush() error {
254256
<-b.done
255257
return b.flushErr
256258
}

0 commit comments

Comments
 (0)