@@ -1571,11 +1571,8 @@ handle_receive_snapshot(#install_snapshot_rpc{term = Term,
15711571 % % the snapshot sending must have been interrupted and restarted
15721572 % % during the init or pre-phase
15731573 % % abort the snapshot, and revert to follower
1574- SnapState0 = ra_log :snapshot_state (Log00 ),
1575- SnapState = ra_snapshot :abort_accept (SnapState0 ),
1576- Log = ra_log :set_snapshot_state (SnapState , Log00 ),
1577- {follower , maps :remove (snapshot_phase , State0 #{log => Log }),
1578- [{next_event , Rpc }]};
1574+ State = abort_receive (State0 ),
1575+ {follower , State , [{next_event , Rpc }]};
15791576 pre when is_list (ChunkOrEntries ) ->
15801577 [{_FstIdx , _ , _ } | _ ] = ChunkOrEntries ,
15811578 % ?DEBUG("~ts: receiving snapshot chunk pre first index ~b snap index ~b, term ~b",
@@ -1664,71 +1661,50 @@ handle_receive_snapshot(#install_snapshot_rpc{term = Term,
16641661 end ;
16651662handle_receive_snapshot (# append_entries_rpc {term = Term } = Msg ,
16661663 #{current_term := CurTerm ,
1667- cfg := # cfg {log_id = LogId },
1668- log := Log0 } = State )
1664+ cfg := # cfg {log_id = LogId }} = State0 )
16691665 when Term > CurTerm ->
16701666 ? INFO (" ~ts : follower receiving snapshot saw append_entries_rpc from ~w for term ~b "
16711667 " abdicates term: ~b !" ,
16721668 [LogId , Msg # append_entries_rpc .leader_id ,
16731669 Term , CurTerm ]),
1674- SnapState0 = ra_log :snapshot_state (Log0 ),
1675- SnapState = ra_snapshot :abort_accept (SnapState0 ),
1676- Log = ra_log :set_snapshot_state (SnapState , Log0 ),
1677- {follower , maps :remove (snapshot_phase ,
1678- update_term (Term ,
1679- clear_leader_id (State #{log => Log }))),
1680- [{next_event , Msg }]};
1670+ State = abort_receive (State0 ),
1671+ {follower , update_term (Term , State ), [{next_event , Msg }]};
16811672handle_receive_snapshot ({ra_log_event , Evt },
16821673 #{cfg := # cfg {log_id = _LogId },
16831674 log := Log0 } = State ) ->
1684- % ?DEBUG("~ts: ~s ra_log_event received: ~w",
1685- % [LogId, ?FUNCTION_NAME, Evt]),
16861675 % simply forward all other events to ra_log
16871676 % whilst the snapshot is being received
16881677 {Log , Effects } = ra_log :handle_event (Evt , Log0 ),
16891678 {receive_snapshot , State #{log => Log }, Effects };
16901679handle_receive_snapshot (receive_snapshot_timeout ,
1691- #{cfg := # cfg {log_id = LogId },
1692- log := Log0 } = State ) ->
1680+ #{cfg := # cfg {log_id = LogId }} = State0 ) ->
16931681 ? INFO (" ~ts : ~s receive snapshot timed out." ,
16941682 [LogId , ? FUNCTION_NAME ]),
1695- SnapState0 = ra_log :snapshot_state (Log0 ),
1696- SnapState = ra_snapshot :abort_accept (SnapState0 ),
1697- Log = ra_log :set_snapshot_state (SnapState , Log0 ),
1698- {follower , maps :remove (snapshot_phase , State #{log => Log }), []};
1683+ State = abort_receive (State0 ),
1684+ {follower , State , []};
16991685handle_receive_snapshot (# info_rpc {term = Term } = Msg ,
17001686 #{current_term := CurTerm ,
1701- cfg := # cfg {log_id = LogId },
1702- log := Log0 } = State )
1687+ cfg := # cfg {log_id = LogId }} = State0 )
17031688 when CurTerm < Term ->
17041689 ? INFO (" ~ts : follower receiving snapshot saw info_rpc from ~w for term ~b "
1705- " abdicates term: ~b !" ,
1690+ " current term: ~b !" ,
17061691 [LogId , Msg # info_rpc .from ,
17071692 Term , CurTerm ]),
1708- SnapState0 = ra_log :snapshot_state (Log0 ),
1709- SnapState = ra_snapshot :abort_accept (SnapState0 ),
1710- Log = ra_log :set_snapshot_state (SnapState , Log0 ),
1711- {follower , maps :remove (snapshot_phase ,
1712- update_term (Term , clear_leader_id (State #{log => Log }))),
1713- [{next_event , Msg }]};
1693+ State = abort_receive (State0 ),
1694+ {follower , update_term (Term , State ), [{next_event , Msg }]};
17141695handle_receive_snapshot (# info_rpc {} = InfoRpc , State ) ->
17151696 InfoReplyEffect = empty_info_reply_effect (State , InfoRpc ),
17161697 {receive_snapshot , State , [InfoReplyEffect ]};
17171698handle_receive_snapshot (# info_reply {term = Term } = Msg ,
17181699 #{current_term := CurTerm ,
1719- cfg := # cfg {log_id = LogId },
1720- log := Log0 } = State )
1700+ cfg := # cfg {log_id = LogId }} = State0 )
17211701 when CurTerm < Term ->
17221702 ? INFO (" ~ts : follower receiving snapshot saw info_reply from ~w for term ~b "
17231703 " abdicates term: ~b !" ,
17241704 [LogId , Msg # info_reply .from ,
17251705 Term , CurTerm ]),
1726- SnapState0 = ra_log :snapshot_state (Log0 ),
1727- SnapState = ra_snapshot :abort_accept (SnapState0 ),
1728- Log = ra_log :set_snapshot_state (SnapState , Log0 ),
1729- {follower , maps :remove (snapshot_phase ,
1730- update_term (Term , clear_leader_id (State #{log => Log }))),
1731- [{next_event , Msg }]};
1706+ State = abort_receive (State0 ),
1707+ {follower , update_term (Term , State ), [{next_event , Msg }]};
17321708handle_receive_snapshot (# info_reply {}, State ) ->
17331709 {receive_snapshot , State , []};
17341710handle_receive_snapshot (Msg , State ) ->
@@ -1737,6 +1713,21 @@ handle_receive_snapshot(Msg, State) ->
17371713 % % TODO: work out what else to handle
17381714 {receive_snapshot , State , [{reply , {error , {unsupported_call , Msg }}}]}.
17391715
1716+ abort_receive (#{snapshot_phase := Phase ,
1717+ last_applied := LastApplied ,
1718+ log := Log0 } = State ) ->
1719+ SnapState0 = ra_log :snapshot_state (Log0 ),
1720+ SnapState = ra_snapshot :abort_accept (SnapState0 ),
1721+ Log1 = ra_log :set_snapshot_state (SnapState , Log0 ),
1722+ Log = case Phase of
1723+ pre ->
1724+ {ok , Log2 } = ra_log :set_last_index (LastApplied , Log1 ),
1725+ Log2 ;
1726+ _ ->
1727+ Log1
1728+ end ,
1729+ clear_leader_id (maps :remove (snapshot_phase , State #{log => Log })).
1730+
17401731-spec handle_await_condition (ra_msg (), ra_server_state ()) ->
17411732 {ra_state (), ra_server_state (), effects ()}.
17421733handle_await_condition (# request_vote_rpc {} = Msg , State ) ->
0 commit comments