Skip to content

fix(flatkv): retain 72 checkpoints instead of mirroring memIAVL's count - #4145

Closed
blindchaser wants to merge 2 commits into
mainfrom
yiren/flatkv-checkpoint-retention
Closed

fix(flatkv): retain 72 checkpoints instead of mirroring memIAVL's count#4145
blindchaser wants to merge 2 commits into
mainfrom
yiren/flatkv-checkpoint-retention

Conversation

@blindchaser

Copy link
Copy Markdown
Contributor

What this changes

FlatKV stops taking its retention count from memIAVL and keeps its own default of
72 old checkpoints. The snapshot interval is still mirrored.

before after
FlatKV checkpoints retained 1 72
Guaranteed reach 10,000 blocks (74 min) 720,000 blocks (~89 h)
Extra disk at mainnet state size ~20 GiB

Why

alignFlatKVSnapshotWithMemIAVL overwrote both of FlatKV's snapshot knobs from
memIAVL's sc-* keys, and sc-keep-recent defaults to 1. FlatKV therefore kept one
old checkpoint. ExternalPruning is false on the composite path, so nothing else
extended that.

Below FlatKV's reach, migrate-evm-status, dump-flatkv and a cross-backend digest
cannot open a past version, and a rollback has no base snapshot to rewind to. The EVM
migration window became unobservable 74 minutes after passing.

The shared knob cannot express the setting we want, and this is not a matter of cost.
Measured on the mainnet-sized pacific-1 shadow node, post-H_done, at the tip:

one further retained version
memIAVL 56,782 MiB (snapshots are independent full copies)
FlatKV ~286 MiB (261 MiB of pinned SSTs + ~25 MiB of state WAL)

A Pebble checkpoint hardlinks its SSTs, so an old checkpoint only pins the bytes
compaction has since obsoleted. The factor is about 200. At the depth FlatKV wants,
the memIAVL side of a shared count would need 3.68 TiB on a 2 TiB volume, so the
knob has to be split before any number can be chosen.

Why 72

72 checkpoints at the 10,000-block interval is a guaranteed reach of 720,000 blocks,
about 89 hours at the shadow node's measured 2.247 blocks/s, against an 85-hour drain
at NumKeysToMigratePerBlock = 1024. It covers the whole migration window with margin.

The cost is linear in depth, not sublinear: each older checkpoint pins exactly the
files obsoleted during its own interval, and those sets are disjoint by construction.
It only saturates at ~93 checkpoints, where the pinned bytes reach the 24,241 MiB of
live SSTs.

How operators get it

The production store is built by parseSCConfigs, which starts from the in-code
defaults and reads no FlatKV retention key, so raising the default reaches every
existing node with no app.toml edit. Nodes carrying a stale explicit
state-commit.flatkv.snapshot-keep-recent from the pre-#3745 template are also
unaffected, because that key never reaches a store.

Making the key actually readable is a deliberate follow-up rather than part of this
change, so the v6.7 backport carries no new config read.

Testing

  • TestAlignFlatKVSnapshotIntervalWithMemIAVL replaces the test that pinned the
    both-values mirror; it now pins that the interval is mirrored, a zero interval is
    still healed before assignment, and neither a set nor a zero memIAVL keep-recent
    reaches FlatKV.
  • TestFlatKVDefaultRetentionSpansTheMigrationWindow guards the reach itself, so a
    later depth change that no longer spans the window fails rather than passing quietly.
  • ./sei-db/state_db/sc/composite/, ./sei-db/state_db/sc/flatkv/...,
    ./sei-db/config/, ./sei-cosmos/server/config/, ./app/ all pass.
  • make fmtcheck and make dblint are clean.

Backport

release/v6.7 is where this is needed, and it is filed separately because main has
since removed the golden config suite and the FlatKV default constants, so the two
diffs are not identical. v6.7.0-rc1 is already cut, so the backport implies an rc2.

FlatKV took both its snapshot interval and its retention count from
memIAVL's sc-* keys, and sc-keep-recent defaults to 1. FlatKV therefore
kept one old checkpoint, a guaranteed reach of 10,000 blocks, or 74
minutes at mainnet's block rate. Below that reach migrate-evm-status,
dump-flatkv and a cross-backend digest cannot open a past version, and a
rollback has no base snapshot to rewind to, so the EVM migration window
was unobservable within an hour of passing.

A single shared count cannot serve both backends, because it is a disk
decision and not a cadence. Measured on a mainnet-sized shadow node, one
further retained memIAVL snapshot costs 56,782 MiB, since memIAVL
snapshots are independent full copies, while one further FlatKV
checkpoint costs about 286 MiB, since Pebble checkpoints hardlink their
SSTs and so only pin the bytes compaction has since obsoleted. The depth
FlatKV wants would ask memIAVL for more than the volume holds.

Mirror only the interval, which must match because a rollback rewinds
both backends and a cross-backend digest opens both at one height, and
give FlatKV its own retention default of 72. That is a guaranteed reach
of 720,000 blocks, about 89 hours, against an 85-hour drain at
K=1024, for roughly 20 GiB.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Sep 11, 2026

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review Completed 2026-09-11T14:41:39.876760Z 7f6998f PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@cursor

cursor Bot commented Sep 11, 2026

Copy link
Copy Markdown

PR Summary

Medium Risk
Changes default state-commit disk retention and historical reach for FlatKV on upgrade; behavior is intentional but operators should expect modest extra disk use and longer checkpoint history.

Overview
FlatKV checkpoint retention is decoupled from memIAVL so nodes keep 72 old Pebble checkpoints by default instead of inheriting memIAVL’s sc-keep-recent (typically 1). Snapshot interval still follows sc-snapshot-interval via the renamed alignFlatKVSnapshotIntervalWithMemIAVL, which no longer copies SnapshotKeepRecent.

That extends how far back FlatKV can serve historical versions—roughly 720,000 blocks (~89h at default interval vs ~74 minutes before)—for migrate-evm-status, dump-flatkv, cross-backend digests, and rollbacks, at an estimated ~20 GiB extra disk on mainnet-sized nodes. No app.toml change is required; stale state-commit.flatkv.snapshot-keep-recent still does not affect the production store path.

Tests and config comments are updated to lock in interval-only mirroring and guard default retention against the EVM migration window.

Reviewed by Cursor Bugbot for commit aabd353. Bugbot is set up for automated code reviews on this repo. Configure here.

@github-actions

github-actions Bot commented Sep 11, 2026

Copy link
Copy Markdown

The latest Buf updates on your PR. Results from workflow Buf / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedSep 11, 2026, 5:38 PM

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7f6998f085

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +18 to +22
// It is this deep because a FlatKV checkpoint is nearly free. Checkpoints
// hardlink their SSTs, so one only costs the bytes compaction has since made
// obsolete: measured at mainnet state size, 261 MiB of pinned SSTs plus about
// 25 MiB of retained state WAL. 72 of them is roughly 20 GiB. The cost is linear
// in depth, because each older checkpoint pins exactly the files obsoleted during

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Rewrite the constant Godoc to describe only the constant

This new Godoc devotes multiple paragraphs to migration rationale, mainnet measurements, hardlink mechanics, and affected commands rather than documenting what DefaultSnapshotKeepRecent represents. The repository explicitly requires Godocs to describe their subject in one or two sentences and keep rationale, mechanisms, and surrounding architecture out, so this should be reduced to the retention semantics and the rationale moved inline or omitted.

AGENTS.md reference: AGENTS.md:L79-L90

Useful? React with 👍 / 👎.

Comment on lines +55 to +58
// It is not mirrored from memIAVL's sc-keep-recent, and the production store
// reads no app.toml key for it, so a node runs the DefaultConfig value. See
// composite.alignFlatKVSnapshotIntervalWithMemIAVL for why the two backends
// share an interval but not a retention count.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Remove configuration wiring from the field Godoc

The added paragraph documents which production reader and composite helper configure this field, rather than what SnapshotKeepRecent means to callers. That system-level wiring is expressly excluded from field Godocs by the repository guidance and will also become stale when configuration paths change; keep the existing retention and pruning semantics here and place wiring rationale at the relevant implementation site instead.

AGENTS.md reference: AGENTS.md:L79-L90

Useful? React with 👍 / 👎.

@codecov

codecov Bot commented Sep 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 60.67%. Comparing base (05987fe) to head (aabd353).
⚠️ Report is 7 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #4145      +/-   ##
==========================================
- Coverage   61.58%   60.67%   -0.92%     
==========================================
  Files        2196     2085     -111     
  Lines      193825   181222   -12603     
==========================================
- Hits       119366   109949    -9417     
+ Misses      62886    60761    -2125     
+ Partials    11573    10512    -1061     
Flag Coverage Δ
sei-chain-pr 91.13% <ø> (?)
sei-db 69.80% <ø> (ø)
sei-db-state-db ?
sei-db-state-db-pr 71.16% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sei-cosmos/server/config/config.go 98.08% <ø> (ø)
sei-db/state_db/sc/composite/store.go 68.94% <100.00%> (-0.05%) ⬇️
sei-db/state_db/sc/flatkv/config/config.go 78.12% <100.00%> (ø)

... and 111 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@seidroid seidroid Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting FlatKV's retention count from memIAVL's sc-keep-recent is the right correction and is applied at the single choke point (alignFlatKVSnapshotIntervalWithMemIAVL), with the config/doc trail updated consistently; production reads of the key were verified to be unaffected. Two non-blocking concerns: the surviving zero-interval test became tautological after the deleted subtest, and the ~20 GiB disk estimate is measured at the tip rather than during the migration drain the depth is sized for.

Findings: 0 blocking | 4 non-blocking | 2 posted inline

Blockers

  • None at the file/PR level.

Non-blocking

  • [suggestion] The PR's motivation table lists "a rollback" as gaining 720,000 blocks of reach, but CompositeCommitStore.Rollback (sei-db/state_db/sc/composite/store.go:1374) rewinds memIAVL first, and memIAVL still runs at sc-keep-recent = 1. On a composite node seid rollback therefore still fails at memIAVL after ~10-20k blocks; only FlatKV-only rollback and the FlatKV-only read paths (migrate-evm-status, dump-flatkv, a FlatKV digest) actually gain the deeper reach. The CHANGELOG wording ("a FlatKV rollback") is technically precise, but worth stating the composite limit explicitly so operators don't size recovery expectations from it.
  • [suggestion] The new DefaultSnapshotKeepRecent godoc (sei-db/state_db/sc/flatkv/config/config.go:13-28) is three paragraphs of rationale — cost measurements, the linearity argument, why reach matters — which is what AGENTS.md's godoc rules ask to keep out ("Explain WHAT, not WHY or HOW", "Multi-paragraph godocs are rare"). The same applies to the expanded alignFlatKVSnapshotIntervalWithMemIAVL doc. Suggest keeping the godoc to what the constant is and its effective reach, and moving the measurement/cost argument to an inline comment at the constant or leaving it in the CHANGELOG entry, which already carries it.
  • 2 suggestion(s)/nit(s) flagged inline on specific lines.

// A raw 0 would disable FlatKV auto-snapshots; instead FlatKV mirrors the
// value FillDefaults will heal memIAVL's interval to.
alignFlatKVSnapshotWithMemIAVL(&cfg)
alignFlatKVSnapshotIntervalWithMemIAVL(&cfg)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[suggestion] This subtest no longer proves anything after the deleted "an explicit FlatKV override loses to memIAVL's healed default" case. It starts from DefaultStateCommitConfig(), so cfg.FlatKVConfig.SnapshotInterval is already 10000, and memiavl.DefaultSnapshotInterval is also 10000 (sei-db/state_db/sc/memiavl/config.go:4) — the assertion passes even if alignFlatKVSnapshotIntervalWithMemIAVL did nothing at all on a zero memIAVL interval.

The deleted subtest was the only one covering resolve-then-assign versus skip-on-zero, i.e. the upgrade case where an old app.toml still pins an explicit state-commit.flatkv.snapshot-interval while sc-snapshot-interval is 0. That scenario is still live for the interval even though the retention half of it is gone. One line restores it:

cfg.MemIAVLConfig.SnapshotInterval = 0
cfg.FlatKVConfig.SnapshotInterval = 7777 // must lose to memIAVL's healed default

// It is this deep because a FlatKV checkpoint is nearly free. Checkpoints
// hardlink their SSTs, so one only costs the bytes compaction has since made
// obsolete: measured at mainnet state size, 261 MiB of pinned SSTs plus about
// 25 MiB of retained state WAL. 72 of them is roughly 20 GiB. The cost is linear

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[suggestion] The 261 MiB / 25 MiB per-checkpoint figures are measured at the tip post-H_done, but the depth is chosen for the drain window — and both components scale with write volume, which is much higher during the drain than at the tip.

The WAL half is the clearer one: tryTruncateWAL (sei-db/state_db/sc/flatkv/snapshot.go:905) prunes below the earliest snapshot, so raising retention from 1 to 72 widens the retained WAL span from ~20k to 720k blocks. During the drain every block's changeset additionally carries NumKeysToMigratePerBlock (1024) migrated keys through s.wal.Write (store_write.go:86), so WAL bytes per interval are far above the steady-state 25 MiB. Near the end of an 85-hour drain the retained WAL approximates the whole migrated EVM dataset in changeset form. The pinned-SST half moves the same way: a bulk ingest obsoletes far more per interval than tip traffic does, so each checkpoint pins more than 261 MiB.

So peak disk during exactly the window this depth exists for is plausibly several times the ~20 GiB the doc and CHANGELOG quote. It likely still fits the 2 TiB volume, but the number operators will size from is the steady-state one. Worth either measuring one interval mid-drain and quoting the peak, or stating that 20 GiB is the post-migration steady state and the drain peak is higher.

This compounds with there being no escape hatch: parseSCConfigs (app/seidb.go:104) reads no FlatKV snapshot key, and this PR removes sc-keep-recent as the indirect lever, so an operator who runs short of disk mid-drain cannot lower the retention without a new binary.

@blindchaser

Copy link
Copy Markdown
Contributor Author

Superseded by #4158, which lands the same fix on release/v6.7 with a retention of 10 rather than 72. Closing rather than retargeting so the depth decision lives in one place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant