log.go 3.7 KB

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