log.go 5.4 KB

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