status.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // Status contains information about this Raft peer and its view of the system.
  21. // The Progress is only populated on the leader.
  22. type Status struct {
  23. BasicStatus
  24. Config tracker.Config
  25. Progress map[uint64]tracker.Progress
  26. }
  27. // BasicStatus contains basic information about the Raft peer. It does not allocate.
  28. type BasicStatus struct {
  29. ID uint64
  30. pb.HardState
  31. SoftState
  32. Applied uint64
  33. LeadTransferee uint64
  34. }
  35. func getProgressCopy(r *raft) map[uint64]tracker.Progress {
  36. m := make(map[uint64]tracker.Progress)
  37. r.prs.Visit(func(id uint64, pr *tracker.Progress) {
  38. var p tracker.Progress
  39. p = *pr
  40. p.Inflights = pr.Inflights.Clone()
  41. pr = nil
  42. m[id] = p
  43. })
  44. return m
  45. }
  46. func getBasicStatus(r *raft) BasicStatus {
  47. s := BasicStatus{
  48. ID: r.id,
  49. LeadTransferee: r.leadTransferee,
  50. }
  51. s.HardState = r.hardState()
  52. s.SoftState = *r.softState()
  53. s.Applied = r.raftLog.applied
  54. return s
  55. }
  56. // getStatus gets a copy of the current raft status.
  57. func getStatus(r *raft) Status {
  58. var s Status
  59. s.BasicStatus = getBasicStatus(r)
  60. if s.RaftState == StateLeader {
  61. s.Progress = getProgressCopy(r)
  62. }
  63. s.Config = r.prs.Config.Clone()
  64. return s
  65. }
  66. // MarshalJSON translates the raft status into JSON.
  67. // TODO: try to simplify this by introducing ID type into raft
  68. func (s Status) MarshalJSON() ([]byte, error) {
  69. j := fmt.Sprintf(`{"id":"%x","term":%d,"vote":"%x","commit":%d,"lead":"%x","raftState":%q,"applied":%d,"progress":{`,
  70. s.ID, s.Term, s.Vote, s.Commit, s.Lead, s.RaftState, s.Applied)
  71. if len(s.Progress) == 0 {
  72. j += "},"
  73. } else {
  74. for k, v := range s.Progress {
  75. subj := fmt.Sprintf(`"%x":{"match":%d,"next":%d,"state":%q},`, k, v.Match, v.Next, v.State)
  76. j += subj
  77. }
  78. // remove the trailing ","
  79. j = j[:len(j)-1] + "},"
  80. }
  81. j += fmt.Sprintf(`"leadtransferee":"%x"}`, s.LeadTransferee)
  82. return []byte(j), nil
  83. }
  84. func (s Status) String() string {
  85. b, err := s.MarshalJSON()
  86. if err != nil {
  87. raftLogger.Panicf("unexpected error: %v", err)
  88. }
  89. return string(b)
  90. }