log.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package raft
  14. import (
  15. "fmt"
  16. pb "github.com/coreos/etcd/raft/raftpb"
  17. )
  18. type raftLog struct {
  19. ents []pb.Entry
  20. unstable uint64
  21. committed uint64
  22. applied uint64
  23. offset uint64
  24. snapshot pb.Snapshot
  25. }
  26. func newLog() *raftLog {
  27. return &raftLog{
  28. ents: make([]pb.Entry, 1),
  29. unstable: 0,
  30. committed: 0,
  31. applied: 0,
  32. }
  33. }
  34. func (l *raftLog) isEmpty() bool {
  35. return l.offset == 0 && len(l.ents) == 1
  36. }
  37. func (l *raftLog) load(ents []pb.Entry) {
  38. l.ents = ents
  39. l.unstable = l.offset + uint64(len(ents))
  40. }
  41. func (l *raftLog) String() string {
  42. return fmt.Sprintf("offset=%d committed=%d applied=%d len(ents)=%d", l.offset, l.committed, l.applied, len(l.ents))
  43. }
  44. // maybeAppend returns (0, false) if the entries cannot be appended. Otherwise,
  45. // it returns (last index of entries, true).
  46. func (l *raftLog) maybeAppend(index, logTerm, committed uint64, ents ...pb.Entry) (lastnewi uint64, ok bool) {
  47. lastnewi = index + uint64(len(ents))
  48. if l.matchTerm(index, logTerm) {
  49. from := index + 1
  50. ci := l.findConflict(from, ents)
  51. switch {
  52. case ci == 0:
  53. case ci <= l.committed:
  54. panic("conflict with committed entry")
  55. default:
  56. l.append(ci-1, ents[ci-from:]...)
  57. }
  58. tocommit := min(committed, lastnewi)
  59. // if toCommit > commitIndex, set commitIndex = toCommit
  60. if l.committed < tocommit {
  61. l.committed = tocommit
  62. }
  63. return lastnewi, true
  64. }
  65. return 0, false
  66. }
  67. func (l *raftLog) append(after uint64, ents ...pb.Entry) uint64 {
  68. l.ents = append(l.slice(l.offset, after+1), ents...)
  69. l.unstable = min(l.unstable, after+1)
  70. return l.lastIndex()
  71. }
  72. // findConflict finds the index of the conflict.
  73. // It returns the first pair of conflicting entries between the existing
  74. // entries and the given entries, if there are any.
  75. // If there is no conflicting entries, and the existing entries contains
  76. // all the given entries, zero will be returned.
  77. // If there is no conflicting entries, but the given entries contains new
  78. // entries, the index of the first new entry will be returned.
  79. // An entry is considered to be conflicting if it has the same index but
  80. // a different term.
  81. // The first entry MUST have an index equal to the argument 'from'.
  82. // The index of the given entries MUST be continuously increasing.
  83. func (l *raftLog) findConflict(from uint64, ents []pb.Entry) uint64 {
  84. // TODO(xiangli): validate the index of ents
  85. for i, ne := range ents {
  86. if oe := l.at(from + uint64(i)); oe == nil || oe.Term != ne.Term {
  87. return from + uint64(i)
  88. }
  89. }
  90. return 0
  91. }
  92. func (l *raftLog) unstableEnts() []pb.Entry {
  93. ents := l.slice(l.unstable, l.lastIndex()+1)
  94. if ents == nil {
  95. return nil
  96. }
  97. cpy := make([]pb.Entry, len(ents))
  98. copy(cpy, ents)
  99. return cpy
  100. }
  101. func (l *raftLog) resetUnstable() {
  102. l.unstable = l.lastIndex() + 1
  103. }
  104. // nextEnts returns all the available entries for execution.
  105. // all the returned entries will be marked as applied.
  106. func (l *raftLog) nextEnts() (ents []pb.Entry) {
  107. if l.committed > l.applied {
  108. return l.slice(l.applied+1, l.committed+1)
  109. }
  110. return nil
  111. }
  112. func (l *raftLog) resetNextEnts() {
  113. if l.committed > l.applied {
  114. l.applied = l.committed
  115. }
  116. }
  117. func (l *raftLog) lastIndex() uint64 { return uint64(len(l.ents)) - 1 + l.offset }
  118. func (l *raftLog) lastTerm() uint64 { return l.term(l.lastIndex()) }
  119. func (l *raftLog) term(i uint64) uint64 {
  120. if e := l.at(i); e != nil {
  121. return e.Term
  122. }
  123. return 0
  124. }
  125. func (l *raftLog) entries(i uint64) []pb.Entry {
  126. // never send out the first entry
  127. // first entry is only used for matching
  128. // prevLogTerm
  129. if i == l.offset {
  130. panic("cannot return the first entry in log")
  131. }
  132. return l.slice(i, l.lastIndex()+1)
  133. }
  134. // isUpToDate determines if the given (lastIndex,term) log is more up-to-date
  135. // by comparing the index and term of the last entries in the existing logs.
  136. // If the logs have last entries with different terms, then the log with the
  137. // later term is more up-to-date. If the logs end with the same term, then
  138. // whichever log has the larger lastIndex is more up-to-date. If the logs are
  139. // the same, the given log is up-to-date.
  140. func (l *raftLog) isUpToDate(lasti, term uint64) bool {
  141. return term > l.lastTerm() || (term == l.lastTerm() && lasti >= l.lastIndex())
  142. }
  143. func (l *raftLog) matchTerm(i, term uint64) bool {
  144. if e := l.at(i); e != nil {
  145. return e.Term == term
  146. }
  147. return false
  148. }
  149. func (l *raftLog) maybeCommit(maxIndex, term uint64) bool {
  150. if maxIndex > l.committed && l.term(maxIndex) == term {
  151. l.committed = maxIndex
  152. return true
  153. }
  154. return false
  155. }
  156. // compact compacts all log entries until i.
  157. // It removes the log entries before i, exclusive.
  158. // i must be not smaller than the index of the first entry
  159. // and not greater than the index of the last entry.
  160. // the number of entries after compaction will be returned.
  161. func (l *raftLog) compact(i uint64) uint64 {
  162. if l.isOutOfAppliedBounds(i) {
  163. panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.applied))
  164. }
  165. l.ents = l.slice(i, l.lastIndex()+1)
  166. l.unstable = max(i+1, l.unstable)
  167. l.offset = i
  168. return uint64(len(l.ents))
  169. }
  170. func (l *raftLog) snap(d []byte, index, term uint64, nodes []uint64) {
  171. l.snapshot = pb.Snapshot{
  172. Data: d,
  173. Nodes: nodes,
  174. Index: index,
  175. Term: term,
  176. }
  177. }
  178. func (l *raftLog) restore(s pb.Snapshot) {
  179. l.ents = []pb.Entry{{Term: s.Term}}
  180. l.unstable = s.Index + 1
  181. l.committed = s.Index
  182. l.applied = s.Index
  183. l.offset = s.Index
  184. l.snapshot = s
  185. }
  186. func (l *raftLog) at(i uint64) *pb.Entry {
  187. if l.isOutOfBounds(i) {
  188. return nil
  189. }
  190. return &l.ents[i-l.offset]
  191. }
  192. // slice returns a slice of log entries from lo through hi-1, inclusive.
  193. func (l *raftLog) slice(lo uint64, hi uint64) []pb.Entry {
  194. if lo >= hi {
  195. return nil
  196. }
  197. if l.isOutOfBounds(lo) || l.isOutOfBounds(hi-1) {
  198. return nil
  199. }
  200. return l.ents[lo-l.offset : hi-l.offset]
  201. }
  202. func (l *raftLog) isOutOfBounds(i uint64) bool {
  203. if i < l.offset || i > l.lastIndex() {
  204. return true
  205. }
  206. return false
  207. }
  208. func (l *raftLog) isOutOfAppliedBounds(i uint64) bool {
  209. if i < l.offset || i > l.applied {
  210. return true
  211. }
  212. return false
  213. }
  214. func min(a, b uint64) uint64 {
  215. if a > b {
  216. return b
  217. }
  218. return a
  219. }
  220. func max(a, b uint64) uint64 {
  221. if a > b {
  222. return a
  223. }
  224. return b
  225. }