util.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. package raft
  15. import (
  16. "bytes"
  17. "fmt"
  18. pb "go.etcd.io/etcd/raft/raftpb"
  19. )
  20. func (st StateType) MarshalJSON() ([]byte, error) {
  21. return []byte(fmt.Sprintf("%q", st.String())), nil
  22. }
  23. func min(a, b uint64) uint64 {
  24. if a > b {
  25. return b
  26. }
  27. return a
  28. }
  29. func max(a, b uint64) uint64 {
  30. if a > b {
  31. return a
  32. }
  33. return b
  34. }
  35. func IsLocalMsg(msgt pb.MessageType) bool {
  36. return msgt == pb.MsgHup || msgt == pb.MsgBeat || msgt == pb.MsgUnreachable ||
  37. msgt == pb.MsgSnapStatus || msgt == pb.MsgCheckQuorum
  38. }
  39. func IsResponseMsg(msgt pb.MessageType) bool {
  40. return msgt == pb.MsgAppResp || msgt == pb.MsgVoteResp || msgt == pb.MsgHeartbeatResp || msgt == pb.MsgUnreachable || msgt == pb.MsgPreVoteResp
  41. }
  42. // voteResponseType maps vote and prevote message types to their corresponding responses.
  43. func voteRespMsgType(msgt pb.MessageType) pb.MessageType {
  44. switch msgt {
  45. case pb.MsgVote:
  46. return pb.MsgVoteResp
  47. case pb.MsgPreVote:
  48. return pb.MsgPreVoteResp
  49. default:
  50. panic(fmt.Sprintf("not a vote message: %s", msgt))
  51. }
  52. }
  53. // EntryFormatter can be implemented by the application to provide human-readable formatting
  54. // of entry data. Nil is a valid EntryFormatter and will use a default format.
  55. type EntryFormatter func([]byte) string
  56. // DescribeMessage returns a concise human-readable description of a
  57. // Message for debugging.
  58. func DescribeMessage(m pb.Message, f EntryFormatter) string {
  59. var buf bytes.Buffer
  60. fmt.Fprintf(&buf, "%x->%x %v Term:%d Log:%d/%d", m.From, m.To, m.Type, m.Term, m.LogTerm, m.Index)
  61. if m.Reject {
  62. fmt.Fprintf(&buf, " Rejected (Hint: %d)", m.RejectHint)
  63. }
  64. if m.Commit != 0 {
  65. fmt.Fprintf(&buf, " Commit:%d", m.Commit)
  66. }
  67. if len(m.Entries) > 0 {
  68. fmt.Fprintf(&buf, " Entries:[")
  69. for i, e := range m.Entries {
  70. if i != 0 {
  71. buf.WriteString(", ")
  72. }
  73. buf.WriteString(DescribeEntry(e, f))
  74. }
  75. fmt.Fprintf(&buf, "]")
  76. }
  77. if !IsEmptySnap(m.Snapshot) {
  78. fmt.Fprintf(&buf, " Snapshot:%v", m.Snapshot)
  79. }
  80. return buf.String()
  81. }
  82. // PayloadSize is the size of the payload of this Entry. Notably, it does not
  83. // depend on its Index or Term.
  84. func PayloadSize(e pb.Entry) int {
  85. return len(e.Data)
  86. }
  87. // DescribeEntry returns a concise human-readable description of an
  88. // Entry for debugging.
  89. func DescribeEntry(e pb.Entry, f EntryFormatter) string {
  90. var formatted string
  91. if e.Type == pb.EntryNormal && f != nil {
  92. formatted = f(e.Data)
  93. } else {
  94. formatted = fmt.Sprintf("%q", e.Data)
  95. }
  96. return fmt.Sprintf("%d/%d %s %s", e.Term, e.Index, e.Type, formatted)
  97. }
  98. // DescribeEntries calls DescribeEntry for each Entry, adding a newline to
  99. // each.
  100. func DescribeEntries(ents []pb.Entry, f EntryFormatter) string {
  101. var buf bytes.Buffer
  102. for _, e := range ents {
  103. _, _ = buf.WriteString(DescribeEntry(e, f) + "\n")
  104. }
  105. return buf.String()
  106. }
  107. func limitSize(ents []pb.Entry, maxSize uint64) []pb.Entry {
  108. if len(ents) == 0 {
  109. return ents
  110. }
  111. size := ents[0].Size()
  112. var limit int
  113. for limit = 1; limit < len(ents); limit++ {
  114. size += ents[limit].Size()
  115. if uint64(size) > maxSize {
  116. break
  117. }
  118. }
  119. return ents[:limit]
  120. }
  121. func assertConfStatesEquivalent(l Logger, cs1, cs2 pb.ConfState) {
  122. err := cs1.Equivalent(cs2)
  123. if err == nil {
  124. return
  125. }
  126. l.Panic(err)
  127. }