log.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. l.unstable = l.lastIndex() + 1
  76. return ents
  77. }
  78. func (l *raftLog) lastIndex() int64 {
  79. return int64(len(l.ents)) - 1 + l.offset
  80. }
  81. func (l *raftLog) term(i int64) int64 {
  82. if e := l.at(i); e != nil {
  83. return e.Term
  84. }
  85. return -1
  86. }
  87. func (l *raftLog) entries(i int64) []Entry {
  88. // never send out the first entry
  89. // first entry is only used for matching
  90. // prevLogTerm
  91. if i == l.offset {
  92. panic("cannot return the first entry in log")
  93. }
  94. return l.slice(i, l.lastIndex()+1)
  95. }
  96. func (l *raftLog) isUpToDate(i, term int64) bool {
  97. e := l.at(l.lastIndex())
  98. return term > e.Term || (term == e.Term && i >= l.lastIndex())
  99. }
  100. func (l *raftLog) matchTerm(i, term int64) bool {
  101. if e := l.at(i); e != nil {
  102. return e.Term == term
  103. }
  104. return false
  105. }
  106. func (l *raftLog) maybeCommit(maxIndex, term int64) bool {
  107. if maxIndex > l.committed && l.term(maxIndex) == term {
  108. l.committed = maxIndex
  109. return true
  110. }
  111. return false
  112. }
  113. // nextEnts returns all the available entries for execution.
  114. // all the returned entries will be marked as applied.
  115. func (l *raftLog) nextEnts() (ents []Entry) {
  116. if l.committed > l.applied {
  117. ents = l.slice(l.applied+1, l.committed+1)
  118. l.applied = l.committed
  119. }
  120. return ents
  121. }
  122. // compact compacts all log entries until i.
  123. // It removes the log entries before i, exclusive.
  124. // i must be not smaller than the index of the first entry
  125. // and not greater than the index of the last entry.
  126. // the number of entries after compaction will be returned.
  127. func (l *raftLog) compact(i int64) int64 {
  128. if l.isOutOfBounds(i) {
  129. panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.lastIndex()))
  130. }
  131. l.ents = l.slice(i, l.lastIndex()+1)
  132. l.unstable = max(i+1, l.unstable)
  133. l.offset = i
  134. return int64(len(l.ents))
  135. }
  136. func (l *raftLog) snap(d []byte, clusterId, index, term int64, nodes []int64) {
  137. l.snapshot = Snapshot{clusterId, d, nodes, index, term}
  138. }
  139. func (l *raftLog) shouldCompact() bool {
  140. return (l.applied - l.offset) > l.compactThreshold
  141. }
  142. func (l *raftLog) restore(s Snapshot) {
  143. l.ents = []Entry{{Term: s.Term}}
  144. l.unstable = s.Index + 1
  145. l.committed = s.Index
  146. l.applied = s.Index
  147. l.offset = s.Index
  148. l.snapshot = s
  149. l.unstableSnapshot = s
  150. }
  151. func (l *raftLog) at(i int64) *Entry {
  152. if l.isOutOfBounds(i) {
  153. return nil
  154. }
  155. return &l.ents[i-l.offset]
  156. }
  157. // slice get a slice of log entries from lo through hi-1, inclusive.
  158. func (l *raftLog) slice(lo int64, hi int64) []Entry {
  159. if lo >= hi {
  160. return nil
  161. }
  162. if l.isOutOfBounds(lo) || l.isOutOfBounds(hi-1) {
  163. return nil
  164. }
  165. return l.ents[lo-l.offset : hi-l.offset]
  166. }
  167. func (l *raftLog) isOutOfBounds(i int64) bool {
  168. if i < l.offset || i > l.lastIndex() {
  169. return true
  170. }
  171. return false
  172. }
  173. func min(a, b int64) int64 {
  174. if a > b {
  175. return b
  176. }
  177. return a
  178. }
  179. func max(a, b int64) int64 {
  180. if a > b {
  181. return a
  182. }
  183. return b
  184. }