log.go 4.9 KB

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