etcd/raft/log.go

278 lines
7.8 KiB
Go
Raw Normal View History

/*
Copyright 2014 CoreOS, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
2014-05-27 23:24:49 +04:00
package raft
2014-08-28 05:53:18 +04:00
import (
"fmt"
"log"
2014-08-28 05:53:18 +04:00
pb "github.com/coreos/etcd/raft/raftpb"
)
2014-07-01 02:15:51 +04:00
2014-07-24 03:15:25 +04:00
type raftLog struct {
// storage contains all stable entries since the last snapshot.
storage Storage
2014-11-26 21:59:13 +03:00
// unstable contains all unstable entries and snapshot.
// they will be saved into storage.
unstable unstable
// committed is the highest log position that is known to be in
// stable storage on a quorum of nodes.
// Invariant: committed < unstable
2014-10-08 14:29:53 +04:00
committed uint64
// applied is the highest log position that the application has
// been instructed to apply to its state machine.
// Invariant: applied <= committed
applied uint64
2014-05-27 23:24:49 +04:00
}
2014-11-25 08:47:12 +03:00
// newLog returns log using the given storage. It recovers the log to the state
// that it just commits and applies the lastest snapshot.
func newLog(storage Storage) *raftLog {
if storage == nil {
2014-11-24 20:01:25 +03:00
log.Panic("storage must not be nil")
2014-05-27 23:24:49 +04:00
}
log := &raftLog{
storage: storage,
}
firstIndex, err := storage.FirstIndex()
if err != nil {
panic(err) // TODO(bdarnell)
}
lastIndex, err := storage.LastIndex()
if err != nil {
panic(err) // TODO(bdarnell)
}
log.unstable.offset = lastIndex + 1
// Initialize our committed and applied pointers to the time of the last compaction.
log.committed = firstIndex - 1
log.applied = firstIndex - 1
return log
}
2014-07-24 03:15:25 +04:00
func (l *raftLog) String() string {
return fmt.Sprintf("unstable=%d committed=%d applied=%d len(unstableEntries)=%d", l.unstable.offset, l.committed, l.applied, len(l.unstable.entries))
}
// maybeAppend returns (0, false) if the entries cannot be appended. Otherwise,
// it returns (last index of new entries, true).
func (l *raftLog) maybeAppend(index, logTerm, committed uint64, ents ...pb.Entry) (lastnewi uint64, ok bool) {
lastnewi = index + uint64(len(ents))
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 {
2014-10-08 14:29:53 +04:00
case ci == 0:
2014-07-14 21:58:41 +04:00
case ci <= l.committed:
2014-11-26 03:21:50 +03:00
log.Panicf("entry %d conflict with committed entry [committed(%d)]", ci, l.committed)
2014-07-14 21:58:41 +04:00
default:
l.append(ci-1, ents[ci-from:]...)
}
l.commitTo(min(committed, lastnewi))
return lastnewi, true
2014-05-27 23:24:49 +04:00
}
return 0, false
2014-05-27 23:24:49 +04:00
}
2014-10-08 14:29:53 +04:00
func (l *raftLog) append(after uint64, ents ...pb.Entry) uint64 {
if after < l.committed {
2014-11-26 03:21:50 +03:00
log.Panicf("after(%d) is out of range [committed(%d)]", after, l.committed)
}
l.unstable.truncateAndAppend(after, ents)
2014-05-28 22:04:21 +04:00
return l.lastIndex()
2014-05-27 23:24:49 +04:00
}
2014-10-26 04:49:49 +03:00
// findConflict finds the index of the conflict.
2014-10-27 21:09:44 +03:00
// It returns the first pair of conflicting entries between the existing
// entries and the given entries, if there are any.
2014-10-26 04:49:49 +03:00
// If there is no conflicting entries, and the existing entries contains
// all the given entries, zero will be returned.
// If there is no conflicting entries, but the given entries contains new
// entries, the index of the first new entry will be returned.
2014-10-27 21:09:44 +03:00
// An entry is considered to be conflicting if it has the same index but
// a different term.
// The first entry MUST have an index equal to the argument 'from'.
// The index of the given entries MUST be continuously increasing.
2014-10-08 14:29:53 +04:00
func (l *raftLog) findConflict(from uint64, ents []pb.Entry) uint64 {
2014-10-27 21:09:44 +03:00
// TODO(xiangli): validate the index of ents
2014-07-14 21:58:41 +04:00
for i, ne := range ents {
if !l.matchTerm(from+uint64(i), ne.Term) {
2014-10-08 14:29:53 +04:00
return from + uint64(i)
2014-07-14 21:58:41 +04:00
}
}
2014-10-08 14:29:53 +04:00
return 0
2014-07-14 21:58:41 +04:00
}
func (l *raftLog) unstableEntries() []pb.Entry {
if len(l.unstable.entries) == 0 {
2014-08-23 02:08:13 +04:00
return nil
}
// copy unstable entries to an empty slice
return append([]pb.Entry{}, l.unstable.entries...)
2014-08-22 23:59:34 +04:00
}
// nextEnts returns all the available entries for execution.
// If applied is smaller than the index of snapshot, it returns all committed
// entries after the index of snapshot.
2014-08-28 05:53:18 +04:00
func (l *raftLog) nextEnts() (ents []pb.Entry) {
off := max(l.applied+1, l.firstIndex())
if l.committed+1 > off {
return l.slice(off, l.committed+1)
2014-08-22 23:59:34 +04:00
}
return nil
}
2014-11-26 21:59:13 +03:00
func (l *raftLog) snapshot() (pb.Snapshot, error) {
if l.unstable.snapshot != nil {
return *l.unstable.snapshot, nil
2014-11-26 21:59:13 +03:00
}
return l.storage.Snapshot()
}
func (l *raftLog) firstIndex() uint64 {
if i, ok := l.unstable.maybeFirstIndex(); ok {
return i
2014-11-26 21:59:13 +03:00
}
index, err := l.storage.FirstIndex()
if err != nil {
panic(err) // TODO(bdarnell)
}
return index
}
func (l *raftLog) lastIndex() uint64 {
if i, ok := l.unstable.maybeLastIndex(); ok {
return i
}
i, err := l.storage.LastIndex()
if err != nil {
panic(err) // TODO(bdarnell)
}
return i
}
func (l *raftLog) commitTo(tocommit uint64) {
// never decrease commit
if l.committed < tocommit {
if l.lastIndex() < tocommit {
2014-11-24 20:01:25 +03:00
log.Panicf("tocommit(%d) is out of range [lastIndex(%d)]", tocommit, l.lastIndex())
}
l.committed = tocommit
}
}
func (l *raftLog) appliedTo(i uint64) {
if i == 0 {
return
}
if l.committed < i || i < l.applied {
2014-11-24 20:01:25 +03:00
log.Panicf("applied(%d) is out of range [prevApplied(%d), committed(%d)]", i, l.applied, l.committed)
}
l.applied = i
}
2014-11-27 02:31:07 +03:00
func (l *raftLog) stableTo(i uint64) { l.unstable.stableTo(i) }
2014-11-27 02:31:07 +03:00
func (l *raftLog) stableSnapTo(i uint64) { l.unstable.stableSnapTo(i) }
2014-11-27 02:31:07 +03:00
func (l *raftLog) lastTerm() uint64 { return l.term(l.lastIndex()) }
2014-05-27 23:24:49 +04:00
2014-10-08 14:29:53 +04:00
func (l *raftLog) term(i uint64) uint64 {
if i > l.lastIndex() {
return 0
}
if t, ok := l.unstable.maybeTerm(i); ok {
return t
}
t, err := l.storage.Term(i)
if err == nil {
return t
}
if err == ErrCompacted {
2014-11-24 21:13:56 +03:00
return 0
2014-05-27 23:24:49 +04:00
}
panic(err) // TODO(bdarnell)
2014-05-27 23:24:49 +04:00
}
2014-11-27 02:31:07 +03:00
func (l *raftLog) entries(i uint64) []pb.Entry { return l.slice(i, l.lastIndex()+1) }
2014-05-27 23:24:49 +04:00
// allEntries returns all entries in the log.
2014-11-27 02:31:07 +03:00
func (l *raftLog) allEntries() []pb.Entry { return l.entries(l.firstIndex()) }
2014-10-26 06:12:54 +03:00
// isUpToDate determines if the given (lastIndex,term) log is more up-to-date
// by comparing the index and term of the last entries in the existing logs.
// If the logs have last entries with different terms, then the log with the
// later term is more up-to-date. If the logs end with the same term, then
2014-10-27 21:09:44 +03:00
// whichever log has the larger lastIndex is more up-to-date. If the logs are
// the same, the given log is up-to-date.
2014-10-26 06:12:54 +03:00
func (l *raftLog) isUpToDate(lasti, term uint64) bool {
return term > l.lastTerm() || (term == l.lastTerm() && lasti >= l.lastIndex())
2014-05-27 23:24:49 +04:00
}
2014-11-27 02:31:07 +03:00
func (l *raftLog) matchTerm(i, term uint64) bool { return l.term(i) == term }
2014-05-27 23:24:49 +04:00
2014-10-08 14:29:53 +04:00
func (l *raftLog) maybeCommit(maxIndex, term uint64) bool {
if maxIndex > l.committed && l.term(maxIndex) == term {
l.commitTo(maxIndex)
2014-05-28 19:24:09 +04:00
return true
}
return false
}
2014-08-28 05:53:18 +04:00
func (l *raftLog) restore(s pb.Snapshot) {
l.committed = s.Metadata.Index
l.unstable.restore(s)
2014-06-28 03:15:04 +04:00
}
2014-09-12 00:59:29 +04:00
// slice returns a slice of log entries from lo through hi-1, inclusive.
2014-10-08 14:29:53 +04:00
func (l *raftLog) slice(lo uint64, hi uint64) []pb.Entry {
2014-06-28 03:15:04 +04:00
if lo >= hi {
return nil
}
if l.isOutOfBounds(lo) || l.isOutOfBounds(hi-1) {
return nil
}
var ents []pb.Entry
if lo < l.unstable.offset {
storedEnts, err := l.storage.Entries(lo, min(hi, l.unstable.offset))
if err == ErrCompacted {
// This should never fail because it has been checked before.
log.Panicf("entries[%d:%d) from storage is out of bound", lo, min(hi, l.unstable.offset))
return nil
} else if err != nil {
panic(err) // TODO(bdarnell)
}
ents = append(ents, storedEnts...)
2014-06-28 03:15:04 +04:00
}
if hi > l.unstable.offset {
unstable := l.unstable.slice(max(lo, l.unstable.offset), hi)
ents = append(ents, unstable...)
}
return ents
2014-06-28 03:15:04 +04:00
}
2014-07-14 21:58:41 +04:00
2014-10-08 14:29:53 +04:00
func (l *raftLog) isOutOfBounds(i uint64) bool {
if i < l.firstIndex() || i > l.lastIndex() {
return true
}
return false
}