Skip to content

Commit 30fc43e

Browse files
committed
refactor: add a new ErrIncompatibleLogStore for recoverFromCommittedLogs
1 parent ad87d86 commit 30fc43e

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

api.go

+14-7
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ var (
7373
// ErrLeadershipTransferInProgress is returned when the leader is rejecting
7474
// client requests because it is attempting to transfer leadership.
7575
ErrLeadershipTransferInProgress = errors.New("leadership transfer in progress")
76+
77+
// ErrIncompatibleLogStore is returned when the log store does not support
78+
// or implement some required methods.
79+
ErrIncompatibleLogStore = errors.New("log store does not implement some required methods or malformed")
7680
)
7781

7882
// Raft implements a Raft node.
@@ -590,7 +594,9 @@ func NewRaft(conf *Config, fsm FSM, logs LogStore, stable StableStore, snaps Sna
590594
return nil, err
591595
}
592596

593-
r.recoverFromCommittedLogs()
597+
if err := r.recoverFromCommittedLogs(); err != nil {
598+
return nil, err
599+
}
594600

595601
// Scan through the log for any configuration change entries.
596602
snapshotIndex, _ := r.getLastSnapshot()
@@ -706,36 +712,37 @@ func (r *Raft) tryRestoreSingleSnapshot(snapshot *SnapshotMeta) bool {
706712
}
707713

708714
// recoverFromCommittedLogs recovers the Raft node from committed logs.
709-
func (r *Raft) recoverFromCommittedLogs() {
715+
func (r *Raft) recoverFromCommittedLogs() error {
710716
if !r.RestoreCommittedLogs {
711-
return
717+
return nil
712718
}
713719

714720
// If the store implements CommitTrackingLogStore, we can read the commit index from the store.
715721
// This is useful when the store is able to track the commit index and we can avoid replaying logs.
716722
store, ok := r.logs.(CommitTrackingLogStore)
717723
if !ok {
718-
r.logger.Warn("fast recovery enabled but log store does not support it", "log_store", fmt.Sprintf("%T", r.logs))
719-
return
724+
r.logger.Warn("restore committed logs enabled but log store does not support it", "log_store", fmt.Sprintf("%T", r.logs))
725+
return ErrIncompatibleLogStore
720726
}
721727

722728
commitIndex, err := store.GetCommitIndex()
723729
if err != nil {
724730
r.logger.Error("failed to get commit index from store", "error", err)
725-
panic(err)
731+
return err
726732
}
727733

728734
lastIndex, err := r.logs.LastIndex()
729735
if err != nil {
730736
r.logger.Error("failed to get last log index from store", "error", err)
731-
panic(err)
737+
return err
732738
}
733739
if commitIndex > lastIndex {
734740
commitIndex = lastIndex
735741
}
736742

737743
r.setCommitIndex(commitIndex)
738744
r.processLogs(commitIndex, nil)
745+
return nil
739746
}
740747

741748
func (r *Raft) config() Config {

0 commit comments

Comments
 (0)