Commit 71a432b
committed
LedgerDB: prune on garbage collection instead of on every change (#1513)
This is in preparation for #1424
This PR is intended to be reviewed commit-by-commit.
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 the `copyAndSnapshot` ChainDB
background) actually observe all immutable ledger states, just as
described in the caveats of our `Watcher` 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 a new dedicated background
thread for ledger maintenance tasks (flushing/snapshotting/garbage
collection) in the ChainDB 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).1 parent 19cda73 commit 71a432b
File tree
13 files changed
+390
-302
lines changed- ouroboros-consensus
- changelog.d
- src/ouroboros-consensus/Ouroboros/Consensus/Storage
- ChainDB
- Impl
- LedgerDB
- V1
- V2
- test/storage-test/Test/Ouroboros/Storage
- ChainDB
- LedgerDB
- V1
13 files changed
+390
-302
lines changedouroboros-consensus/changelog.d/20250626_193647_alexander.esgen_ledgerdb_garbage_collect_states.md
Lines changed: 9 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
Lines changed: 3 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
279 | 279 | | |
280 | 280 | | |
281 | 281 | | |
282 | | - | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
283 | 285 | | |
284 | 286 | | |
285 | 287 | | |
| |||
ouroboros-consensus/src/ouroboros-consensus/Ouroboros/Consensus/Storage/ChainDB/Impl/Background.hs
Lines changed: 129 additions & 42 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
| 4 | + | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
| |||
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | | - | |
23 | 23 | | |
24 | 24 | | |
25 | 25 | | |
26 | | - | |
| 26 | + | |
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| |||
76 | 76 | | |
77 | 77 | | |
78 | 78 | | |
| 79 | + | |
79 | 80 | | |
80 | 81 | | |
81 | 82 | | |
| |||
99 | 100 | | |
100 | 101 | | |
101 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
102 | 109 | | |
103 | 110 | | |
104 | | - | |
| 111 | + | |
105 | 112 | | |
106 | | - | |
107 | | - | |
108 | | - | |
109 | | - | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
110 | 119 | | |
111 | 120 | | |
112 | | - | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
113 | 127 | | |
114 | 128 | | |
115 | 129 | | |
| |||
198 | 212 | | |
199 | 213 | | |
200 | 214 | | |
201 | | - | |
| 215 | + | |
202 | 216 | | |
203 | 217 | | |
204 | | - | |
205 | | - | |
| 218 | + | |
| 219 | + | |
206 | 220 | | |
207 | 221 | | |
208 | 222 | | |
209 | 223 | | |
210 | 224 | | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
| 225 | + | |
| 226 | + | |
217 | 227 | | |
218 | 228 | | |
219 | 229 | | |
| |||
228 | 238 | | |
229 | 239 | | |
230 | 240 | | |
231 | | - | |
| 241 | + | |
232 | 242 | | |
233 | 243 | | |
234 | 244 | | |
235 | 245 | | |
236 | 246 | | |
| 247 | + | |
237 | 248 | | |
238 | | - | |
239 | | - | |
240 | 249 | | |
241 | 250 | | |
242 | | - | |
| 251 | + | |
243 | 252 | | |
244 | 253 | | |
245 | 254 | | |
246 | | - | |
| 255 | + | |
247 | 256 | | |
248 | 257 | | |
249 | 258 | | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
| 259 | + | |
| 260 | + | |
257 | 261 | | |
258 | 262 | | |
259 | 263 | | |
| |||
264 | 268 | | |
265 | 269 | | |
266 | 270 | | |
267 | | - | |
| 271 | + | |
268 | 272 | | |
269 | | - | |
270 | | - | |
271 | | - | |
272 | | - | |
273 | | - | |
274 | | - | |
| 273 | + | |
| 274 | + | |
275 | 275 | | |
276 | 276 | | |
277 | 277 | | |
| |||
285 | 285 | | |
286 | 286 | | |
287 | 287 | | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
288 | 379 | | |
289 | 380 | | |
290 | 381 | | |
291 | 382 | | |
292 | 383 | | |
293 | 384 | | |
294 | 385 | | |
295 | | - | |
296 | | - | |
297 | | - | |
298 | 386 | | |
299 | 387 | | |
300 | 388 | | |
| |||
304 | 392 | | |
305 | 393 | | |
306 | 394 | | |
307 | | - | |
308 | | - | |
| 395 | + | |
| 396 | + | |
309 | 397 | | |
310 | 398 | | |
311 | | - | |
312 | 399 | | |
313 | 400 | | |
314 | 401 | | |
| |||
Lines changed: 26 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
255 | | - | |
256 | | - | |
257 | | - | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
258 | 264 | | |
259 | 265 | | |
260 | 266 | | |
| |||
299 | 305 | | |
300 | 306 | | |
301 | 307 | | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
302 | 311 | | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
303 | 316 | | |
304 | 317 | | |
305 | 318 | | |
| |||
470 | 483 | | |
471 | 484 | | |
472 | 485 | | |
473 | | - | |
| 486 | + | |
| 487 | + | |
474 | 488 | | |
475 | 489 | | |
476 | | - | |
477 | | - | |
478 | 490 | | |
479 | 491 | | |
480 | 492 | | |
| |||
559 | 571 | | |
560 | 572 | | |
561 | 573 | | |
562 | | - | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | | - | |
568 | | - | |
| 574 | + | |
569 | 575 | | |
570 | 576 | | |
571 | 577 | | |
| |||
617 | 623 | | |
618 | 624 | | |
619 | 625 | | |
620 | | - | |
621 | | - | |
622 | | - | |
| 626 | + | |
623 | 627 | | |
624 | 628 | | |
625 | 629 | | |
| |||
789 | 793 | | |
790 | 794 | | |
791 | 795 | | |
792 | | - | |
793 | | - | |
794 | | - | |
795 | | - | |
796 | | - | |
797 | | - | |
| 796 | + | |
| 797 | + | |
| 798 | + | |
| 799 | + | |
| 800 | + | |
| 801 | + | |
798 | 802 | | |
0 commit comments