log.go 5.3 KB

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