1 // Copyright 2015 The etcd Authors
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 pb "github.com/coreos/etcd/raft/raftpb"
24 func (st StateType) MarshalJSON() ([]byte, error) {
25 return []byte(fmt.Sprintf("%q", st.String())), nil
28 // uint64Slice implements sort interface
29 type uint64Slice []uint64
31 func (p uint64Slice) Len() int { return len(p) }
32 func (p uint64Slice) Less(i, j int) bool { return p[i] < p[j] }
33 func (p uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
35 func min(a, b uint64) uint64 {
42 func max(a, b uint64) uint64 {
49 func IsLocalMsg(msgt pb.MessageType) bool {
50 return msgt == pb.MsgHup || msgt == pb.MsgBeat || msgt == pb.MsgUnreachable ||
51 msgt == pb.MsgSnapStatus || msgt == pb.MsgCheckQuorum
54 func IsResponseMsg(msgt pb.MessageType) bool {
55 return msgt == pb.MsgAppResp || msgt == pb.MsgVoteResp || msgt == pb.MsgHeartbeatResp || msgt == pb.MsgUnreachable || msgt == pb.MsgPreVoteResp
58 // voteResponseType maps vote and prevote message types to their corresponding responses.
59 func voteRespMsgType(msgt pb.MessageType) pb.MessageType {
64 return pb.MsgPreVoteResp
66 panic(fmt.Sprintf("not a vote message: %s", msgt))
70 // EntryFormatter can be implemented by the application to provide human-readable formatting
71 // of entry data. Nil is a valid EntryFormatter and will use a default format.
72 type EntryFormatter func([]byte) string
74 // DescribeMessage returns a concise human-readable description of a
75 // Message for debugging.
76 func DescribeMessage(m pb.Message, f EntryFormatter) string {
78 fmt.Fprintf(&buf, "%x->%x %v Term:%d Log:%d/%d", m.From, m.To, m.Type, m.Term, m.LogTerm, m.Index)
80 fmt.Fprintf(&buf, " Rejected")
81 if m.RejectHint != 0 {
82 fmt.Fprintf(&buf, "(Hint:%d)", m.RejectHint)
86 fmt.Fprintf(&buf, " Commit:%d", m.Commit)
88 if len(m.Entries) > 0 {
89 fmt.Fprintf(&buf, " Entries:[")
90 for i, e := range m.Entries {
94 buf.WriteString(DescribeEntry(e, f))
96 fmt.Fprintf(&buf, "]")
98 if !IsEmptySnap(m.Snapshot) {
99 fmt.Fprintf(&buf, " Snapshot:%v", m.Snapshot)
104 // DescribeEntry returns a concise human-readable description of an
105 // Entry for debugging.
106 func DescribeEntry(e pb.Entry, f EntryFormatter) string {
108 if e.Type == pb.EntryNormal && f != nil {
109 formatted = f(e.Data)
111 formatted = fmt.Sprintf("%q", e.Data)
113 return fmt.Sprintf("%d/%d %s %s", e.Term, e.Index, e.Type, formatted)
116 func limitSize(ents []pb.Entry, maxSize uint64) []pb.Entry {
120 size := ents[0].Size()
122 for limit = 1; limit < len(ents); limit++ {
123 size += ents[limit].Size()
124 if uint64(size) > maxSize {