-
Notifications
You must be signed in to change notification settings - Fork 268
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
feat: remove legacy orphans background #863
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ import ( | |
"strconv" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
"cosmossdk.io/log" | ||
dbm "github.com/cosmos/cosmos-db" | ||
|
@@ -470,9 +471,38 @@ func (ndb *nodeDB) deleteLegacyVersions() error { | |
ndb.legacyLatestVersion = -1 | ||
|
||
// Delete all orphan nodes of the legacy versions | ||
return ndb.traversePrefix(legacyOrphanKeyFormat.Key(), func(key, value []byte) error { | ||
return ndb.batch.Delete(key) | ||
}) | ||
go func() { | ||
if err := ndb.deleteOrphans(); err != nil { | ||
ndb.logger.Error("failed to clean legacy orphans", "err", err) | ||
} | ||
}() | ||
Comment on lines
+474
to
+478
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of a goroutine to call |
||
|
||
return nil | ||
} | ||
|
||
// deleteOrphans cleans all legacy orphans from the nodeDB. | ||
func (ndb *nodeDB) deleteOrphans() error { | ||
itr, err := dbm.IteratePrefix(ndb.db, legacyOrphanKeyFormat.Key()) | ||
if err != nil { | ||
return err | ||
} | ||
defer itr.Close() | ||
|
||
count := 0 | ||
for ; itr.Valid(); itr.Next() { | ||
if err := ndb.batch.Delete(itr.Key()); err != nil { | ||
return err | ||
} | ||
|
||
// Sleep for a while to avoid blocking the main thread i/o. | ||
count++ | ||
if count > 1000 { | ||
count = 0 | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
Comment on lines
+491
to
+502
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The sleep mechanism in |
||
} | ||
|
||
return nil | ||
} | ||
|
||
// DeleteVersionsFrom permanently deletes all tree versions from the given version upwards. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider protecting
Write
,WriteSync
,Close
, andGetByteSize
methods with the mutex to prevent potential race conditions.