log.go 5.0 KB

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