Skip to content

Commit 098e501

Browse files
committed
etcdutl: reading from last snapshot in migrate command
To improve correctness of etcdutl migrate command Signed-off-by: fykaa <[email protected]>
1 parent a77b146 commit 098e501

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

etcdutl/etcdutl/migrate_command.go

+18-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ func (o *migrateOptions) Config() (*migrateConfig, error) {
9595
c.be = backend.NewDefaultBackend(GetLogger(), dbPath)
9696

9797
walPath := datadir.ToWALDir(o.dataDir)
98-
w, err := wal.OpenForRead(c.lg, walPath, walpb.Snapshot{})
98+
lastSnapshot, err := getLastSnapshotIndex(walPath)
99+
if err != nil {
100+
return nil, fmt.Errorf(`failed to find last snapshot: %v`, err)
101+
}
102+
w, err := wal.OpenForRead(c.lg, walPath, walpb.Snapshot{Index: lastSnapshot})
99103
if err != nil {
100104
return nil, fmt.Errorf(`failed to open wal: %v`, err)
101105
}
@@ -156,3 +160,16 @@ func migrateForce(lg *zap.Logger, tx backend.BatchTx, target *semver.Version) {
156160
func storageVersionToString(ver *semver.Version) string {
157161
return fmt.Sprintf("%d.%d", ver.Major, ver.Minor)
158162
}
163+
164+
func getLastSnapshotIndex(walPath string) (uint64, error) {
165+
walSnaps, err := wal.ValidSnapshotEntries(nil, walPath)
166+
if err != nil {
167+
return 0, err
168+
}
169+
if len(walSnaps) == 0 {
170+
return 0, fmt.Errorf("no valid snapshot entries found")
171+
}
172+
173+
snapshot := walSnaps[len(walSnaps)-1]
174+
return snapshot.Index, nil
175+
}

0 commit comments

Comments
 (0)