log.go 4.9 KB

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