Skip to content

Commit

Permalink
Merge pull request #1 from Ubiquiti-Cloud/remove-stale-reverse-pid-ma…
Browse files Browse the repository at this point in the history
…pping

Remove stale reverse pid mapping
  • Loading branch information
AeroNotix authored Jul 7, 2016
2 parents 480023c + 73fb723 commit 8d49cf3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ deps
priv
*.o
*.beam
*.plt
*.plt
_build/**
29 changes: 18 additions & 11 deletions src/cpg.erl
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
remove_leave_callback/3]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
code_change/3, terminate/2]).

-include("cpg_constants.hrl").
Expand Down Expand Up @@ -3116,10 +3116,10 @@ delete_group(GroupName,
true = erlang:demonitor(Ref, [flush]),
cpg_callbacks:notify_leave(Callbacks, GroupName, Pid,
leave_local),
dict:update(Pid,
fun(OldValue) ->
lists:delete(GroupName, OldValue)
end, P)
clear_pid_mapping(Pid, dict:update(Pid,
fun(OldValue) ->
lists:delete(GroupName, OldValue)
end, P))
end, Pids, Local ++ Remote),
NewGroupsData = DictI:erase(GroupName, GroupsData),
State#state{groups = {DictI, NewGroupsData},
Expand Down Expand Up @@ -3175,7 +3175,7 @@ leave_group(GroupName, Pid, Reason,
pids = Pids,
callbacks = Callbacks} = State) ->
Fselect = fun(#cpg_data_pid{pid = P, monitor = Ref}) ->
if
if
P == Pid ->
true = erlang:demonitor(Ref, [flush]),
true;
Expand Down Expand Up @@ -3242,14 +3242,14 @@ leave_group(GroupName, Pid, Reason,
{NextGroupsData, Pids}
end,
State#state{groups = {DictI, NewGroupsData},
pids = NewPids}.
pids = clear_pid_mapping(Pid, NewPids)}.

leave_group_completely(GroupName, Pid, Reason,
#state{groups = {DictI, GroupsData},
pids = Pids,
callbacks = Callbacks} = State) ->
Fpartition = fun(#cpg_data_pid{pid = P, monitor = Ref}) ->
if
if
P == Pid ->
true = erlang:demonitor(Ref, [flush]),
true;
Expand Down Expand Up @@ -3304,7 +3304,7 @@ leave_group_completely(GroupName, Pid, Reason,
NextGroupsData
end,
State#state{groups = {DictI, NewGroupsData},
pids = NewPids}.
pids = clear_pid_mapping(Pid, NewPids)}.

store_conflict_add_entries(0, Entries, _) ->
Entries;
Expand Down Expand Up @@ -3349,7 +3349,7 @@ store_conflict_f([Pid | V1AllPids],
#state{callbacks = Callbacks} = State) ->
% for each external Pid, check the internal Pids within the same group
Fpartition = fun(#cpg_data_pid{pid = P}) ->
if
if
P == Pid ->
true;
true ->
Expand Down Expand Up @@ -3503,6 +3503,14 @@ member_died(Pid, Reason, #state{pids = Pids} = State) ->
NewState#state{pids = dict:erase(Pid, NewPids)}
end.

clear_pid_mapping(Pid, PidM) ->
case dict:fetch(Pid, PidM) of
[] ->
dict:erase(Pid, PidM);
Pids when is_list(Pids) ->
PidM
end.

whereis_name_random(1, [Pid]) ->
Pid;
whereis_name_random(N, L) ->
Expand Down Expand Up @@ -3542,4 +3550,3 @@ select([H | T], Output, F) ->

random(N) ->
quickrand:uniform(N).

20 changes: 15 additions & 5 deletions test/cpg_test.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
%%% @end
%%%
%%% BSD LICENSE
%%%
%%%
%%% Copyright (c) 2013-2014, Michael Truog <mjtruog at gmail dot com>
%%% All rights reserved.
%%%
%%%
%%% Redistribution and use in source and binary forms, with or without
%%% modification, are permitted provided that the following conditions are met:
%%%
%%%
%%% * Redistributions of source code must retain the above copyright
%%% notice, this list of conditions and the following disclaimer.
%%% * Redistributions in binary form must reproduce the above copyright
Expand All @@ -26,7 +26,7 @@
%%% * The name of the author may not be used to endorse or promote
%%% products derived from this software without specific prior
%%% written permission
%%%
%%%
%%% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
%%% CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
%%% INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Expand Down Expand Up @@ -227,6 +227,17 @@ pid_age_2_test() ->
ok = kill_pids([Pid1, Pid2, Pid3]),
ok.

join_leaves_remove_stale_reverse_mappings_test() ->
StartStateSize = byte_size(term_to_binary(sys:get_state(cpg_default_scope))),
Pid1 = erlang:spawn(fun busy_pid/0),
[begin
ok = cpg:join("GroupA", Pid1),
1 = cpg:join_count("GroupA", Pid1),
cpg:leave("GroupA", Pid1),
AfterLeaveSize = byte_size(term_to_binary(sys:get_state(cpg_default_scope))),
StartStateSize = AfterLeaveSize
end || _ <- lists:seq(1,100)].

callbacks_test() ->
F = fun(F1, L) ->
receive
Expand Down Expand Up @@ -354,4 +365,3 @@ index(Item, [Item | _], I) ->
I;
index(Item, [_ | T], I) ->
index(Item, T, I + 1).

0 comments on commit 8d49cf3

Please sign in to comment.