log.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. ents := l.slice(l.applied+1, l.committed+1)
  84. if ents == nil {
  85. return nil
  86. }
  87. cpy := make([]pb.Entry, len(ents))
  88. copy(cpy, ents)
  89. return cpy
  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.isOutOfBounds(i) {
  140. panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.lastIndex()))
  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) {
  148. l.snapshot = pb.Snapshot{d, nodes, index, term, nil}
  149. }
  150. func (l *raftLog) shouldCompact() bool {
  151. return (l.applied - l.offset) > l.compactThreshold
  152. }
  153. func (l *raftLog) restore(s pb.Snapshot) {
  154. l.ents = []pb.Entry{{Term: s.Term}}
  155. l.unstable = s.Index + 1
  156. l.committed = s.Index
  157. l.applied = s.Index
  158. l.offset = s.Index
  159. l.snapshot = s
  160. }
  161. func (l *raftLog) at(i int64) *pb.Entry {
  162. if l.isOutOfBounds(i) {
  163. return nil
  164. }
  165. return &l.ents[i-l.offset]
  166. }
  167. // slice get a slice of log entries from lo through hi-1, inclusive.
  168. func (l *raftLog) slice(lo int64, hi int64) []pb.Entry {
  169. if lo >= hi {
  170. return nil
  171. }
  172. if l.isOutOfBounds(lo) || l.isOutOfBounds(hi-1) {
  173. return nil
  174. }
  175. return l.ents[lo-l.offset : hi-l.offset]
  176. }
  177. func (l *raftLog) isOutOfBounds(i int64) bool {
  178. if i < l.offset || i > l.lastIndex() {
  179. return true
  180. }
  181. return false
  182. }
  183. func min(a, b int64) int64 {
  184. if a > b {
  185. return b
  186. }
  187. return a
  188. }
  189. func max(a, b int64) int64 {
  190. if a > b {
  191. return a
  192. }
  193. return b
  194. }