Skip to content

Commit fe7aebe

Browse files
committed
update snapshots timings as per review
1 parent faf1694 commit fe7aebe

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

cardano-db-sync/src/Cardano/DbSync/Config/Types.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ data JsonTypeConfig
281281
deriving (Eq, Show)
282282

283283
data SnapshotIntervalConfig = SnapshotIntervalConfig
284-
{ sicFollowing :: !Word64
284+
{ sicNearTipEpoch :: !Word64
285285
, sicLagging :: !Word64
286286
}
287287
deriving (Eq, Show)
@@ -739,20 +739,20 @@ instance FromJSON JsonTypeConfig where
739739
instance ToJSON SnapshotIntervalConfig where
740740
toJSON cfg =
741741
Aeson.object
742-
[ "following" .= sicFollowing cfg
742+
[ "near_tip_epoch" .= sicNearTipEpoch cfg
743743
, "lagging" .= sicLagging cfg
744744
]
745745

746746
instance FromJSON SnapshotIntervalConfig where
747747
parseJSON = Aeson.withObject "snapshot_interval" $ \obj ->
748748
SnapshotIntervalConfig
749-
<$> obj .: "following"
750-
<*> obj .: "lagging"
749+
<$> obj .:? "near_tip_epoch" .!= sicNearTipEpoch def
750+
<*> obj .:? "lagging" .!= sicLagging def
751751

752752
instance Default SnapshotIntervalConfig where
753753
def =
754754
SnapshotIntervalConfig
755-
{ sicFollowing = 500 -- Every 500 blocks when near tip
755+
{ sicNearTipEpoch = 580 -- Epoch threshold to consider being near tip
756756
, sicLagging = 100000 -- Every 100,000 blocks when syncing (less frequent)
757757
}
758758

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ mkHasLedgerEnv trce protoInfo dir nw systemStart syncOptions = do
186186
, leNetwork = nw
187187
, leSystemStart = systemStart
188188
, leAbortOnPanic = soptAbortOnInvalid syncOptions
189-
, leSnapshotEveryFollowing = sicFollowing $ soptSnapshotInterval syncOptions
189+
, leSnapshotNearTipEpoch = sicNearTipEpoch $ soptSnapshotInterval syncOptions
190190
, leSnapshotEveryLagging = sicLagging $ soptSnapshotInterval syncOptions
191191
, leInterpreter = intervar
192192
, leStateVar = svar
@@ -218,7 +218,8 @@ readStateUnsafe env = do
218218
applyBlockAndSnapshot :: HasLedgerEnv -> CardanoBlock -> Bool -> IO (ApplyResult, Bool)
219219
applyBlockAndSnapshot ledgerEnv blk isCons = do
220220
(oldState, appResult) <- applyBlock ledgerEnv blk
221-
tookSnapshot <- storeSnapshotAndCleanupMaybe ledgerEnv oldState appResult (blockNo blk) isCons (isSyncedWithinSeconds (apSlotDetails appResult) 600)
221+
-- 864000 seconds = 10 days; consider synced "near tip" if within 10 days of current time
222+
tookSnapshot <- storeSnapshotAndCleanupMaybe ledgerEnv oldState appResult (blockNo blk) isCons (isSyncedWithinSeconds (apSlotDetails appResult) 864000)
222223
pure (appResult, tookSnapshot)
223224

224225
-- The function 'tickThenReapply' does zero validation, so add minimal validation ('blockPrevHash'
@@ -330,8 +331,8 @@ storeSnapshotAndCleanupMaybe env oldState appResult blkNo isCons syncState =
330331
Just newEpoch
331332
| newEpochNo <- unEpochNo (Generic.neEpoch newEpoch)
332333
, newEpochNo > 0
333-
, -- Snapshot every epoch when near tip, every 10 epochs when lagging, or always for epoch >= 580
334-
(isCons && syncState == SyncFollowing) || (newEpochNo `mod` 10 == 0) || newEpochNo >= 580 ->
334+
, -- Snapshot every epoch when near tip, every 10 epochs when lagging, or always for epoch >= threshold
335+
(isCons && syncState == SyncFollowing) || (newEpochNo `mod` 10 == 0) || newEpochNo >= leSnapshotNearTipEpoch env ->
335336
do
336337
-- TODO: Instead of newEpochNo - 1, is there any way to get the epochNo from 'lssOldState'?
337338
liftIO $ saveCleanupState env oldState (Just $ EpochNo $ newEpochNo - 1)
@@ -346,8 +347,8 @@ storeSnapshotAndCleanupMaybe env oldState appResult blkNo isCons syncState =
346347
timeToSnapshot :: SyncState -> BlockNo -> Bool
347348
timeToSnapshot syncSt bNo =
348349
case (syncSt, unBlockNo bNo) of
349-
(SyncFollowing, bno) -> bno `mod` leSnapshotEveryFollowing env == 0
350-
(SyncLagging, _) -> False
350+
(SyncFollowing, _) -> False -- No block-based snapshots when following
351+
(SyncLagging, bno) -> bno `mod` leSnapshotEveryLagging env == 0
351352

352353
saveCurrentLedgerState :: HasLedgerEnv -> CardanoLedgerState -> Maybe EpochNo -> IO ()
353354
saveCurrentLedgerState env lState mEpochNo = do

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ data HasLedgerEnv = HasLedgerEnv
6767
, leNetwork :: !Ledger.Network
6868
, leSystemStart :: !SystemStart
6969
, leAbortOnPanic :: !Bool
70-
, leSnapshotEveryFollowing :: !Word64
70+
, leSnapshotNearTipEpoch :: !Word64
7171
, leSnapshotEveryLagging :: !Word64
7272
, leInterpreter :: !(StrictTVar IO (Strict.Maybe CardanoInterpreter))
7373
, leStateVar :: !(StrictTVar IO (Strict.Maybe LedgerDB))

doc/configuration.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -616,17 +616,17 @@ Snapshot Interval Properties:
616616

617617
| Property | Type | Required | Default |
618618
| :------------------------------- | :-------- | :------- | :------ |
619-
| [following](#following) | `integer` | Optional | 500 |
619+
| [near\_tip\_epoch](#near-tip-epoch) | `integer` | Optional | 580 |
620620
| [lagging](#lagging) | `integer` | Optional | 100000 |
621621

622-
### Following
622+
### Near Tip Epoch
623623

624-
`snapshot_interval.following`
624+
`snapshot_interval.near_tip_epoch`
625625

626626
* Type: `integer`
627-
* Default: `500`
627+
* Default: `580`
628628

629-
Number of blocks between snapshots when db-sync is near the tip of the chain (within approximately 10 minutes). More frequent snapshots when following the tip ensure faster recovery from rollbacks.
629+
Epoch threshold used to determine snapshot behavior. When syncing reaches this epoch or later, db-sync is considered to be approaching or at the current tip of the chain. Combined with time-based detection (within 10 days of current time), this ensures snapshots are taken every epoch when near the tip for fast rollback recovery. During earlier epochs or when syncing behind, snapshots are taken every 10 epochs or according to the `lagging` block interval.
630630

631631
### Lagging
632632

@@ -635,23 +635,24 @@ Number of blocks between snapshots when db-sync is near the tip of the chain (wi
635635
* Type: `integer`
636636
* Default: `100000`
637637

638-
Number of blocks between snapshots when db-sync is syncing and significantly behind the tip of the chain. Less frequent snapshots during initial sync improves performance by reducing expensive disk operations.
638+
Number of blocks between snapshots when db-sync is syncing and significantly behind the tip of the chain (more than 10 days behind current time). Less frequent snapshots during initial sync improves performance by reducing expensive disk operations.
639639

640640
### Example
641641

642642
```json
643643
{
644644
"snapshot_interval": {
645-
"following": 500,
645+
"near_tip_epoch": 580,
646646
"lagging": 100000
647647
}
648648
}
649649
```
650650

651651
### Performance Considerations
652652

653-
- **Smaller `following` value**: Faster recovery from rollbacks when near the tip, but more frequent disk writes
653+
- **Lower `near_tip_epoch` value**: Start taking frequent epoch-based snapshots earlier in the chain history
654+
- **Higher `near_tip_epoch` value**: Delay frequent snapshots until later in the chain, improving sync speed for longer
654655
- **Larger `lagging` value**: Faster initial sync with reduced IOPS, but slower recovery if rollback is needed during sync
655-
- **Recommended for initial sync**: Use a large `lagging` value (50000-100000) to maximise sync speed
656-
- **Recommended when near tip**: Use a small `following` value (500-1000) to enable quick rollback recovery
656+
- **Recommended for initial sync**: Use a large `lagging` value (100000+) and appropriate `near_tip_epoch` to maximize sync speed
657+
- **Near tip detection**: Automatically switches to epoch-based snapshots when within 10 days of current time, regardless of epoch number
657658

0 commit comments

Comments
 (0)