log.go 5.0 KB

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