2016-08-11 19:41:44 -04:00
|
|
|
// Copyright 2015 The etcd Authors
|
2016-06-07 17:28:28 -04:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package raft
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-03-10 16:07:02 -05:00
|
|
|
pb "go.etcd.io/etcd/raft/v3/raftpb"
|
|
|
|
"go.etcd.io/etcd/raft/v3/tracker"
|
2016-06-07 17:28:28 -04:00
|
|
|
)
|
|
|
|
|
2022-03-10 16:07:02 -05:00
|
|
|
// Status contains information about this Raft peer and its view of the system.
|
|
|
|
// The Progress is only populated on the leader.
|
2016-06-07 17:28:28 -04:00
|
|
|
type Status struct {
|
2022-03-10 16:07:02 -05:00
|
|
|
BasicStatus
|
|
|
|
Config tracker.Config
|
|
|
|
Progress map[uint64]tracker.Progress
|
|
|
|
}
|
|
|
|
|
|
|
|
// BasicStatus contains basic information about the Raft peer. It does not allocate.
|
|
|
|
type BasicStatus struct {
|
2016-06-07 17:28:28 -04:00
|
|
|
ID uint64
|
|
|
|
|
|
|
|
pb.HardState
|
|
|
|
SoftState
|
|
|
|
|
2022-03-10 16:07:02 -05:00
|
|
|
Applied uint64
|
2018-09-08 02:39:41 -04:00
|
|
|
|
|
|
|
LeadTransferee uint64
|
2016-06-07 17:28:28 -04:00
|
|
|
}
|
|
|
|
|
2022-03-10 16:07:02 -05:00
|
|
|
func getProgressCopy(r *raft) map[uint64]tracker.Progress {
|
|
|
|
m := make(map[uint64]tracker.Progress)
|
|
|
|
r.prs.Visit(func(id uint64, pr *tracker.Progress) {
|
|
|
|
p := *pr
|
|
|
|
p.Inflights = pr.Inflights.Clone()
|
|
|
|
pr = nil
|
|
|
|
|
|
|
|
m[id] = p
|
|
|
|
})
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func getBasicStatus(r *raft) BasicStatus {
|
|
|
|
s := BasicStatus{
|
2018-09-08 02:39:41 -04:00
|
|
|
ID: r.id,
|
|
|
|
LeadTransferee: r.leadTransferee,
|
|
|
|
}
|
2016-06-07 17:28:28 -04:00
|
|
|
s.HardState = r.hardState()
|
|
|
|
s.SoftState = *r.softState()
|
|
|
|
s.Applied = r.raftLog.applied
|
2022-03-10 16:07:02 -05:00
|
|
|
return s
|
|
|
|
}
|
2016-06-07 17:28:28 -04:00
|
|
|
|
2022-03-10 16:07:02 -05:00
|
|
|
// getStatus gets a copy of the current raft status.
|
|
|
|
func getStatus(r *raft) Status {
|
|
|
|
var s Status
|
|
|
|
s.BasicStatus = getBasicStatus(r)
|
2016-06-07 17:28:28 -04:00
|
|
|
if s.RaftState == StateLeader {
|
2022-03-10 16:07:02 -05:00
|
|
|
s.Progress = getProgressCopy(r)
|
2016-06-07 17:28:28 -04:00
|
|
|
}
|
2022-03-10 16:07:02 -05:00
|
|
|
s.Config = r.prs.Config.Clone()
|
2016-06-07 17:28:28 -04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON translates the raft status into JSON.
|
|
|
|
// TODO: try to simplify this by introducing ID type into raft
|
|
|
|
func (s Status) MarshalJSON() ([]byte, error) {
|
2018-09-08 02:39:41 -04:00
|
|
|
j := fmt.Sprintf(`{"id":"%x","term":%d,"vote":"%x","commit":%d,"lead":"%x","raftState":%q,"applied":%d,"progress":{`,
|
|
|
|
s.ID, s.Term, s.Vote, s.Commit, s.Lead, s.RaftState, s.Applied)
|
2016-06-07 17:28:28 -04:00
|
|
|
|
|
|
|
if len(s.Progress) == 0 {
|
2018-09-08 02:39:41 -04:00
|
|
|
j += "},"
|
2016-06-07 17:28:28 -04:00
|
|
|
} else {
|
|
|
|
for k, v := range s.Progress {
|
|
|
|
subj := fmt.Sprintf(`"%x":{"match":%d,"next":%d,"state":%q},`, k, v.Match, v.Next, v.State)
|
|
|
|
j += subj
|
|
|
|
}
|
|
|
|
// remove the trailing ","
|
2018-09-08 02:39:41 -04:00
|
|
|
j = j[:len(j)-1] + "},"
|
2016-06-07 17:28:28 -04:00
|
|
|
}
|
2018-09-08 02:39:41 -04:00
|
|
|
|
|
|
|
j += fmt.Sprintf(`"leadtransferee":"%x"}`, s.LeadTransferee)
|
2016-06-07 17:28:28 -04:00
|
|
|
return []byte(j), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Status) String() string {
|
|
|
|
b, err := s.MarshalJSON()
|
|
|
|
if err != nil {
|
2022-03-10 16:07:02 -05:00
|
|
|
getLogger().Panicf("unexpected error: %v", err)
|
2016-06-07 17:28:28 -04:00
|
|
|
}
|
|
|
|
return string(b)
|
|
|
|
}
|