log.go 5.0 KB

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