Skip to content
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

[3.5] support custom content check offline in v2store #19113

Open
wants to merge 1 commit into
base: release-3.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions etcdutl/ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func init() {
etcdutl.NewDefragCommand(),
etcdutl.NewSnapshotCommand(),
etcdutl.NewVersionCommand(),
etcdutl.NewCheckCommand(),
)
}

Expand Down
111 changes: 111 additions & 0 deletions etcdutl/etcdutl/check_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// Copyright 2024 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 (
"errors"
"fmt"
"path/filepath"

"github.com/spf13/cobra"

"go.etcd.io/etcd/pkg/v3/cobrautl"
"go.etcd.io/etcd/server/v3/etcdserver"
"go.etcd.io/etcd/server/v3/etcdserver/api/membership"
"go.etcd.io/etcd/server/v3/etcdserver/api/snap"
"go.etcd.io/etcd/server/v3/etcdserver/api/v2store"
"go.etcd.io/etcd/server/v3/wal"
)

// NewCheckCommand returns the cobra command for "check".
func NewCheckCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "check <subcommand>",
Short: "commands for checking properties",
}
cmd.AddCommand(NewCheckV2StoreCommand())
return cmd
}

var (
argCheckV2StoreDataDir string
)

// NewCheckV2StoreCommand returns the cobra command for "check v2store".
func NewCheckV2StoreCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "v2store",
Short: "Check custom content in v2store",
Run: checkV2StoreRunFunc,
}
cmd.Flags().StringVar(&argCheckV2StoreDataDir, "data-dir", "", "Required. A data directory not in use by etcd.")
cmd.MarkFlagRequired("data-dir")
return cmd
}

func checkV2StoreRunFunc(_ *cobra.Command, _ []string) {
err := checkV2StoreDataDir(argCheckV2StoreDataDir)
if err != nil {
cobrautl.ExitWithError(cobrautl.ExitError, err)
}
fuweid marked this conversation as resolved.
Show resolved Hide resolved
fmt.Println("No custom content found in v2store.")
}

func checkV2StoreDataDir(dataDir string) error {
var (
lg = GetLogger()

walDir = filepath.Join(dataDir, "member", "wal")
snapDir = filepath.Join(dataDir, "member", "snap")
)

walSnaps, err := wal.ValidSnapshotEntries(lg, walDir)
if err != nil {
if errors.Is(err, wal.ErrFileNotFound) {
return nil
}
return err
}

ss := snap.New(lg, snapDir)
snapshot, err := ss.LoadNewestAvailable(walSnaps)
if err != nil {
if errors.Is(err, snap.ErrNoSnapshot) {
return nil
}
return err
}
if snapshot == nil {
return nil
}

st := v2store.New(etcdserver.StoreClusterPrefix, etcdserver.StoreKeysPrefix)

if err := st.Recovery(snapshot.Data); err != nil {
return fmt.Errorf("failed to recover v2store from snapshot: %w", err)
}
return assertNoV2StoreContent(st)
}

func assertNoV2StoreContent(st v2store.Store) error {
metaOnly, err := membership.IsMetaStoreOnly(st)
if err != nil {
return err
}
if metaOnly {
return nil
}
return fmt.Errorf("detected custom content in v2store")
}
57 changes: 57 additions & 0 deletions tests/e2e/v2store_deprecation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,60 @@ func TestV2DeprecationWriteOnlyNoV2Api(t *testing.T) {
_, err = proc.Expect("--enable-v2 and --v2-deprecation=write-only are mutually exclusive")
assert.NoError(t, err)
}

func TestV2DeprecationCheckCustomContentOffline(t *testing.T) {
e2e.BeforeTest(t)

t.Run("WithCustomContent", func(t *testing.T) {
dataDirPath := t.TempDir()

createV2store(t, dataDirPath)

assertVerifyCheckCustomContentOffline(t, dataDirPath)
})

t.Run("WithoutCustomContent", func(t *testing.T) {
dataDirPath := ""

func() {
cCtx := getDefaultCtlCtx(t)

cfg := cCtx.cfg
cfg.ClusterSize = 3
cfg.SnapshotCount = 5
cfg.EnableV2 = true

// create a cluster with 3 members
epc, err := e2e.NewEtcdProcessCluster(t, &cfg)
assert.NoError(t, err)

cCtx.epc = epc
dataDirPath = epc.Procs[0].Config().DataDirPath

defer func() {
assert.NoError(t, epc.Stop())
}()

// create key-values with v3 api
for i := 0; i < 10; i++ {
assert.NoError(t, ctlV3Put(cCtx, fmt.Sprintf("key%d", i), fmt.Sprintf("value%d", i), ""))
}
}()

proc, err := e2e.SpawnCmd([]string{e2e.BinDir + "/etcdutl", "check", "v2store", "--data-dir=" + dataDirPath}, nil)
assert.NoError(t, err)

_, err = proc.Expect("No custom content found in v2store")
assert.NoError(t, err)
})
}

func assertVerifyCheckCustomContentOffline(t *testing.T, dataDirPath string) {
t.Logf("Checking custom content in v2store - %s", dataDirPath)

proc, err := e2e.SpawnCmd([]string{e2e.BinDir + "/etcdutl", "check", "v2store", "--data-dir=" + dataDirPath}, nil)
assert.NoError(t, err)

_, err = proc.Expect("detected custom content in v2store")
assert.NoError(t, err)
}
Loading