Skip to content

Commit

Permalink
fix: revert PR-5520 (#5592)
Browse files Browse the repository at this point in the history
* Revert "refactor: change archiving strategy to always store last finalized (#5520)"

This reverts commit 4fd72b2.

* chore: add more log to StatesArchiver
  • Loading branch information
twoeths committed Jun 6, 2023
1 parent 0207d66 commit 5f4ca9e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 153 deletions.
53 changes: 25 additions & 28 deletions packages/beacon-node/src/chain/archiver/archiveStates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,49 +35,46 @@ export class StatesArchiver {
/**
* Persist states every some epochs to
* - Minimize disk space, storing the least states possible
* - Minimize the sync progress lost on unexpected crash, storing last finalized always
* - Minimize the sync progress lost on unexpected crash, storing temp state every few epochs
*
* At epoch `e` there will be states peristed at intervals of `PERSIST_STATE_EVERY_EPOCHS` = 32
* and one at `PERSIST_TEMP_STATE_EVERY_EPOCHS` = 1024
* ```
* | | | .
* epoch - 1024*2 epoch - 1024 epoch - 32 epoch
* ```
*
* An extra last finalized state is stored which might be cleaned up in next archiving cycle
* if there is already a previous state in the `PERSIST_STATE_EVERY_EPOCHS` window
*/
async maybeArchiveState(finalized: CheckpointWithHex): Promise<void> {
const lastStoredSlot = await this.db.stateArchive.lastKey();
const lastStoredEpoch = computeEpochAtSlot(lastStoredSlot ?? 0);
const {archiveStateEpochFrequency} = this.opts;

// If the new big window has started, we need to cleanup on the big interval archiveStateEpochFrequency(1024)
// else we need to cleanup within the small PERSIST_TEMP_STATE_EVERY_EPOCHS interval
const frequency =
Math.floor(lastStoredEpoch / archiveStateEpochFrequency) <
Math.floor(finalized.epoch / archiveStateEpochFrequency)
? archiveStateEpochFrequency
: PERSIST_TEMP_STATE_EVERY_EPOCHS;
if (finalized.epoch - lastStoredEpoch > Math.min(PERSIST_TEMP_STATE_EVERY_EPOCHS, archiveStateEpochFrequency)) {
await this.archiveState(finalized);

// Only check the current and previous intervals
const minEpoch = Math.max(
0,
(Math.floor(finalized.epoch / archiveStateEpochFrequency) - 1) * archiveStateEpochFrequency
);

// Only check the current and previous intervals
const minEpoch = Math.max(0, (Math.floor(finalized.epoch / frequency) - 1) * frequency);
const storedStateSlots = await this.db.stateArchive.keys({
lt: computeStartSlotAtEpoch(finalized.epoch),
gte: computeStartSlotAtEpoch(minEpoch),
});

const storedStateSlots = await this.db.stateArchive.keys({
lt: computeStartSlotAtEpoch(finalized.epoch),
gte: computeStartSlotAtEpoch(minEpoch),
});
const stateSlotsToDelete = computeStateSlotsToDelete(storedStateSlots, frequency);
const statesSlotsToDelete = computeStateSlotsToDelete(storedStateSlots, archiveStateEpochFrequency);
if (statesSlotsToDelete.length > 0) {
await this.db.stateArchive.batchDelete(statesSlotsToDelete);
}

// 1. Always archive latest state so as to allow restarts to happen from last finalized.
// It will be cleaned up next cycle even if it results into two states for this interval
//
// 2. Delete the states only after storing the latest finalized so as to prevent any race in non
// availability of finalized state to boot from
//
await this.archiveState(finalized);
if (stateSlotsToDelete.length > 0) {
await this.db.stateArchive.batchDelete(stateSlotsToDelete);
// More logs to investigate the rss spike issue https://github.com/ChainSafe/lodestar/issues/5591
this.logger.verbose("Archived state completed", {
finalizedEpoch: finalized.epoch,
minEpoch,
storedStateSlots: storedStateSlots.join(","),
statesSlotsToDelete: statesSlotsToDelete.join(","),
});
}
}

Expand All @@ -92,7 +89,7 @@ export class StatesArchiver {
}
await this.db.stateArchive.put(finalizedState.slot, finalizedState);
// don't delete states before the finalized state, auto-prune will take care of it
this.logger.verbose("Archive states completed", {finalizedEpoch: finalized.epoch});
this.logger.verbose("Archived finalized state", {finalizedEpoch: finalized.epoch});
}
}

Expand Down
125 changes: 0 additions & 125 deletions packages/beacon-node/test/unit/chain/archive/maybeArchiveState.test.ts

This file was deleted.

0 comments on commit 5f4ca9e

Please sign in to comment.