Skip to content

Commit

Permalink
Merge pull request #9118 from gyuho/snapshot-code
Browse files Browse the repository at this point in the history
*: add snapshot package
  • Loading branch information
gyuho authored Jan 23, 2018
2 parents 962505f + df689f4 commit 4a12eaf
Show file tree
Hide file tree
Showing 10 changed files with 558 additions and 350 deletions.
20 changes: 7 additions & 13 deletions e2e/ctl_v3_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/coreos/etcd/pkg/expect"
"github.com/coreos/etcd/pkg/testutil"
"github.com/coreos/etcd/snapshot"
)

func TestCtlV3Snapshot(t *testing.T) { testCtl(t, snapshotTest) }
Expand Down Expand Up @@ -127,33 +128,26 @@ func ctlV3SnapshotSave(cx ctlCtx, fpath string) error {
return spawnWithExpect(cmdArgs, fmt.Sprintf("Snapshot saved at %s", fpath))
}

type snapshotStatus struct {
Hash uint32 `json:"hash"`
Revision int64 `json:"revision"`
TotalKey int `json:"totalKey"`
TotalSize int64 `json:"totalSize"`
}

func getSnapshotStatus(cx ctlCtx, fpath string) (snapshotStatus, error) {
func getSnapshotStatus(cx ctlCtx, fpath string) (snapshot.Status, error) {
cmdArgs := append(cx.PrefixArgs(), "--write-out", "json", "snapshot", "status", fpath)

proc, err := spawnCmd(cmdArgs)
if err != nil {
return snapshotStatus{}, err
return snapshot.Status{}, err
}
var txt string
txt, err = proc.Expect("totalKey")
if err != nil {
return snapshotStatus{}, err
return snapshot.Status{}, err
}
if err = proc.Close(); err != nil {
return snapshotStatus{}, err
return snapshot.Status{}, err
}

resp := snapshotStatus{}
resp := snapshot.Status{}
dec := json.NewDecoder(strings.NewReader(txt))
if err := dec.Decode(&resp); err == io.EOF {
return snapshotStatus{}, err
return snapshot.Status{}, err
}
return resp, nil
}
Expand Down
11 changes: 6 additions & 5 deletions etcdctl/ctlv3/command/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ import (
"strings"

v3 "github.com/coreos/etcd/clientv3"
"github.com/dustin/go-humanize"

pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
"github.com/coreos/etcd/snapshot"

"github.com/dustin/go-humanize"
)

type printer interface {
Expand All @@ -48,7 +49,7 @@ type printer interface {
MoveLeader(leader, target uint64, r v3.MoveLeaderResponse)

Alarm(v3.AlarmResponse)
DBStatus(dbstatus)
DBStatus(snapshot.Status)

RoleAdd(role string, r v3.AuthRoleAddResponse)
RoleGet(role string, r v3.AuthRoleGetResponse)
Expand Down Expand Up @@ -150,7 +151,7 @@ func newPrinterUnsupported(n string) printer {

func (p *printerUnsupported) EndpointStatus([]epStatus) { p.p(nil) }
func (p *printerUnsupported) EndpointHashKV([]epHashKV) { p.p(nil) }
func (p *printerUnsupported) DBStatus(dbstatus) { p.p(nil) }
func (p *printerUnsupported) DBStatus(snapshot.Status) { p.p(nil) }

func (p *printerUnsupported) MoveLeader(leader, target uint64, r v3.MoveLeaderResponse) { p.p(nil) }

Expand Down Expand Up @@ -200,7 +201,7 @@ func makeEndpointHashKVTable(hashList []epHashKV) (hdr []string, rows [][]string
return hdr, rows
}

func makeDBStatusTable(ds dbstatus) (hdr []string, rows [][]string) {
func makeDBStatusTable(ds snapshot.Status) (hdr []string, rows [][]string) {
hdr = []string{"hash", "revision", "total keys", "total size"}
rows = append(rows, []string{
fmt.Sprintf("%x", ds.Hash),
Expand Down
3 changes: 2 additions & 1 deletion etcdctl/ctlv3/command/printer_fields.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
v3 "github.com/coreos/etcd/clientv3"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
spb "github.com/coreos/etcd/mvcc/mvccpb"
"github.com/coreos/etcd/snapshot"
)

type fieldsPrinter struct{ printer }
Expand Down Expand Up @@ -172,7 +173,7 @@ func (p *fieldsPrinter) Alarm(r v3.AlarmResponse) {
}
}

func (p *fieldsPrinter) DBStatus(r dbstatus) {
func (p *fieldsPrinter) DBStatus(r snapshot.Status) {
fmt.Println(`"Hash" :`, r.Hash)
fmt.Println(`"Revision" :`, r.Revision)
fmt.Println(`"Keys" :`, r.TotalKey)
Expand Down
4 changes: 3 additions & 1 deletion etcdctl/ctlv3/command/printer_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import (
"encoding/json"
"fmt"
"os"

"github.com/coreos/etcd/snapshot"
)

type jsonPrinter struct{ printer }
Expand All @@ -30,7 +32,7 @@ func newJSONPrinter() printer {

func (p *jsonPrinter) EndpointStatus(r []epStatus) { printJSON(r) }
func (p *jsonPrinter) EndpointHashKV(r []epHashKV) { printJSON(r) }
func (p *jsonPrinter) DBStatus(r dbstatus) { printJSON(r) }
func (p *jsonPrinter) DBStatus(r snapshot.Status) { printJSON(r) }

func printJSON(v interface{}) {
b, err := json.Marshal(v)
Expand Down
3 changes: 2 additions & 1 deletion etcdctl/ctlv3/command/printer_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
v3 "github.com/coreos/etcd/clientv3"
pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
"github.com/coreos/etcd/pkg/types"
"github.com/coreos/etcd/snapshot"
)

type simplePrinter struct {
Expand Down Expand Up @@ -155,7 +156,7 @@ func (s *simplePrinter) EndpointHashKV(hashList []epHashKV) {
}
}

func (s *simplePrinter) DBStatus(ds dbstatus) {
func (s *simplePrinter) DBStatus(ds snapshot.Status) {
_, rows := makeDBStatusTable(ds)
for _, row := range rows {
fmt.Println(strings.Join(row, ", "))
Expand Down
7 changes: 4 additions & 3 deletions etcdctl/ctlv3/command/printer_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ package command
import (
"os"

"github.com/olekukonko/tablewriter"

v3 "github.com/coreos/etcd/clientv3"
"github.com/coreos/etcd/snapshot"

"github.com/olekukonko/tablewriter"
)

type tablePrinter struct{ printer }
Expand Down Expand Up @@ -54,7 +55,7 @@ func (tp *tablePrinter) EndpointHashKV(r []epHashKV) {
table.SetAlignment(tablewriter.ALIGN_RIGHT)
table.Render()
}
func (tp *tablePrinter) DBStatus(r dbstatus) {
func (tp *tablePrinter) DBStatus(r snapshot.Status) {
hdr, rows := makeDBStatusTable(r)
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader(hdr)
Expand Down
Loading

0 comments on commit 4a12eaf

Please sign in to comment.