status.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2015 The etcd Authors
  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 raft
  15. import (
  16. "fmt"
  17. pb "go.etcd.io/etcd/raft/raftpb"
  18. "go.etcd.io/etcd/raft/tracker"
  19. )
  20. type Status struct {
  21. ID uint64
  22. pb.HardState
  23. SoftState
  24. Applied uint64
  25. Progress map[uint64]tracker.Progress
  26. LeadTransferee uint64
  27. }
  28. func getProgressCopy(r *raft) map[uint64]tracker.Progress {
  29. m := make(map[uint64]tracker.Progress)
  30. r.prs.Visit(func(id uint64, pr *tracker.Progress) {
  31. var p tracker.Progress
  32. p, pr = *pr, nil /* avoid accidental reuse below */
  33. // The inflight buffer is tricky to copy and besides, it isn't exposed
  34. // to the client, so pretend it's nil.
  35. p.Inflights = nil
  36. m[id] = p
  37. })
  38. return m
  39. }
  40. func getStatusWithoutProgress(r *raft) Status {
  41. s := Status{
  42. ID: r.id,
  43. LeadTransferee: r.leadTransferee,
  44. }
  45. s.HardState = r.hardState()
  46. s.SoftState = *r.softState()
  47. s.Applied = r.raftLog.applied
  48. return s
  49. }
  50. // getStatus gets a copy of the current raft status.
  51. func getStatus(r *raft) Status {
  52. s := getStatusWithoutProgress(r)
  53. if s.RaftState == StateLeader {
  54. s.Progress = getProgressCopy(r)
  55. }
  56. return s
  57. }
  58. // MarshalJSON translates the raft status into JSON.
  59. // TODO: try to simplify this by introducing ID type into raft
  60. func (s Status) MarshalJSON() ([]byte, error) {
  61. j := fmt.Sprintf(`{"id":"%x","term":%d,"vote":"%x","commit":%d,"lead":"%x","raftState":%q,"applied":%d,"progress":{`,
  62. s.ID, s.Term, s.Vote, s.Commit, s.Lead, s.RaftState, s.Applied)
  63. if len(s.Progress) == 0 {
  64. j += "},"
  65. } else {
  66. for k, v := range s.Progress {
  67. subj := fmt.Sprintf(`"%x":{"match":%d,"next":%d,"state":%q},`, k, v.Match, v.Next, v.State)
  68. j += subj
  69. }
  70. // remove the trailing ","
  71. j = j[:len(j)-1] + "},"
  72. }
  73. j += fmt.Sprintf(`"leadtransferee":"%x"}`, s.LeadTransferee)
  74. return []byte(j), nil
  75. }
  76. func (s Status) String() string {
  77. b, err := s.MarshalJSON()
  78. if err != nil {
  79. raftLogger.Panicf("unexpected error: %v", err)
  80. }
  81. return string(b)
  82. }