Skip to content

Commit

Permalink
Revert "chore: remove snapshot commands (#23015)"
Browse files Browse the repository at this point in the history
This reverts commit 4f5c3d8.
  • Loading branch information
aljo242 committed Jan 21, 2025
1 parent fbdca39 commit 69a38f0
Show file tree
Hide file tree
Showing 7 changed files with 459 additions and 0 deletions.
24 changes: 24 additions & 0 deletions client/snapshot/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package snapshot

import (
"github.com/spf13/cobra"

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

// Cmd returns the snapshots group command
func Cmd[T servertypes.Application](appCreator servertypes.AppCreator[T]) *cobra.Command {
cmd := &cobra.Command{
Use: "snapshots",
Short: "Manage local snapshots",
}
cmd.AddCommand(
ListSnapshotsCmd,
RestoreSnapshotCmd(appCreator),
ExportSnapshotCmd(appCreator),
DumpArchiveCmd(),
LoadArchiveCmd(),
DeleteSnapshotCmd(),
)
return cmd
}
37 changes: 37 additions & 0 deletions client/snapshot/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package snapshot

import (
"strconv"

"github.com/spf13/cobra"

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

func DeleteSnapshotCmd() *cobra.Command {
return &cobra.Command{
Use: "delete <height> <format>",
Short: "Delete a local snapshot",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
viper := client.GetViperFromCmd(cmd)

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}
format, err := strconv.ParseUint(args[1], 10, 32)
if err != nil {
return err
}

snapshotStore, err := server.GetSnapshotStore(viper)
if err != nil {
return err
}

return snapshotStore.Delete(height, uint32(format))
},
}
}
136 changes: 136 additions & 0 deletions client/snapshot/dump.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
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"
)

// DumpArchiveCmd returns a command to dump the snapshot as portable archive format
func DumpArchiveCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "dump <height> <format>",
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)
if err != nil {
return err
}

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

height, err := strconv.ParseUint(args[0], 10, 64)
if err != nil {
return err
}
format, err := strconv.ParseUint(args[1], 10, 32)
if err != nil {
return err
}

if output == "" {
output = fmt.Sprintf("%d-%d.tar.gz", height, format)
}

snapshot, err := snapshotStore.Get(height, uint32(format))
if err != nil {
return err
}

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

bz, err := snapshot.Marshal()
if err != nil {
return err
}

fp, err := os.Create(output)
if err != nil {
return err
}
defer fp.Close()

// since the chunk files are already compressed, we just use fastest compression here
gzipWriter, err := gzip.NewWriterLevel(fp, gzip.BestSpeed)
if err != nil {
return err
}
tarWriter := tar.NewWriter(gzipWriter)
if err := tarWriter.WriteHeader(&tar.Header{
Name: SnapshotFileName,
Mode: 0o644,
Size: int64(len(bz)),
}); err != nil {
return fmt.Errorf("failed to write snapshot header to tar: %w", err)
}
if _, err := tarWriter.Write(bz); err != nil {
return fmt.Errorf("failed to write snapshot to tar: %w", err)
}

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
}
}

if err := tarWriter.Close(); err != nil {
return fmt.Errorf("failed to close tar writer: %w", err)
}

if err := gzipWriter.Close(); err != nil {
return fmt.Errorf("failed to close gzip writer: %w", err)
}

return fp.Close()
},
}

cmd.Flags().StringP("output", "o", "", "output file")

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
}
54 changes: 54 additions & 0 deletions client/snapshot/export.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package snapshot

import (
"github.com/spf13/cobra"

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

// ExportSnapshotCmd returns a command to take a snapshot of the application state
func ExportSnapshotCmd[T servertypes.Application](appCreator servertypes.AppCreator[T]) *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 {
viper := client.GetViperFromCmd(cmd)
logger := client.GetLoggerFromCmd(cmd)

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

home := viper.GetString(flags.FlagHome)
db, err := openDB(home, server.GetAppDBBackend(viper))
if err != nil {
return err
}
app := appCreator(logger, db, nil, viper)

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

cmd.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)
return nil
},
}

cmd.Flags().Int64("height", 0, "Height to export, default to latest state height")

return cmd
}
37 changes: 37 additions & 0 deletions client/snapshot/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package snapshot

import (
"fmt"

"github.com/spf13/cobra"

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

// 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)
if err != nil {
return err
}
snapshots, err := snapshotStore.List()
if err != nil {
return fmt.Errorf("failed to list snapshots: %w", err)
}

if len(snapshots) == 0 {
cmd.Println("no snapshots found")
}

for _, snapshot := range snapshots {
cmd.Println("height:", snapshot.Height, "format:", snapshot.Format, "chunks:", snapshot.Chunks)
}

return nil
},
}
Loading

0 comments on commit 69a38f0

Please sign in to comment.