main.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "flag"
  17. "fmt"
  18. "log"
  19. "path"
  20. "time"
  21. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. "github.com/coreos/etcd/pkg/pbutil"
  23. "github.com/coreos/etcd/pkg/types"
  24. "github.com/coreos/etcd/raft/raftpb"
  25. "github.com/coreos/etcd/snap"
  26. "github.com/coreos/etcd/wal"
  27. "github.com/coreos/etcd/wal/walpb"
  28. )
  29. func main() {
  30. from := flag.String("data-dir", "", "")
  31. snapfile := flag.String("snap-file", "", "The base name of snapshot file to read")
  32. flag.Parse()
  33. if *from == "" {
  34. log.Fatal("Must provide -data-dir flag")
  35. }
  36. var (
  37. snapshot *raftpb.Snapshot
  38. err error
  39. )
  40. if *snapfile == "" {
  41. ss := snap.New(snapDir(*from))
  42. snapshot, err = ss.Load()
  43. } else {
  44. snapshot, err = snap.Read(path.Join(snapDir(*from), *snapfile))
  45. }
  46. var walsnap walpb.Snapshot
  47. switch err {
  48. case nil:
  49. walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term
  50. nodes := genIDSlice(snapshot.Metadata.ConfState.Nodes)
  51. fmt.Printf("Snapshot:\nterm=%d index=%d nodes=%s\n",
  52. walsnap.Term, walsnap.Index, nodes)
  53. case snap.ErrNoSnapshot:
  54. fmt.Printf("Snapshot:\nempty\n")
  55. default:
  56. log.Fatalf("Failed loading snapshot: %v", err)
  57. }
  58. w, err := wal.Open(walDir(*from), walsnap)
  59. if err != nil {
  60. log.Fatalf("Failed opening WAL: %v", err)
  61. }
  62. wmetadata, state, ents, err := w.ReadAll()
  63. w.Close()
  64. if err != nil {
  65. log.Fatalf("Failed reading WAL: %v", err)
  66. }
  67. id, cid := parseWALMetadata(wmetadata)
  68. vid := types.ID(state.Vote)
  69. fmt.Printf("WAL metadata:\nnodeID=%s clusterID=%s term=%d commitIndex=%d vote=%s\n",
  70. id, cid, state.Term, state.Commit, vid)
  71. fmt.Printf("WAL entries:\n")
  72. fmt.Printf("lastIndex=%d\n", ents[len(ents)-1].Index)
  73. fmt.Printf("%4s\t%10s\ttype\tdata\n", "term", "index")
  74. for _, e := range ents {
  75. msg := fmt.Sprintf("%4d\t%10d", e.Term, e.Index)
  76. switch e.Type {
  77. case raftpb.EntryNormal:
  78. msg = fmt.Sprintf("%s\tnorm", msg)
  79. var r etcdserverpb.Request
  80. if err := r.Unmarshal(e.Data); err != nil {
  81. msg = fmt.Sprintf("%s\t???", msg)
  82. break
  83. }
  84. switch r.Method {
  85. case "":
  86. msg = fmt.Sprintf("%s\tnoop", msg)
  87. case "SYNC":
  88. msg = fmt.Sprintf("%s\tmethod=SYNC time=%q", msg, time.Unix(0, r.Time))
  89. case "QGET", "DELETE":
  90. msg = fmt.Sprintf("%s\tmethod=%s path=%s", msg, r.Method, excerpt(r.Path, 64, 64))
  91. default:
  92. msg = fmt.Sprintf("%s\tmethod=%s path=%s val=%s", msg, r.Method, excerpt(r.Path, 64, 64), excerpt(r.Val, 128, 0))
  93. }
  94. case raftpb.EntryConfChange:
  95. msg = fmt.Sprintf("%s\tconf", msg)
  96. var r raftpb.ConfChange
  97. if err := r.Unmarshal(e.Data); err != nil {
  98. msg = fmt.Sprintf("%s\t???", msg)
  99. } else {
  100. msg = fmt.Sprintf("%s\tmethod=%s id=%s", msg, r.Type, types.ID(r.NodeID))
  101. }
  102. }
  103. fmt.Println(msg)
  104. }
  105. }
  106. func walDir(dataDir string) string { return path.Join(dataDir, "member", "wal") }
  107. func snapDir(dataDir string) string { return path.Join(dataDir, "member", "snap") }
  108. func parseWALMetadata(b []byte) (id, cid types.ID) {
  109. var metadata etcdserverpb.Metadata
  110. pbutil.MustUnmarshal(&metadata, b)
  111. id = types.ID(metadata.NodeID)
  112. cid = types.ID(metadata.ClusterID)
  113. return
  114. }
  115. func genIDSlice(a []uint64) []types.ID {
  116. ids := make([]types.ID, len(a))
  117. for i, id := range a {
  118. ids[i] = types.ID(id)
  119. }
  120. return ids
  121. }
  122. // excerpt replaces middle part with ellipsis and returns a double-quoted
  123. // string safely escaped with Go syntax.
  124. func excerpt(str string, pre, suf int) string {
  125. if pre+suf > len(str) {
  126. return fmt.Sprintf("%q", str)
  127. }
  128. return fmt.Sprintf("%q...%q", str[:pre], str[len(str)-suf:])
  129. }