log.go 6.9 KB

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