|
@@ -21,6 +21,7 @@ type log struct {
|
|
|
ents []Entry
|
|
ents []Entry
|
|
|
committed int
|
|
committed int
|
|
|
applied int
|
|
applied int
|
|
|
|
|
+ offset int
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func newLog() *log {
|
|
func newLog() *log {
|
|
@@ -41,38 +42,35 @@ func (l *log) maybeAppend(index, logTerm, committed int, ents ...Entry) bool {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (l *log) append(after int, ents ...Entry) int {
|
|
func (l *log) append(after int, ents ...Entry) int {
|
|
|
- l.ents = append(l.ents[:after+1], ents...)
|
|
|
|
|
|
|
+ l.ents = append(l.slice(0, after+1), ents...)
|
|
|
return l.lastIndex()
|
|
return l.lastIndex()
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (l *log) lastIndex() int {
|
|
func (l *log) lastIndex() int {
|
|
|
- return len(l.ents) - 1
|
|
|
|
|
|
|
+ return len(l.ents) - 1 + l.offset
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (l *log) term(i int) int {
|
|
func (l *log) term(i int) int {
|
|
|
- if i > l.lastIndex() {
|
|
|
|
|
- return -1
|
|
|
|
|
|
|
+ if e := l.at(i); e != nil {
|
|
|
|
|
+ return e.Term
|
|
|
}
|
|
}
|
|
|
- return l.ents[i].Term
|
|
|
|
|
|
|
+ return -1
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (l *log) entries(i int) []Entry {
|
|
func (l *log) entries(i int) []Entry {
|
|
|
- if i > l.lastIndex() {
|
|
|
|
|
- return nil
|
|
|
|
|
- }
|
|
|
|
|
- return l.ents[i:]
|
|
|
|
|
|
|
+ return l.slice(i, l.lastIndex()+1)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (l *log) isUpToDate(i, term int) bool {
|
|
func (l *log) isUpToDate(i, term int) bool {
|
|
|
- e := l.ents[l.lastIndex()]
|
|
|
|
|
|
|
+ e := l.at(l.lastIndex())
|
|
|
return term > e.Term || (term == e.Term && i >= l.lastIndex())
|
|
return term > e.Term || (term == e.Term && i >= l.lastIndex())
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (l *log) matchTerm(i, term int) bool {
|
|
func (l *log) matchTerm(i, term int) bool {
|
|
|
- if i > l.lastIndex() {
|
|
|
|
|
- return false
|
|
|
|
|
|
|
+ if e := l.at(i); e != nil {
|
|
|
|
|
+ return e.Term == term
|
|
|
}
|
|
}
|
|
|
- return l.ents[i].Term == term
|
|
|
|
|
|
|
+ return false
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func (l *log) maybeCommit(maxIndex, term int) bool {
|
|
func (l *log) maybeCommit(maxIndex, term int) bool {
|
|
@@ -87,8 +85,33 @@ func (l *log) maybeCommit(maxIndex, term int) bool {
|
|
|
// all the returned entries will be marked as applied.
|
|
// all the returned entries will be marked as applied.
|
|
|
func (l *log) nextEnts() (ents []Entry) {
|
|
func (l *log) nextEnts() (ents []Entry) {
|
|
|
if l.committed > l.applied {
|
|
if l.committed > l.applied {
|
|
|
- ents = l.ents[l.applied+1 : l.committed+1]
|
|
|
|
|
|
|
+ ents = l.slice(l.applied+1, l.committed+1)
|
|
|
l.applied = l.committed
|
|
l.applied = l.committed
|
|
|
}
|
|
}
|
|
|
return ents
|
|
return ents
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+func (l *log) at(i int) *Entry {
|
|
|
|
|
+ if l.isOutOfBounds(i) {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ return &l.ents[i-l.offset]
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// slice get a slice of log entries from lo through hi-1, inclusive.
|
|
|
|
|
+func (l *log) slice(lo int, hi int) []Entry {
|
|
|
|
|
+ if lo >= hi {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ if l.isOutOfBounds(lo) || l.isOutOfBounds(hi-1) {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+ return l.ents[lo-l.offset : hi-l.offset]
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func (l *log) isOutOfBounds(i int) bool {
|
|
|
|
|
+ if i < l.offset || i > l.lastIndex() {
|
|
|
|
|
+ return true
|
|
|
|
|
+ }
|
|
|
|
|
+ return false
|
|
|
|
|
+}
|