log.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 the first pair of confilcting entries between the existing
  74. // entries and the given entries, if there is 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. // Conflicting entries has the same index but different term.
  80. // The first given entry MUST have the index equal to from.
  81. // The index of the given entries MUST be continously increasing.
  82. func (l *raftLog) findConflict(from uint64, ents []pb.Entry) uint64 {
  83. for i, ne := range ents {
  84. if oe := l.at(from + uint64(i)); oe == nil || oe.Term != ne.Term {
  85. return from + uint64(i)
  86. }
  87. }
  88. return 0
  89. }
  90. func (l *raftLog) unstableEnts() []pb.Entry {
  91. ents := l.slice(l.unstable, l.lastIndex()+1)
  92. if ents == nil {
  93. return nil
  94. }
  95. cpy := make([]pb.Entry, len(ents))
  96. copy(cpy, ents)
  97. return cpy
  98. }
  99. func (l *raftLog) resetUnstable() {
  100. l.unstable = l.lastIndex() + 1
  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) resetNextEnts() {
  111. if l.committed > l.applied {
  112. l.applied = l.committed
  113. }
  114. }
  115. func (l *raftLog) lastIndex() uint64 {
  116. return uint64(len(l.ents)) - 1 + l.offset
  117. }
  118. func (l *raftLog) term(i uint64) uint64 {
  119. if e := l.at(i); e != nil {
  120. return e.Term
  121. }
  122. return 0
  123. }
  124. func (l *raftLog) entries(i uint64) []pb.Entry {
  125. // never send out the first entry
  126. // first entry is only used for matching
  127. // prevLogTerm
  128. if i == l.offset {
  129. panic("cannot return the first entry in log")
  130. }
  131. return l.slice(i, l.lastIndex()+1)
  132. }
  133. func (l *raftLog) isUpToDate(i, term uint64) bool {
  134. e := l.at(l.lastIndex())
  135. return term > e.Term || (term == e.Term && i >= l.lastIndex())
  136. }
  137. func (l *raftLog) matchTerm(i, term uint64) bool {
  138. if e := l.at(i); e != nil {
  139. return e.Term == term
  140. }
  141. return false
  142. }
  143. func (l *raftLog) maybeCommit(maxIndex, term uint64) bool {
  144. if maxIndex > l.committed && l.term(maxIndex) == term {
  145. l.committed = maxIndex
  146. return true
  147. }
  148. return false
  149. }
  150. // compact compacts all log entries until i.
  151. // It removes the log entries before i, exclusive.
  152. // i must be not smaller than the index of the first entry
  153. // and not greater than the index of the last entry.
  154. // the number of entries after compaction will be returned.
  155. func (l *raftLog) compact(i uint64) uint64 {
  156. if l.isOutOfAppliedBounds(i) {
  157. panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.applied))
  158. }
  159. l.ents = l.slice(i, l.lastIndex()+1)
  160. l.unstable = max(i+1, l.unstable)
  161. l.offset = i
  162. return uint64(len(l.ents))
  163. }
  164. func (l *raftLog) snap(d []byte, index, term uint64, nodes []uint64) {
  165. l.snapshot = pb.Snapshot{
  166. Data: d,
  167. Nodes: nodes,
  168. Index: index,
  169. Term: term,
  170. }
  171. }
  172. func (l *raftLog) restore(s pb.Snapshot) {
  173. l.ents = []pb.Entry{{Term: s.Term}}
  174. l.unstable = s.Index + 1
  175. l.committed = s.Index
  176. l.applied = s.Index
  177. l.offset = s.Index
  178. l.snapshot = s
  179. }
  180. func (l *raftLog) at(i uint64) *pb.Entry {
  181. if l.isOutOfBounds(i) {
  182. return nil
  183. }
  184. return &l.ents[i-l.offset]
  185. }
  186. // slice returns a slice of log entries from lo through hi-1, inclusive.
  187. func (l *raftLog) slice(lo uint64, hi uint64) []pb.Entry {
  188. if lo >= hi {
  189. return nil
  190. }
  191. if l.isOutOfBounds(lo) || l.isOutOfBounds(hi-1) {
  192. return nil
  193. }
  194. return l.ents[lo-l.offset : hi-l.offset]
  195. }
  196. func (l *raftLog) isOutOfBounds(i uint64) bool {
  197. if i < l.offset || i > l.lastIndex() {
  198. return true
  199. }
  200. return false
  201. }
  202. func (l *raftLog) isOutOfAppliedBounds(i uint64) bool {
  203. if i < l.offset || i > l.applied {
  204. return true
  205. }
  206. return false
  207. }
  208. func min(a, b uint64) uint64 {
  209. if a > b {
  210. return b
  211. }
  212. return a
  213. }
  214. func max(a, b uint64) uint64 {
  215. if a > b {
  216. return a
  217. }
  218. return b
  219. }