...

Source file src/go.etcd.io/etcd/raft/v3/status.go

Documentation: go.etcd.io/etcd/raft/v3

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

View as plain text