main.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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("start-snap", "", "The base name of snapshot file to start dumping")
  32. index := flag.Uint64("start-index", 0, "The index to start dumping")
  33. flag.Parse()
  34. if *from == "" {
  35. log.Fatal("Must provide -data-dir flag.")
  36. }
  37. if *snapfile != "" && *index != 0 {
  38. log.Fatal("start-snap and start-index flags cannot be used together.")
  39. }
  40. var (
  41. walsnap walpb.Snapshot
  42. snapshot *raftpb.Snapshot
  43. err error
  44. )
  45. isIndex := *index != 0
  46. if isIndex {
  47. fmt.Printf("Start dumping log entries from index %d.\n", *index)
  48. walsnap.Index = *index
  49. } else {
  50. if *snapfile == "" {
  51. ss := snap.New(snapDir(*from))
  52. snapshot, err = ss.Load()
  53. } else {
  54. snapshot, err = snap.Read(path.Join(snapDir(*from), *snapfile))
  55. }
  56. switch err {
  57. case nil:
  58. walsnap.Index, walsnap.Term = snapshot.Metadata.Index, snapshot.Metadata.Term
  59. nodes := genIDSlice(snapshot.Metadata.ConfState.Nodes)
  60. fmt.Printf("Snapshot:\nterm=%d index=%d nodes=%s\n",
  61. walsnap.Term, walsnap.Index, nodes)
  62. case snap.ErrNoSnapshot:
  63. fmt.Printf("Snapshot:\nempty\n")
  64. default:
  65. log.Fatalf("Failed loading snapshot: %v", err)
  66. }
  67. fmt.Println("Start dupmping log entries from snapshot.")
  68. }
  69. w, err := wal.Open(walDir(*from), walsnap)
  70. if err != nil {
  71. log.Fatalf("Failed opening WAL: %v", err)
  72. }
  73. wmetadata, state, ents, err := w.ReadAll()
  74. w.Close()
  75. if err != nil && (!isIndex || err != wal.ErrSnapshotNotFound) {
  76. log.Fatalf("Failed reading WAL: %v", err)
  77. }
  78. id, cid := parseWALMetadata(wmetadata)
  79. vid := types.ID(state.Vote)
  80. fmt.Printf("WAL metadata:\nnodeID=%s clusterID=%s term=%d commitIndex=%d vote=%s\n",
  81. id, cid, state.Term, state.Commit, vid)
  82. fmt.Printf("WAL entries:\n")
  83. fmt.Printf("lastIndex=%d\n", ents[len(ents)-1].Index)
  84. fmt.Printf("%4s\t%10s\ttype\tdata\n", "term", "index")
  85. for _, e := range ents {
  86. msg := fmt.Sprintf("%4d\t%10d", e.Term, e.Index)
  87. switch e.Type {
  88. case raftpb.EntryNormal:
  89. msg = fmt.Sprintf("%s\tnorm", msg)
  90. var rr etcdserverpb.InternalRaftRequest
  91. if err := rr.Unmarshal(e.Data); err == nil {
  92. msg = fmt.Sprintf("%s\t%s", msg, rr.String())
  93. break
  94. }
  95. var r etcdserverpb.Request
  96. if err := r.Unmarshal(e.Data); err == nil {
  97. switch r.Method {
  98. case "":
  99. msg = fmt.Sprintf("%s\tnoop", msg)
  100. case "SYNC":
  101. msg = fmt.Sprintf("%s\tmethod=SYNC time=%q", msg, time.Unix(0, r.Time))
  102. case "QGET", "DELETE":
  103. msg = fmt.Sprintf("%s\tmethod=%s path=%s", msg, r.Method, excerpt(r.Path, 64, 64))
  104. default:
  105. msg = fmt.Sprintf("%s\tmethod=%s path=%s val=%s", msg, r.Method, excerpt(r.Path, 64, 64), excerpt(r.Val, 128, 0))
  106. }
  107. break
  108. }
  109. msg = fmt.Sprintf("%s\t???", msg)
  110. case raftpb.EntryConfChange:
  111. msg = fmt.Sprintf("%s\tconf", msg)
  112. var r raftpb.ConfChange
  113. if err := r.Unmarshal(e.Data); err != nil {
  114. msg = fmt.Sprintf("%s\t???", msg)
  115. } else {
  116. msg = fmt.Sprintf("%s\tmethod=%s id=%s", msg, r.Type, types.ID(r.NodeID))
  117. }
  118. }
  119. fmt.Println(msg)
  120. }
  121. }
  122. func walDir(dataDir string) string { return path.Join(dataDir, "member", "wal") }
  123. func snapDir(dataDir string) string { return path.Join(dataDir, "member", "snap") }
  124. func parseWALMetadata(b []byte) (id, cid types.ID) {
  125. var metadata etcdserverpb.Metadata
  126. pbutil.MustUnmarshal(&metadata, b)
  127. id = types.ID(metadata.NodeID)
  128. cid = types.ID(metadata.ClusterID)
  129. return
  130. }
  131. func genIDSlice(a []uint64) []types.ID {
  132. ids := make([]types.ID, len(a))
  133. for i, id := range a {
  134. ids[i] = types.ID(id)
  135. }
  136. return ids
  137. }
  138. // excerpt replaces middle part with ellipsis and returns a double-quoted
  139. // string safely escaped with Go syntax.
  140. func excerpt(str string, pre, suf int) string {
  141. if pre+suf > len(str) {
  142. return fmt.Sprintf("%q", str)
  143. }
  144. return fmt.Sprintf("%q...%q", str[:pre], str[len(str)-suf:])
  145. }