Skip to content

Commit

Permalink
Add a new snapshot directory config (#305)
Browse files Browse the repository at this point in the history
## Describe your changes and provide context
This change add a new config `snapshot-directory` in app.toml to
override default directory for storing state sync snapshot.

## Testing performed to validate your change
Tests are covered in sei-protocol/sei-chain#944

---------

Co-authored-by: Yiming Zang <[email protected]>
  • Loading branch information
yzang2019 and Yiming Zang authored Jun 30, 2023
1 parent 58083f6 commit 24b93d5
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ type snapshotData struct {
snapshotManager *snapshots.Manager
snapshotInterval uint64 // block interval between state sync snapshots
snapshotKeepRecent uint32 // recent state sync snapshots to keep
snapshotDirectory string // state sync snapshots directory
}

// NewBaseApp returns a reference to an initialized BaseApp. It accepts a
Expand Down
13 changes: 13 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,11 @@ func SetSnapshotKeepRecent(keepRecent uint32) func(*BaseApp) {
return func(app *BaseApp) { app.SetSnapshotKeepRecent(keepRecent) }
}

// SetSnapshotDirectory sets the snapshot directory.
func SetSnapshotDirectory(dir string) func(*BaseApp) {
return func(app *BaseApp) { app.SetSnapshotDirectory(dir) }
}

// SetSnapshotStore sets the snapshot store.
func SetSnapshotStore(snapshotStore *snapshots.Store) func(*BaseApp) {
return func(app *BaseApp) { app.SetSnapshotStore(snapshotStore) }
Expand Down Expand Up @@ -298,6 +303,14 @@ func (app *BaseApp) SetSnapshotKeepRecent(snapshotKeepRecent uint32) {
app.snapshotKeepRecent = snapshotKeepRecent
}

// SetSnapshotDirectory sets the snapshot directory.
func (app *BaseApp) SetSnapshotDirectory(dir string) {
if app.sealed {
panic("SetSnapshotDirectory() on sealed BaseApp")
}
app.snapshotDirectory = dir
}

// SetInterfaceRegistry sets the InterfaceRegistry.
func (app *BaseApp) SetInterfaceRegistry(registry types.InterfaceRegistry) {
app.interfaceRegistry = registry
Expand Down
6 changes: 6 additions & 0 deletions server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ type StateSyncConfig struct {
// SnapshotKeepRecent sets the number of recent state sync snapshots to keep.
// 0 keeps all snapshots.
SnapshotKeepRecent uint32 `mapstructure:"snapshot-keep-recent"`

// SnapshotDirectory sets the parent directory for where state sync snapshots are persisted.
// Default is emtpy which will then store under the app home directory.
SnapshotDirectory string `mapstructure:"snapshot-directory"`
}

// Config defines the server's top level configuration
Expand Down Expand Up @@ -264,6 +268,7 @@ func DefaultConfig() *Config {
StateSync: StateSyncConfig{
SnapshotInterval: 0,
SnapshotKeepRecent: 2,
SnapshotDirectory: "",
},
}
}
Expand Down Expand Up @@ -345,6 +350,7 @@ func GetConfig(v *viper.Viper) (Config, error) {
StateSync: StateSyncConfig{
SnapshotInterval: v.GetUint64("state-sync.snapshot-interval"),
SnapshotKeepRecent: v.GetUint32("state-sync.snapshot-keep-recent"),
SnapshotDirectory: v.GetString("state-sync.snapshot-directory"),
},
}, nil
}
Expand Down
5 changes: 5 additions & 0 deletions server/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ func TestSetMinimumFees(t *testing.T) {
cfg.SetMinGasPrices(sdk.DecCoins{sdk.NewInt64DecCoin("foo", 5)})
require.Equal(t, "5.000000000000000000foo", cfg.MinGasPrices)
}

func TestSetSnapshotDirectory(t *testing.T) {
cfg := DefaultConfig()
require.Equal(t, "", cfg.StateSync.SnapshotDirectory)
}
5 changes: 5 additions & 0 deletions server/config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ snapshot-interval = {{ .StateSync.SnapshotInterval }}
# snapshot-keep-recent specifies the number of recent snapshots to keep and serve (0 to keep all).
snapshot-keep-recent = {{ .StateSync.SnapshotKeepRecent }}
# snapshot-directory sets the directory for where state sync snapshots are persisted.
# default is emtpy which will then store under the app home directory same as before.
snapshot-directory = "{{ .StateSync.SnapshotDirectory }}"
`

var configTemplate *template.Template
Expand Down
1 change: 1 addition & 0 deletions server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ const (
// state sync-related flags
FlagStateSyncSnapshotInterval = "state-sync.snapshot-interval"
FlagStateSyncSnapshotKeepRecent = "state-sync.snapshot-keep-recent"
FlagStateSyncSnapshotDir = "state-sync.snapshot-directory"

// gRPC-related flags
flagGRPCOnly = "grpc-only"
Expand Down
11 changes: 8 additions & 3 deletions simapp/simd/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,16 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, t
panic(err)
}

snapshotDir := filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots")
snapshotDB, err := sdk.NewLevelDB("metadata", snapshotDir)
snapshotDirectory := cast.ToString(appOpts.Get(server.FlagStateSyncSnapshotDir))
if snapshotDirectory == "" {
snapshotDirectory = filepath.Join(cast.ToString(appOpts.Get(flags.FlagHome)), "data", "snapshots")
}

snapshotDB, err := sdk.NewLevelDB("metadata", snapshotDirectory)
if err != nil {
panic(err)
}
snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDir)
snapshotStore, err := snapshots.NewStore(snapshotDB, snapshotDirectory)
if err != nil {
panic(err)
}
Expand All @@ -298,6 +302,7 @@ func (a appCreator) newApp(logger log.Logger, db dbm.DB, traceStore io.Writer, t
baseapp.SetSnapshotStore(snapshotStore),
baseapp.SetSnapshotInterval(cast.ToUint64(appOpts.Get(server.FlagStateSyncSnapshotInterval))),
baseapp.SetSnapshotKeepRecent(cast.ToUint32(appOpts.Get(server.FlagStateSyncSnapshotKeepRecent))),
baseapp.SetSnapshotDirectory(snapshotDirectory),
baseapp.SetIAVLCacheSize(cast.ToInt(appOpts.Get(server.FlagIAVLCacheSize))),
baseapp.SetIAVLDisableFastNode(cast.ToBool(appOpts.Get(server.FlagIAVLFastNode))),
baseapp.SetCompactionInterval(cast.ToUint64(appOpts.Get(server.FlagCompactionInterval))),
Expand Down

0 comments on commit 24b93d5

Please sign in to comment.