etcd/raft/log.go

197 lines
4.0 KiB
Go
Raw Normal View History

2014-05-27 23:24:49 +04:00
package raft
2014-07-01 02:15:51 +04:00
import "fmt"
const (
2014-07-11 09:51:37 +04:00
Normal int64 = iota
2014-07-20 21:51:15 +04:00
ClusterInit
AddNode
RemoveNode
)
2014-07-01 02:15:51 +04:00
const (
defaultCompactThreshold = 10000
)
2014-05-27 23:24:49 +04:00
type Entry struct {
2014-07-11 09:51:37 +04:00
Type int64
Term int64
2014-05-27 23:24:49 +04:00
Data []byte
}
2014-06-20 02:56:47 +04:00
func (e *Entry) isConfig() bool {
return e.Type == AddNode || e.Type == RemoveNode
}
2014-05-27 23:24:49 +04:00
type log struct {
ents []Entry
2014-07-11 09:51:37 +04:00
committed int64
applied int64
offset int64
2014-07-01 02:15:51 +04:00
// want a compact after the number of entries exceeds the threshold
// TODO(xiangli) size might be a better criteria
2014-07-11 09:51:37 +04:00
compactThreshold int64
2014-05-27 23:24:49 +04:00
}
func newLog() *log {
return &log{
2014-07-01 02:15:51 +04:00
ents: make([]Entry, 1),
committed: 0,
applied: 0,
compactThreshold: defaultCompactThreshold,
2014-05-27 23:24:49 +04:00
}
}
func (l *log) String() string {
return fmt.Sprintf("offset=%d committed=%d applied=%d len(ents)=%d", l.offset, l.committed, l.applied, len(l.ents))
}
2014-07-11 09:51:37 +04:00
func (l *log) maybeAppend(index, logTerm, committed int64, ents ...Entry) bool {
2014-05-28 09:47:13 +04:00
if l.matchTerm(index, logTerm) {
2014-07-14 21:58:41 +04:00
from := index + 1
ci := l.findConflict(from, ents)
switch {
case ci == -1:
case ci <= l.committed:
panic("conflict with committed entry")
default:
l.append(ci-1, ents[ci-from:]...)
}
if l.committed < committed {
l.committed = min(committed, l.lastIndex())
}
2014-05-27 23:24:49 +04:00
return true
}
return false
}
2014-07-11 09:51:37 +04:00
func (l *log) append(after int64, ents ...Entry) int64 {
2014-07-01 02:15:51 +04:00
l.ents = append(l.slice(l.offset, after+1), ents...)
2014-05-28 22:04:21 +04:00
return l.lastIndex()
2014-05-27 23:24:49 +04:00
}
2014-07-14 21:58:41 +04:00
func (l *log) findConflict(from int64, ents []Entry) int64 {
for i, ne := range ents {
if oe := l.at(from + int64(i)); oe == nil || oe.Term != ne.Term {
return from + int64(i)
}
}
return -1
}
2014-07-11 09:51:37 +04:00
func (l *log) lastIndex() int64 {
return int64(len(l.ents)) - 1 + l.offset
2014-05-27 23:24:49 +04:00
}
2014-07-11 09:51:37 +04:00
func (l *log) term(i int64) int64 {
2014-06-28 03:15:04 +04:00
if e := l.at(i); e != nil {
return e.Term
2014-05-27 23:24:49 +04:00
}
2014-06-28 03:15:04 +04:00
return -1
2014-05-27 23:24:49 +04:00
}
2014-07-11 09:51:37 +04:00
func (l *log) entries(i int64) []Entry {
2014-07-01 23:10:43 +04:00
// never send out the first entry
// first entry is only used for matching
// prevLogTerm
if i == l.offset {
panic("cannot return the first entry in log")
}
2014-06-28 03:15:04 +04:00
return l.slice(i, l.lastIndex()+1)
2014-05-27 23:24:49 +04:00
}
2014-07-11 09:51:37 +04:00
func (l *log) isUpToDate(i, term int64) bool {
2014-06-28 03:15:04 +04:00
e := l.at(l.lastIndex())
2014-05-28 09:50:47 +04:00
return term > e.Term || (term == e.Term && i >= l.lastIndex())
2014-05-27 23:24:49 +04:00
}
2014-07-11 09:51:37 +04:00
func (l *log) matchTerm(i, term int64) bool {
2014-06-28 03:15:04 +04:00
if e := l.at(i); e != nil {
return e.Term == term
2014-05-27 23:24:49 +04:00
}
2014-06-28 03:15:04 +04:00
return false
2014-05-27 23:24:49 +04:00
}
2014-07-11 09:51:37 +04:00
func (l *log) maybeCommit(maxIndex, term int64) bool {
if maxIndex > l.committed && l.term(maxIndex) == term {
l.committed = maxIndex
2014-05-28 19:24:09 +04:00
return true
}
return false
}
2014-07-01 02:15:51 +04:00
// nextEnts returns all the available entries for execution.
2014-05-28 22:06:02 +04:00
// all the returned entries will be marked as applied.
2014-05-27 23:24:49 +04:00
func (l *log) nextEnts() (ents []Entry) {
if l.committed > l.applied {
2014-06-28 03:15:04 +04:00
ents = l.slice(l.applied+1, l.committed+1)
l.applied = l.committed
2014-05-27 23:24:49 +04:00
}
return ents
}
2014-06-28 03:15:04 +04:00
2014-07-01 02:15:51 +04:00
// compact removes the log entries before i, exclusive.
// i must be not smaller than the index of the first entry
// and not greater than the index of the last entry.
// the number of entries after compaction will be returned.
2014-07-11 09:51:37 +04:00
func (l *log) compact(i int64) int64 {
2014-07-01 02:15:51 +04:00
if l.isOutOfBounds(i) {
panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.lastIndex()))
}
l.ents = l.slice(i, l.lastIndex()+1)
l.offset = i
2014-07-11 09:51:37 +04:00
return int64(len(l.ents))
2014-07-01 02:15:51 +04:00
}
func (l *log) shouldCompact() bool {
2014-07-01 23:10:43 +04:00
return (l.applied - l.offset) > l.compactThreshold
}
2014-07-11 09:51:37 +04:00
func (l *log) restore(index, term int64) {
2014-07-01 23:10:43 +04:00
l.ents = []Entry{{Term: term}}
l.committed = index
l.applied = index
l.offset = index
2014-07-01 02:15:51 +04:00
}
2014-07-11 09:51:37 +04:00
func (l *log) at(i int64) *Entry {
2014-06-28 03:15:04 +04:00
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.
2014-07-11 09:51:37 +04:00
func (l *log) slice(lo int64, hi int64) []Entry {
2014-06-28 03:15:04 +04:00
if lo >= hi {
return nil
}
if l.isOutOfBounds(lo) || l.isOutOfBounds(hi-1) {
return nil
}
return l.ents[lo-l.offset : hi-l.offset]
}
2014-07-11 09:51:37 +04:00
func (l *log) isOutOfBounds(i int64) bool {
2014-06-28 03:15:04 +04:00
if i < l.offset || i > l.lastIndex() {
return true
}
return false
}
2014-07-14 21:58:41 +04:00
func min(a, b int64) int64 {
if a > b {
return b
}
return a
}
func max(a, b int64) int64 {
if a > b {
return a
}
return b
}