diff --git a/etcdutl/etcdutl/common.go b/etcdutl/etcdutl/common.go index d54827d0457..c7473cc7fd7 100644 --- a/etcdutl/etcdutl/common.go +++ b/etcdutl/etcdutl/common.go @@ -15,11 +15,18 @@ package etcdutl import ( + "errors" + "go.uber.org/zap" "go.uber.org/zap/zapcore" "go.etcd.io/etcd/client/pkg/v3/logutil" "go.etcd.io/etcd/pkg/v3/cobrautl" + "go.etcd.io/etcd/server/v3/etcdserver/api/snap" + "go.etcd.io/etcd/server/v3/storage/datadir" + "go.etcd.io/etcd/server/v3/storage/wal" + "go.etcd.io/etcd/server/v3/storage/wal/walpb" + "go.etcd.io/raft/v3/raftpb" ) func GetLogger() *zap.Logger { @@ -32,3 +39,32 @@ func GetLogger() *zap.Logger { } return lg } + +func getLatestWALSnap(lg *zap.Logger, dataDir string) (walpb.Snapshot, error) { + snapshot, err := getLatestV2Snapshot(lg, dataDir) + if err != nil { + return walpb.Snapshot{}, err + } + + var walsnap walpb.Snapshot + if snapshot != nil { + walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term + } + return walsnap, nil +} + +func getLatestV2Snapshot(lg *zap.Logger, dataDir string) (*raftpb.Snapshot, error) { + walPath := datadir.ToWALDir(dataDir) + walSnaps, err := wal.ValidSnapshotEntries(lg, walPath) + if err != nil { + return nil, err + } + + ss := snap.New(lg, datadir.ToSnapDir(dataDir)) + snapshot, err := ss.LoadNewestAvailable(walSnaps) + if err != nil && !errors.Is(err, snap.ErrNoSnapshot) { + return nil, err + } + + return snapshot, nil +} diff --git a/etcdutl/etcdutl/migrate_command.go b/etcdutl/etcdutl/migrate_command.go index 7f435b80a28..a265c7baae7 100644 --- a/etcdutl/etcdutl/migrate_command.go +++ b/etcdutl/etcdutl/migrate_command.go @@ -15,7 +15,6 @@ package etcdutl import ( - "errors" "fmt" "strings" @@ -25,12 +24,10 @@ import ( "go.etcd.io/etcd/api/v3/version" "go.etcd.io/etcd/pkg/v3/cobrautl" - "go.etcd.io/etcd/server/v3/etcdserver/api/snap" "go.etcd.io/etcd/server/v3/storage/backend" "go.etcd.io/etcd/server/v3/storage/datadir" "go.etcd.io/etcd/server/v3/storage/schema" "go.etcd.io/etcd/server/v3/storage/wal" - "go.etcd.io/etcd/server/v3/storage/wal/walpb" ) // NewMigrateCommand prints out the version of etcd. @@ -114,26 +111,6 @@ func (o *migrateOptions) Config() (*migrateConfig, error) { return c, nil } -func getLatestWALSnap(lg *zap.Logger, dataDir string) (walpb.Snapshot, error) { - walPath := datadir.ToWALDir(dataDir) - walSnaps, err := wal.ValidSnapshotEntries(lg, walPath) - if err != nil { - return walpb.Snapshot{}, err - } - - ss := snap.New(lg, datadir.ToSnapDir(dataDir)) - snapshot, err := ss.LoadNewestAvailable(walSnaps) - if err != nil && !errors.Is(err, snap.ErrNoSnapshot) { - return walpb.Snapshot{}, err - } - - var walsnap walpb.Snapshot - if snapshot != nil { - walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term - } - return walsnap, nil -} - type migrateConfig struct { lg *zap.Logger be backend.Backend