@@ -1375,42 +1375,52 @@ func TestStepIgnoreOldTermMsg(t *testing.T) {
13751375// delete the existing entry and all that follow it; append any new entries not already in the log.
13761376// 3. If leaderCommit > commitIndex, set commitIndex = min(leaderCommit, index of last new entry).
13771377func TestHandleMsgApp (t * testing.T ) {
1378- tests := []struct {
1378+ const term = 2
1379+ init := index (1 ).terms (1 , 2 ) // the initial log
1380+
1381+ msgApp := func (term uint64 , ls LogSlice , commit uint64 ) pb.Message {
1382+ return pb.Message {
1383+ From : 2 , To : 1 , Type : pb .MsgApp , Term : term ,
1384+ Index : ls .prev .index , LogTerm : ls .prev .term , Entries : ls .Entries (),
1385+ Commit : commit ,
1386+ }
1387+ }
1388+ for _ , tt := range []struct {
13791389 m pb.Message
13801390 wIndex uint64
13811391 wCommit uint64
13821392 wReject bool
13831393 }{
13841394 // Ensure 1
1385- {pb. Message { Type : pb . MsgApp , Term : 3 , LogTerm : 3 , Index : 2 , Commit : 3 }, 2 , 0 , true }, // previous log mismatch
1386- {pb. Message { Type : pb . MsgApp , Term : 3 , LogTerm : 3 , Index : 3 , Commit : 3 }, 2 , 0 , true }, // previous log non-exist
1395+ {m : msgApp ( 3 , entryID { index : 2 , term : 3 }. terms (), 3 ), wIndex : 2 , wReject : true }, // previous log mismatch
1396+ {m : msgApp ( 3 , entryID { index : 3 , term : 3 }. terms (), 3 ), wIndex : 2 , wReject : true }, // previous log non-exist
13871397
13881398 // Ensure 2
1389- {pb. Message { Type : pb . MsgApp , Term : 2 , LogTerm : 1 , Index : 1 , Commit : 1 }, 2 , 1 , false },
1390- {pb. Message { Type : pb . MsgApp , Term : 3 , LogTerm : 0 , Index : 0 , Commit : 1 , Entries : []pb. Entry {{ Index : 1 , Term : 3 }}}, 1 , 1 , false },
1391- {pb. Message { Type : pb . MsgApp , Term : 2 , LogTerm : 2 , Index : 2 , Commit : 3 , Entries : []pb. Entry {{ Index : 3 , Term : 2 }, { Index : 4 , Term : 2 }}}, 4 , 3 , false },
1392- {pb. Message { Type : pb . MsgApp , Term : 2 , LogTerm : 2 , Index : 2 , Commit : 4 , Entries : []pb. Entry {{ Index : 3 , Term : 2 }}}, 3 , 3 , false },
1393- {pb. Message { Type : pb . MsgApp , Term : 2 , LogTerm : 1 , Index : 1 , Commit : 4 , Entries : []pb. Entry {{ Index : 2 , Term : 2 }}}, 2 , 2 , false },
1399+ {m : msgApp ( 2 , entryID { index : 1 , term : 1 }. terms (), 1 ), wIndex : 2 , wCommit : 1 },
1400+ {m : msgApp ( 3 , entryID {}. terms ( 3 ), 1 ), wIndex : 1 , wCommit : 1 },
1401+ {m : msgApp ( 2 , entryID { index : 2 , term : 2 }. terms ( 2 , 2 ), 3 ), wIndex : 4 , wCommit : 3 },
1402+ {m : msgApp ( 2 , entryID { index : 2 , term : 2 }. terms ( 2 ), 3 ), wIndex : 3 , wCommit : 3 },
1403+ {m : msgApp ( 2 , entryID { index : 1 , term : 1 }. terms ( 2 ), 4 ), wIndex : 2 , wCommit : 2 },
13941404
13951405 // Ensure 3
1396- {pb. Message { Type : pb . MsgApp , Term : 1 , LogTerm : 1 , Index : 1 , Commit : 3 }, 2 , 1 , false }, // match entry 1, commit up to last new entry 1
1397- {pb. Message { Type : pb . MsgApp , Term : 2 , LogTerm : 1 , Index : 1 , Commit : 3 , Entries : []pb. Entry {{ Index : 2 , Term : 2 }}}, 2 , 2 , false }, // match entry 1, commit up to last new entry 2
1398- {pb. Message { Type : pb . MsgApp , Term : 2 , LogTerm : 2 , Index : 2 , Commit : 3 }, 2 , 2 , false }, // match entry 2, commit up to last new entry 2
1399- {pb. Message { Type : pb . MsgApp , Term : 2 , LogTerm : 2 , Index : 2 , Commit : 4 }, 2 , 2 , false }, // commit up to log.last()
1400- }
1401-
1402- for i , tt := range tests {
1403- storage := newTestMemoryStorage ( withPeers ( 1 ))
1404- require . NoError ( t , storage . Append ( index ( 1 ). terms ( 1 , 2 )) )
1405- sm := newTestRaft ( 1 , 10 , 1 , storage )
1406- sm . becomeFollower ( 2 , None )
1407-
1408- sm . handleAppendEntries ( tt .m )
1409- assert .Equal (t , tt .wIndex , sm .raftLog .lastIndex (), "#%d" , i )
1410- assert . Equal ( t , tt . wCommit , sm .raftLog . committed , "#%d" , i )
1411- m := sm . readMessages ( )
1412- require . Len (t , m , 1 , "#%d" , i )
1413- assert . Equal ( t , tt . wReject , m [ 0 ]. Reject , "#%d" , i )
1406+ {m : msgApp ( 1 , entryID { index : 1 , term : 1 }. terms (), 3 ), wIndex : 2 , wCommit : 1 }, // match entry 1, commit up to last new entry 1
1407+ {m : msgApp ( 2 , entryID { index : 1 , term : 1 }. terms ( 2 ), 3 ), wIndex : 2 , wCommit : 2 }, // match entry 1, commit up to last new entry 2
1408+ {m : msgApp ( 2 , entryID { index : 2 , term : 2 }. terms (), 3 ), wIndex : 2 , wCommit : 2 }, // match entry 2, commit up to last new entry 2
1409+ {m : msgApp ( 2 , entryID { index : 2 , term : 2 }. terms (), 4 ), wIndex : 2 , wCommit : 2 }, // commit up to log.last()
1410+ } {
1411+ t . Run ( "" , func ( t * testing. T ) {
1412+ storage := newTestMemoryStorage ( withPeers ( 1 , 2 ))
1413+ require . NoError ( t , storage . Append ( init ))
1414+ sm := newTestRaft ( 1 , 10 , 1 , storage )
1415+ sm . becomeFollower ( term , None )
1416+
1417+ sm . handleAppendEntries ( tt . m )
1418+ assert . Equal ( t , tt .wIndex , sm . raftLog . lastIndex () )
1419+ assert .Equal (t , tt .wCommit , sm .raftLog .committed )
1420+ m := sm .readMessages ( )
1421+ require . Len ( t , m , 1 )
1422+ assert . Equal (t , tt . wReject , m [ 0 ]. Reject )
1423+ } )
14141424 }
14151425}
14161426
0 commit comments