Tizen_4.0 base
[platform/upstream/docker-engine.git] / vendor / github.com / coreos / etcd / raft / util.go
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 "github.com/coreos/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")
81                 if m.RejectHint != 0 {
82                         fmt.Fprintf(&buf, "(Hint:%d)", m.RejectHint)
83                 }
84         }
85         if m.Commit != 0 {
86                 fmt.Fprintf(&buf, " Commit:%d", m.Commit)
87         }
88         if len(m.Entries) > 0 {
89                 fmt.Fprintf(&buf, " Entries:[")
90                 for i, e := range m.Entries {
91                         if i != 0 {
92                                 buf.WriteString(", ")
93                         }
94                         buf.WriteString(DescribeEntry(e, f))
95                 }
96                 fmt.Fprintf(&buf, "]")
97         }
98         if !IsEmptySnap(m.Snapshot) {
99                 fmt.Fprintf(&buf, " Snapshot:%v", m.Snapshot)
100         }
101         return buf.String()
102 }
103
104 // DescribeEntry returns a concise human-readable description of an
105 // Entry for debugging.
106 func DescribeEntry(e pb.Entry, f EntryFormatter) string {
107         var formatted string
108         if e.Type == pb.EntryNormal && f != nil {
109                 formatted = f(e.Data)
110         } else {
111                 formatted = fmt.Sprintf("%q", e.Data)
112         }
113         return fmt.Sprintf("%d/%d %s %s", e.Term, e.Index, e.Type, formatted)
114 }
115
116 func limitSize(ents []pb.Entry, maxSize uint64) []pb.Entry {
117         if len(ents) == 0 {
118                 return ents
119         }
120         size := ents[0].Size()
121         var limit int
122         for limit = 1; limit < len(ents); limit++ {
123                 size += ents[limit].Size()
124                 if uint64(size) > maxSize {
125                         break
126                 }
127         }
128         return ents[:limit]
129 }