main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 r etcdserverpb.Request
  91. if err := r.Unmarshal(e.Data); err != nil {
  92. msg = fmt.Sprintf("%s\t???", msg)
  93. break
  94. }
  95. switch r.Method {
  96. case "":
  97. msg = fmt.Sprintf("%s\tnoop", msg)
  98. case "SYNC":
  99. msg = fmt.Sprintf("%s\tmethod=SYNC time=%q", msg, time.Unix(0, r.Time))
  100. case "QGET", "DELETE":
  101. msg = fmt.Sprintf("%s\tmethod=%s path=%s", msg, r.Method, excerpt(r.Path, 64, 64))
  102. default:
  103. msg = fmt.Sprintf("%s\tmethod=%s path=%s val=%s", msg, r.Method, excerpt(r.Path, 64, 64), excerpt(r.Val, 128, 0))
  104. }
  105. case raftpb.EntryConfChange:
  106. msg = fmt.Sprintf("%s\tconf", msg)
  107. var r raftpb.ConfChange
  108. if err := r.Unmarshal(e.Data); err != nil {
  109. msg = fmt.Sprintf("%s\t???", msg)
  110. } else {
  111. msg = fmt.Sprintf("%s\tmethod=%s id=%s", msg, r.Type, types.ID(r.NodeID))
  112. }
  113. }
  114. fmt.Println(msg)
  115. }
  116. }
  117. func walDir(dataDir string) string { return path.Join(dataDir, "member", "wal") }
  118. func snapDir(dataDir string) string { return path.Join(dataDir, "member", "snap") }
  119. func parseWALMetadata(b []byte) (id, cid types.ID) {
  120. var metadata etcdserverpb.Metadata
  121. pbutil.MustUnmarshal(&metadata, b)
  122. id = types.ID(metadata.NodeID)
  123. cid = types.ID(metadata.ClusterID)
  124. return
  125. }
  126. func genIDSlice(a []uint64) []types.ID {
  127. ids := make([]types.ID, len(a))
  128. for i, id := range a {
  129. ids[i] = types.ID(id)
  130. }
  131. return ids
  132. }
  133. // excerpt replaces middle part with ellipsis and returns a double-quoted
  134. // string safely escaped with Go syntax.
  135. func excerpt(str string, pre, suf int) string {
  136. if pre+suf > len(str) {
  137. return fmt.Sprintf("%q", str)
  138. }
  139. return fmt.Sprintf("%q...%q", str[:pre], str[len(str)-suf:])
  140. }