-
Notifications
You must be signed in to change notification settings - Fork 9.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
etcdutl: Implement migrate --force command #13220
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright 2021 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package etcdutl | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/coreos/go-semver/semver" | ||
"github.com/spf13/cobra" | ||
"go.uber.org/zap" | ||
|
||
"go.etcd.io/etcd/pkg/v3/cobrautl" | ||
"go.etcd.io/etcd/server/v3/storage/backend" | ||
"go.etcd.io/etcd/server/v3/storage/datadir" | ||
"go.etcd.io/etcd/server/v3/storage/schema" | ||
) | ||
|
||
// NewMigrateCommand prints out the version of etcd. | ||
func NewMigrateCommand() *cobra.Command { | ||
o := newMigrateOptions() | ||
cmd := &cobra.Command{ | ||
Use: "migrate", | ||
Short: "Migrates schema of etcd data dir files to make them compatible with different etcd version", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cfg, err := o.Config() | ||
if err != nil { | ||
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err) | ||
} | ||
err = migrateCommandFunc(cfg) | ||
if err != nil { | ||
cobrautl.ExitWithError(cobrautl.ExitError, err) | ||
} | ||
}, | ||
} | ||
o.AddFlags(cmd) | ||
return cmd | ||
} | ||
|
||
type migrateOptions struct { | ||
dataDir string | ||
targetVersion string | ||
force bool | ||
} | ||
|
||
func newMigrateOptions() *migrateOptions { | ||
return &migrateOptions{} | ||
} | ||
|
||
func (o *migrateOptions) AddFlags(cmd *cobra.Command) { | ||
cmd.Flags().StringVar(&o.dataDir, "data-dir", o.dataDir, "Path to the etcd data dir") | ||
cmd.MarkFlagRequired("data-dir") | ||
cmd.MarkFlagDirname("data-dir") | ||
|
||
cmd.Flags().StringVar(&o.targetVersion, "target-version", o.targetVersion, `Target etcd version to migrate contents of data dir. Minmal value 3.5. Format "X.Y" for example 3.6.`) | ||
cmd.MarkFlagRequired("target-version") | ||
|
||
cmd.Flags().BoolVar(&o.force, "force", o.force, "Ignore migration failure and forcefully override storage version. Not recommended.") | ||
} | ||
|
||
func (o *migrateOptions) Config() (*migrateConfig, error) { | ||
c := &migrateConfig{ | ||
force: o.force, | ||
} | ||
var err error | ||
dotCount := strings.Count(o.targetVersion, ".") | ||
if dotCount != 1 { | ||
return nil, fmt.Errorf(`wrong target version format, expected "X.Y", got %q`, o.targetVersion) | ||
} | ||
c.targetVersion, err = semver.NewVersion(o.targetVersion + ".0") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse target version: %w", err) | ||
} | ||
if c.targetVersion.LessThan(schema.V3_5) { | ||
return nil, fmt.Errorf(`target version %q not supported. Minimal "3.5".`, storageVersionToString(c.targetVersion)) | ||
} | ||
|
||
dbPath := datadir.ToBackendFileName(o.dataDir) | ||
c.be = backend.NewDefaultBackend(dbPath) | ||
|
||
return c, nil | ||
} | ||
|
||
type migrateConfig struct { | ||
be backend.Backend | ||
targetVersion *semver.Version | ||
force bool | ||
} | ||
|
||
func migrateCommandFunc(c *migrateConfig) error { | ||
defer c.be.Close() | ||
lg := GetLogger() | ||
tx := c.be.BatchTx() | ||
tx.Lock() | ||
current, err := schema.DetectSchemaVersion(lg, tx) | ||
if err != nil { | ||
tx.Unlock() | ||
lg.Error("failed to detect storage version. Please make sure you are using data dir from etcd v3.5 and older") | ||
return err | ||
} | ||
if *current == *c.targetVersion { | ||
tx.Unlock() | ||
lg.Info("storage version up-to-date", zap.String("storage-version", storageVersionToString(current))) | ||
return nil | ||
} | ||
if c.force { | ||
unsafeMigrateForce(lg, tx, c.targetVersion) | ||
tx.Unlock() | ||
c.be.ForceCommit() | ||
return nil | ||
} | ||
tx.Unlock() | ||
return fmt.Errorf("storage version migration is not yet supported") | ||
} | ||
|
||
func unsafeMigrateForce(lg *zap.Logger, tx backend.BatchTx, target *semver.Version) { | ||
// Storage version is only supported since v3.6 | ||
if target.LessThan(schema.V3_6) { | ||
schema.UnsafeClearStorageVersion(tx) | ||
lg.Warn("forcefully cleared storage version") | ||
} else { | ||
schema.UnsafeSetStorageVersion(tx, target) | ||
lg.Warn("forcefully set storage version", zap.String("storage-version", storageVersionToString(target))) | ||
} | ||
} | ||
|
||
func storageVersionToString(ver *semver.Version) string { | ||
return fmt.Sprintf("%d.%d", ver.Major, ver.Minor) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
// Copyright 2021 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package e2e | ||
serathius marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/coreos/go-semver/semver" | ||
"github.com/stretchr/testify/assert" | ||
"go.etcd.io/etcd/client/pkg/v3/fileutil" | ||
"go.etcd.io/etcd/server/v3/storage/backend" | ||
"go.etcd.io/etcd/server/v3/storage/schema" | ||
) | ||
|
||
func TestEtctlutlMigrate(t *testing.T) { | ||
lastReleaseBinary := binDir + "/etcd-last-release" | ||
|
||
tcs := []struct { | ||
name string | ||
targetVersion string | ||
binary string | ||
force bool | ||
|
||
expectLogsSubString string | ||
expectStorageVersion *semver.Version | ||
}{ | ||
{ | ||
name: "Invalid target version string", | ||
targetVersion: "abc", | ||
expectLogsSubString: `Error: wrong target version format, expected "X.Y", got "abc"`, | ||
expectStorageVersion: &schema.V3_6, | ||
}, | ||
{ | ||
name: "Invalid target version", | ||
targetVersion: "3.a", | ||
expectLogsSubString: `Error: failed to parse target version: strconv.ParseInt: parsing "a": invalid syntax`, | ||
expectStorageVersion: &schema.V3_6, | ||
}, | ||
{ | ||
name: "Target with only major version is invalid", | ||
targetVersion: "3", | ||
expectLogsSubString: `Error: wrong target version format, expected "X.Y", got "3"`, | ||
expectStorageVersion: &schema.V3_6, | ||
}, | ||
{ | ||
name: "Target with patch version is invalid", | ||
targetVersion: "3.6.0", | ||
expectLogsSubString: `Error: wrong target version format, expected "X.Y", got "3.6.0"`, | ||
expectStorageVersion: &schema.V3_6, | ||
}, | ||
{ | ||
name: "Migrate v3.5 to v3.5 is no-op", | ||
binary: lastReleaseBinary, | ||
targetVersion: "3.5", | ||
expectLogsSubString: "storage version up-to-date\t" + `{"storage-version": "3.5"}`, | ||
}, | ||
{ | ||
name: "Upgrade v3.5 to v3.6 should fail until it's implemented", | ||
binary: lastReleaseBinary, | ||
targetVersion: "3.6", | ||
expectLogsSubString: "Error: storage version migration is not yet supported", | ||
}, | ||
{ | ||
name: "Migrate v3.6 to v3.6 is no-op", | ||
targetVersion: "3.6", | ||
expectLogsSubString: "storage version up-to-date\t" + `{"storage-version": "3.6"}`, | ||
expectStorageVersion: &schema.V3_6, | ||
}, | ||
{ | ||
name: "Downgrade v3.6 to v3.5 should fail until it's implemented", | ||
targetVersion: "3.5", | ||
expectLogsSubString: "Error: storage version migration is not yet supported", | ||
expectStorageVersion: &schema.V3_6, | ||
}, | ||
{ | ||
name: "Downgrade v3.6 to v3.5 with force should work", | ||
targetVersion: "3.5", | ||
force: true, | ||
expectLogsSubString: "forcefully cleared storage version", | ||
}, | ||
{ | ||
name: "Upgrade v3.6 to v3.7 with force should work", | ||
targetVersion: "3.7", | ||
force: true, | ||
expectLogsSubString: "forcefully set storage version\t" + `{"storage-version": "3.7"}`, | ||
expectStorageVersion: &semver.Version{Major: 3, Minor: 7}, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.name, func(t *testing.T) { | ||
BeforeTest(t) | ||
if tc.binary != "" && !fileutil.Exist(tc.binary) { | ||
t.Skipf("%q does not exist", lastReleaseBinary) | ||
} | ||
dataDirPath := t.TempDir() | ||
|
||
epc, err := newEtcdProcessCluster(t, &etcdProcessClusterConfig{ | ||
execPath: tc.binary, | ||
dataDirPath: dataDirPath, | ||
clusterSize: 1, | ||
initialToken: "new", | ||
keepDataDir: true, | ||
// Set low snapshotCount to ensure wal snapshot is done | ||
snapshotCount: 1, | ||
}) | ||
if err != nil { | ||
t.Fatalf("could not start etcd process cluster (%v)", err) | ||
} | ||
defer func() { | ||
if errC := epc.Close(); errC != nil { | ||
t.Fatalf("error closing etcd processes (%v)", errC) | ||
} | ||
}() | ||
|
||
dialTimeout := 10 * time.Second | ||
prefixArgs := []string{ctlBinPath, "--endpoints", strings.Join(epc.EndpointsV3(), ","), "--dial-timeout", dialTimeout.String()} | ||
|
||
t.Log("Write keys to ensure wal snapshot is created and all v3.5 fields are set...") | ||
for i := 0; i < 10; i++ { | ||
if err = spawnWithExpect(append(prefixArgs, "put", fmt.Sprintf("%d", i), "value"), "OK"); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
t.Log("Stopping the server...") | ||
if err = epc.procs[0].Stop(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
t.Log("etcdutl migrate...") | ||
args := []string{utlBinPath, "migrate", "--data-dir", dataDirPath, "--target-version", tc.targetVersion} | ||
if tc.force { | ||
args = append(args, "--force") | ||
} | ||
err = spawnWithExpect(args, tc.expectLogsSubString) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
t.Log("etcdutl migrate...") | ||
be := backend.NewDefaultBackend(dataDirPath + "/member/snap/db") | ||
defer be.Close() | ||
|
||
ver := schema.ReadStorageVersion(be.ReadTx()) | ||
assert.Equal(t, tc.expectStorageVersion, ver) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would the line 82 succeed if the version didn't contain "." ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, semver parsing always requires "Major.Minor.Patch". Here we just hardcode patch to 0.
I think we should have a special struct just to represent "Major.Minor" instead of reusing semver
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see: It's just about adding more human friendly error...