diff --git a/tools/etcd-dump-logs/etcd-dump-log_test.go b/tools/etcd-dump-logs/etcd-dump-log_test.go new file mode 100644 index 000000000000..e4ddc06712f7 --- /dev/null +++ b/tools/etcd-dump-logs/etcd-dump-log_test.go @@ -0,0 +1,217 @@ +// Copyright 2018 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 main + +import ( + "bytes" + "io/ioutil" + "os" + "os/exec" + "path" + "path/filepath" + "strings" + "testing" + + "github.com/coreos/etcd/etcdserver/etcdserverpb" + "github.com/coreos/etcd/pkg/pbutil" + "github.com/coreos/etcd/raft/raftpb" + "github.com/coreos/etcd/wal" + "go.uber.org/zap" +) + +func TestEtcdDumpLogEntryType(t *testing.T) { + // directory where the command is + dir, err := os.Getwd() + if err != nil { + t.Fatal("%v", err) + } + + p, err := ioutil.TempDir(os.TempDir(), "etcddumplogstest") + if err != nil { + t.Fatal("%v", err) + } + defer os.RemoveAll(p) + memberdir := filepath.Join(p, "member") + err = os.Mkdir(memberdir, 0744) + if err != nil { + t.Fatal("%v", err) + } + waldir := walDir(p) + snapdir := snapDir(p) + + w, err := wal.Create(zap.NewExample(), waldir, nil) + if err != nil { + t.Fatalf("err = %v, want nil", err) + } + + err = os.Mkdir(snapdir, 0744) + if err != nil { + t.Fatal("%v", err) + } + + ents := make([]raftpb.Entry, 0) + + // append entries into wal log + appendConfigChangeEnts(&ents) + appendNormalRequestEnts(&ents) + appendNormalIRREnts(&ents) + + // force commit newly appended entries + err = w.Save(raftpb.HardState{}, ents) + if err != nil { + t.Fatal("%v", err) + } + w.Close() + + argtests := []struct { + name string + args []string + fileExpected string + }{ + {"no entry-type", []string{p}, "expectedoutput/listAll.output"}, + {"confchange entry-type", []string{"-entry-type", "ConfigChange", p}, "expectedoutput/listConfChange.output"}, + {"normal entry-type", []string{"-entry-type", "Normal", p}, "expectedoutput/listNormal.output"}, + {"request entry-type", []string{"-entry-type", "Request", p}, "expectedoutput/listRequest.output"}, + {"internalRaftRequest entry-type", []string{"-entry-type", "InternalRaftRequest", p}, "expectedoutput/listInternalRaftRequest.output"}, + {"range entry-type", []string{"-entry-type", "InternalRaftRequestRange", p}, "expectedoutput/listRange.output"}, + {"put entry-type", []string{"-entry-type", "InternalRaftRequestPut", p}, "expectedoutput/listPut.output"}, + {"del entry-type", []string{"-entry-type", "InternalRaftRequestDeleteRange", p}, "expectedoutput/listDeleterange.output"}, + {"txn entry-type", []string{"-entry-type", "InternalRaftRequestTxn", p}, "expectedoutput/listTxn.output"}, + {"confchange and txn entry-type", []string{"-entry-type", "ConfigChange,InternalRaftRequestTxn", p}, "expectedoutput/listConfigChangeInternalRaftRequestTxn.output"}, + } + + for _, argtest := range argtests { + t.Run(argtest.name, func(t *testing.T) { + cmd := exec.Command(path.Join(dir, "etcd-dump-logs"), argtest.args...) + actual, err := cmd.CombinedOutput() + if err != nil { + t.Fatal("%v", err) + } + expected, err := ioutil.ReadFile(path.Join(dir, argtest.fileExpected)) + if err != nil { + t.Fatal("%v", err) + } + if !bytes.Equal(actual, expected) { + t.Errorf(`Got input of length %d, wanted input of length %d +==== BEGIN RECIEVED FILE ==== +%s +==== END RECIEVED FILE ==== +==== BEGIN EXPECTED FILE ==== +%s +==== END EXPECTED FILE ==== +`, len(actual), len(expected), actual, expected) + } + }) + } + +} + +func appendConfigChangeEnts(ents *[]raftpb.Entry) { + configChangeData := []raftpb.ConfChange{ + {1, raftpb.ConfChangeAddNode, 2, []byte(""), []byte("")}, + {2, raftpb.ConfChangeRemoveNode, 2, []byte(""), []byte("")}, + {3, raftpb.ConfChangeUpdateNode, 2, []byte(""), []byte("")}, + {4, raftpb.ConfChangeAddLearnerNode, 3, []byte(""), []byte("")}, + } + configChangeEntries := []raftpb.Entry{ + {1, 1, raftpb.EntryConfChange, pbutil.MustMarshal(&configChangeData[0]), []byte("")}, + {2, 2, raftpb.EntryConfChange, pbutil.MustMarshal(&configChangeData[1]), []byte("")}, + {2, 3, raftpb.EntryConfChange, pbutil.MustMarshal(&configChangeData[2]), []byte("")}, + {2, 4, raftpb.EntryConfChange, pbutil.MustMarshal(&configChangeData[3]), []byte("")}, + } + *ents = append(*ents, configChangeEntries...) +} + +func appendNormalRequestEnts(ents *[]raftpb.Entry) { + a := true + b := false + + requestdata := []etcdserverpb.Request{ + {0, "", "/path0", "{\"hey\":\"ho\",\"hi\":[\"yo\"]}", true, "", 0, &b, 9, false, 1, false, false, false, 1, false, &b, []byte("")}, + {1, "QGET", "/path1", "{\"0\":\"1\",\"2\":[\"3\"]}", false, "", 0, &b, 9, false, 1, false, false, false, 1, false, &b, []byte("")}, + {2, "SYNC", "/path2", "{\"0\":\"1\",\"2\":[\"3\"]}", false, "", 0, &b, 2, false, 1, false, false, false, 1, false, &b, []byte("")}, + {3, "DELETE", "/path3", "{\"hey\":\"ho\",\"hi\":[\"yo\"]}", false, "", 0, &a, 2, false, 1, false, false, false, 1, false, &b, []byte("")}, + {4, "RANDOM", "/path4/superlong" + strings.Repeat("/path", 30), "{\"hey\":\"ho\",\"hi\":[\"yo\"]}", false, "", 0, &b, 2, false, 1, false, false, false, 1, false, &b, []byte("")}, + } + e := []raftpb.Entry{ + {3, 5, raftpb.EntryNormal, pbutil.MustMarshal(&requestdata[0]), []byte("")}, + {3, 6, raftpb.EntryNormal, pbutil.MustMarshal(&requestdata[1]), []byte("")}, + {3, 7, raftpb.EntryNormal, pbutil.MustMarshal(&requestdata[2]), []byte("")}, + {3, 8, raftpb.EntryNormal, pbutil.MustMarshal(&requestdata[3]), []byte("")}, + {4, 9, raftpb.EntryNormal, pbutil.MustMarshal(&requestdata[4]), []byte("")}, + } + *ents = append(*ents, e...) +} + +func appendNormalIRREnts(ents *[]raftpb.Entry) { + r := &etcdserverpb.RangeRequest{ + Key: []byte("1"), + RangeEnd: []byte("hi"), + Limit: 6, + Revision: 1, + SortOrder: 1, + SortTarget: 0, + Serializable: false, + KeysOnly: false, + CountOnly: false, + MinModRevision: 0, + MaxModRevision: 20000, + MinCreateRevision: 0, + MaxCreateRevision: 20000, + } + + p := &etcdserverpb.PutRequest{ + Key: []byte("foo1"), + Value: []byte("bar1"), + Lease: 1, + PrevKv: false, + IgnoreValue: false, + IgnoreLease: true, + } + + d := &etcdserverpb.DeleteRangeRequest{ + Key: []byte("0"), + RangeEnd: []byte("9"), + PrevKv: true, + } + + delInRangeReq := &etcdserverpb.RequestOp{Request: &etcdserverpb.RequestOp_RequestDeleteRange{ + RequestDeleteRange: &etcdserverpb.DeleteRangeRequest{ + Key: []byte("a"), RangeEnd: []byte("b"), + }, + }, + } + + t := &etcdserverpb.TxnRequest{ + Success: []*etcdserverpb.RequestOp{delInRangeReq}, + Failure: []*etcdserverpb.RequestOp{delInRangeReq}, + } + + irrdata := []etcdserverpb.InternalRaftRequest{ + {ID: 5, Range: r}, + {ID: 6, Put: p}, + {ID: 7, DeleteRange: d}, + {ID: 8, Txn: t}, + } + + e := []raftpb.Entry{ + {4, 10, raftpb.EntryNormal, pbutil.MustMarshal(&irrdata[0]), []byte("")}, + {5, 11, raftpb.EntryNormal, pbutil.MustMarshal(&irrdata[1]), []byte("")}, + {6, 12, raftpb.EntryNormal, pbutil.MustMarshal(&irrdata[2]), []byte("")}, + {7, 13, raftpb.EntryNormal, pbutil.MustMarshal(&irrdata[3]), []byte("")}, + } + + *ents = append(*ents, e...) +} diff --git a/tools/etcd-dump-logs/expectedoutput/listAll.output b/tools/etcd-dump-logs/expectedoutput/listAll.output new file mode 100644 index 000000000000..cd1a8b6c9aec --- /dev/null +++ b/tools/etcd-dump-logs/expectedoutput/listAll.output @@ -0,0 +1,23 @@ +Snapshot: +empty +Start dupmping log entries from snapshot. +WAL metadata: +nodeID=0 clusterID=0 term=0 commitIndex=0 vote=0 +WAL entries: +lastIndex=13 +term index type data + 1 1 conf method=ConfChangeAddNode id=2 + 2 2 conf method=ConfChangeRemoveNode id=2 + 2 3 conf method=ConfChangeUpdateNode id=2 + 2 4 conf method=ConfChangeAddLearnerNode id=3 + 3 5 norm noop + 3 6 norm method=QGET path="/path1" + 3 7 norm method=SYNC time="1969-12-31 16:00:00.000000001 -0800 PST" + 3 8 norm method=DELETE path="/path3" + 4 9 norm method=RANDOM path="/path4/superlong/path/path/path/path/path/path/path/path/path/pa"..."path/path/path/path/path/path/path/path/path/path/path/path/path" val="{\"hey\":\"ho\",\"hi\":[\"yo\"]}" + 4 10 norm ID:5 range: + 5 11 norm ID:6 put: + 6 12 norm ID:7 delete_range: + 7 13 norm ID:8 txn: > failure: > > + +Entry types () count is : 13 \ No newline at end of file diff --git a/tools/etcd-dump-logs/expectedoutput/listConfChange.output b/tools/etcd-dump-logs/expectedoutput/listConfChange.output new file mode 100644 index 000000000000..24d00e7693e7 --- /dev/null +++ b/tools/etcd-dump-logs/expectedoutput/listConfChange.output @@ -0,0 +1,14 @@ +Snapshot: +empty +Start dupmping log entries from snapshot. +WAL metadata: +nodeID=0 clusterID=0 term=0 commitIndex=0 vote=0 +WAL entries: +lastIndex=13 +term index type data + 1 1 conf method=ConfChangeAddNode id=2 + 2 2 conf method=ConfChangeRemoveNode id=2 + 2 3 conf method=ConfChangeUpdateNode id=2 + 2 4 conf method=ConfChangeAddLearnerNode id=3 + +Entry types (ConfigChange) count is : 4 \ No newline at end of file diff --git a/tools/etcd-dump-logs/expectedoutput/listConfigChangeInternalRaftRequestTxn.output b/tools/etcd-dump-logs/expectedoutput/listConfigChangeInternalRaftRequestTxn.output new file mode 100644 index 000000000000..ffba7fa61309 --- /dev/null +++ b/tools/etcd-dump-logs/expectedoutput/listConfigChangeInternalRaftRequestTxn.output @@ -0,0 +1,15 @@ +Snapshot: +empty +Start dupmping log entries from snapshot. +WAL metadata: +nodeID=0 clusterID=0 term=0 commitIndex=0 vote=0 +WAL entries: +lastIndex=13 +term index type data + 1 1 conf method=ConfChangeAddNode id=2 + 2 2 conf method=ConfChangeRemoveNode id=2 + 2 3 conf method=ConfChangeUpdateNode id=2 + 2 4 conf method=ConfChangeAddLearnerNode id=3 + 7 13 norm ID:8 txn: > failure: > > + +Entry types (ConfigChange,InternalRaftRequestTxn) count is : 5 \ No newline at end of file diff --git a/tools/etcd-dump-logs/expectedoutput/listDeleterange.output b/tools/etcd-dump-logs/expectedoutput/listDeleterange.output new file mode 100644 index 000000000000..5f2a64dfc6da --- /dev/null +++ b/tools/etcd-dump-logs/expectedoutput/listDeleterange.output @@ -0,0 +1,11 @@ +Snapshot: +empty +Start dupmping log entries from snapshot. +WAL metadata: +nodeID=0 clusterID=0 term=0 commitIndex=0 vote=0 +WAL entries: +lastIndex=13 +term index type data + 6 12 norm ID:7 delete_range: + +Entry types (InternalRaftRequestDeleteRange) count is : 1 \ No newline at end of file diff --git a/tools/etcd-dump-logs/expectedoutput/listInternalRaftRequest.output b/tools/etcd-dump-logs/expectedoutput/listInternalRaftRequest.output new file mode 100644 index 000000000000..1fdbbcd47e94 --- /dev/null +++ b/tools/etcd-dump-logs/expectedoutput/listInternalRaftRequest.output @@ -0,0 +1,14 @@ +Snapshot: +empty +Start dupmping log entries from snapshot. +WAL metadata: +nodeID=0 clusterID=0 term=0 commitIndex=0 vote=0 +WAL entries: +lastIndex=13 +term index type data + 4 10 norm ID:5 range: + 5 11 norm ID:6 put: + 6 12 norm ID:7 delete_range: + 7 13 norm ID:8 txn: > failure: > > + +Entry types (InternalRaftRequest) count is : 4 \ No newline at end of file diff --git a/tools/etcd-dump-logs/expectedoutput/listNormal.output b/tools/etcd-dump-logs/expectedoutput/listNormal.output new file mode 100644 index 000000000000..9116a2d47784 --- /dev/null +++ b/tools/etcd-dump-logs/expectedoutput/listNormal.output @@ -0,0 +1,19 @@ +Snapshot: +empty +Start dupmping log entries from snapshot. +WAL metadata: +nodeID=0 clusterID=0 term=0 commitIndex=0 vote=0 +WAL entries: +lastIndex=13 +term index type data + 3 5 norm noop + 3 6 norm method=QGET path="/path1" + 3 7 norm method=SYNC time="1969-12-31 16:00:00.000000001 -0800 PST" + 3 8 norm method=DELETE path="/path3" + 4 9 norm method=RANDOM path="/path4/superlong/path/path/path/path/path/path/path/path/path/pa"..."path/path/path/path/path/path/path/path/path/path/path/path/path" val="{\"hey\":\"ho\",\"hi\":[\"yo\"]}" + 4 10 norm ID:5 range: + 5 11 norm ID:6 put: + 6 12 norm ID:7 delete_range: + 7 13 norm ID:8 txn: > failure: > > + +Entry types (Normal) count is : 9 \ No newline at end of file diff --git a/tools/etcd-dump-logs/expectedoutput/listPut.output b/tools/etcd-dump-logs/expectedoutput/listPut.output new file mode 100644 index 000000000000..b96f3ee34a94 --- /dev/null +++ b/tools/etcd-dump-logs/expectedoutput/listPut.output @@ -0,0 +1,11 @@ +Snapshot: +empty +Start dupmping log entries from snapshot. +WAL metadata: +nodeID=0 clusterID=0 term=0 commitIndex=0 vote=0 +WAL entries: +lastIndex=13 +term index type data + 5 11 norm ID:6 put: + +Entry types (InternalRaftRequestPut) count is : 1 \ No newline at end of file diff --git a/tools/etcd-dump-logs/expectedoutput/listRange.output b/tools/etcd-dump-logs/expectedoutput/listRange.output new file mode 100644 index 000000000000..4c94a601771c --- /dev/null +++ b/tools/etcd-dump-logs/expectedoutput/listRange.output @@ -0,0 +1,11 @@ +Snapshot: +empty +Start dupmping log entries from snapshot. +WAL metadata: +nodeID=0 clusterID=0 term=0 commitIndex=0 vote=0 +WAL entries: +lastIndex=13 +term index type data + 4 10 norm ID:5 range: + +Entry types (InternalRaftRequestRange) count is : 1 \ No newline at end of file diff --git a/tools/etcd-dump-logs/expectedoutput/listRequest.output b/tools/etcd-dump-logs/expectedoutput/listRequest.output new file mode 100644 index 000000000000..5335c465bfcb --- /dev/null +++ b/tools/etcd-dump-logs/expectedoutput/listRequest.output @@ -0,0 +1,18 @@ +Snapshot: +empty +Start dupmping log entries from snapshot. +WAL metadata: +nodeID=0 clusterID=0 term=0 commitIndex=0 vote=0 +WAL entries: +lastIndex=13 +term index type data + 3 5 norm noop + 3 6 norm method=QGET path="/path1" + 3 7 norm method=SYNC time="1969-12-31 16:00:00.000000001 -0800 PST" + 3 8 norm method=DELETE path="/path3" + 4 9 norm method=RANDOM path="/path4/superlong/path/path/path/path/path/path/path/path/path/pa"..."path/path/path/path/path/path/path/path/path/path/path/path/path" val="{\"hey\":\"ho\",\"hi\":[\"yo\"]}" + 4 10 norm noop + 5 11 norm noop + 7 13 norm noop + +Entry types (Request) count is : 8 \ No newline at end of file diff --git a/tools/etcd-dump-logs/expectedoutput/listTxn.output b/tools/etcd-dump-logs/expectedoutput/listTxn.output new file mode 100644 index 000000000000..ab7889369058 --- /dev/null +++ b/tools/etcd-dump-logs/expectedoutput/listTxn.output @@ -0,0 +1,11 @@ +Snapshot: +empty +Start dupmping log entries from snapshot. +WAL metadata: +nodeID=0 clusterID=0 term=0 commitIndex=0 vote=0 +WAL entries: +lastIndex=13 +term index type data + 7 13 norm ID:8 txn: > failure: > > + +Entry types (InternalRaftRequestTxn) count is : 1 \ No newline at end of file diff --git a/tools/etcd-dump-logs/main.go b/tools/etcd-dump-logs/main.go index 6ac060b2333a..c14b820f466a 100644 --- a/tools/etcd-dump-logs/main.go +++ b/tools/etcd-dump-logs/main.go @@ -1,4 +1,4 @@ -// Copyright 2015 The etcd Authors +// Copyright 2018 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. @@ -20,6 +20,7 @@ import ( "log" "path/filepath" "time" + "strings" "github.com/coreos/etcd/etcdserver/etcdserverpb" "github.com/coreos/etcd/pkg/pbutil" @@ -35,6 +36,8 @@ import ( func main() { snapfile := flag.String("start-snap", "", "The base name of snapshot file to start dumping") index := flag.Uint64("start-index", 0, "The index to start dumping") + entrytype := flag.String("entry-type", "", "If set, filters output by entry type. Must be one or more than one of: ConfigChange, Normal, Request, InternalRaftRequest, InternalRaftRequestRange, InternalRaftRequestPut, InternalRaftRequestDeleteRange, InternalRaftRequestTxn") + flag.Parse() if len(flag.Args()) != 1 { @@ -96,46 +99,7 @@ func main() { fmt.Printf("WAL entries:\n") fmt.Printf("lastIndex=%d\n", ents[len(ents)-1].Index) fmt.Printf("%4s\t%10s\ttype\tdata\n", "term", "index") - for _, e := range ents { - msg := fmt.Sprintf("%4d\t%10d", e.Term, e.Index) - switch e.Type { - case raftpb.EntryNormal: - msg = fmt.Sprintf("%s\tnorm", msg) - - var rr etcdserverpb.InternalRaftRequest - if err := rr.Unmarshal(e.Data); err == nil { - msg = fmt.Sprintf("%s\t%s", msg, rr.String()) - break - } - - // TODO: remove sensitive information - // (https://github.com/coreos/etcd/issues/7620) - var r etcdserverpb.Request - if err := r.Unmarshal(e.Data); err == nil { - switch r.Method { - case "": - msg = fmt.Sprintf("%s\tnoop", msg) - case "SYNC": - msg = fmt.Sprintf("%s\tmethod=SYNC time=%q", msg, time.Unix(0, r.Time)) - case "QGET", "DELETE": - msg = fmt.Sprintf("%s\tmethod=%s path=%s", msg, r.Method, excerpt(r.Path, 64, 64)) - default: - msg = fmt.Sprintf("%s\tmethod=%s path=%s val=%s", msg, r.Method, excerpt(r.Path, 64, 64), excerpt(r.Val, 128, 0)) - } - break - } - msg = fmt.Sprintf("%s\t???", msg) - case raftpb.EntryConfChange: - msg = fmt.Sprintf("%s\tconf", msg) - var r raftpb.ConfChange - if err := r.Unmarshal(e.Data); err != nil { - msg = fmt.Sprintf("%s\t???", msg) - } else { - msg = fmt.Sprintf("%s\tmethod=%s id=%s", msg, r.Type, types.ID(r.NodeID)) - } - } - fmt.Println(msg) - } + listEntriesType(*entrytype, ents) } func walDir(dataDir string) string { return filepath.Join(dataDir, "member", "wal") } @@ -166,3 +130,140 @@ func excerpt(str string, pre, suf int) string { } return fmt.Sprintf("%q...%q", str[:pre], str[len(str)-suf:]) } + +type EntryFilter func(e raftpb.Entry) (bool, string) + +// The 6 pass functions below takes the raftpb.Entry and return if the entry should be printed and the type of entry, +// the type of the entry will used in the following print function +func passConfChange(entry raftpb.Entry) (bool, string) { + return entry.Type == raftpb.EntryConfChange, "ConfigChange" +} + +func passRequest(entry raftpb.Entry) (bool, string) { + var rr etcdserverpb.Request + return entry.Type == raftpb.EntryNormal && rr.Unmarshal(entry.Data) == nil, "Request" +} + +func passInternalRaftRequestRange(entry raftpb.Entry) (bool, string) { + var rr etcdserverpb.InternalRaftRequest + return entry.Type == raftpb.EntryNormal && rr.Unmarshal(entry.Data) == nil && rr.Range != nil, "InternalRaftRequest" +} + +func passInternalRaftRequestPut(entry raftpb.Entry) (bool, string) { + var rr etcdserverpb.InternalRaftRequest + return entry.Type == raftpb.EntryNormal && rr.Unmarshal(entry.Data) == nil && rr.Put != nil, "InternalRaftRequest" +} + +func passInternalRaftRequestDeleteRange(entry raftpb.Entry) (bool, string) { + var rr etcdserverpb.InternalRaftRequest + return entry.Type == raftpb.EntryNormal && rr.Unmarshal(entry.Data) == nil && rr.DeleteRange != nil, "InternalRaftRequest" +} + +func passInternalRaftRequestTxn(entry raftpb.Entry) (bool, string) { + var rr etcdserverpb.InternalRaftRequest + return entry.Type == raftpb.EntryNormal && rr.Unmarshal(entry.Data) == nil && rr.Txn != nil, "InternalRaftRequest" +} + +type EntryPrinter func(e raftpb.Entry) + +// The 3 print functions below print the entry format based on there types +func printConfChange(entry raftpb.Entry) { + fmt.Printf("%4d\t%10d", entry.Term, entry.Index) + fmt.Printf("\tconf") + var r raftpb.ConfChange + if err := r.Unmarshal(entry.Data); err != nil { + fmt.Printf("\t???\n") + } else { + fmt.Printf("\tmethod=%s id=%s\n", r.Type, types.ID(r.NodeID)) + } +} + +func printRequest(entry raftpb.Entry) { + var r etcdserverpb.Request + if err := r.Unmarshal(entry.Data); err == nil { + fmt.Printf("%4d\t%10d\tnorm", entry.Term, entry.Index) + //cnt ++ + switch r.Method { + case "": + fmt.Printf("\tnoop\n") + case "SYNC": + fmt.Printf("\tmethod=SYNC time=%q\n", time.Unix(0, r.Time)) + case "QGET", "DELETE": + fmt.Printf("\tmethod=%s path=%s\n", r.Method, excerpt(r.Path, 64, 64)) + default: + fmt.Printf("\tmethod=%s path=%s val=%s\n", r.Method, excerpt(r.Path, 64, 64), excerpt(r.Val, 128, 0)) + } + } +} + +// printInternalRaftRequest is used to print entry information for InternalRaftRequestRange, InternalRaftRequestPut, +// InternalRaftRequestDeleteRange and InternalRaftRequestTxn entries +func printInternalRaftRequest(entry raftpb.Entry) { + var rr etcdserverpb.InternalRaftRequest + if err := rr.Unmarshal(entry.Data); err == nil { + fmt.Printf("%4d\t%10d\tnorm\t%s\n", entry.Term, entry.Index, rr.String()) + } +} + +// evaluateEntrytypeFlag evaluates entry-type flag and choose proper filter/filters to filter entries +func evaluateEntrytypeFlag(entrytype string) []EntryFilter { + var entrytypelist []string + if entrytype != "" { + entrytypelist = strings.Split(entrytype, ",") + } + + validRequest := map[string][]EntryFilter{"ConfigChange": {passConfChange}, + "Normal": {passInternalRaftRequestRange, passInternalRaftRequestPut, passInternalRaftRequestDeleteRange, passInternalRaftRequestTxn, passRequest}, + "Request": {passRequest}, + "InternalRaftRequest": {passInternalRaftRequestRange, passInternalRaftRequestPut, passInternalRaftRequestDeleteRange, passInternalRaftRequestTxn}, + "InternalRaftRequestRange": {passInternalRaftRequestRange}, + "InternalRaftRequestPut": {passInternalRaftRequestPut}, + "InternalRaftRequestDeleteRange": {passInternalRaftRequestDeleteRange}, + "InternalRaftRequestTxn": {passInternalRaftRequestTxn}} + filters := make([]EntryFilter, 0) + if len(entrytypelist) == 0 { + filters = append(filters, passConfChange) + filters = append(filters, passInternalRaftRequestRange) + filters = append(filters, passInternalRaftRequestPut) + filters = append(filters, passInternalRaftRequestDeleteRange) + filters = append(filters, passInternalRaftRequestTxn) + filters = append(filters, passRequest) + } + for _, et := range entrytypelist { + if f, ok := validRequest[et]; ok { + filters = append(filters, f...) + } else { + log.Printf(`[%+v] is not a valid entry-type, ignored. +Please set entry-type to one or more of the following: +ConfigChange, Normal, Request, InternalRaftRequest, +InternalRaftRequestRange, InternalRaftRequestPut, InternalRaftRequestDeleteRange, InternalRaftRequestTxn`, et) + } + } + + return filters +} + +// listEntriesType filters and prints entries based on the entry-type flag, +func listEntriesType(entrytype string, ents []raftpb.Entry) { + entryFilters := evaluateEntrytypeFlag(entrytype) + printerMap := map[string]EntryPrinter{"ConfigChange": printConfChange, + "Request": printRequest, + "InternalRaftRequest": printInternalRaftRequest} + cnt := 0 + for _, e := range ents { + passed := false + currtype := "" + for _, filter := range entryFilters { + passed, currtype = filter(e) + if passed { + cnt++ + break + } + } + if passed { + printer := printerMap[currtype] + printer(e) + } + } + fmt.Printf("\nEntry types (%s) count is : %d", entrytype, cnt) +}