Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: performance issue when load journal #2438

Merged
merged 3 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions triedb/pathdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,15 @@ type Database struct {
// readOnly is the flag whether the mutation is allowed to be applied.
// It will be set automatically when the database is journaled during
// the shutdown to reject all following unexpected mutations.
readOnly bool // Flag if database is opened in read only mode
waitSync bool // Flag if database is deactivated due to initial state sync
bufferSize int // Memory allowance (in bytes) for caching dirty nodes
config *Config // Configuration for database
diskdb ethdb.Database // Persistent storage for matured trie nodes
tree *layerTree // The group for all known layers
freezer *rawdb.ResettableFreezer // Freezer for storing trie histories, nil possible in tests
lock sync.RWMutex // Lock to prevent mutations from happening at the same time
readOnly bool // Flag if database is opened in read only mode
waitSync bool // Flag if database is deactivated due to initial state sync
bufferSize int // Memory allowance (in bytes) for caching dirty nodes
config *Config // Configuration for database
diskdb ethdb.Database // Persistent storage for matured trie nodes
tree *layerTree // The group for all known layers
freezer *rawdb.ResettableFreezer // Freezer for storing trie histories, nil possible in tests
lock sync.RWMutex // Lock to prevent mutations from happening at the same time
journalTypeForReader JournalType
sysvm marked this conversation as resolved.
Show resolved Hide resolved
}

// New attempts to load an already existing layer from a persistent key-value
Expand Down Expand Up @@ -544,17 +545,23 @@ func (db *Database) DetermineJournalTypeForWriter() JournalType {
}
}

// DetermineJournalTypeForReader is used when loading the journal. It loads based on whether JournalKV or JournalFile currently exists.
func (db *Database) DetermineJournalTypeForReader() JournalType {
// JournalTypeForReader is used when loading the journal. It loads based on whether JournalKV or JournalFile currently exists.
func (db *Database) JournalTypeForReader() JournalType {
return db.journalTypeForReader
}

func (db *Database) DetermineJournalTypeForReader() {
if journal := rawdb.ReadTrieJournal(db.diskdb); len(journal) != 0 {
fynnss marked this conversation as resolved.
Show resolved Hide resolved
return JournalKVType
db.journalTypeForReader = JournalKVType
return
}

if fileInfo, stateErr := os.Stat(db.config.JournalFilePath); stateErr == nil && !fileInfo.IsDir() {
return JournalFileType
db.journalTypeForReader = JournalFileType
return
}

return JournalKVType
db.journalTypeForReader = JournalKVType
}

func (db *Database) DeleteTrieJournal(writer ethdb.KeyValueWriter) error {
Expand Down
11 changes: 6 additions & 5 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())
db.DetermineJournalTypeForReader()
reader, err := newJournalReader(db.config.JournalFilePath, db.diskdb, db.JournalTypeForReader())

if err != nil {
return nil, err
Expand Down Expand Up @@ -269,7 +270,7 @@ func (db *Database) loadDiskLayer(r *rlp.Stream) (layer, error) {
journalBuf *rlp.Stream
journalEncodedBuff []byte
)
if db.DetermineJournalTypeForReader() == JournalFileType {
if db.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 db.JournalTypeForReader() == JournalFileType {
var shaSum [32]byte
if err := r.Decode(&shaSum); err != nil {
return nil, fmt.Errorf("load shasum: %v", err)
Expand All @@ -336,7 +337,7 @@ func (db *Database) loadDiffLayer(parent layer, r *rlp.Stream) (layer, error) {
journalBuf *rlp.Stream
journalEncodedBuff []byte
)
if db.DetermineJournalTypeForReader() == JournalFileType {
if db.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 db.JournalTypeForReader() == JournalFileType {
var shaSum [32]byte
if err := r.Decode(&shaSum); err != nil {
return nil, fmt.Errorf("load shasum: %v", err)
Expand Down
Loading