log.go 6.5 KB

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