From d634c55bed13f5d0a51dc7fde86f934015773f77 Mon Sep 17 00:00:00 2001 From: Cenk Alti Date: Tue, 30 May 2023 13:43:46 -0400 Subject: [PATCH] Improve snapshot status Signed-off-by: Cenk Alti --- etcdutl/snapshot/util.go | 10 +++++++--- etcdutl/snapshot/v3_snapshot.go | 12 +++++++++++- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/etcdutl/snapshot/util.go b/etcdutl/snapshot/util.go index 2c1fae21fa15..543f21f60048 100644 --- a/etcdutl/snapshot/util.go +++ b/etcdutl/snapshot/util.go @@ -16,6 +16,7 @@ package snapshot import ( "encoding/binary" + "fmt" ) type revision struct { @@ -23,9 +24,12 @@ type revision struct { sub int64 } -func bytesToRev(bytes []byte) revision { +func bytesToRev(bytes []byte) (revision, error) { + if len(bytes) != 17 { + return revision{}, fmt.Errorf("invalid revision size: %d, expected size is 17 (2 * size(int64) + delimiter)", len(bytes)) + } return revision{ main: int64(binary.BigEndian.Uint64(bytes[0:8])), - sub: int64(binary.BigEndian.Uint64(bytes[9:])), - } + sub: int64(binary.BigEndian.Uint64(bytes[9:17])), + }, nil } diff --git a/etcdutl/snapshot/v3_snapshot.go b/etcdutl/snapshot/v3_snapshot.go index 8958ba80da13..934ee3827f7b 100644 --- a/etcdutl/snapshot/v3_snapshot.go +++ b/etcdutl/snapshot/v3_snapshot.go @@ -30,6 +30,7 @@ import ( bolt "go.etcd.io/bbolt" "go.etcd.io/etcd/api/v3/etcdserverpb" + "go.etcd.io/etcd/api/v3/mvccpb" "go.etcd.io/etcd/client/pkg/v3/fileutil" "go.etcd.io/etcd/client/pkg/v3/types" clientv3 "go.etcd.io/etcd/client/v3" @@ -138,6 +139,7 @@ func (s *v3Manager) Status(dbPath string) (ds Status, err error) { if v != nil { ds.Version = v.String() } + var kv mvccpb.KeyValue c := tx.Cursor() for next, _ := c.First(); next != nil; next, _ = c.Next() { b := tx.Bucket(next) @@ -156,8 +158,16 @@ func (s *v3Manager) Status(dbPath string) (ds Status, err error) { return fmt.Errorf("cannot write to bucket %s", err.Error()) } if iskeyb { - rev := bytesToRev(k) + rev, err := bytesToRev(k) + if err != nil { + return fmt.Errorf("cannot parse revision key : %s", err.Error()) + } ds.Revision = rev.main + + err = kv.Unmarshal(v) + if err != nil { + return fmt.Errorf("cannot unmarshal value : %s", err.Error()) + } } ds.TotalKey++ return nil