Skip to content

Commit

Permalink
Handle case where mem table is deleted during segment writer flush
Browse files Browse the repository at this point in the history
  • Loading branch information
kjnilsson committed Nov 27, 2024
1 parent 16d2996 commit be458a7
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions src/ra_log_segment_writer.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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) ->
Expand Down

0 comments on commit be458a7

Please sign in to comment.