etcd/raft/log.go

243 lines
5.4 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"
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 {
2014-09-17 05:18:45 +04:00
ents []pb.Entry
2014-10-08 14:29:53 +04:00
unstable uint64
committed uint64
applied uint64
offset uint64
2014-09-17 05:18:45 +04:00
snapshot pb.Snapshot
2014-05-27 23:24:49 +04:00
}
2014-07-24 03:15:25 +04:00
func newLog() *raftLog {
return &raftLog{
2014-10-17 02:32:57 +04:00
ents: make([]pb.Entry, 1),
unstable: 0,
committed: 0,
applied: 0,
2014-05-27 23:24:49 +04:00
}
}
2014-07-28 07:51:16 +04:00
func (l *raftLog) isEmpty() bool {
return l.offset == 0 && len(l.ents) == 1
}
func (l *raftLog) load(ents []pb.Entry) {
l.ents = ents
2014-10-08 14:29:53 +04:00
l.unstable = l.offset + uint64(len(ents))
}
2014-07-24 03:15:25 +04:00
func (l *raftLog) String() string {
return fmt.Sprintf("offset=%d committed=%d applied=%d len(ents)=%d", l.offset, l.committed, l.applied, len(l.ents))
}
// maybeAppend returns (0, false) if the entries cannot be appended. Otherwise,
// it returns (last index of 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:
panic("conflict with committed entry")
default:
l.append(ci-1, ents[ci-from:]...)
}
tocommit := min(committed, lastnewi)
// if toCommit > commitIndex, set commitIndex = toCommit
if l.committed < tocommit {
l.committed = tocommit
2014-07-14 21:58:41 +04:00
}
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 {
2014-07-01 02:15:51 +04:00
l.ents = append(l.slice(l.offset, after+1), ents...)
l.unstable = min(l.unstable, after+1)
2014-05-28 22:04:21 +04:00
return l.lastIndex()
2014-05-27 23:24:49 +04:00
}
2014-10-08 14:29:53 +04:00
func (l *raftLog) findConflict(from uint64, ents []pb.Entry) uint64 {
2014-07-14 21:58:41 +04:00
for i, ne := range ents {
2014-10-08 14:29:53 +04:00
if oe := l.at(from + uint64(i)); oe == nil || oe.Term != ne.Term {
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
}
2014-08-28 05:53:18 +04:00
func (l *raftLog) unstableEnts() []pb.Entry {
2014-09-12 23:23:43 +04:00
ents := l.slice(l.unstable, l.lastIndex()+1)
2014-08-23 02:08:13 +04:00
if ents == nil {
return nil
}
2014-08-28 05:53:18 +04:00
cpy := make([]pb.Entry, len(ents))
2014-08-23 02:08:13 +04:00
copy(cpy, ents)
return cpy
2014-08-22 23:59:34 +04:00
}
func (l *raftLog) resetUnstable() {
l.unstable = l.lastIndex() + 1
2014-08-22 23:59:34 +04:00
}
// nextEnts returns all the available entries for execution.
// all the returned entries will be marked as applied.
2014-08-28 05:53:18 +04:00
func (l *raftLog) nextEnts() (ents []pb.Entry) {
2014-08-22 23:59:34 +04:00
if l.committed > l.applied {
return l.slice(l.applied+1, l.committed+1)
2014-08-22 23:59:34 +04:00
}
return nil
}
func (l *raftLog) resetNextEnts() {
if l.committed > l.applied {
l.applied = l.committed
}
}
2014-10-08 14:29:53 +04:00
func (l *raftLog) lastIndex() uint64 {
return uint64(len(l.ents)) - 1 + l.offset
2014-05-27 23:24:49 +04:00
}
2014-10-08 14:29:53 +04:00
func (l *raftLog) term(i uint64) uint64 {
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-10-08 14:29:53 +04:00
return 0
2014-05-27 23:24:49 +04:00
}
2014-10-08 14:29:53 +04:00
func (l *raftLog) entries(i uint64) []pb.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-10-08 14:29:53 +04:00
func (l *raftLog) isUpToDate(i, term uint64) 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-10-08 14:29:53 +04:00
func (l *raftLog) matchTerm(i, term uint64) 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-10-08 14:29:53 +04:00
func (l *raftLog) maybeCommit(maxIndex, term uint64) bool {
if maxIndex > l.committed && l.term(maxIndex) == term {
l.committed = maxIndex
2014-05-28 19:24:09 +04:00
return true
}
return false
}
// compact compacts all log entries until i.
// It removes the log entries before i, exclusive.
2014-07-01 02:15:51 +04:00
// 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-10-08 14:29:53 +04:00
func (l *raftLog) compact(i uint64) uint64 {
if l.isOutOfAppliedBounds(i) {
panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.applied))
2014-07-01 02:15:51 +04:00
}
l.ents = l.slice(i, l.lastIndex()+1)
l.unstable = max(i+1, l.unstable)
2014-07-01 02:15:51 +04:00
l.offset = i
2014-10-08 14:29:53 +04:00
return uint64(len(l.ents))
2014-07-01 02:15:51 +04:00
}
func (l *raftLog) snap(d []byte, index, term uint64, nodes []uint64) {
l.snapshot = pb.Snapshot{
Data: d,
Nodes: nodes,
Index: index,
Term: term,
}
2014-07-31 04:21:27 +04:00
}
2014-08-28 05:53:18 +04:00
func (l *raftLog) restore(s pb.Snapshot) {
l.ents = []pb.Entry{{Term: s.Term}}
2014-08-01 02:18:44 +04:00
l.unstable = s.Index + 1
l.committed = s.Index
l.applied = s.Index
l.offset = s.Index
l.snapshot = s
2014-07-01 02:15:51 +04:00
}
2014-10-08 14:29:53 +04:00
func (l *raftLog) at(i uint64) *pb.Entry {
2014-06-28 03:15:04 +04:00
if l.isOutOfBounds(i) {
return nil
}
return &l.ents[i-l.offset]
}
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
}
return l.ents[lo-l.offset : hi-l.offset]
}
2014-10-08 14:29:53 +04:00
func (l *raftLog) isOutOfBounds(i uint64) 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
2014-10-08 14:29:53 +04:00
func (l *raftLog) isOutOfAppliedBounds(i uint64) bool {
if i < l.offset || i > l.applied {
return true
}
return false
}
2014-10-08 14:29:53 +04:00
func min(a, b uint64) uint64 {
2014-07-14 21:58:41 +04:00
if a > b {
return b
}
return a
}
2014-10-08 14:29:53 +04:00
func max(a, b uint64) uint64 {
if a > b {
return a
}
return b
}