Skip to content

Commit

Permalink
fix: performance issue when load journal (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
jingjunLi authored May 8, 2024
1 parent 1d81f33 commit 901ea2e
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions triedb/pathdb/journal.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ func newJournalReader(file string, db ethdb.Database, journalType JournalType) (
// loadJournal tries to parse the layer journal from the disk.
func (db *Database) loadJournal(diskRoot common.Hash) (layer, error) {
start := time.Now()
reader, err := newJournalReader(db.config.JournalFilePath, db.diskdb, db.DetermineJournalTypeForReader())
journalTypeForReader := db.DetermineJournalTypeForReader()
reader, err := newJournalReader(db.config.JournalFilePath, db.diskdb, journalTypeForReader)

if err != nil {
return nil, err
Expand Down Expand Up @@ -226,12 +227,12 @@ func (db *Database) loadJournal(diskRoot common.Hash) (layer, error) {
return nil, fmt.Errorf("%w want %x got %x", errUnmatchedJournal, root, diskRoot)
}
// Load the disk layer from the journal
base, err := db.loadDiskLayer(r)
base, err := db.loadDiskLayer(r, journalTypeForReader)
if err != nil {
return nil, err
}
// Load all the diff layers from the journal
head, err := db.loadDiffLayer(base, r)
head, err := db.loadDiffLayer(base, r, journalTypeForReader)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -262,14 +263,14 @@ func (db *Database) loadLayers() layer {

// loadDiskLayer reads the binary blob from the layer journal, reconstructing
// a new disk layer on it.
func (db *Database) loadDiskLayer(r *rlp.Stream) (layer, error) {
func (db *Database) loadDiskLayer(r *rlp.Stream, journalTypeForReader JournalType) (layer, error) {
// Resolve disk layer root
var (
root common.Hash
journalBuf *rlp.Stream
journalEncodedBuff []byte
)
if db.DetermineJournalTypeForReader() == JournalFileType {
if journalTypeForReader == JournalFileType {
if err := r.Decode(&journalEncodedBuff); err != nil {
return nil, fmt.Errorf("load disk journal: %v", err)
}
Expand Down Expand Up @@ -310,7 +311,7 @@ func (db *Database) loadDiskLayer(r *rlp.Stream) (layer, error) {
nodes[entry.Owner] = subset
}

if db.DetermineJournalTypeForReader() == JournalFileType {
if journalTypeForReader == JournalFileType {
var shaSum [32]byte
if err := r.Decode(&shaSum); err != nil {
return nil, fmt.Errorf("load shasum: %v", err)
Expand All @@ -329,14 +330,14 @@ func (db *Database) loadDiskLayer(r *rlp.Stream) (layer, error) {

// loadDiffLayer reads the next sections of a layer journal, reconstructing a new
// diff and verifying that it can be linked to the requested parent.
func (db *Database) loadDiffLayer(parent layer, r *rlp.Stream) (layer, error) {
func (db *Database) loadDiffLayer(parent layer, r *rlp.Stream, journalTypeForReader JournalType) (layer, error) {
// Read the next diff journal entry
var (
root common.Hash
journalBuf *rlp.Stream
journalEncodedBuff []byte
)
if db.DetermineJournalTypeForReader() == JournalFileType {
if journalTypeForReader == JournalFileType {
if err := r.Decode(&journalEncodedBuff); err != nil {
// The first read may fail with EOF, marking the end of the journal
if err == io.EOF {
Expand Down Expand Up @@ -409,7 +410,7 @@ func (db *Database) loadDiffLayer(parent layer, r *rlp.Stream) (layer, error) {
storages[entry.Account] = set
}

if db.DetermineJournalTypeForReader() == JournalFileType {
if journalTypeForReader == JournalFileType {
var shaSum [32]byte
if err := r.Decode(&shaSum); err != nil {
return nil, fmt.Errorf("load shasum: %v", err)
Expand All @@ -423,7 +424,7 @@ func (db *Database) loadDiffLayer(parent layer, r *rlp.Stream) (layer, error) {

log.Debug("Loaded diff layer journal", "root", root, "parent", parent.rootHash(), "id", parent.stateID()+1, "block", block)

return db.loadDiffLayer(newDiffLayer(parent, root, parent.stateID()+1, block, nodes, triestate.New(accounts, storages, incomplete)), r)
return db.loadDiffLayer(newDiffLayer(parent, root, parent.stateID()+1, block, nodes, triestate.New(accounts, storages, incomplete)), r, journalTypeForReader)
}

// journal implements the layer interface, marshaling the un-flushed trie nodes
Expand Down

0 comments on commit 901ea2e

Please sign in to comment.