util.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Copyright 2015 CoreOS, Inc.
  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 "github.com/coreos/etcd/raft/raftpb"
  19. )
  20. func (st StateType) MarshalJSON() ([]byte, error) {
  21. return []byte(fmt.Sprintf("%q", st.String())), nil
  22. }
  23. // uint64Slice implements sort interface
  24. type uint64Slice []uint64
  25. func (p uint64Slice) Len() int { return len(p) }
  26. func (p uint64Slice) Less(i, j int) bool { return p[i] < p[j] }
  27. func (p uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
  28. func min(a, b uint64) uint64 {
  29. if a > b {
  30. return b
  31. }
  32. return a
  33. }
  34. func max(a, b uint64) uint64 {
  35. if a > b {
  36. return a
  37. }
  38. return b
  39. }
  40. func IsLocalMsg(m pb.Message) bool {
  41. return m.Type == pb.MsgHup || m.Type == pb.MsgBeat || m.Type == pb.MsgUnreachable || m.Type == pb.MsgSnapStatus
  42. }
  43. func IsResponseMsg(m pb.Message) bool {
  44. return m.Type == pb.MsgAppResp || m.Type == pb.MsgVoteResp || m.Type == pb.MsgHeartbeatResp || m.Type == pb.MsgUnreachable
  45. }
  46. // EntryFormatter can be implemented by the application to provide human-readable formatting
  47. // of entry data. Nil is a valid EntryFormatter and will use a default format.
  48. type EntryFormatter func([]byte) string
  49. // DescribeMessage returns a concise human-readable description of a
  50. // Message for debugging.
  51. func DescribeMessage(m pb.Message, f EntryFormatter) string {
  52. var buf bytes.Buffer
  53. fmt.Fprintf(&buf, "%x->%x %s Term:%d Log:%d/%d", m.From, m.To, m.Type, m.Term, m.LogTerm, m.Index)
  54. if m.Reject {
  55. fmt.Fprintf(&buf, " Rejected")
  56. }
  57. if m.Commit != 0 {
  58. fmt.Fprintf(&buf, " Commit:%d", m.Commit)
  59. }
  60. if len(m.Entries) > 0 {
  61. fmt.Fprintf(&buf, " Entries:[")
  62. for _, e := range m.Entries {
  63. buf.WriteString(DescribeEntry(e, f))
  64. }
  65. fmt.Fprintf(&buf, "]")
  66. }
  67. if !IsEmptySnap(m.Snapshot) {
  68. fmt.Fprintf(&buf, " Snapshot:%v", m.Snapshot)
  69. }
  70. return buf.String()
  71. }
  72. // DescribeEntry returns a concise human-readable description of an
  73. // Entry for debugging.
  74. func DescribeEntry(e pb.Entry, f EntryFormatter) string {
  75. var formatted string
  76. if e.Type == pb.EntryNormal && f != nil {
  77. formatted = f(e.Data)
  78. } else {
  79. formatted = fmt.Sprintf("%q", e.Data)
  80. }
  81. return fmt.Sprintf("%d/%d %s %s", e.Term, e.Index, e.Type, formatted)
  82. }
  83. func limitSize(ents []pb.Entry, maxSize uint64) []pb.Entry {
  84. if len(ents) == 0 {
  85. return ents
  86. }
  87. size := ents[0].Size()
  88. var limit int
  89. for limit = 1; limit < len(ents); limit++ {
  90. size += ents[limit].Size()
  91. if uint64(size) > maxSize {
  92. break
  93. }
  94. }
  95. return ents[:limit]
  96. }