Skip to content

Commit

Permalink
Fix: reading last snapshot in etcdutl migrate command
Browse files Browse the repository at this point in the history
  • Loading branch information
fykaa committed May 14, 2024
1 parent 22c40e1 commit 0758c72
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion etcdutl/etcdutl/migrate_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ func (o *migrateOptions) Config() (*migrateConfig, error) {
c.be = backend.NewDefaultBackend(GetLogger(), dbPath)

walPath := datadir.ToWALDir(o.dataDir)
w, err := wal.OpenForRead(c.lg, walPath, walpb.Snapshot{})
lastSnapshot, err := getLastSnapshotIndex(walPath)
if err != nil {
return nil, fmt.Errorf(`failed to find last snapshot: %v`, err)
}
w, err := wal.OpenForRead(c.lg, walPath, walpb.Snapshot{Index: lastSnapshot})
if err != nil {
return nil, fmt.Errorf(`failed to open wal: %v`, err)
}
Expand Down Expand Up @@ -156,3 +160,16 @@ func migrateForce(lg *zap.Logger, tx backend.BatchTx, target *semver.Version) {
func storageVersionToString(ver *semver.Version) string {
return fmt.Sprintf("%d.%d", ver.Major, ver.Minor)
}

func getLastSnapshotIndex(walPath string) (uint64, error) {
walSnaps, err := wal.ValidSnapshotEntries(nil, walPath)
if err != nil {
return 0, err
}
if len(walSnaps) == 0 {
return 0, fmt.Errorf("no valid snapshot entries found")
}

snapshot := walSnaps[len(walSnaps)-1]
return snapshot.Index, nil
}

0 comments on commit 0758c72

Please sign in to comment.