main.go 3.8 KB

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