2016-05-13 06:49:15 +03:00
|
|
|
// Copyright 2015 The etcd Authors
|
2015-01-25 06:19:16 +03:00
|
|
|
//
|
|
|
|
// 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-10-18 02:41:22 +04:00
|
|
|
|
2014-05-27 23:24:49 +04:00
|
|
|
package raft
|
|
|
|
|
2014-08-28 05:53:18 +04:00
|
|
|
import (
|
|
|
|
"fmt"
|
2014-11-03 23:26:13 +03:00
|
|
|
"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 {
|
2014-11-03 23:04:24 +03:00
|
|
|
// storage contains all stable entries since the last snapshot.
|
|
|
|
storage Storage
|
2014-11-26 21:59:13 +03:00
|
|
|
|
2014-11-27 00:36:17 +03:00
|
|
|
// unstable contains all unstable entries and snapshot.
|
|
|
|
// they will be saved into storage.
|
|
|
|
unstable unstable
|
|
|
|
|
2014-11-03 23:04:24 +03:00
|
|
|
// committed is the highest log position that is known to be in
|
|
|
|
// stable storage on a quorum of nodes.
|
2014-10-08 14:29:53 +04:00
|
|
|
committed uint64
|
2014-11-03 23:04:24 +03:00
|
|
|
// applied is the highest log position that the application has
|
|
|
|
// been instructed to apply to its state machine.
|
|
|
|
// Invariant: applied <= committed
|
2014-11-20 00:17:50 +03:00
|
|
|
applied uint64
|
2015-08-10 18:04:38 +03:00
|
|
|
|
|
|
|
logger Logger
|
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
|
2014-12-29 22:17:48 +03:00
|
|
|
// that it just commits and applies the latest snapshot.
|
2015-08-10 18:04:38 +03:00
|
|
|
func newLog(storage Storage, logger Logger) *raftLog {
|
2014-11-03 23:04:24 +03:00
|
|
|
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
|
|
|
}
|
2014-11-13 02:21:30 +03:00
|
|
|
log := &raftLog{
|
|
|
|
storage: storage,
|
2015-08-10 18:04:38 +03:00
|
|
|
logger: logger,
|
2014-11-13 02:21:30 +03:00
|
|
|
}
|
2014-11-20 00:17:50 +03:00
|
|
|
firstIndex, err := storage.FirstIndex()
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // TODO(bdarnell)
|
|
|
|
}
|
2014-11-14 21:53:42 +03:00
|
|
|
lastIndex, err := storage.LastIndex()
|
2014-11-18 00:37:46 +03:00
|
|
|
if err != nil {
|
2014-11-03 23:04:24 +03:00
|
|
|
panic(err) // TODO(bdarnell)
|
2014-10-30 19:42:42 +03:00
|
|
|
}
|
2014-11-27 00:36:17 +03:00
|
|
|
log.unstable.offset = lastIndex + 1
|
2015-08-10 18:04:38 +03:00
|
|
|
log.unstable.logger = logger
|
2014-11-20 00:17:50 +03:00
|
|
|
// Initialize our committed and applied pointers to the time of the last compaction.
|
|
|
|
log.committed = firstIndex - 1
|
|
|
|
log.applied = firstIndex - 1
|
2014-11-18 00:37:46 +03:00
|
|
|
|
2014-11-13 02:21:30 +03:00
|
|
|
return log
|
2014-09-15 03:45:45 +04:00
|
|
|
}
|
|
|
|
|
2014-07-24 03:15:25 +04:00
|
|
|
func (l *raftLog) String() string {
|
2015-04-24 10:28:57 +03:00
|
|
|
return fmt.Sprintf("committed=%d, applied=%d, unstable.offset=%d, len(unstable.Entries)=%d", l.committed, l.applied, l.unstable.offset, len(l.unstable.entries))
|
2014-07-24 00:58:25 +04:00
|
|
|
}
|
|
|
|
|
2014-10-23 23:37:13 +04:00
|
|
|
// maybeAppend returns (0, false) if the entries cannot be appended. Otherwise,
|
2014-10-30 00:57:39 +03:00
|
|
|
// it returns (last index of new entries, true).
|
2014-10-23 23:37:13 +04:00
|
|
|
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-12-02 19:50:15 +03:00
|
|
|
ci := l.findConflict(ents)
|
2014-07-14 21:58:41 +04:00
|
|
|
switch {
|
2014-10-08 14:29:53 +04:00
|
|
|
case ci == 0:
|
2014-07-14 21:58:41 +04:00
|
|
|
case ci <= l.committed:
|
2015-08-10 18:04:38 +03:00
|
|
|
l.logger.Panicf("entry %d conflict with committed entry [committed(%d)]", ci, l.committed)
|
2014-07-14 21:58:41 +04:00
|
|
|
default:
|
2014-12-02 19:50:15 +03:00
|
|
|
offset := index + 1
|
|
|
|
l.append(ents[ci-offset:]...)
|
2014-07-14 21:58:41 +04:00
|
|
|
}
|
2014-11-18 03:58:49 +03:00
|
|
|
l.commitTo(min(committed, lastnewi))
|
2014-10-23 23:37:13 +04:00
|
|
|
return lastnewi, true
|
2014-05-27 23:24:49 +04:00
|
|
|
}
|
2014-10-23 23:37:13 +04:00
|
|
|
return 0, false
|
2014-05-27 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
2014-12-02 19:50:15 +03:00
|
|
|
func (l *raftLog) append(ents ...pb.Entry) uint64 {
|
|
|
|
if len(ents) == 0 {
|
|
|
|
return l.lastIndex()
|
|
|
|
}
|
|
|
|
if after := ents[0].Index - 1; after < l.committed {
|
2015-08-10 18:04:38 +03:00
|
|
|
l.logger.Panicf("after(%d) is out of range [committed(%d)]", after, l.committed)
|
2014-11-14 21:53:42 +03:00
|
|
|
}
|
2014-12-02 19:50:15 +03:00
|
|
|
l.unstable.truncateAndAppend(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-12-02 19:50:15 +03:00
|
|
|
func (l *raftLog) findConflict(ents []pb.Entry) uint64 {
|
|
|
|
for _, ne := range ents {
|
|
|
|
if !l.matchTerm(ne.Index, ne.Term) {
|
|
|
|
if ne.Index <= l.lastIndex() {
|
2015-08-10 18:04:38 +03:00
|
|
|
l.logger.Infof("found conflict at index %d [existing term: %d, conflicting term: %d]",
|
|
|
|
ne.Index, l.zeroTermOnErrCompacted(l.term(ne.Index)), ne.Term)
|
2014-12-02 10:50:10 +03:00
|
|
|
}
|
2014-12-02 19:50:15 +03:00
|
|
|
return ne.Index
|
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-11-03 23:04:24 +03:00
|
|
|
func (l *raftLog) unstableEntries() []pb.Entry {
|
2014-11-27 00:36:17 +03:00
|
|
|
if len(l.unstable.entries) == 0 {
|
2014-08-23 02:08:13 +04:00
|
|
|
return nil
|
|
|
|
}
|
2014-11-27 10:35:06 +03:00
|
|
|
return l.unstable.entries
|
2014-08-22 23:59:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// nextEnts returns all the available entries for execution.
|
2014-11-14 11:09:54 +03:00
|
|
|
// 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) {
|
2014-11-20 00:17:50 +03:00
|
|
|
off := max(l.applied+1, l.firstIndex())
|
|
|
|
if l.committed+1 > off {
|
2015-06-13 08:24:40 +03:00
|
|
|
ents, err := l.slice(off, l.committed+1, noLimit)
|
|
|
|
if err != nil {
|
2015-08-10 18:04:38 +03:00
|
|
|
l.logger.Panicf("unexpected error when getting unapplied entries (%v)", err)
|
2015-06-13 08:24:40 +03:00
|
|
|
}
|
|
|
|
return ents
|
2014-08-22 23:59:34 +04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-20 10:42:59 +03:00
|
|
|
// hasNextEnts returns if there is any available entries for execution. This
|
|
|
|
// is a fast check without heavy raftLog.slice() in raftLog.nextEnts().
|
|
|
|
func (l *raftLog) hasNextEnts() bool {
|
|
|
|
off := max(l.applied+1, l.firstIndex())
|
2016-04-03 04:27:54 +03:00
|
|
|
return l.committed+1 > off
|
2015-11-20 10:42:59 +03:00
|
|
|
}
|
|
|
|
|
2014-11-26 21:59:13 +03:00
|
|
|
func (l *raftLog) snapshot() (pb.Snapshot, error) {
|
2014-11-27 00:36:17 +03:00
|
|
|
if l.unstable.snapshot != nil {
|
|
|
|
return *l.unstable.snapshot, nil
|
2014-11-26 21:59:13 +03:00
|
|
|
}
|
|
|
|
return l.storage.Snapshot()
|
|
|
|
}
|
|
|
|
|
2014-11-03 23:04:24 +03:00
|
|
|
func (l *raftLog) firstIndex() uint64 {
|
2014-11-27 01:40:58 +03:00
|
|
|
if i, ok := l.unstable.maybeFirstIndex(); ok {
|
|
|
|
return i
|
2014-11-26 21:59:13 +03:00
|
|
|
}
|
2014-11-14 21:53:42 +03:00
|
|
|
index, err := l.storage.FirstIndex()
|
2014-11-03 23:04:24 +03:00
|
|
|
if err != nil {
|
|
|
|
panic(err) // TODO(bdarnell)
|
|
|
|
}
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *raftLog) lastIndex() uint64 {
|
2014-11-27 01:40:58 +03:00
|
|
|
if i, ok := l.unstable.maybeLastIndex(); ok {
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
i, err := l.storage.LastIndex()
|
|
|
|
if err != nil {
|
|
|
|
panic(err) // TODO(bdarnell)
|
|
|
|
}
|
|
|
|
return i
|
2014-11-03 23:04:24 +03:00
|
|
|
}
|
|
|
|
|
2014-11-18 03:58:49 +03:00
|
|
|
func (l *raftLog) commitTo(tocommit uint64) {
|
|
|
|
// never decrease commit
|
|
|
|
if l.committed < tocommit {
|
|
|
|
if l.lastIndex() < tocommit {
|
2015-09-12 22:17:02 +03:00
|
|
|
l.logger.Panicf("tocommit(%d) is out of range [lastIndex(%d)]. Was the raft log corrupted, truncated, or lost?", tocommit, l.lastIndex())
|
2014-11-18 03:58:49 +03:00
|
|
|
}
|
|
|
|
l.committed = tocommit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-03 23:26:13 +03:00
|
|
|
func (l *raftLog) appliedTo(i uint64) {
|
|
|
|
if i == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if l.committed < i || i < l.applied {
|
2015-08-10 18:04:38 +03:00
|
|
|
l.logger.Panicf("applied(%d) is out of range [prevApplied(%d), committed(%d)]", i, l.applied, l.committed)
|
2014-11-03 23:26:13 +03:00
|
|
|
}
|
|
|
|
l.applied = i
|
|
|
|
}
|
|
|
|
|
2014-11-29 01:13:07 +03:00
|
|
|
func (l *raftLog) stableTo(i, t uint64) { l.unstable.stableTo(i, t) }
|
2014-11-27 01:40:58 +03:00
|
|
|
|
2014-11-27 02:31:07 +03:00
|
|
|
func (l *raftLog) stableSnapTo(i uint64) { l.unstable.stableSnapTo(i) }
|
2014-11-03 23:26:13 +03:00
|
|
|
|
2015-06-13 08:24:40 +03:00
|
|
|
func (l *raftLog) lastTerm() uint64 {
|
|
|
|
t, err := l.term(l.lastIndex())
|
|
|
|
if err != nil {
|
2015-08-10 18:04:38 +03:00
|
|
|
l.logger.Panicf("unexpected error when getting the last term (%v)", err)
|
2015-06-13 08:24:40 +03:00
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|
2014-05-27 23:24:49 +04:00
|
|
|
|
2015-06-13 08:24:40 +03:00
|
|
|
func (l *raftLog) term(i uint64) (uint64, error) {
|
2014-12-03 10:57:02 +03:00
|
|
|
// the valid term range is [index of dummy entry, last index]
|
|
|
|
dummyIndex := l.firstIndex() - 1
|
|
|
|
if i < dummyIndex || i > l.lastIndex() {
|
2015-06-13 08:24:40 +03:00
|
|
|
// TODO: return an error instead?
|
|
|
|
return 0, nil
|
2014-11-27 01:40:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if t, ok := l.unstable.maybeTerm(i); ok {
|
2015-06-13 08:24:40 +03:00
|
|
|
return t, nil
|
2014-11-27 01:40:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
t, err := l.storage.Term(i)
|
|
|
|
if err == nil {
|
2015-06-13 08:24:40 +03:00
|
|
|
return t, nil
|
2014-11-27 01:40:58 +03:00
|
|
|
}
|
|
|
|
if err == ErrCompacted {
|
2015-06-13 08:24:40 +03:00
|
|
|
return 0, err
|
2014-05-27 23:24:49 +04:00
|
|
|
}
|
2014-11-27 01:40:58 +03:00
|
|
|
panic(err) // TODO(bdarnell)
|
2014-05-27 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
2015-06-13 08:24:40 +03:00
|
|
|
func (l *raftLog) entries(i, maxsize uint64) ([]pb.Entry, error) {
|
2014-12-03 03:19:37 +03:00
|
|
|
if i > l.lastIndex() {
|
2015-06-13 08:24:40 +03:00
|
|
|
return nil, nil
|
2014-12-03 03:19:37 +03:00
|
|
|
}
|
2015-03-18 22:36:29 +03:00
|
|
|
return l.slice(i, l.lastIndex()+1, maxsize)
|
2014-12-03 03:19:37 +03:00
|
|
|
}
|
2014-05-27 23:24:49 +04:00
|
|
|
|
2014-11-18 00:37:46 +03:00
|
|
|
// allEntries returns all entries in the log.
|
2015-06-13 08:24:40 +03:00
|
|
|
func (l *raftLog) allEntries() []pb.Entry {
|
|
|
|
ents, err := l.entries(l.firstIndex(), noLimit)
|
|
|
|
if err == nil {
|
|
|
|
return ents
|
|
|
|
}
|
|
|
|
if err == ErrCompacted { // try again if there was a racing compaction
|
|
|
|
return l.allEntries()
|
|
|
|
}
|
|
|
|
// TODO (xiangli): handle error?
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-11-03 23:04:24 +03:00
|
|
|
|
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 {
|
2014-10-26 06:34:14 +03:00
|
|
|
return term > l.lastTerm() || (term == l.lastTerm() && lasti >= l.lastIndex())
|
2014-05-27 23:24:49 +04:00
|
|
|
}
|
|
|
|
|
2015-06-13 08:24:40 +03:00
|
|
|
func (l *raftLog) matchTerm(i, term uint64) bool {
|
|
|
|
t, err := l.term(i)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return t == 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 {
|
2015-08-10 18:04:38 +03:00
|
|
|
if maxIndex > l.committed && l.zeroTermOnErrCompacted(l.term(maxIndex)) == term {
|
2014-11-18 03:58:49 +03:00
|
|
|
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) {
|
2015-08-10 18:04:38 +03:00
|
|
|
l.logger.Infof("log [%s] starts to restore snapshot [index: %d, term: %d]", l, s.Metadata.Index, s.Metadata.Term)
|
2014-11-20 00:17:50 +03:00
|
|
|
l.committed = s.Metadata.Index
|
2014-11-27 01:40:58 +03:00
|
|
|
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.
|
2015-06-13 08:24:40 +03:00
|
|
|
func (l *raftLog) slice(lo, hi, maxSize uint64) ([]pb.Entry, error) {
|
|
|
|
err := l.mustCheckOutOfBounds(lo, hi)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-12-03 03:19:37 +03:00
|
|
|
if lo == hi {
|
2015-06-13 08:24:40 +03:00
|
|
|
return nil, nil
|
2014-06-28 03:15:04 +04:00
|
|
|
}
|
2014-11-03 23:04:24 +03:00
|
|
|
var ents []pb.Entry
|
2014-11-27 00:36:17 +03:00
|
|
|
if lo < l.unstable.offset {
|
2015-03-18 22:36:29 +03:00
|
|
|
storedEnts, err := l.storage.Entries(lo, min(hi, l.unstable.offset), maxSize)
|
2014-11-18 21:15:10 +03:00
|
|
|
if err == ErrCompacted {
|
2015-06-13 08:24:40 +03:00
|
|
|
return nil, err
|
2014-12-03 01:27:52 +03:00
|
|
|
} else if err == ErrUnavailable {
|
2015-08-10 18:04:38 +03:00
|
|
|
l.logger.Panicf("entries[%d:%d) is unavailable from storage", lo, min(hi, l.unstable.offset))
|
2014-11-18 00:37:46 +03:00
|
|
|
} else if err != nil {
|
2014-11-03 23:04:24 +03:00
|
|
|
panic(err) // TODO(bdarnell)
|
|
|
|
}
|
2015-03-18 22:36:29 +03:00
|
|
|
|
|
|
|
// check if ents has reached the size limitation
|
|
|
|
if uint64(len(storedEnts)) < min(hi, l.unstable.offset)-lo {
|
2015-06-13 08:24:40 +03:00
|
|
|
return storedEnts, nil
|
2015-03-18 22:36:29 +03:00
|
|
|
}
|
|
|
|
|
2014-12-11 00:41:04 +03:00
|
|
|
ents = storedEnts
|
2014-06-28 03:15:04 +04:00
|
|
|
}
|
2014-11-27 00:36:17 +03:00
|
|
|
if hi > l.unstable.offset {
|
2014-11-27 01:40:58 +03:00
|
|
|
unstable := l.unstable.slice(max(lo, l.unstable.offset), hi)
|
2014-12-11 00:41:04 +03:00
|
|
|
if len(ents) > 0 {
|
|
|
|
ents = append([]pb.Entry{}, ents...)
|
|
|
|
ents = append(ents, unstable...)
|
|
|
|
} else {
|
|
|
|
ents = unstable
|
|
|
|
}
|
2014-11-03 23:04:24 +03:00
|
|
|
}
|
2015-06-13 08:24:40 +03:00
|
|
|
return limitSize(ents, maxSize), nil
|
2014-06-28 03:15:04 +04:00
|
|
|
}
|
2014-07-14 21:58:41 +04:00
|
|
|
|
2014-12-03 03:19:37 +03:00
|
|
|
// l.firstIndex <= lo <= hi <= l.firstIndex + len(l.entries)
|
2015-06-13 08:24:40 +03:00
|
|
|
func (l *raftLog) mustCheckOutOfBounds(lo, hi uint64) error {
|
2014-12-03 03:19:37 +03:00
|
|
|
if lo > hi {
|
2015-08-10 18:04:38 +03:00
|
|
|
l.logger.Panicf("invalid slice %d > %d", lo, hi)
|
2014-12-03 03:19:37 +03:00
|
|
|
}
|
2015-06-13 08:24:40 +03:00
|
|
|
fi := l.firstIndex()
|
|
|
|
if lo < fi {
|
|
|
|
return ErrCompacted
|
|
|
|
}
|
|
|
|
|
|
|
|
length := l.lastIndex() - fi + 1
|
|
|
|
if lo < fi || hi > fi+length {
|
2015-08-10 18:04:38 +03:00
|
|
|
l.logger.Panicf("slice[%d,%d) out of bound [%d,%d]", lo, hi, fi, l.lastIndex())
|
2014-09-10 22:32:01 +04:00
|
|
|
}
|
2015-06-13 08:24:40 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-08-10 18:04:38 +03:00
|
|
|
func (l *raftLog) zeroTermOnErrCompacted(t uint64, err error) uint64 {
|
2015-06-13 08:24:40 +03:00
|
|
|
if err == nil {
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
if err == ErrCompacted {
|
|
|
|
return 0
|
|
|
|
}
|
2015-08-10 18:04:38 +03:00
|
|
|
l.logger.Panicf("unexpected error (%v)", err)
|
2015-06-13 08:24:40 +03:00
|
|
|
return 0
|
2014-09-10 22:32:01 +04:00
|
|
|
}
|