Skip to content

Commit

Permalink
Merge pull request #281 from rabbitmq/wal-remove-lists-reverse
Browse files Browse the repository at this point in the history
Remove lists:reverse/1 call in ra_log_wal
  • Loading branch information
michaelklishin authored May 18, 2022
2 parents 6fd55eb + ea7bc38 commit 0459728
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/ra_log_wal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ append_data(#state{conf = Cfg,
writers = Writers} = State,
{UId, Pid}, Idx, Term, Entry, DataSize, Data, Truncate) ->
Batch = incr_batch(Cfg, Batch0, UId, Pid,
{Idx, Term}, Data, Entry, Truncate),
{Idx, Term, Entry}, Data, Truncate),
State#state{file_size = FileSize + DataSize,
batch = Batch,
writers = Writers#{UId => {in_seq, Idx}} }.
Expand All @@ -463,14 +463,29 @@ incr_batch(#conf{open_mem_tbls_name = OpnMemTbl} = Cfg,
#batch{writes = Writes,
waiting = Waiting0,
pending = Pend} = Batch,
UId, Pid, {Idx, Term}, Data, Entry, Truncate) ->
UId, Pid, {Idx, Term, _} = Record, Data, Truncate) ->
Waiting = case Waiting0 of
#{Pid := #batch_writer{tbl_start = TblStart0,
tid = _Tid,
from = From,
inserts = Inserts0} = W} ->
TblStart = table_start(Truncate, Idx, TblStart0),
Inserts = [{Idx, Term, Entry} | Inserts0],
Inserts = case Inserts0 of
[] ->
[Record];
[{PrevIdx, _, _} | RemIdxs] when Idx =< PrevIdx ->
%% we are overwriting, this should rarely,
%% if ever happen within the timeframe of a batch
%% Drop all inserts that are higher or equal
%% to the current Idx
Filtered =
lists:dropwhile(fun ({I, _, _}) ->
Idx =< I
end, RemIdxs),
[Record | Filtered];
_ ->
[Record | Inserts0]
end,
Waiting0#{Pid => W#batch_writer{from = min(Idx, From),
tbl_start = TblStart,
to = Idx,
Expand All @@ -496,7 +511,7 @@ incr_batch(#conf{open_mem_tbls_name = OpnMemTbl} = Cfg,
tid = Tid,
uid = UId,
term = Term,
inserts = [{Idx, Term, Entry}]},
inserts = [Record]},
Waiting0#{Pid => Writer}
end,

Expand Down Expand Up @@ -720,10 +735,7 @@ complete_batch(#state{batch = #batch{waiting = Waiting,
term = Term,
inserts = Inserts,
tid = Tid}) ->
%% need to reverse inserts in case an index overwrite
%% came to be processed in the same batch.
%% Unlikely, but possible
true = ets:insert(Tid, lists:reverse(Inserts)),
true = ets:insert(Tid, Inserts),
true = ets:update_element(OpnTbl, UId,
[{2, TblStart}, {3, To}]),
Pid ! {ra_log_event, {written, {From, To, Term}}},
Expand Down

0 comments on commit 0459728

Please sign in to comment.