log.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package raft
  2. import "fmt"
  3. const (
  4. Normal int64 = iota
  5. ClusterInit
  6. AddNode
  7. RemoveNode
  8. )
  9. const (
  10. defaultCompactThreshold = 10000
  11. )
  12. type Entry struct {
  13. Type int64
  14. Term int64
  15. Index int64
  16. Data []byte
  17. }
  18. func (e *Entry) isConfig() bool {
  19. return e.Type == AddNode || e.Type == RemoveNode
  20. }
  21. type raftLog struct {
  22. ents []Entry
  23. unstable int64
  24. committed int64
  25. applied int64
  26. offset int64
  27. // want a compact after the number of entries exceeds the threshold
  28. // TODO(xiangli) size might be a better criteria
  29. compactThreshold int64
  30. }
  31. func newLog() *raftLog {
  32. return &raftLog{
  33. ents: make([]Entry, 1),
  34. unstable: 1,
  35. committed: 0,
  36. applied: 0,
  37. compactThreshold: defaultCompactThreshold,
  38. }
  39. }
  40. func (l *raftLog) isEmpty() bool {
  41. return l.offset == 0 && len(l.ents) == 1
  42. }
  43. func (l *raftLog) String() string {
  44. return fmt.Sprintf("offset=%d committed=%d applied=%d len(ents)=%d", l.offset, l.committed, l.applied, len(l.ents))
  45. }
  46. func (l *raftLog) maybeAppend(index, logTerm, committed int64, ents ...Entry) bool {
  47. if l.matchTerm(index, logTerm) {
  48. from := index + 1
  49. ci := l.findConflict(from, ents)
  50. switch {
  51. case ci == -1:
  52. case ci <= l.committed:
  53. panic("conflict with committed entry")
  54. default:
  55. l.append(ci-1, ents[ci-from:]...)
  56. }
  57. if l.committed < committed {
  58. l.committed = min(committed, l.lastIndex())
  59. }
  60. return true
  61. }
  62. return false
  63. }
  64. func (l *raftLog) append(after int64, ents ...Entry) int64 {
  65. l.ents = append(l.slice(l.offset, after+1), ents...)
  66. l.unstable = min(l.unstable, after+1)
  67. return l.lastIndex()
  68. }
  69. func (l *raftLog) findConflict(from int64, ents []Entry) int64 {
  70. for i, ne := range ents {
  71. if oe := l.at(from + int64(i)); oe == nil || oe.Term != ne.Term {
  72. return from + int64(i)
  73. }
  74. }
  75. return -1
  76. }
  77. func (l *raftLog) unstableEnts() []Entry {
  78. ents := l.entries(l.unstable)
  79. l.unstable = l.lastIndex() + 1
  80. return ents
  81. }
  82. func (l *raftLog) lastIndex() int64 {
  83. return int64(len(l.ents)) - 1 + l.offset
  84. }
  85. func (l *raftLog) term(i int64) int64 {
  86. if e := l.at(i); e != nil {
  87. return e.Term
  88. }
  89. return -1
  90. }
  91. func (l *raftLog) entries(i int64) []Entry {
  92. // never send out the first entry
  93. // first entry is only used for matching
  94. // prevLogTerm
  95. if i == l.offset {
  96. panic("cannot return the first entry in log")
  97. }
  98. return l.slice(i, l.lastIndex()+1)
  99. }
  100. func (l *raftLog) isUpToDate(i, term int64) bool {
  101. e := l.at(l.lastIndex())
  102. return term > e.Term || (term == e.Term && i >= l.lastIndex())
  103. }
  104. func (l *raftLog) matchTerm(i, term int64) bool {
  105. if e := l.at(i); e != nil {
  106. return e.Term == term
  107. }
  108. return false
  109. }
  110. func (l *raftLog) maybeCommit(maxIndex, term int64) bool {
  111. if maxIndex > l.committed && l.term(maxIndex) == term {
  112. l.committed = maxIndex
  113. return true
  114. }
  115. return false
  116. }
  117. // nextEnts returns all the available entries for execution.
  118. // all the returned entries will be marked as applied.
  119. func (l *raftLog) nextEnts() (ents []Entry) {
  120. if l.committed > l.applied {
  121. ents = l.slice(l.applied+1, l.committed+1)
  122. l.applied = l.committed
  123. }
  124. return ents
  125. }
  126. // compact compacts all log entries until i.
  127. // It removes the log entries before i, exclusive.
  128. // i must be not smaller than the index of the first entry
  129. // and not greater than the index of the last entry.
  130. // the number of entries after compaction will be returned.
  131. func (l *raftLog) compact(i int64) int64 {
  132. if l.isOutOfBounds(i) {
  133. panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.lastIndex()))
  134. }
  135. l.ents = l.slice(i, l.lastIndex()+1)
  136. l.unstable = max(i+1, l.unstable)
  137. l.offset = i
  138. return int64(len(l.ents))
  139. }
  140. func (l *raftLog) shouldCompact() bool {
  141. return (l.applied - l.offset) > l.compactThreshold
  142. }
  143. func (l *raftLog) restore(index, term int64) {
  144. l.ents = []Entry{{Term: term}}
  145. l.unstable = index + 1
  146. l.committed = index
  147. l.applied = index
  148. l.offset = index
  149. }
  150. func (l *raftLog) at(i int64) *Entry {
  151. if l.isOutOfBounds(i) {
  152. return nil
  153. }
  154. return &l.ents[i-l.offset]
  155. }
  156. // slice get a slice of log entries from lo through hi-1, inclusive.
  157. func (l *raftLog) slice(lo int64, hi int64) []Entry {
  158. if lo >= hi {
  159. return nil
  160. }
  161. if l.isOutOfBounds(lo) || l.isOutOfBounds(hi-1) {
  162. return nil
  163. }
  164. return l.ents[lo-l.offset : hi-l.offset]
  165. }
  166. func (l *raftLog) isOutOfBounds(i int64) bool {
  167. if i < l.offset || i > l.lastIndex() {
  168. return true
  169. }
  170. return false
  171. }
  172. func min(a, b int64) int64 {
  173. if a > b {
  174. return b
  175. }
  176. return a
  177. }
  178. func max(a, b int64) int64 {
  179. if a > b {
  180. return a
  181. }
  182. return b
  183. }