Skip to content

Commit

Permalink
Merge pull request #19207 from ahrtr/migrate_command_20250116
Browse files Browse the repository at this point in the history
Minor refactor on the etcdutl migrate command
  • Loading branch information
ahrtr authored Jan 16, 2025
2 parents 76aadef + 39ccd99 commit ab819b5
Showing 1 changed file with 33 additions and 22 deletions.
55 changes: 33 additions & 22 deletions etcdutl/etcdutl/migrate_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ func (o *migrateOptions) AddFlags(cmd *cobra.Command) {

func (o *migrateOptions) Config() (*migrateConfig, error) {
c := &migrateConfig{
force: o.force,
lg: GetLogger(),
force: o.force,
dataDir: o.dataDir,
lg: GetLogger(),
}
var err error
dotCount := strings.Count(o.targetVersion, ".")
Expand All @@ -90,39 +91,43 @@ func (o *migrateOptions) Config() (*migrateConfig, error) {
return nil, fmt.Errorf(`target version %q not supported. Minimal "3.5"`, storageVersionToString(c.targetVersion))
}

dbPath := datadir.ToBackendFileName(o.dataDir)
c.be = backend.NewDefaultBackend(GetLogger(), dbPath)
return c, nil
}

type migrateConfig struct {
lg *zap.Logger
targetVersion *semver.Version
walVersion schema.WALVersion
dataDir string
force bool
}

walPath := datadir.ToWALDir(o.dataDir)
walSnap, err := getLatestWALSnap(c.lg, o.dataDir)
func (c *migrateConfig) finalize() error {
walPath := datadir.ToWALDir(c.dataDir)
walSnap, err := getLatestWALSnap(c.lg, c.dataDir)
if err != nil {
return nil, fmt.Errorf("failed to get the lastest snapshot: %w", err)
return fmt.Errorf("failed to get the lastest snapshot: %w", err)
}
w, err := wal.OpenForRead(c.lg, walPath, walSnap)
if err != nil {
return nil, fmt.Errorf(`failed to open wal: %w`, err)
return fmt.Errorf(`failed to open wal: %w`, err)
}
defer w.Close()
c.walVersion, err = wal.ReadWALVersion(w)
if err != nil {
return nil, fmt.Errorf(`failed to read wal: %w`, err)
return fmt.Errorf(`failed to read wal: %w`, err)
}

return c, nil
}

type migrateConfig struct {
lg *zap.Logger
be backend.Backend
targetVersion *semver.Version
walVersion schema.WALVersion
force bool
return nil
}

func migrateCommandFunc(c *migrateConfig) error {
defer c.be.Close()
tx := c.be.BatchTx()
current, err := schema.DetectSchemaVersion(c.lg, c.be.ReadTx())
dbPath := datadir.ToBackendFileName(c.dataDir)
be := backend.NewDefaultBackend(GetLogger(), dbPath)
defer be.Close()

tx := be.BatchTx()
current, err := schema.DetectSchemaVersion(c.lg, be.ReadTx())
if err != nil {
c.lg.Error("failed to detect storage version. Please make sure you are using data dir from etcd v3.5 and older")
return err
Expand All @@ -131,6 +136,12 @@ func migrateCommandFunc(c *migrateConfig) error {
c.lg.Info("storage version up-to-date", zap.String("storage-version", storageVersionToString(&current)))
return nil
}

if err = c.finalize(); err != nil {
c.lg.Error("Failed to finalize config", zap.Error(err))
return err
}

err = schema.Migrate(c.lg, tx, c.walVersion, *c.targetVersion)
if err != nil {
if !c.force {
Expand All @@ -139,7 +150,7 @@ func migrateCommandFunc(c *migrateConfig) error {
c.lg.Info("normal migrate failed, trying with force", zap.Error(err))
migrateForce(c.lg, tx, c.targetVersion)
}
c.be.ForceCommit()
be.ForceCommit()
return nil
}

Expand Down

0 comments on commit ab819b5

Please sign in to comment.