Skip to content
This repository was archived by the owner on Jan 20, 2026. It is now read-only.

[MemIAVL] Create snapshot whenever height diff exceed interval#109

Merged
yzang2019 merged 6 commits intomainfrom
yzang/snapshot-creation-after-restart
Sep 30, 2025
Merged

[MemIAVL] Create snapshot whenever height diff exceed interval#109
yzang2019 merged 6 commits intomainfrom
yzang/snapshot-creation-after-restart

Conversation

@yzang2019
Copy link
Copy Markdown
Collaborator

@yzang2019 yzang2019 commented Sep 24, 2025

Describe your changes and provide context

Problem:
Currently, if snapshot interval is set to 10k, and previous snapshot is created at height 10000, when node got restart at height 20001, it will not start the next snapshot creation until height 30000. This will actually lead to higher snapshot gaps which will then lead to higher restart time.

Instead of using fixed window interval, we actually switched to a new snapshot policy where the next snapshot creation will happen immediately at current height after commit as long as the current height - last snapshot height >= snapshot interval.

Testing performed to validate your change

Added unit test + Tested on RPC node

Comment thread sc/memiavl/db_test.go Outdated
Comment thread sc/memiavl/db_test.go Outdated
time.Sleep(10 * time.Millisecond)
require.Nil(t, db.snapshotRewriteChan, "rewrite should not start at height %d", i)
// snapshot version should remain 0 until rewrite
require.Equal(t, int64(0), db.MultiTree.SnapshotVersion())
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

You can use EqualValues to avoid casting to int64. Ditto in other places.

Comment thread sc/memiavl/db_test.go Outdated
Comment thread sc/memiavl/db_test.go Outdated
require.NoError(t, err)

// helper to commit one change to bump height
commitOnce := func(key, val string) int64 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Generally I recommend refactoring this type of function into one that laso takes t, with its name prefixed with require, e.g. requireCommitOnce(t *testing.T, key,val string), then call t.Helper() in its first line.

That then makes up an executable way to document intent and would play nice with the go testing toolkit.

Comment thread sc/memiavl/db_test.go
// After completion, snapshot version should be 6
require.Equal(t, int64(6), db.MultiTree.SnapshotVersion())

require.NoError(t, db.Close())
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Move this into t.Cleanup to make sure it's executed after test ends (whether it fails or not).

Comment thread sc/memiavl/snapshot_test.go Outdated
import (
"context"
"errors"
"os"
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The check failures here are genuine. Please clean up imports.

Comment thread sc/memiavl/snapshot_test.go Outdated
Comment thread sc/memiavl/db.go Outdated
if err := db.rewriteSnapshotBackground(); err != nil {
db.logger.Error("failed to rewrite snapshot in background", "err", err)
// create snapshot when current height - last snapshot height > interval
if db.snapshotInterval > 0 && height-db.MultiTree.SnapshotVersion() > int64(db.snapshotInterval) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thinking out loud, do we need to sanity check the height? For example could a bad hight result in a snapshot rewrite by accident? Might be worth clamping the range of height etc.

* main:
  MemIAVL should only only keep 1 snapshot (#110)
  Integrate UCI go lint (#111)
@codecov
Copy link
Copy Markdown

codecov Bot commented Sep 27, 2025

Codecov Report

❌ Patch coverage is 69.23077% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 64.08%. Comparing base (37c20d5) to head (0d913f8).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
sc/memiavl/db.go 69.23% 2 Missing and 2 partials ⚠️

❌ Your patch status has failed because the patch coverage (69.23%) is below the target coverage (70.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #109      +/-   ##
==========================================
+ Coverage   64.03%   64.08%   +0.05%     
==========================================
  Files          28       28              
  Lines        4048     4057       +9     
==========================================
+ Hits         2592     2600       +8     
  Misses       1161     1161              
- Partials      295      296       +1     
Files with missing lines Coverage Δ
sc/memiavl/db.go 60.74% <69.23%> (-0.43%) ⬇️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@yzang2019 yzang2019 requested a review from masih September 27, 2025 00:28
@yzang2019 yzang2019 merged commit 00d174d into main Sep 30, 2025
7 of 8 checks passed
@yzang2019 yzang2019 deleted the yzang/snapshot-creation-after-restart branch September 30, 2025 18:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants