Skip to content

Commit

Permalink
feat: add local snapshots management commands (backport cosmos#16067) (
Browse files Browse the repository at this point in the history
…cosmos#16103)

Co-authored-by: yihuang <huang@crypto.com>
Co-authored-by: marbar3778 <marbar3778@yahoo.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
  • Loading branch information
4 people authored and JeancarloBarrios committed Sep 28, 2024
1 parent 39fa80a commit 42f1adf
Show file tree
Hide file tree
Showing 15 changed files with 270 additions and 264 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (simapp) [#15903](https://github.com/cosmos/cosmos-sdk/pull/15903) Add `AppStateFnWithExtendedCbs` with moduleStateCb callback function to allow access moduleState. Note, this function is present in simtestutil from `v0.47.2+`.
* (gov) [#15979](https://github.com/cosmos/cosmos-sdk/pull/15979) Improve gov error message when failing to convert v1 proposal to v1beta1.
* (server) [#16061](https://github.com/cosmos/cosmos-sdk/pull/16061) add comet bootstrap command
* (store) [#16067](https://github.com/cosmos/cosmos-sdk/pull/16067) Add local snapshots management commands.

### Bug Fixes

Expand Down
6 changes: 3 additions & 3 deletions client/snapshot/cmd.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package snapshot

import (
"github.com/spf13/cobra"

servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/spf13/cobra"
)

// Cmd returns the snapshots group command
func Cmd[T servertypes.Application](appCreator servertypes.AppCreator[T]) *cobra.Command {
func Cmd(appCreator servertypes.AppCreator) *cobra.Command {
cmd := &cobra.Command{
Use: "snapshots",
Short: "Manage local snapshots",
Long: "Manage local snapshots",
}
cmd.AddCommand(
ListSnapshotsCmd,
Expand Down
8 changes: 3 additions & 5 deletions client/snapshot/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package snapshot
import (
"strconv"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
"github.com/spf13/cobra"
)

func DeleteSnapshotCmd() *cobra.Command {
Expand All @@ -15,7 +13,7 @@ func DeleteSnapshotCmd() *cobra.Command {
Short: "Delete a local snapshot",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
viper := client.GetViperFromCmd(cmd)
ctx := server.GetServerContextFromCmd(cmd)

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
Expand All @@ -26,7 +24,7 @@ func DeleteSnapshotCmd() *cobra.Command {
return err
}

snapshotStore, err := server.GetSnapshotStore(viper)
snapshotStore, err := server.GetSnapshotStore(ctx.Viper)
if err != nil {
return err
}
Expand Down
64 changes: 24 additions & 40 deletions client/snapshot/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@ package snapshot
import (
"archive/tar"
"compress/gzip"
"errors"
"fmt"
"io"
"os"
"strconv"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
"github.com/spf13/cobra"
)

// DumpArchiveCmd returns a command to dump the snapshot as portable archive format
Expand All @@ -22,8 +19,8 @@ func DumpArchiveCmd() *cobra.Command {
Short: "Dump the snapshot as portable archive format",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
viper := client.GetViperFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(viper)
ctx := server.GetServerContextFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(ctx.Viper)
if err != nil {
return err
}
Expand Down Expand Up @@ -51,10 +48,6 @@ func DumpArchiveCmd() *cobra.Command {
return err
}

if snapshot == nil {
return errors.New("snapshot doesn't exist")
}

bz, err := snapshot.Marshal()
if err != nil {
return err
Expand Down Expand Up @@ -85,9 +78,27 @@ func DumpArchiveCmd() *cobra.Command {

for i := uint32(0); i < snapshot.Chunks; i++ {
path := snapshotStore.PathChunk(height, uint32(format), i)
tarName := strconv.FormatUint(uint64(i), 10)
if err := processChunk(tarWriter, path, tarName); err != nil {
return err
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open chunk file %s: %w", path, err)
}
defer file.Close()

st, err := file.Stat()
if err != nil {
return fmt.Errorf("failed to stat chunk file %s: %w", path, err)
}

if err := tarWriter.WriteHeader(&tar.Header{
Name: strconv.FormatUint(uint64(i), 10),
Mode: 0o644,
Size: st.Size(),
}); err != nil {
return fmt.Errorf("failed to write chunk header to tar: %w", err)
}

if _, err := io.Copy(tarWriter, file); err != nil {
return fmt.Errorf("failed to write chunk to tar: %w", err)
}
}

Expand All @@ -107,30 +118,3 @@ func DumpArchiveCmd() *cobra.Command {

return cmd
}

func processChunk(tarWriter *tar.Writer, path, tarName string) error {
file, err := os.Open(path)
if err != nil {
return fmt.Errorf("failed to open chunk file %s: %w", path, err)
}
defer file.Close()

st, err := file.Stat()
if err != nil {
return fmt.Errorf("failed to stat chunk file %s: %w", path, err)
}

if err := tarWriter.WriteHeader(&tar.Header{
Name: tarName,
Mode: 0o644,
Size: st.Size(),
}); err != nil {
return fmt.Errorf("failed to write chunk header to tar: %w", err)
}

if _, err := io.Copy(tarWriter, file); err != nil {
return fmt.Errorf("failed to write chunk to tar: %w", err)
}

return nil
}
23 changes: 10 additions & 13 deletions client/snapshot/export.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
package snapshot

import (
"github.com/spf13/cobra"

"cosmossdk.io/log"
"fmt"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/spf13/cobra"
)

// ExportSnapshotCmd returns a command to take a snapshot of the application state
func ExportSnapshotCmd[T servertypes.Application](appCreator servertypes.AppCreator[T]) *cobra.Command {
func ExportSnapshotCmd(appCreator servertypes.AppCreator) *cobra.Command {
cmd := &cobra.Command{
Use: "export",
Short: "Export app state to snapshot store",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := client.GetConfigFromCmd(cmd)
viper := client.GetViperFromCmd(cmd)
ctx := server.GetServerContextFromCmd(cmd)

height, err := cmd.Flags().GetInt64("height")
if err != nil {
return err
}

home := cfg.RootDir
db, err := openDB(home, server.GetAppDBBackend(viper))
home := ctx.Config.RootDir
db, err := openDB(home, server.GetAppDBBackend(ctx.Viper))
if err != nil {
return err
}
logger := log.NewLogger(cmd.OutOrStdout())
app := appCreator(logger, db, nil, viper)

app := appCreator(ctx.Logger, db, nil, ctx.Viper)

if height == 0 {
height = app.CommitMultiStore().LastCommitID().Version
}

cmd.Printf("Exporting snapshot for height %d\n", height)
fmt.Printf("Exporting snapshot for height %d\n", height)

sm := app.SnapshotManager()
snapshot, err := sm.Create(uint64(height))
if err != nil {
return err
}

cmd.Printf("Snapshot created at height %d, format %d, chunks %d\n", snapshot.Height, snapshot.Format, snapshot.Chunks)
fmt.Printf("Snapshot created at height %d, format %d, chunks %d\n", snapshot.Height, snapshot.Format, snapshot.Chunks)
return nil
},
}
Expand Down
10 changes: 4 additions & 6 deletions client/snapshot/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ package snapshot
import (
"fmt"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
"github.com/spf13/cobra"
)

// ListSnapshotsCmd returns the command to list local snapshots
var ListSnapshotsCmd = &cobra.Command{
Use: "list",
Short: "List local snapshots",
RunE: func(cmd *cobra.Command, args []string) error {
viper := client.GetViperFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(viper)
ctx := server.GetServerContextFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(ctx.Viper)
if err != nil {
return err
}
Expand All @@ -24,7 +22,7 @@ var ListSnapshotsCmd = &cobra.Command{
return fmt.Errorf("failed to list snapshots: %w", err)
}
for _, snapshot := range snapshots {
cmd.Println("height:", snapshot.Height, "format:", snapshot.Format, "chunks:", snapshot.Chunks)
fmt.Println("height:", snapshot.Height, "format:", snapshot.Format, "chunks:", snapshot.Chunks)
}

return nil
Expand Down
21 changes: 9 additions & 12 deletions client/snapshot/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,16 @@ import (
"archive/tar"
"bytes"
"compress/gzip"
"errors"
"fmt"
"io"
"os"
"reflect"
"strconv"

"github.com/cosmos/cosmos-sdk/server"
"github.com/spf13/cobra"

snapshottypes "cosmossdk.io/store/snapshots/types"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
)

const SnapshotFileName = "_snapshot"
Expand All @@ -25,11 +22,11 @@ const SnapshotFileName = "_snapshot"
func LoadArchiveCmd() *cobra.Command {
return &cobra.Command{
Use: "load <archive-file>",
Short: "Load a snapshot archive file (.tar.gz) into snapshot store",
Short: "Load a snapshot archive file into snapshot store",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
viper := client.GetViperFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(viper)
ctx := server.GetServerContextFromCmd(cmd)
snapshotStore, err := server.GetSnapshotStore(ctx.Viper)
if err != nil {
return err
}
Expand Down Expand Up @@ -73,7 +70,7 @@ func LoadArchiveCmd() *cobra.Command {

savedSnapshot, err := snapshotStore.Save(snapshot.Height, snapshot.Format, chunks)
if err != nil {
cmd.Println("failed to save snapshot", err)
fmt.Println("failed to save snapshot", err)
return
}
quitChan <- savedSnapshot
Expand All @@ -82,7 +79,7 @@ func LoadArchiveCmd() *cobra.Command {
for i := uint32(0); i < snapshot.Chunks; i++ {
hdr, err = tr.Next()
if err != nil {
if errors.Is(err, io.EOF) {
if err == io.EOF {
break
}
return err
Expand All @@ -102,12 +99,12 @@ func LoadArchiveCmd() *cobra.Command {

savedSnapshot := <-quitChan
if savedSnapshot == nil {
return errors.New("failed to save snapshot")
return fmt.Errorf("failed to save snapshot")
}

if !reflect.DeepEqual(&snapshot, savedSnapshot) {
_ = snapshotStore.Delete(snapshot.Height, snapshot.Format)
return errors.New("invalid archive, the saved snapshot is not equal to the original one")
return fmt.Errorf("invalid archive, the saved snapshot is not equal to the original one")
}

return nil
Expand Down
20 changes: 7 additions & 13 deletions client/snapshot/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,22 @@ import (
"path/filepath"
"strconv"

dbm "github.com/cosmos/cosmos-db"
"github.com/spf13/cobra"

corestore "cosmossdk.io/core/store"
"cosmossdk.io/log"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/server"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
dbm "github.com/tendermint/tm-db"
)

// RestoreSnapshotCmd returns a command to restore a snapshot
func RestoreSnapshotCmd[T servertypes.Application](appCreator servertypes.AppCreator[T]) *cobra.Command {
func RestoreSnapshotCmd(appCreator servertypes.AppCreator) *cobra.Command {
cmd := &cobra.Command{
Use: "restore <height> <format>",
Short: "Restore app state from local snapshot",
Long: "Restore app state from local snapshot",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cfg := client.GetConfigFromCmd(cmd)
viper := client.GetViperFromCmd(cmd)
ctx := server.GetServerContextFromCmd(cmd)

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
Expand All @@ -35,13 +30,12 @@ func RestoreSnapshotCmd[T servertypes.Application](appCreator servertypes.AppCre
return err
}

home := cfg.RootDir
db, err := openDB(home, server.GetAppDBBackend(viper))
home := ctx.Config.RootDir
db, err := openDB(home, server.GetAppDBBackend(ctx.Viper))
if err != nil {
return err
}
logger := log.NewLogger(cmd.OutOrStdout())
app := appCreator(logger, db, nil, viper)
app := appCreator(ctx.Logger, db, nil, ctx.Viper)

sm := app.SnapshotManager()
return sm.RestoreLocalSnapshot(height, uint32(format))
Expand All @@ -50,7 +44,7 @@ func RestoreSnapshotCmd[T servertypes.Application](appCreator servertypes.AppCre
return cmd
}

func openDB(rootDir string, backendType dbm.BackendType) (corestore.KVStoreWithBatch, error) {
func openDB(rootDir string, backendType dbm.BackendType) (dbm.DB, error) {
dataDir := filepath.Join(rootDir, "data")
return dbm.NewDB("application", backendType, dataDir)
}
Loading

0 comments on commit 42f1adf

Please sign in to comment.