log.go 4.9 KB

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