Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 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 | "bytes" |
| 19 | "fmt" |
| 20 | |
| 21 | pb "go.etcd.io/etcd/raft/raftpb" |
| 22 | ) |
| 23 | |
| 24 | func (st StateType) MarshalJSON() ([]byte, error) { |
| 25 | return []byte(fmt.Sprintf("%q", st.String())), nil |
| 26 | } |
| 27 | |
| 28 | // uint64Slice implements sort interface |
| 29 | type uint64Slice []uint64 |
| 30 | |
| 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] } |
| 34 | |
| 35 | func min(a, b uint64) uint64 { |
| 36 | if a > b { |
| 37 | return b |
| 38 | } |
| 39 | return a |
| 40 | } |
| 41 | |
| 42 | func max(a, b uint64) uint64 { |
| 43 | if a > b { |
| 44 | return a |
| 45 | } |
| 46 | return b |
| 47 | } |
| 48 | |
| 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 |
| 52 | } |
| 53 | |
| 54 | func IsResponseMsg(msgt pb.MessageType) bool { |
| 55 | return msgt == pb.MsgAppResp || msgt == pb.MsgVoteResp || msgt == pb.MsgHeartbeatResp || msgt == pb.MsgUnreachable || msgt == pb.MsgPreVoteResp |
| 56 | } |
| 57 | |
| 58 | // voteResponseType maps vote and prevote message types to their corresponding responses. |
| 59 | func voteRespMsgType(msgt pb.MessageType) pb.MessageType { |
| 60 | switch msgt { |
| 61 | case pb.MsgVote: |
| 62 | return pb.MsgVoteResp |
| 63 | case pb.MsgPreVote: |
| 64 | return pb.MsgPreVoteResp |
| 65 | default: |
| 66 | panic(fmt.Sprintf("not a vote message: %s", msgt)) |
| 67 | } |
| 68 | } |
| 69 | |
| 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 |
| 73 | |
| 74 | // DescribeMessage returns a concise human-readable description of a |
| 75 | // Message for debugging. |
| 76 | func DescribeMessage(m pb.Message, f EntryFormatter) string { |
| 77 | var buf bytes.Buffer |
| 78 | fmt.Fprintf(&buf, "%x->%x %v Term:%d Log:%d/%d", m.From, m.To, m.Type, m.Term, m.LogTerm, m.Index) |
| 79 | if m.Reject { |
| 80 | fmt.Fprintf(&buf, " Rejected (Hint: %d)", m.RejectHint) |
| 81 | } |
| 82 | if m.Commit != 0 { |
| 83 | fmt.Fprintf(&buf, " Commit:%d", m.Commit) |
| 84 | } |
| 85 | if len(m.Entries) > 0 { |
| 86 | fmt.Fprintf(&buf, " Entries:[") |
| 87 | for i, e := range m.Entries { |
| 88 | if i != 0 { |
| 89 | buf.WriteString(", ") |
| 90 | } |
| 91 | buf.WriteString(DescribeEntry(e, f)) |
| 92 | } |
| 93 | fmt.Fprintf(&buf, "]") |
| 94 | } |
| 95 | if !IsEmptySnap(m.Snapshot) { |
| 96 | fmt.Fprintf(&buf, " Snapshot:%v", m.Snapshot) |
| 97 | } |
| 98 | return buf.String() |
| 99 | } |
| 100 | |
| 101 | // PayloadSize is the size of the payload of this Entry. Notably, it does not |
| 102 | // depend on its Index or Term. |
| 103 | func PayloadSize(e pb.Entry) int { |
| 104 | return len(e.Data) |
| 105 | } |
| 106 | |
| 107 | // DescribeEntry returns a concise human-readable description of an |
| 108 | // Entry for debugging. |
| 109 | func DescribeEntry(e pb.Entry, f EntryFormatter) string { |
| 110 | var formatted string |
| 111 | if e.Type == pb.EntryNormal && f != nil { |
| 112 | formatted = f(e.Data) |
| 113 | } else { |
| 114 | formatted = fmt.Sprintf("%q", e.Data) |
| 115 | } |
| 116 | return fmt.Sprintf("%d/%d %s %s", e.Term, e.Index, e.Type, formatted) |
| 117 | } |
| 118 | |
| 119 | // DescribeEntries calls DescribeEntry for each Entry, adding a newline to |
| 120 | // each. |
| 121 | func DescribeEntries(ents []pb.Entry, f EntryFormatter) string { |
| 122 | var buf bytes.Buffer |
| 123 | for _, e := range ents { |
| 124 | _, _ = buf.WriteString(DescribeEntry(e, f) + "\n") |
| 125 | } |
| 126 | return buf.String() |
| 127 | } |
| 128 | |
| 129 | func limitSize(ents []pb.Entry, maxSize uint64) []pb.Entry { |
| 130 | if len(ents) == 0 { |
| 131 | return ents |
| 132 | } |
| 133 | size := ents[0].Size() |
| 134 | var limit int |
| 135 | for limit = 1; limit < len(ents); limit++ { |
| 136 | size += ents[limit].Size() |
| 137 | if uint64(size) > maxSize { |
| 138 | break |
| 139 | } |
| 140 | } |
| 141 | return ents[:limit] |
| 142 | } |