@@ -1569,11 +1569,8 @@ handle_receive_snapshot(#install_snapshot_rpc{term = Term,
15691569 % % the snapshot sending must have been interrupted and restarted
15701570 % % during the init or pre-phase
15711571 % % abort the snapshot, and revert to follower
1572- SnapState0 = ra_log :snapshot_state (Log00 ),
1573- SnapState = ra_snapshot :abort_accept (SnapState0 ),
1574- Log = ra_log :set_snapshot_state (SnapState , Log00 ),
1575- {follower , maps :remove (snapshot_phase , State0 #{log => Log }),
1576- [{next_event , Rpc }]};
1572+ State = abort_receive (State0 ),
1573+ {follower , State , [{next_event , Rpc }]};
15771574 pre when is_list (ChunkOrEntries ) ->
15781575 [{_FstIdx , _ , _ } | _ ] = ChunkOrEntries ,
15791576 % ?DEBUG("~ts: receiving snapshot chunk pre first index ~b snap index ~b, term ~b",
@@ -1662,71 +1659,50 @@ handle_receive_snapshot(#install_snapshot_rpc{term = Term,
16621659 end ;
16631660handle_receive_snapshot (# append_entries_rpc {term = Term } = Msg ,
16641661 #{current_term := CurTerm ,
1665- cfg := # cfg {log_id = LogId },
1666- log := Log0 } = State )
1662+ cfg := # cfg {log_id = LogId }} = State0 )
16671663 when Term > CurTerm ->
16681664 ? INFO (" ~ts : follower receiving snapshot saw append_entries_rpc from ~w for term ~b "
16691665 " abdicates term: ~b !" ,
16701666 [LogId , Msg # append_entries_rpc .leader_id ,
16711667 Term , CurTerm ]),
1672- SnapState0 = ra_log :snapshot_state (Log0 ),
1673- SnapState = ra_snapshot :abort_accept (SnapState0 ),
1674- Log = ra_log :set_snapshot_state (SnapState , Log0 ),
1675- {follower , maps :remove (snapshot_phase ,
1676- update_term (Term ,
1677- clear_leader_id (State #{log => Log }))),
1678- [{next_event , Msg }]};
1668+ State = abort_receive (State0 ),
1669+ {follower , update_term (Term , State ), [{next_event , Msg }]};
16791670handle_receive_snapshot ({ra_log_event , Evt },
16801671 #{cfg := # cfg {log_id = _LogId },
16811672 log := Log0 } = State ) ->
1682- % ?DEBUG("~ts: ~s ra_log_event received: ~w",
1683- % [LogId, ?FUNCTION_NAME, Evt]),
16841673 % simply forward all other events to ra_log
16851674 % whilst the snapshot is being received
16861675 {Log , Effects } = ra_log :handle_event (Evt , Log0 ),
16871676 {receive_snapshot , State #{log => Log }, Effects };
16881677handle_receive_snapshot (receive_snapshot_timeout ,
1689- #{cfg := # cfg {log_id = LogId },
1690- log := Log0 } = State ) ->
1678+ #{cfg := # cfg {log_id = LogId }} = State0 ) ->
16911679 ? INFO (" ~ts : ~s receive snapshot timed out." ,
16921680 [LogId , ? FUNCTION_NAME ]),
1693- SnapState0 = ra_log :snapshot_state (Log0 ),
1694- SnapState = ra_snapshot :abort_accept (SnapState0 ),
1695- Log = ra_log :set_snapshot_state (SnapState , Log0 ),
1696- {follower , maps :remove (snapshot_phase , State #{log => Log }), []};
1681+ State = abort_receive (State0 ),
1682+ {follower , State , []};
16971683handle_receive_snapshot (# info_rpc {term = Term } = Msg ,
16981684 #{current_term := CurTerm ,
1699- cfg := # cfg {log_id = LogId },
1700- log := Log0 } = State )
1685+ cfg := # cfg {log_id = LogId }} = State0 )
17011686 when CurTerm < Term ->
17021687 ? INFO (" ~ts : follower receiving snapshot saw info_rpc from ~w for term ~b "
1703- " abdicates term: ~b !" ,
1688+ " current term: ~b !" ,
17041689 [LogId , Msg # info_rpc .from ,
17051690 Term , CurTerm ]),
1706- SnapState0 = ra_log :snapshot_state (Log0 ),
1707- SnapState = ra_snapshot :abort_accept (SnapState0 ),
1708- Log = ra_log :set_snapshot_state (SnapState , Log0 ),
1709- {follower , maps :remove (snapshot_phase ,
1710- update_term (Term , clear_leader_id (State #{log => Log }))),
1711- [{next_event , Msg }]};
1691+ State = abort_receive (State0 ),
1692+ {follower , update_term (Term , State ), [{next_event , Msg }]};
17121693handle_receive_snapshot (# info_rpc {} = InfoRpc , State ) ->
17131694 InfoReplyEffect = empty_info_reply_effect (State , InfoRpc ),
17141695 {receive_snapshot , State , [InfoReplyEffect ]};
17151696handle_receive_snapshot (# info_reply {term = Term } = Msg ,
17161697 #{current_term := CurTerm ,
1717- cfg := # cfg {log_id = LogId },
1718- log := Log0 } = State )
1698+ cfg := # cfg {log_id = LogId }} = State0 )
17191699 when CurTerm < Term ->
17201700 ? INFO (" ~ts : follower receiving snapshot saw info_reply from ~w for term ~b "
17211701 " abdicates term: ~b !" ,
17221702 [LogId , Msg # info_reply .from ,
17231703 Term , CurTerm ]),
1724- SnapState0 = ra_log :snapshot_state (Log0 ),
1725- SnapState = ra_snapshot :abort_accept (SnapState0 ),
1726- Log = ra_log :set_snapshot_state (SnapState , Log0 ),
1727- {follower , maps :remove (snapshot_phase ,
1728- update_term (Term , clear_leader_id (State #{log => Log }))),
1729- [{next_event , Msg }]};
1704+ State = abort_receive (State0 ),
1705+ {follower , update_term (Term , State ), [{next_event , Msg }]};
17301706handle_receive_snapshot (# info_reply {}, State ) ->
17311707 {receive_snapshot , State , []};
17321708handle_receive_snapshot (Msg , State ) ->
@@ -1735,6 +1711,21 @@ handle_receive_snapshot(Msg, State) ->
17351711 % % TODO: work out what else to handle
17361712 {receive_snapshot , State , [{reply , {error , {unsupported_call , Msg }}}]}.
17371713
1714+ abort_receive (#{snapshot_phase := Phase ,
1715+ last_applied := LastApplied ,
1716+ log := Log0 } = State ) ->
1717+ SnapState0 = ra_log :snapshot_state (Log0 ),
1718+ SnapState = ra_snapshot :abort_accept (SnapState0 ),
1719+ Log1 = ra_log :set_snapshot_state (SnapState , Log0 ),
1720+ Log = case Phase of
1721+ pre ->
1722+ {ok , Log2 } = ra_log :set_last_index (LastApplied , Log1 ),
1723+ Log2 ;
1724+ _ ->
1725+ Log1
1726+ end ,
1727+ clear_leader_id (maps :remove (snapshot_phase , State #{log => Log })).
1728+
17381729-spec handle_await_condition (ra_msg (), ra_server_state ()) ->
17391730 {ra_state (), ra_server_state (), effects ()}.
17401731handle_await_condition (# request_vote_rpc {} = Msg , State ) ->
0 commit comments