From be458a7987b695a2cc7e9496bef1728a78aeadd4 Mon Sep 17 00:00:00 2001 From: Karl Nilsson Date: Wed, 27 Nov 2024 13:56:55 +0000 Subject: [PATCH] Handle case where mem table is deleted during segment writer flush --- src/ra_log_segment_writer.erl | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/ra_log_segment_writer.erl b/src/ra_log_segment_writer.erl index 1e618bc3..87be5737 100644 --- a/src/ra_log_segment_writer.erl +++ b/src/ra_log_segment_writer.erl @@ -360,16 +360,6 @@ send_segments(System, ServerUId, TidRanges, SegRefs) -> ok end. -clean_closed_mem_tables(System, UId, Tid) -> - {ok, ClosedTbl} = ra_system:lookup_name(System, closed_mem_tbls), - Tables = ets:lookup(ClosedTbl, UId), - [begin - ?DEBUG("~w: cleaning closed table for '~ts' range: ~b-~b", - [?MODULE, UId, From, To]), - %% delete the entry in the closed table lookup - true = ets:delete_object(ClosedTbl, O) - end || {_, _, From, To, T} = O <- Tables, T == Tid]. - append_to_segment(UId, Tid, StartIdx0, EndIdx, Seg, State) -> StartIdx = start_index(UId, StartIdx0), % EndIdx + 1 because FP @@ -379,7 +369,7 @@ append_to_segment(_, _, StartIdx, EndIdx, Seg, Closed, _State) when StartIdx >= EndIdx -> {Seg, Closed}; append_to_segment(UId, Tid, Idx, EndIdx, Seg0, Closed, State) -> - case ets:lookup(Tid, Idx) of + try ets:lookup(Tid, Idx) of [] -> StartIdx = start_index(UId, Idx), case Idx < StartIdx of @@ -422,8 +412,6 @@ append_to_segment(UId, Tid, Idx, EndIdx, Seg0, Closed, State) -> undefined -> %% a successor cannot be opened - this is most likely due %% to the directory having been deleted. - %% clear close mem tables here - _ = clean_closed_mem_tables(State#state.system, UId, Tid), undefined; Seg -> ok = counters:add(State#state.counter, ?C_SEGMENTS, 1), @@ -437,6 +425,16 @@ append_to_segment(UId, Tid, Idx, EndIdx, Seg0, Closed, State) -> FileName = ra_log_segment:filename(Seg0), exit({segment_writer_append_error, FileName, Posix}) end + catch _:badarg -> + ?ERROR("segment_writer: uid ~s ets table deleted", [UId]), + %% ets table has been deleted. + %% this could be due to two reasons + %% 1. the ra server has been deleted. + %% 2. an old mem table has been deleted due to snapshotting + %% but the member is still active + %% skipping this table + undefined + end. find_segment_files(Dir) ->