log.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package raft
  2. import (
  3. "fmt"
  4. pb "github.com/coreos/etcd/raft/raftpb"
  5. )
  6. const (
  7. defaultCompactThreshold = 10000
  8. )
  9. type raftLog struct {
  10. ents []pb.Entry
  11. unstable int64
  12. committed int64
  13. applied int64
  14. offset int64
  15. snapshot pb.Snapshot
  16. // want a compact after the number of entries exceeds the threshold
  17. // TODO(xiangli) size might be a better criteria
  18. compactThreshold int64
  19. }
  20. func newLog() *raftLog {
  21. return &raftLog{
  22. ents: make([]pb.Entry, 1),
  23. unstable: 0,
  24. committed: 0,
  25. applied: 0,
  26. compactThreshold: defaultCompactThreshold,
  27. }
  28. }
  29. func (l *raftLog) isEmpty() bool {
  30. return l.offset == 0 && len(l.ents) == 1
  31. }
  32. func (l *raftLog) load(ents []pb.Entry) {
  33. l.ents = ents
  34. l.unstable = l.offset + int64(len(ents))
  35. }
  36. func (l *raftLog) String() string {
  37. return fmt.Sprintf("offset=%d committed=%d applied=%d len(ents)=%d", l.offset, l.committed, l.applied, len(l.ents))
  38. }
  39. func (l *raftLog) maybeAppend(index, logTerm, committed int64, ents ...pb.Entry) bool {
  40. lastnewi := index + int64(len(ents))
  41. if l.matchTerm(index, logTerm) {
  42. from := index + 1
  43. ci := l.findConflict(from, ents)
  44. switch {
  45. case ci == -1:
  46. case ci <= l.committed:
  47. panic("conflict with committed entry")
  48. default:
  49. l.append(ci-1, ents[ci-from:]...)
  50. }
  51. tocommit := min(committed, lastnewi)
  52. // if toCommit > commitIndex, set commitIndex = toCommit
  53. if l.committed < tocommit {
  54. l.committed = tocommit
  55. }
  56. return true
  57. }
  58. return false
  59. }
  60. func (l *raftLog) append(after int64, ents ...pb.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 []pb.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() []pb.Entry {
  74. ents := l.slice(l.unstable, l.lastIndex()+1)
  75. if ents == nil {
  76. return nil
  77. }
  78. cpy := make([]pb.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 []pb.Entry) {
  88. if l.committed > l.applied {
  89. return l.slice(l.applied+1, l.committed+1)
  90. }
  91. return nil
  92. }
  93. func (l *raftLog) resetNextEnts() {
  94. if l.committed > l.applied {
  95. l.applied = l.committed
  96. }
  97. }
  98. func (l *raftLog) lastIndex() int64 {
  99. return int64(len(l.ents)) - 1 + l.offset
  100. }
  101. func (l *raftLog) term(i int64) int64 {
  102. if e := l.at(i); e != nil {
  103. return e.Term
  104. }
  105. return -1
  106. }
  107. func (l *raftLog) entries(i int64) []pb.Entry {
  108. // never send out the first entry
  109. // first entry is only used for matching
  110. // prevLogTerm
  111. if i == l.offset {
  112. panic("cannot return the first entry in log")
  113. }
  114. return l.slice(i, l.lastIndex()+1)
  115. }
  116. func (l *raftLog) isUpToDate(i, term int64) bool {
  117. e := l.at(l.lastIndex())
  118. return term > e.Term || (term == e.Term && i >= l.lastIndex())
  119. }
  120. func (l *raftLog) matchTerm(i, term int64) bool {
  121. if e := l.at(i); e != nil {
  122. return e.Term == term
  123. }
  124. return false
  125. }
  126. func (l *raftLog) maybeCommit(maxIndex, term int64) bool {
  127. if maxIndex > l.committed && l.term(maxIndex) == term {
  128. l.committed = maxIndex
  129. return true
  130. }
  131. return false
  132. }
  133. // compact compacts all log entries until i.
  134. // It removes the log entries before i, exclusive.
  135. // i must be not smaller than the index of the first entry
  136. // and not greater than the index of the last entry.
  137. // the number of entries after compaction will be returned.
  138. func (l *raftLog) compact(i int64) int64 {
  139. if l.isOutOfAppliedBounds(i) {
  140. panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.applied))
  141. }
  142. l.ents = l.slice(i, l.lastIndex()+1)
  143. l.unstable = max(i+1, l.unstable)
  144. l.offset = i
  145. return int64(len(l.ents))
  146. }
  147. func (l *raftLog) snap(d []byte, index, term int64, nodes []int64, removed []int64) {
  148. l.snapshot = pb.Snapshot{
  149. Data: d,
  150. Nodes: nodes,
  151. Index: index,
  152. Term: term,
  153. RemovedNodes: removed,
  154. }
  155. }
  156. func (l *raftLog) shouldCompact() bool {
  157. return (l.applied - l.offset) > l.compactThreshold
  158. }
  159. func (l *raftLog) restore(s pb.Snapshot) {
  160. l.ents = []pb.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) *pb.Entry {
  168. if l.isOutOfBounds(i) {
  169. return nil
  170. }
  171. return &l.ents[i-l.offset]
  172. }
  173. // slice returns a slice of log entries from lo through hi-1, inclusive.
  174. func (l *raftLog) slice(lo int64, hi int64) []pb.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 (l *raftLog) isOutOfAppliedBounds(i int64) bool {
  190. if i < l.offset || i > l.applied {
  191. return true
  192. }
  193. return false
  194. }
  195. func min(a, b int64) int64 {
  196. if a > b {
  197. return b
  198. }
  199. return a
  200. }
  201. func max(a, b int64) int64 {
  202. if a > b {
  203. return a
  204. }
  205. return b
  206. }