@@ -60,7 +60,7 @@ func NodeToString(role NodeRole) string {
6060// raft stack definition
6161type Raft struct {
6262 mu sync.RWMutex
63- peers []* RaftClientEnd // rpc client end
63+ peers []* RaftPeerNode // rpc client end
6464 id int64
6565 dead int32
6666 applyCh chan * pb.ApplyMsg
@@ -71,7 +71,6 @@ type Raft struct {
7171 votedFor int64
7272 grantedVotes int
7373 logs * RaftLog
74- persister * RaftLog
7574 commitIdx int64
7675 lastApplied int64
7776 nextIdx []int
@@ -85,7 +84,7 @@ type Raft struct {
8584 baseElecTimeout uint64
8685}
8786
88- func MakeRaft (peers []* RaftClientEnd , me int64 , newdbEng storage_eng.KvStore , applyCh chan * pb.ApplyMsg , heartbeatTimeOutMs uint64 , baseElectionTimeOutMs uint64 ) * Raft {
87+ func MakeRaft (peers []* RaftPeerNode , me int64 , newdbEng storage_eng.KvStore , applyCh chan * pb.ApplyMsg , heartbeatTimeOutMs uint64 , baseElectionTimeOutMs uint64 ) * Raft {
8988 rf := & Raft {
9089 peers : peers ,
9190 id : me ,
@@ -98,15 +97,14 @@ func MakeRaft(peers []*RaftClientEnd, me int64, newdbEng storage_eng.KvStore, ap
9897 grantedVotes : 0 ,
9998 isSnapshoting : false ,
10099 logs : MakePersistRaftLog (newdbEng ),
101- persister : MakePersistRaftLog (newdbEng ),
102100 nextIdx : make ([]int , len (peers )),
103101 matchIdx : make ([]int , len (peers )),
104102 heartbeatTimer : time .NewTimer (time .Millisecond * time .Duration (heartbeatTimeOutMs )),
105103 electionTimer : time .NewTimer (time .Millisecond * time .Duration (MakeAnRandomElectionTimeout (int (baseElectionTimeOutMs )))),
106104 baseElecTimeout : baseElectionTimeOutMs ,
107105 heartBeatTimeout : heartbeatTimeOutMs ,
108106 }
109- rf .curTerm , rf .votedFor = rf .persister .ReadRaftState ()
107+ rf .curTerm , rf .votedFor = rf .logs .ReadRaftState ()
110108 rf .applyCond = sync .NewCond (& rf .mu )
111109 last_log := rf .logs .GetLast ()
112110 for _ , peer := range peers {
@@ -126,7 +124,7 @@ func MakeRaft(peers []*RaftClientEnd, me int64, newdbEng storage_eng.KvStore, ap
126124}
127125
128126func (rf * Raft ) PersistRaftState () {
129- rf .persister .PersistRaftState (rf .curTerm , rf .votedFor )
127+ rf .logs .PersistRaftState (rf .curTerm , rf .votedFor )
130128}
131129
132130func (rf * Raft ) Kill () {
@@ -137,10 +135,6 @@ func (rf *Raft) IsKilled() bool {
137135 return atomic .LoadInt32 (& rf .dead ) == 1
138136}
139137
140- func (rf * Raft ) GetFirstLogEnt () * pb.Entry {
141- return rf .logs .GetFirst ()
142- }
143-
144138func (rf * Raft ) SwitchRaftNodeRole (role NodeRole ) {
145139 if rf .role == role {
146140 return
@@ -257,9 +251,9 @@ func (rf *Raft) HandleAppendEntries(req *pb.AppendEntriesRequest, resp *pb.Appen
257251 resp .ConflictIndex = last_index + 1
258252 } else {
259253 first_index := rf .logs .GetFirst ().Index
260- resp .ConflictTerm = int64 (rf .logs .GetEntry (req .PrevLogIndex - int64 ( first_index ) ).Term )
254+ resp .ConflictTerm = int64 (rf .logs .GetEntry (req .PrevLogIndex ).Term )
261255 index := req .PrevLogIndex - 1
262- for index >= int64 (first_index ) && rf .logs .GetEntry (index - first_index ).Term == uint64 (resp .ConflictTerm ) {
256+ for index >= int64 (first_index ) && rf .logs .GetEntry (index ).Term == uint64 (resp .ConflictTerm ) {
263257 index --
264258 }
265259 resp .ConflictIndex = index
@@ -269,7 +263,7 @@ func (rf *Raft) HandleAppendEntries(req *pb.AppendEntriesRequest, resp *pb.Appen
269263
270264 first_index := rf .logs .GetFirst ().Index
271265 for index , entry := range req .Entries {
272- if int (entry .Index - first_index ) >= rf .logs .LogItemCount () || rf .logs .GetEntry (entry .Index - first_index ).Term != entry .Term {
266+ if int (entry .Index - first_index ) >= rf .logs .LogItemCount () || rf .logs .GetEntry (entry .Index ).Term != entry .Term {
273267 rf .logs .EraseAfter (entry .Index - first_index , true )
274268 for _ , newEnt := range req .Entries [index :] {
275269 rf .logs .Append (newEnt )
@@ -294,11 +288,11 @@ func (rf *Raft) CondInstallSnapshot(lastIncluedTerm int, lastIncludedIndex int,
294288 if lastIncludedIndex > int (rf .logs .GetLast ().Index ) {
295289 rf .logs .ReInitLogs ()
296290 } else {
297- rf .logs .EraseBefore (int64 (lastIncludedIndex )- rf . logs . GetFirst (). Index , true )
291+ rf .logs .EraseBefore (int64 (lastIncludedIndex ), true )
298292 rf .logs .SetEntFirstData ([]byte {})
299293 }
300294 // update dummy entry with lastIncludedTerm and lastIncludedIndex
301- rf .logs .SetEntFirstTermAndIndex (int64 (lastIncluedTerm ), int64 (lastIncludedIndex ))
295+ rf .logs .ResetFirstEntryTermAndIndex (int64 (lastIncluedTerm ), int64 (lastIncludedIndex ))
302296
303297 rf .lastApplied = int64 (lastIncludedIndex )
304298 rf .commitIdx = int64 (lastIncludedIndex )
@@ -317,7 +311,7 @@ func (rf *Raft) Snapshot(index int, snapshot []byte) {
317311 logger .ELogger ().Sugar ().Warnf ("reject snapshot, current snapshotIndex is larger in cur term" )
318312 return
319313 }
320- rf .logs .EraseBefore (int64 (index )- int64 ( snapshot_index ) , true )
314+ rf .logs .EraseBefore (int64 (index ), true )
321315 rf .logs .SetEntFirstData ([]byte {})
322316 logger .ELogger ().Sugar ().Debugf ("del log entry before idx %d" , index )
323317 rf .isSnapshoting = false
@@ -382,7 +376,7 @@ func (rf *Raft) advanceCommitIndexForLeader() {
382376 new_commit_index := rf .matchIdx [n - (n / 2 + 1 )]
383377 if new_commit_index > int (rf .commitIdx ) {
384378 if rf .MatchLog (rf .curTerm , int64 (new_commit_index )) {
385- logger .ELogger ().Sugar ().Debugf ("peer %d advance commit index %d at term %d" , rf .id , rf .commitIdx , rf .curTerm )
379+ logger .ELogger ().Sugar ().Debugf ("leader advance commit lid %d index %d at term %d" , rf .id , rf .commitIdx , rf .curTerm )
386380 rf .commitIdx = int64 (new_commit_index )
387381 rf .applyCond .Signal ()
388382 }
@@ -400,7 +394,7 @@ func (rf *Raft) advanceCommitIndexForFollower(leaderCommit int) {
400394
401395// MatchLog is log matched
402396func (rf * Raft ) MatchLog (term , index int64 ) bool {
403- return index <= int64 (rf .logs .GetLast ().Index ) && rf .logs .GetEntry (index - int64 ( rf . logs . GetFirst (). Index ) ).Term == uint64 (term )
397+ return index <= int64 (rf .logs .GetLast ().Index ) && rf .logs .GetEntry (index ).Term == uint64 (term )
404398}
405399
406400// Election make a new election
@@ -420,7 +414,7 @@ func (rf *Raft) Election() {
420414 if int64 (peer .id ) == rf .id {
421415 continue
422416 }
423- go func (peer * RaftClientEnd ) {
417+ go func (peer * RaftPeerNode ) {
424418 logger .ELogger ().Sugar ().Debugf ("send request vote to %s %s" , peer .addr , vote_req .String ())
425419
426420 request_vote_resp , err := (* peer .raftServiceCli ).RequestVote (context .Background (), vote_req )
@@ -473,7 +467,7 @@ func (rf *Raft) BroadcastHeartbeat() {
473467 continue
474468 }
475469 logger .ELogger ().Sugar ().Debugf ("send heart beat to %s" , peer .addr )
476- go func (peer * RaftClientEnd ) {
470+ go func (peer * RaftPeerNode ) {
477471 rf .replicateOneRound (peer )
478472 }(peer )
479473 }
@@ -545,7 +539,7 @@ func (rf *Raft) CloseEndsConn() {
545539}
546540
547541// Replicator manager duplicate run
548- func (rf * Raft ) Replicator (peer * RaftClientEnd ) {
542+ func (rf * Raft ) Replicator (peer * RaftPeerNode ) {
549543 rf .replicatorCond [peer .id ].L .Lock ()
550544 defer rf .replicatorCond [peer .id ].L .Unlock ()
551545 for ! rf .IsKilled () {
@@ -558,7 +552,7 @@ func (rf *Raft) Replicator(peer *RaftClientEnd) {
558552}
559553
560554// replicateOneRound duplicate log entries to other nodes in the cluster
561- func (rf * Raft ) replicateOneRound (peer * RaftClientEnd ) {
555+ func (rf * Raft ) replicateOneRound (peer * RaftPeerNode ) {
562556 rf .mu .RLock ()
563557 if rf .role != NodeRoleLeader {
564558 rf .mu .RUnlock ()
@@ -606,15 +600,15 @@ func (rf *Raft) replicateOneRound(peer *RaftClientEnd) {
606600 } else {
607601 first_index := rf .logs .GetFirst ().Index
608602 logger .ELogger ().Sugar ().Debugf ("first log index %d" , first_index )
609- _ , new_ents := rf .logs .EraseBefore (int64 (prev_log_index )+ 1 - first_index , false )
603+ new_ents , _ := rf .logs .EraseBefore (int64 (prev_log_index )+ 1 , false )
610604 entries := make ([]* pb.Entry , len (new_ents ))
611605 copy (entries , new_ents )
612606
613607 append_ent_req := & pb.AppendEntriesRequest {
614608 Term : rf .curTerm ,
615609 LeaderId : int64 (rf .id ),
616610 PrevLogIndex : int64 (prev_log_index ),
617- PrevLogTerm : int64 (rf .logs .GetEntry (int64 (prev_log_index ) - first_index ).Term ),
611+ PrevLogTerm : int64 (rf .logs .GetEntry (int64 (prev_log_index )).Term ),
618612 Entries : entries ,
619613 LeaderCommit : rf .commitIdx ,
620614 }
@@ -644,7 +638,7 @@ func (rf *Raft) replicateOneRound(peer *RaftClientEnd) {
644638 rf .nextIdx [peer .id ] = int (resp .ConflictIndex )
645639 if resp .ConflictTerm != - 1 {
646640 for i := append_ent_req .PrevLogIndex ; i >= int64 (first_index ); i -- {
647- if rf .logs .GetEntry (i - int64 ( first_index ) ).Term == uint64 (resp .ConflictTerm ) {
641+ if rf .logs .GetEntry (i ).Term == uint64 (resp .ConflictTerm ) {
648642 rf .nextIdx [peer .id ] = int (i + 1 )
649643 break
650644 }
@@ -667,9 +661,9 @@ func (rf *Raft) Applier() {
667661 rf .applyCond .Wait ()
668662 }
669663
670- first_index , commit_index , last_applied := rf . logs . GetFirst (). Index , rf .commitIdx , rf .lastApplied
664+ commit_index , last_applied := rf .commitIdx , rf .lastApplied
671665 entries := make ([]* pb.Entry , commit_index - last_applied )
672- copy (entries , rf .logs .GetRange (last_applied + 1 - int64 ( first_index ) , commit_index + 1 - int64 ( first_index ) ))
666+ copy (entries , rf .logs .GetRange (last_applied + 1 , commit_index ))
673667 logger .ELogger ().Sugar ().Debugf ("%d, applies entries %d-%d in term %d" , rf .id , rf .lastApplied , commit_index , rf .curTerm )
674668
675669 rf .mu .Unlock ()
0 commit comments