Skip to content

Commit 45d54e3

Browse files
Fix LSM snapshot restoration by resolving double path nesting
The LSM session was being initialised with the full lsmPath as both the HasBlockIO root and the session's FsPath inside it, producing a doubled on-disk layout like: <state-dir>/lsm/<state-dir>/lsm/{active,snapshots,...} instead of: <state-dir>/lsm/{active,snapshots,...}. Effect on snapshots: scripts/postgresql-setup.sh --create-snapshot looks for the LSM tree at <state-dir>/lsm/snapshots/<slot>, didn't find it because of the doubled path, silently fell back to the InMemory branch, and produced a tarball missing the LSM tree files. On --restore-snapshot + db-sync startup the snapshot couldn't be loaded (ErrSnapshotDoesNotExist) and sync replayed from genesis. Fix: Pass mkFsPath [] as the session's FsPath so it's rooted at the HasBlockIO root directly. Also harden the snapshot script to abort with a clear error when an lsm/ subtree exists but no LSM snapshot is found at the expected path, instead of producing an incomplete tarball. Files staged for commit: - cardano-db-sync/src/Cardano/DbSync/Ledger/State.hs - scripts/postgresql-setup.sh
1 parent d323ee3 commit 45d54e3

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

cardano-db-sync/src/Cardano/DbSync/Ledger/State.hs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ import Ouroboros.Network.Block (HeaderHash, pointSlot)
118118
import System.FS.API (SomeHasFS (..), mkFsPath)
119119
import System.FS.API.Types (MountPoint (..))
120120
import System.FS.IO (ioHasFS)
121-
import System.FilePath (splitDirectories, (</>))
121+
import System.FilePath ((</>))
122122
import System.Mem (performMajorGC)
123123
import System.Random (genWord64, newStdGen)
124124

@@ -227,7 +227,13 @@ mkHasLedgerEnv trce protoInfo dir nw maxLovelaceSupply systemStart syncOptions b
227227
LedgerBackendLSM mPath -> do
228228
let lsmPath = fromMaybe (unLedgerStateDir dir </> "lsm") mPath
229229
salt <- fst . genWord64 <$> newStdGen
230-
let args = LSM.LSMArgs (mkFsPath $ splitDirectories lsmPath) salt (LSM.stdMkBlockIOFS lsmPath)
230+
-- The HasBlockIO is rooted at lsmPath, so the session's FsPath inside it
231+
-- must be the empty path. Passing the full lsmPath here causes the LSM
232+
-- session to be created at <lsmPath>/<lsmPath>, which breaks snapshot
233+
-- bundling in scripts/postgresql-setup.sh (it falls back to the InMemory
234+
-- branch and ships an incomplete tarball; on restore db-sync then can't
235+
-- find the snapshot and replays from genesis).
236+
let args = LSM.LSMArgs (mkFsPath []) salt (LSM.stdMkBlockIOFS lsmPath)
231237
res <-
232238
runWithTempRegistry $
233239
(,())

scripts/postgresql-setup.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ function create_snapshot {
176176
# The active/ directory is not needed — it gets cleared on session restore.
177177
tar czf "${tmp_dir}/${ledger_dir_name}.tar.gz" \
178178
-C "${state_dir}" "${ledger_dir_name}" "lsm/snapshots/${ledger_dir_name}" "lsm/metadata"
179+
elif [ -d "${state_dir}/lsm" ]; then
180+
# State directory contains an lsm/ subtree but the expected snapshot path is missing.
181+
# Refuse to silently fall back to the InMemory branch — the resulting tarball would
182+
# exclude the LSM tree files and restoration would replay from genesis.
183+
echo "ERROR: ${state_dir}/lsm exists but no LSM snapshot found at ${lsm_snap_dir}." >&2
184+
echo " The LSM session may be using a non-standard layout. Aborting to avoid" >&2
185+
echo " producing an incomplete snapshot." >&2
186+
rm "${recursive}" "${force}" "${tmp_dir}"
187+
exit 1
179188
else
180189
echo "Detected InMemory backend snapshot at slot ${ledger_dir_name}"
181190
# InMemory backend: just the ledger snapshot directory

0 commit comments

Comments
 (0)