2014-05-27 23:24:49 +04:00
|
|
|
package raft
|
|
|
|
|
2014-06-06 00:00:18 +04:00
|
|
|
const (
|
|
|
|
normal int = iota
|
|
|
|
config
|
|
|
|
)
|
|
|
|
|
2014-05-27 23:24:49 +04:00
|
|
|
type Entry struct {
|
2014-06-06 00:00:18 +04:00
|
|
|
Type int
|
2014-05-27 23:24:49 +04:00
|
|
|
Term int
|
|
|
|
Data []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type log struct {
|
2014-05-28 22:08:32 +04:00
|
|
|
ents []Entry
|
|
|
|
committed int
|
|
|
|
applied int
|
2014-05-27 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func newLog() *log {
|
|
|
|
return &log{
|
2014-05-28 22:08:32 +04:00
|
|
|
ents: make([]Entry, 1),
|
|
|
|
committed: 0,
|
|
|
|
applied: 0,
|
2014-05-27 23:24:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 22:08:32 +04:00
|
|
|
func (l *log) maybeAppend(index, logTerm, committed int, ents ...Entry) bool {
|
2014-05-28 09:47:13 +04:00
|
|
|
if l.matchTerm(index, logTerm) {
|
2014-05-27 23:24:49 +04:00
|
|
|
l.append(index, ents...)
|
2014-05-28 22:08:32 +04:00
|
|
|
l.committed = committed
|
2014-05-27 23:24:49 +04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *log) append(after int, ents ...Entry) int {
|
|
|
|
l.ents = append(l.ents[:after+1], ents...)
|
2014-05-28 22:04:21 +04:00
|
|
|
return l.lastIndex()
|
2014-05-27 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
2014-05-28 09:50:47 +04:00
|
|
|
func (l *log) lastIndex() int {
|
2014-05-27 23:24:49 +04:00
|
|
|
return len(l.ents) - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *log) term(i int) int {
|
2014-05-28 09:50:47 +04:00
|
|
|
if i > l.lastIndex() {
|
2014-05-27 23:24:49 +04:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return l.ents[i].Term
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *log) entries(i int) []Entry {
|
2014-05-28 09:50:47 +04:00
|
|
|
if i > l.lastIndex() {
|
2014-05-27 23:24:49 +04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return l.ents[i:]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *log) isUpToDate(i, term int) bool {
|
2014-05-28 09:50:47 +04:00
|
|
|
e := l.ents[l.lastIndex()]
|
|
|
|
return term > e.Term || (term == e.Term && i >= l.lastIndex())
|
2014-05-27 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
2014-05-28 09:47:13 +04:00
|
|
|
func (l *log) matchTerm(i, term int) bool {
|
2014-05-28 09:50:47 +04:00
|
|
|
if i > l.lastIndex() {
|
2014-05-27 23:24:49 +04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return l.ents[i].Term == term
|
|
|
|
}
|
|
|
|
|
2014-05-28 19:24:09 +04:00
|
|
|
func (l *log) maybeCommit(maxIndex, term int) bool {
|
2014-05-28 22:08:32 +04:00
|
|
|
if maxIndex > l.committed && l.term(maxIndex) == term {
|
|
|
|
l.committed = maxIndex
|
2014-05-28 19:24:09 +04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-05-28 22:06:02 +04:00
|
|
|
// nextEnts returns all the avaliable entries for execution.
|
|
|
|
// all the returned entries will be marked as applied.
|
2014-05-27 23:24:49 +04:00
|
|
|
func (l *log) nextEnts() (ents []Entry) {
|
2014-05-28 22:08:32 +04:00
|
|
|
if l.committed > l.applied {
|
|
|
|
ents = l.ents[l.applied+1 : l.committed+1]
|
|
|
|
l.applied = l.committed
|
2014-05-27 23:24:49 +04:00
|
|
|
}
|
|
|
|
return ents
|
|
|
|
}
|