-
Notifications
You must be signed in to change notification settings - Fork 29
LedgerDB: prune on garbage collection instead of on every change #1513
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
base: main
Are you sure you want to change the base?
Conversation
8b48bb3
to
045f1cc
Compare
13e5533
to
68402ed
Compare
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.
Looks good.
ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/LedgerDB/V2.hs
Show resolved
Hide resolved
981971e
to
0c5b137
Compare
68402ed
to
4d6fd67
Compare
0c5b137
to
7900088
Compare
4d6fd67
to
7049fd4
Compare
Sync benchmarks are looking good (mainnet, first 1e6 slots/blocks): LMDB benchmark (of course, this is a bit degenerate as Byron doesn't have tables, but this still serves as a regression test for the Note that baf3e7f is crucial; otherwise, there is a significant (2x) regression in max heap size. |
2e01b1c
to
b9e25f5
Compare
19faf20
to
4010598
Compare
b9e25f5
to
894940c
Compare
It is not necessary to perform the garbage collection of the LedgerDB and the map of invalid blocks in the same STM transaction. In the past, this was important, but it is not anymore, see #1507.
This is an optimization to reduce the maximum memory usage (more relevant with the in-memory backend), see the added commit and the benchmark in the pull request.
894940c
to
a8fa7e2
Compare
LedgerDbPruneAll | ||
| -- | Prune to only keep the last @k@ states. | ||
LedgerDbPruneKeeping SecurityParam | ||
| -- | Prune such that all (non-anchor) states are older than the given slot. |
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.
| -- | Prune such that all (non-anchor) states are older than the given slot. | |
| -- | Prune such that all (non-anchor) states are younger than the given slot. |
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.
Thanks for pointing out this inversion in a couple of places!
Changed, using "not older" instead of "younger" to account for the case of equality (we want to keep states with the same slot as the argument slot).
-- hold the last \(k\) in-memory ledger states. This data type is impemented | ||
-- using the /finger tree/ data structure and has the following time | ||
-- hold (at least) the last \(k\) in-memory ledger states. This data type is | ||
-- impemented using the /finger tree/ data structure and has the following time |
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.
-- impemented using the /finger tree/ data structure and has the following time | |
-- implemented using the /finger tree/ data structure and has the following time |
where | ||
DbChangelog{changelogStates} = dblog | ||
|
||
-- The anchor of @vol'@ might still have a tip slot larger than @slot@, which |
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.
-- The anchor of @vol'@ might still have a tip slot larger than @slot@, which | |
-- The anchor of @vol'@ might still have a tip slot smaller than @slot@, which |
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.
or older
LedgerDbPruneBeforeSlot slot -> | ||
(closeButHead before, LedgerSeq after) | ||
where | ||
-- The anchor of @vol'@ might still have a tip slot larger than @slot@, |
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.
-- The anchor of @vol'@ might still have a tip slot larger than @slot@, | |
-- The anchor of @vol'@ might still have a tip slot older than @slot@, |
regarding the previous few commits
For consistency with V1. This only makes a difference if there are non-pruned states. Also, a very small benefit is that we get (very slightly) faster replay on node startup.
This is used in db-analyser only, where everything happens synchronously in a single thread, so it is fine to immediately prune. V1 already does this.
a8fa7e2
to
b503dc3
Compare
-- ^ Garbage collect references to old blocks that have been previously | ||
-- applied and committed. | ||
, garbageCollect :: SlotNo -> m () | ||
-- ^ Garbage collect references to old state that is older than the given |
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.
-- ^ Garbage collect references to old state that is older than the given | |
-- ^ Garbage collect references to old states that are older than the given |
data LedgerDbPrune | ||
= -- | Prune all states, keeping only the current tip. | ||
LedgerDbPruneAll | ||
| -- | Prune to only keep the last @k@ states. |
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.
Isn't LedgerDbPruneKeeping
redundant, now that we have LedgerDbPruneBeforeSlot
? Or rather, could this be replaced with the new value? (But I understand this is a different concern that the current PR should not address).
. readTVar | ||
$ ldbChangelog env | ||
where | ||
k = unNonZero $ maxRollbacks $ ledgerDbCfgSecParam $ ldbCfg env |
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.
Would it make sense, in a different PR, to add the value of k
directly to the configuration environment, or is this indirection not costly enough to justify this?
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.
We also see this pattern a couple of times, which might be another justification why we might want to add k
to the environment.
Right ImmutableTip -> rollbackTo immTip | ||
Right (SpecificPoint pt) -> rollbackTo pt | ||
Left n -> do | ||
let rollbackMax = maxRollback dblog `min` k |
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.
Is it sound/safe that the db-changelog reports a maximum rollback larger than k
?
|
||
-- See the Haddocks above as for why we garbage-collect the LedgerDB already | ||
-- here (instead of as part of the scheduled GC). | ||
whenJust (withOriginToMaybe gcSlotNo) $ LedgerDB.garbageCollect cdbLedgerDB |
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.
The comment here suggests that we perform garbage-collection on the LedgerDB as part of the scheduled GC of ChainDB. However, when I look at ChainDB.Impl.Background.garbageCollect, I only see a call to the VolatileDB GC.
My question is: are we performing garbage collection on the LedgerDB as part of the ChainDB GC? If yes, could you point me to the place in the code where that happens?
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.
oops, now I see that changing this is the entire point of e414367!! Please disregard my previous comment.
@@ -0,0 +1,6 @@ | |||
### Breaking | |||
|
|||
- Changed pruning of immutable ledger states to happen on LedgerDB/ChainDB |
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.
The changelog entry here seems misleading, but please correct me if I'm wrong.
If I understand correctly, we are now pruning the LedgerDB in copyAndSnapshotRunner
, i.e. when moving blocks from VolatileDB to ImmutableDB. In the scheduled ChainDB GCs, we will not prune the LedgerDB at all.
This is in preparation for #1424
Currently, we prune the LedgerDB (ie remove all but the last
k+1
states) every time we adopt a longer chain. This means that we can not rely on the fact that other threads (like thecopyAndSnapshot
ChainDB background) actually observe all immutable ledger states, just as described in the caveats of ourWatcher
abstraction.However, a predictable ledger snapshotting rule (#1424) requires this property; otherwise, when the node is under high load and/or we are adopting multiple blocks in quick succession, the node might not be able to create a snapshot for its desired block.
This PR changes this fact: Now, when adopting new blocks, the LedgerDB is not immediately pruned. Instead, the
copyAndSnapshot
ChainDB thread will periodically (on every new immutable block) wake up and (in particular) garbage collect the LedgerDB based on a slot number.Also, this makes the semantics more consistent with the existing garbage collection of previously-applied blocks in the LedgerDB, and also with how the ChainDB works, where we also don't immediately delete blocks from the VolatileDB once they are buried beneath
k+1
blocks.See #1513 (comment) for benchmarks demonstrating that the peak memory usage does not increase while syncing (where we now briefly might hold more than
k+1
ledger states in memory).