log.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. "log"
  17. pb "github.com/coreos/etcd/raft/raftpb"
  18. )
  19. type raftLog struct {
  20. ents []pb.Entry
  21. unstable uint64
  22. committed uint64
  23. applied uint64
  24. offset uint64
  25. snapshot pb.Snapshot
  26. }
  27. func newLog() *raftLog {
  28. return &raftLog{
  29. ents: make([]pb.Entry, 1),
  30. unstable: 0,
  31. committed: 0,
  32. applied: 0,
  33. }
  34. }
  35. func (l *raftLog) load(ents []pb.Entry) {
  36. if l.offset != ents[0].Index {
  37. panic("entries loaded don't match offset index")
  38. }
  39. l.ents = ents
  40. l.unstable = l.offset + uint64(len(ents))
  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. // maybeAppend returns (0, false) if the entries cannot be appended. Otherwise,
  46. // it returns (last index of new entries, true).
  47. func (l *raftLog) maybeAppend(index, logTerm, committed uint64, ents ...pb.Entry) (lastnewi uint64, ok bool) {
  48. lastnewi = index + uint64(len(ents))
  49. if l.matchTerm(index, logTerm) {
  50. from := index + 1
  51. ci := l.findConflict(from, ents)
  52. switch {
  53. case ci == 0:
  54. case ci <= l.committed:
  55. panic("conflict with committed entry")
  56. default:
  57. l.append(ci-1, ents[ci-from:]...)
  58. }
  59. tocommit := min(committed, lastnewi)
  60. // if toCommit > commitIndex, set commitIndex = toCommit
  61. if l.committed < tocommit {
  62. l.committed = tocommit
  63. }
  64. return lastnewi, true
  65. }
  66. return 0, false
  67. }
  68. func (l *raftLog) append(after uint64, ents ...pb.Entry) uint64 {
  69. l.ents = append(l.slice(l.offset, after+1), ents...)
  70. l.unstable = min(l.unstable, after+1)
  71. return l.lastIndex()
  72. }
  73. // findConflict finds the index of the conflict.
  74. // It returns the first pair of conflicting entries between the existing
  75. // entries and the given entries, if there are any.
  76. // If there is no conflicting entries, and the existing entries contains
  77. // all the given entries, zero will be returned.
  78. // If there is no conflicting entries, but the given entries contains new
  79. // entries, the index of the first new entry will be returned.
  80. // An entry is considered to be conflicting if it has the same index but
  81. // a different term.
  82. // The first entry MUST have an index equal to the argument 'from'.
  83. // The index of the given entries MUST be continuously increasing.
  84. func (l *raftLog) findConflict(from uint64, ents []pb.Entry) uint64 {
  85. // TODO(xiangli): validate the index of ents
  86. for i, ne := range ents {
  87. if oe := l.at(from + uint64(i)); oe == nil || oe.Term != ne.Term {
  88. return from + uint64(i)
  89. }
  90. }
  91. return 0
  92. }
  93. func (l *raftLog) unstableEnts() []pb.Entry {
  94. ents := l.slice(l.unstable, l.lastIndex()+1)
  95. if ents == nil {
  96. return nil
  97. }
  98. cpy := make([]pb.Entry, len(ents))
  99. copy(cpy, ents)
  100. return cpy
  101. }
  102. func (l *raftLog) resetUnstable() {
  103. l.unstable = l.lastIndex() + 1
  104. }
  105. // nextEnts returns all the available entries for execution.
  106. // all the returned entries will be marked as applied.
  107. func (l *raftLog) nextEnts() (ents []pb.Entry) {
  108. if l.committed > l.applied {
  109. return l.slice(l.applied+1, l.committed+1)
  110. }
  111. return nil
  112. }
  113. func (l *raftLog) resetNextEnts() {
  114. if l.committed > l.applied {
  115. l.applied = l.committed
  116. }
  117. }
  118. func (l *raftLog) appliedTo(i uint64) {
  119. if i == 0 {
  120. return
  121. }
  122. if l.committed < i || i < l.applied {
  123. log.Panicf("applied[%d] is out of range [prevApplied(%d), committed(%d)]", i, l.applied, l.committed)
  124. }
  125. l.applied = i
  126. }
  127. func (l *raftLog) stableTo(i uint64) {
  128. if i == 0 {
  129. return
  130. }
  131. l.unstable = i + 1
  132. }
  133. func (l *raftLog) lastIndex() uint64 { return uint64(len(l.ents)) - 1 + l.offset }
  134. func (l *raftLog) lastTerm() uint64 { return l.term(l.lastIndex()) }
  135. func (l *raftLog) term(i uint64) uint64 {
  136. if e := l.at(i); e != nil {
  137. return e.Term
  138. }
  139. return 0
  140. }
  141. func (l *raftLog) entries(i uint64) []pb.Entry {
  142. // never send out the first entry
  143. // first entry is only used for matching
  144. // prevLogTerm
  145. if i == l.offset {
  146. panic("cannot return the first entry in log")
  147. }
  148. return l.slice(i, l.lastIndex()+1)
  149. }
  150. // isUpToDate determines if the given (lastIndex,term) log is more up-to-date
  151. // by comparing the index and term of the last entries in the existing logs.
  152. // If the logs have last entries with different terms, then the log with the
  153. // later term is more up-to-date. If the logs end with the same term, then
  154. // whichever log has the larger lastIndex is more up-to-date. If the logs are
  155. // the same, the given log is up-to-date.
  156. func (l *raftLog) isUpToDate(lasti, term uint64) bool {
  157. return term > l.lastTerm() || (term == l.lastTerm() && lasti >= l.lastIndex())
  158. }
  159. func (l *raftLog) matchTerm(i, term uint64) bool {
  160. if e := l.at(i); e != nil {
  161. return e.Term == term
  162. }
  163. return false
  164. }
  165. func (l *raftLog) maybeCommit(maxIndex, term uint64) bool {
  166. if maxIndex > l.committed && l.term(maxIndex) == term {
  167. l.committed = maxIndex
  168. return true
  169. }
  170. return false
  171. }
  172. // compact compacts all log entries until i.
  173. // It removes the log entries before i, exclusive.
  174. // i must be not smaller than the index of the first entry
  175. // and not greater than the index of the last entry.
  176. // the number of entries after compaction will be returned.
  177. func (l *raftLog) compact(i uint64) uint64 {
  178. if l.isOutOfAppliedBounds(i) {
  179. panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.applied))
  180. }
  181. l.ents = l.slice(i, l.lastIndex()+1)
  182. l.unstable = max(i+1, l.unstable)
  183. l.offset = i
  184. return uint64(len(l.ents))
  185. }
  186. func (l *raftLog) snap(d []byte, index, term uint64, nodes []uint64) {
  187. l.snapshot = pb.Snapshot{
  188. Data: d,
  189. Nodes: nodes,
  190. Index: index,
  191. Term: term,
  192. }
  193. }
  194. func (l *raftLog) restore(s pb.Snapshot) {
  195. l.ents = []pb.Entry{{Term: s.Term}}
  196. l.unstable = s.Index + 1
  197. l.committed = s.Index
  198. l.applied = s.Index
  199. l.offset = s.Index
  200. l.snapshot = s
  201. }
  202. func (l *raftLog) at(i uint64) *pb.Entry {
  203. if l.isOutOfBounds(i) {
  204. return nil
  205. }
  206. return &l.ents[i-l.offset]
  207. }
  208. // slice returns a slice of log entries from lo through hi-1, inclusive.
  209. func (l *raftLog) slice(lo uint64, hi uint64) []pb.Entry {
  210. if lo >= hi {
  211. return nil
  212. }
  213. if l.isOutOfBounds(lo) || l.isOutOfBounds(hi-1) {
  214. return nil
  215. }
  216. return l.ents[lo-l.offset : hi-l.offset]
  217. }
  218. func (l *raftLog) isOutOfBounds(i uint64) bool {
  219. if i < l.offset || i > l.lastIndex() {
  220. return true
  221. }
  222. return false
  223. }
  224. func (l *raftLog) isOutOfAppliedBounds(i uint64) bool {
  225. if i < l.offset || i > l.applied {
  226. return true
  227. }
  228. return false
  229. }
  230. func min(a, b uint64) uint64 {
  231. if a > b {
  232. return b
  233. }
  234. return a
  235. }
  236. func max(a, b uint64) uint64 {
  237. if a > b {
  238. return a
  239. }
  240. return b
  241. }