Skip to content

Commit 5703ae2

Browse files
authored
e2e: automatically prune old app snapshots (tendermint#7034)
This PR tackles the case of using the e2e application in a long lived testnet. The application continually saves snapshots (usually every 100 blocks) which after a while bloats the size of the application. This PR prunes older snapshots so that only the most recent 10 snapshots remain.
1 parent 03ad7d6 commit 5703ae2

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

test/e2e/app/app.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ func (app *Application) Commit() abci.ResponseCommit {
184184
panic(err)
185185
}
186186
app.logger.Info("Created state sync snapshot", "height", snapshot.Height)
187+
err = app.snapshots.Prune(maxSnapshotCount)
188+
if err != nil {
189+
app.logger.Error("Failed to prune snapshots", "err", err)
190+
}
187191
}
188192
retainHeight := int64(0)
189193
if app.cfg.RetainBlocks > 0 {

test/e2e/app/snapshots.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import (
1616

1717
const (
1818
snapshotChunkSize = 1e6
19+
20+
// Keep only the most recent 10 snapshots. Older snapshots are pruned
21+
maxSnapshotCount = 10
1922
)
2023

2124
// SnapshotStore stores state sync snapshots. Snapshots are stored simply as
@@ -105,6 +108,27 @@ func (s *SnapshotStore) Create(state *State) (abci.Snapshot, error) {
105108
return snapshot, nil
106109
}
107110

111+
// Prune removes old snapshots ensuring only the most recent n snapshots remain
112+
func (s *SnapshotStore) Prune(n int) error {
113+
s.Lock()
114+
defer s.Unlock()
115+
// snapshots are appended to the metadata struct, hence pruning removes from
116+
// the front of the array
117+
i := 0
118+
for ; i < len(s.metadata)-n; i++ {
119+
h := s.metadata[i].Height
120+
if err := os.Remove(filepath.Join(s.dir, fmt.Sprintf("%v.json", h))); err != nil {
121+
return err
122+
}
123+
}
124+
125+
// update metadata by removing the deleted snapshots
126+
pruned := make([]abci.Snapshot, len(s.metadata[i:]))
127+
copy(pruned, s.metadata[i:])
128+
s.metadata = pruned
129+
return nil
130+
}
131+
108132
// List lists available snapshots.
109133
func (s *SnapshotStore) List() ([]*abci.Snapshot, error) {
110134
s.RLock()

test/e2e/generator/generate.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var (
5353
e2e.StateSyncRPC: 45,
5454
}
5555
nodePersistIntervals = uniformChoice{0, 1, 5}
56-
nodeSnapshotIntervals = uniformChoice{0, 3}
56+
nodeSnapshotIntervals = uniformChoice{0, 5}
5757
nodeRetainBlocks = uniformChoice{0, 2 * int(e2e.EvidenceAgeHeight), 4 * int(e2e.EvidenceAgeHeight)}
5858
nodePerturbations = probSetChoice{
5959
"disconnect": 0.1,

0 commit comments

Comments
 (0)