log.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. // nextEnts returns all the available entries for execution.
  103. // all the returned entries will be marked as applied.
  104. func (l *raftLog) nextEnts() (ents []pb.Entry) {
  105. if l.committed > l.applied {
  106. return l.slice(l.applied+1, l.committed+1)
  107. }
  108. return nil
  109. }
  110. func (l *raftLog) appliedTo(i uint64) {
  111. if i == 0 {
  112. return
  113. }
  114. if l.committed < i || i < l.applied {
  115. log.Panicf("applied[%d] is out of range [prevApplied(%d), committed(%d)]", i, l.applied, l.committed)
  116. }
  117. l.applied = i
  118. }
  119. func (l *raftLog) stableTo(i uint64) {
  120. l.unstable = i + 1
  121. }
  122. func (l *raftLog) lastIndex() uint64 { return uint64(len(l.ents)) - 1 + l.offset }
  123. func (l *raftLog) lastTerm() uint64 { return l.term(l.lastIndex()) }
  124. func (l *raftLog) term(i uint64) uint64 {
  125. if e := l.at(i); e != nil {
  126. return e.Term
  127. }
  128. return 0
  129. }
  130. func (l *raftLog) entries(i uint64) []pb.Entry {
  131. // never send out the first entry
  132. // first entry is only used for matching
  133. // prevLogTerm
  134. if i == l.offset {
  135. panic("cannot return the first entry in log")
  136. }
  137. return l.slice(i, l.lastIndex()+1)
  138. }
  139. // isUpToDate determines if the given (lastIndex,term) log is more up-to-date
  140. // by comparing the index and term of the last entries in the existing logs.
  141. // If the logs have last entries with different terms, then the log with the
  142. // later term is more up-to-date. If the logs end with the same term, then
  143. // whichever log has the larger lastIndex is more up-to-date. If the logs are
  144. // the same, the given log is up-to-date.
  145. func (l *raftLog) isUpToDate(lasti, term uint64) bool {
  146. return term > l.lastTerm() || (term == l.lastTerm() && lasti >= l.lastIndex())
  147. }
  148. func (l *raftLog) matchTerm(i, term uint64) bool {
  149. if e := l.at(i); e != nil {
  150. return e.Term == term
  151. }
  152. return false
  153. }
  154. func (l *raftLog) maybeCommit(maxIndex, term uint64) bool {
  155. if maxIndex > l.committed && l.term(maxIndex) == term {
  156. l.committed = maxIndex
  157. return true
  158. }
  159. return false
  160. }
  161. // compact compacts all log entries until i.
  162. // It removes the log entries before i, exclusive.
  163. // i must be not smaller than the index of the first entry
  164. // and not greater than the index of the last entry.
  165. // the number of entries after compaction will be returned.
  166. func (l *raftLog) compact(i uint64) uint64 {
  167. if l.isOutOfAppliedBounds(i) {
  168. panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.applied))
  169. }
  170. l.ents = l.slice(i, l.lastIndex()+1)
  171. l.unstable = max(i+1, l.unstable)
  172. l.offset = i
  173. return uint64(len(l.ents))
  174. }
  175. func (l *raftLog) snap(d []byte, index, term uint64, nodes []uint64) {
  176. l.snapshot = pb.Snapshot{
  177. Data: d,
  178. Nodes: nodes,
  179. Index: index,
  180. Term: term,
  181. }
  182. }
  183. func (l *raftLog) restore(s pb.Snapshot) {
  184. l.ents = []pb.Entry{{Term: s.Term}}
  185. l.unstable = s.Index + 1
  186. l.committed = s.Index
  187. l.applied = s.Index
  188. l.offset = s.Index
  189. l.snapshot = s
  190. }
  191. func (l *raftLog) at(i uint64) *pb.Entry {
  192. if l.isOutOfBounds(i) {
  193. return nil
  194. }
  195. return &l.ents[i-l.offset]
  196. }
  197. // slice returns a slice of log entries from lo through hi-1, inclusive.
  198. func (l *raftLog) slice(lo uint64, hi uint64) []pb.Entry {
  199. if lo >= hi {
  200. return nil
  201. }
  202. if l.isOutOfBounds(lo) || l.isOutOfBounds(hi-1) {
  203. return nil
  204. }
  205. return l.ents[lo-l.offset : hi-l.offset]
  206. }
  207. func (l *raftLog) isOutOfBounds(i uint64) bool {
  208. if i < l.offset || i > l.lastIndex() {
  209. return true
  210. }
  211. return false
  212. }
  213. func (l *raftLog) isOutOfAppliedBounds(i uint64) bool {
  214. if i < l.offset || i > l.applied {
  215. return true
  216. }
  217. return false
  218. }
  219. func min(a, b uint64) uint64 {
  220. if a > b {
  221. return b
  222. }
  223. return a
  224. }
  225. func max(a, b uint64) uint64 {
  226. if a > b {
  227. return a
  228. }
  229. return b
  230. }