Skip to content

Commit 59e1953

Browse files
holimanfjl
authored andcommitted
core, ethdb, trie: mode dirty data to clean cache on flush (#19307)
This PR is a more advanced form of the dirty-to-clean cacher (#18995), where we reuse previous database write batches as datasets to uncache, saving a dirty-trie-iteration and a dirty-trie-rlp-reencoding per block.
1 parent df717ab commit 59e1953

12 files changed

Lines changed: 156 additions & 67 deletions

File tree

‎core/blockchain.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func (bc *BlockChain) SetHead(head uint64) error {
301301
defer bc.chainmu.Unlock()
302302

303303
// Rewind the header chain, deleting all block bodies until then
304-
delFn := func(db ethdb.Deleter, hash common.Hash, num uint64) {
304+
delFn := func(db ethdb.Writer, hash common.Hash, num uint64) {
305305
rawdb.DeleteBody(db, hash, num)
306306
}
307307
bc.hc.SetHead(head, delFn)

‎core/headerchain.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ func (hc *HeaderChain) SetCurrentHeader(head *types.Header) {
455455

456456
// DeleteCallback is a callback function that is called by SetHead before
457457
// each header is deleted.
458-
type DeleteCallback func(ethdb.Deleter, common.Hash, uint64)
458+
type DeleteCallback func(ethdb.Writer, common.Hash, uint64)
459459

460460
// SetHead rewinds the local chain to a new head. Everything above the new head
461461
// will be deleted and the new one set.

‎core/rawdb/accessors_chain.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func WriteCanonicalHash(db ethdb.Writer, hash common.Hash, number uint64) {
4545
}
4646

4747
// DeleteCanonicalHash removes the number to hash canonical mapping.
48-
func DeleteCanonicalHash(db ethdb.Deleter, number uint64) {
48+
func DeleteCanonicalHash(db ethdb.Writer, number uint64) {
4949
if err := db.Delete(headerHashKey(number)); err != nil {
5050
log.Crit("Failed to delete number to hash mapping", "err", err)
5151
}
@@ -180,7 +180,7 @@ func WriteHeader(db ethdb.Writer, header *types.Header) {
180180
}
181181

182182
// DeleteHeader removes all block header data associated with a hash.
183-
func DeleteHeader(db ethdb.Deleter, hash common.Hash, number uint64) {
183+
func DeleteHeader(db ethdb.Writer, hash common.Hash, number uint64) {
184184
deleteHeaderWithoutNumber(db, hash, number)
185185
if err := db.Delete(headerNumberKey(hash)); err != nil {
186186
log.Crit("Failed to delete hash to number mapping", "err", err)
@@ -189,7 +189,7 @@ func DeleteHeader(db ethdb.Deleter, hash common.Hash, number uint64) {
189189

190190
// deleteHeaderWithoutNumber removes only the block header but does not remove
191191
// the hash to number mapping.
192-
func deleteHeaderWithoutNumber(db ethdb.Deleter, hash common.Hash, number uint64) {
192+
func deleteHeaderWithoutNumber(db ethdb.Writer, hash common.Hash, number uint64) {
193193
if err := db.Delete(headerKey(number, hash)); err != nil {
194194
log.Crit("Failed to delete header", "err", err)
195195
}
@@ -240,7 +240,7 @@ func WriteBody(db ethdb.Writer, hash common.Hash, number uint64, body *types.Bod
240240
}
241241

242242
// DeleteBody removes all block body data associated with a hash.
243-
func DeleteBody(db ethdb.Deleter, hash common.Hash, number uint64) {
243+
func DeleteBody(db ethdb.Writer, hash common.Hash, number uint64) {
244244
if err := db.Delete(blockBodyKey(number, hash)); err != nil {
245245
log.Crit("Failed to delete block body", "err", err)
246246
}
@@ -278,7 +278,7 @@ func WriteTd(db ethdb.Writer, hash common.Hash, number uint64, td *big.Int) {
278278
}
279279

280280
// DeleteTd removes all block total difficulty data associated with a hash.
281-
func DeleteTd(db ethdb.Deleter, hash common.Hash, number uint64) {
281+
func DeleteTd(db ethdb.Writer, hash common.Hash, number uint64) {
282282
if err := db.Delete(headerTDKey(number, hash)); err != nil {
283283
log.Crit("Failed to delete block total difficulty", "err", err)
284284
}
@@ -347,7 +347,7 @@ func WriteReceipts(db ethdb.Writer, hash common.Hash, number uint64, receipts ty
347347
}
348348

349349
// DeleteReceipts removes all receipt data associated with a block hash.
350-
func DeleteReceipts(db ethdb.Deleter, hash common.Hash, number uint64) {
350+
func DeleteReceipts(db ethdb.Writer, hash common.Hash, number uint64) {
351351
if err := db.Delete(blockReceiptsKey(number, hash)); err != nil {
352352
log.Crit("Failed to delete block receipts", "err", err)
353353
}
@@ -378,7 +378,7 @@ func WriteBlock(db ethdb.Writer, block *types.Block) {
378378
}
379379

380380
// DeleteBlock removes all block data associated with a hash.
381-
func DeleteBlock(db ethdb.Deleter, hash common.Hash, number uint64) {
381+
func DeleteBlock(db ethdb.Writer, hash common.Hash, number uint64) {
382382
DeleteReceipts(db, hash, number)
383383
DeleteHeader(db, hash, number)
384384
DeleteBody(db, hash, number)
@@ -387,7 +387,7 @@ func DeleteBlock(db ethdb.Deleter, hash common.Hash, number uint64) {
387387

388388
// deleteBlockWithoutNumber removes all block data associated with a hash, except
389389
// the hash to number mapping.
390-
func deleteBlockWithoutNumber(db ethdb.Deleter, hash common.Hash, number uint64) {
390+
func deleteBlockWithoutNumber(db ethdb.Writer, hash common.Hash, number uint64) {
391391
DeleteReceipts(db, hash, number)
392392
deleteHeaderWithoutNumber(db, hash, number)
393393
DeleteBody(db, hash, number)

‎core/rawdb/accessors_indexes.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ func WriteTxLookupEntries(db ethdb.Writer, block *types.Block) {
5454
}
5555

5656
// DeleteTxLookupEntry removes all transaction data associated with a hash.
57-
func DeleteTxLookupEntry(db ethdb.Deleter, hash common.Hash) {
57+
func DeleteTxLookupEntry(db ethdb.Writer, hash common.Hash) {
5858
db.Delete(txLookupKey(hash))
5959
}
6060

‎core/rawdb/table.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,8 @@ func (b *tableBatch) Write() error {
148148
func (b *tableBatch) Reset() {
149149
b.batch.Reset()
150150
}
151+
152+
// Replay replays the batch contents.
153+
func (b *tableBatch) Replay(w ethdb.Writer) error {
154+
return b.batch.Replay(w)
155+
}

‎core/state/statedb.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ func (n *proofList) Put(key []byte, value []byte) error {
5353
return nil
5454
}
5555

56+
func (n *proofList) Delete(key []byte) error {
57+
panic("not supported")
58+
}
59+
5660
// StateDBs within the ethereum protocol are used to store anything
5761
// within the merkle trie. StateDBs take care of caching and storing
5862
// nested states. It's the general query interface to retrieve:

‎ethdb/batch.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ const IdealBatchSize = 100 * 1024
2424
// when Write is called. A batch cannot be used concurrently.
2525
type Batch interface {
2626
Writer
27-
Deleter
2827

2928
// ValueSize retrieves the amount of data queued up for writing.
3029
ValueSize() int
3130

3231
// Write flushes any accumulated data to disk.
3332
Write() error
3433

35-
// Reset resets the batch for reuse
34+
// Reset resets the batch for reuse.
3635
Reset()
36+
37+
// Replay replays the batch contents.
38+
Replay(w Writer) error
3739
}
3840

3941
// Batcher wraps the NewBatch method of a backing data store.

‎ethdb/database.go‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
// You should have received a copy of the GNU Lesser General Public License
1515
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
1616

17-
// Package database defines the interfaces for an Ethereum data store.
17+
// Package ethdb defines the interfaces for an Ethereum data store.
1818
package ethdb
1919

2020
import "io"
@@ -32,10 +32,7 @@ type Reader interface {
3232
type Writer interface {
3333
// Put inserts the given value into the key-value data store.
3434
Put(key []byte, value []byte) error
35-
}
3635

37-
// Deleter wraps the Delete method of a backing data store.
38-
type Deleter interface {
3936
// Delete removes the key from the key-value data store.
4037
Delete(key []byte) error
4138
}
@@ -63,7 +60,6 @@ type Compacter interface {
6360
type KeyValueStore interface {
6461
Reader
6562
Writer
66-
Deleter
6763
Batcher
6864
Iteratee
6965
Stater
@@ -76,7 +72,6 @@ type KeyValueStore interface {
7672
type Database interface {
7773
Reader
7874
Writer
79-
Deleter
8075
Batcher
8176
Iteratee
8277
Stater

‎ethdb/leveldb/leveldb.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,32 @@ func (b *batch) Reset() {
416416
b.b.Reset()
417417
b.size = 0
418418
}
419+
420+
// Replay replays the batch contents.
421+
func (b *batch) Replay(w ethdb.Writer) error {
422+
return b.b.Replay(&replayer{writer: w})
423+
}
424+
425+
// replayer is a small wrapper to implement the correct replay methods.
426+
type replayer struct {
427+
writer ethdb.Writer
428+
failure error
429+
}
430+
431+
// Put inserts the given value into the key-value data store.
432+
func (r *replayer) Put(key, value []byte) {
433+
// If the replay already failed, stop executing ops
434+
if r.failure != nil {
435+
return
436+
}
437+
r.failure = r.writer.Put(key, value)
438+
}
439+
440+
// Delete removes the key from the key-value data store.
441+
func (r *replayer) Delete(key []byte) {
442+
// If the replay already failed, stop executing ops
443+
if r.failure != nil {
444+
return
445+
}
446+
r.failure = r.writer.Delete(key)
447+
}

‎ethdb/memorydb/memorydb.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,22 @@ func (b *batch) Reset() {
240240
b.size = 0
241241
}
242242

243+
// Replay replays the batch contents.
244+
func (b *batch) Replay(w ethdb.Writer) error {
245+
for _, keyvalue := range b.writes {
246+
if keyvalue.delete {
247+
if err := w.Delete(keyvalue.key); err != nil {
248+
return err
249+
}
250+
continue
251+
}
252+
if err := w.Put(keyvalue.key, keyvalue.value); err != nil {
253+
return err
254+
}
255+
}
256+
return nil
257+
}
258+
243259
// iterator can walk over the (potentially partial) keyspace of a memory key
244260
// value store. Internally it is a deep copy of the entire iterated state,
245261
// sorted by keys.

0 commit comments

Comments
 (0)