Skip to content

Commit

Permalink
Merge pull request #1 from solvip/master
Browse files Browse the repository at this point in the history
Work around a race condition when deleting slide counters
  • Loading branch information
joewilliams committed Oct 30, 2015
2 parents 8914823 + 1daa48c commit 86e3e85
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/folsom_sample_slide_server.erl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,14 @@ handle_cast(_Msg, State) ->
{noreply, State}.

handle_info(timeout, State=#state{sample_mod = SampleMod, reservoir = Reservoir, window = Window}) ->
SampleMod:trim(Reservoir, Window),
{noreply, State, timeout(Window)};
try
SampleMod:trim(Reservoir, Window),
{noreply, State, timeout(Window)}
catch error:badarg ->
%% The ets table must have gone away; stop normally instead of bringing
%% the supervisor down with us.
{stop, normal, State}
end;
handle_info(_Info, State) ->
{noreply, State}.

Expand Down
26 changes: 25 additions & 1 deletion test/folsom_sample_slide_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,22 @@ exercise() ->
Trimmed2 = folsom_sample_slide:trim(Slide#slide.reservoir, ?WINDOW),
?assertEqual((?RUNTIME * ?READINGS) - ((?RUNTIME - ?WINDOW - 1) * ?READINGS), Trimmed2),
check_table(Slide, []),
ok.

%% trim shuld throw badarg in the case of nonexistant ETS tables
NonExistant = ets:new(non_existant, []),
ets:delete(NonExistant),
?assertError(badarg, folsom_sample_slide:trim(NonExistant, ?WINDOW)),
?assertError(badarg, folsom_sample_slide_uniform:trim(NonExistant, ?WINDOW)),

%% The slide server exits with reason normal in case the ets table it's trimming goes away
process_flag(trap_exit, true),
{ok, SlideServer} = folsom_sample_slide_server:start_link(folsom_sample_slide, NonExistant, 1),
ExitMsg = wait_for_exit(SlideServer, timer:seconds(2)),
?assertMatch({'EXIT', SlideServer, normal}, ExitMsg),
process_flag(trap_exit, false),
ok.


expand_window() ->
%% create a new histogram
%% will leave the trim server running, as resize() needs it
Expand Down Expand Up @@ -207,3 +221,13 @@ check_table(Slide, Moments) ->
StrippedKeys = lists:usort([X || {X, _} <- Ks]),
?assertEqual(Moments, StrippedKeys),
?assertEqual(ExpectedVs, lists:sort(Vs)).

wait_for_exit(Pid, Timeout) ->
receive
M = {'EXIT', Pid, _Reason} ->
M;
Other ->
Other
after Timeout ->
timeout
end.

0 comments on commit 86e3e85

Please sign in to comment.