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. Data []byte
  16. }
  17. func (e *Entry) isConfig() bool {
  18. return e.Type == AddNode || e.Type == RemoveNode
  19. }
  20. type raftLog struct {
  21. ents []Entry
  22. unstable int64
  23. committed int64
  24. applied int64
  25. offset int64
  26. // want a compact after the number of entries exceeds the threshold
  27. // TODO(xiangli) size might be a better criteria
  28. compactThreshold int64
  29. }
  30. func newLog() *raftLog {
  31. return &raftLog{
  32. ents: make([]Entry, 1),
  33. unstable: 1,
  34. committed: 0,
  35. applied: 0,
  36. compactThreshold: defaultCompactThreshold,
  37. }
  38. }
  39. func (l *raftLog) isEmpty() bool {
  40. return l.offset == 0 && len(l.ents) == 1
  41. }
  42. func (l *raftLog) String() string {
  43. return fmt.Sprintf("offset=%d committed=%d applied=%d len(ents)=%d", l.offset, l.committed, l.applied, len(l.ents))
  44. }
  45. func (l *raftLog) maybeAppend(index, logTerm, committed int64, ents ...Entry) bool {
  46. if l.matchTerm(index, logTerm) {
  47. from := index + 1
  48. ci := l.findConflict(from, ents)
  49. switch {
  50. case ci == -1:
  51. case ci <= l.committed:
  52. panic("conflict with committed entry")
  53. default:
  54. l.append(ci-1, ents[ci-from:]...)
  55. }
  56. if l.committed < committed {
  57. l.committed = min(committed, l.lastIndex())
  58. }
  59. return true
  60. }
  61. return false
  62. }
  63. func (l *raftLog) append(after int64, ents ...Entry) int64 {
  64. l.ents = append(l.slice(l.offset, after+1), ents...)
  65. l.unstable = min(l.unstable, after+1)
  66. return l.lastIndex()
  67. }
  68. func (l *raftLog) findConflict(from int64, ents []Entry) int64 {
  69. for i, ne := range ents {
  70. if oe := l.at(from + int64(i)); oe == nil || oe.Term != ne.Term {
  71. return from + int64(i)
  72. }
  73. }
  74. return -1
  75. }
  76. func (l *raftLog) unstableEnts() (int64, []Entry) {
  77. offset := l.unstable
  78. ents := l.entries(l.unstable)
  79. l.unstable = l.lastIndex() + 1
  80. return offset, 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. }