status.go 2.4 KB

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