-
Notifications
You must be signed in to change notification settings - Fork 9.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cli: Add etcdutl snapshot hashkv command
Signed-off-by: Cenk Alti <cenkalti@gmail.com> Apply suggestions from code review Co-authored-by: Benjamin Wang <benjamin.wang@broadcom.com>
- Loading branch information
Showing
12 changed files
with
160 additions
and
5 deletions.
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,79 @@ | ||
// 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 ( | ||
"fmt" | ||
|
||
"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/mvcc" | ||
) | ||
|
||
var ( | ||
hashKVRevision int64 | ||
) | ||
|
||
// NewHashKVCommand returns the cobra command for "hashkv". | ||
func NewHashKVCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "hashkv <filename>", | ||
Short: "Prints the KV history hash of a given file", | ||
Run: hashKVCommandFunc, | ||
} | ||
cmd.Flags().Int64Var(&hashKVRevision, "rev", 0, "maximum revision to hash (default: latest revision)") | ||
return cmd | ||
} | ||
|
||
func hashKVCommandFunc(cmd *cobra.Command, args []string) { | ||
if len(args) != 1 { | ||
err := fmt.Errorf("hashkv requires exactly one argument") | ||
cobrautl.ExitWithError(cobrautl.ExitBadArgs, err) | ||
} | ||
printer := initPrinterFromCmd(cmd) | ||
|
||
ds, err := calculateHashKV(args[0], hashKVRevision) | ||
if err != nil { | ||
cobrautl.ExitWithError(cobrautl.ExitError, err) | ||
} | ||
printer.DBHashKV(ds) | ||
} | ||
|
||
type HashKV struct { | ||
Hash uint32 `json:"hash"` | ||
HashRevision int64 `json:"hashRevision"` | ||
CompactRevision int64 `json:"compactRevision"` | ||
} | ||
|
||
func calculateHashKV(dbPath string, rev int64) (ds HashKV, err error) { | ||
cfg := backend.DefaultBackendConfig(zap.NewNop()) | ||
cfg.Path = dbPath | ||
b := backend.New(cfg) | ||
st := mvcc.NewStore(zap.NewNop(), b, nil, mvcc.StoreConfig{}) | ||
hst := mvcc.NewHashStorage(zap.NewNop(), st) | ||
|
||
h, _, err := hst.HashByRev(rev) | ||
if err != nil { | ||
return HashKV{}, err | ||
} | ||
return HashKV{ | ||
Hash: h.Hash, | ||
HashRevision: h.Revision, | ||
CompactRevision: h.CompactRevision, | ||
}, nil | ||
} |
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
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
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