Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved support for 'r' objects #183

Merged
merged 3 commits into from
Sep 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
WIP More consistent coverage for 'r' objects
  • Loading branch information
uwiger committed Sep 18, 2020
commit 27a5e873161321912ee1bc257777aafe99c9c487
50 changes: 36 additions & 14 deletions src/gproc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@
lookup_global_properties/1,
lookup_local_counters/1,
lookup_global_counters/1,
lookup_local_resources/1,
lookup_global_resources/1,
lookup_local_aggr_counter/1,
lookup_global_aggr_counter/1]).

Expand Down Expand Up @@ -354,6 +356,25 @@ lookup_local_counters(P) -> lookup_values({c,l,P}).
%%
lookup_global_counters(P) -> lookup_values({c,g,P}).

%% @spec (Resource::any()) -> [{pid(), Value::integer()}]
%%
%% @doc Look up all local (non-unique) instances of a given Resource.
%% Returns a list of {Pid, Value} tuples for all matching objects.
%% @equiv lookup_values({c, l, Resource})
uwiger marked this conversation as resolved.
Show resolved Hide resolved
%% @end
%%
lookup_local_resources(P) -> lookup_values({r,l,P}).


%% @spec (Resource::any()) -> [{pid(), Value::integer()}]
%%
%% @doc Look up all global (non-unique) instances of a given Resource.
%% Returns a list of {Pid, Value} tuples for all matching objects.
%% @equiv lookup_values({c, g, Resource})
uwiger marked this conversation as resolved.
Show resolved Hide resolved
%% @end
%%
lookup_global_resources(P) -> lookup_values({r,g,P}).

%% @spec get_env(Scope::scope(), App::atom(), Key::atom()) -> term()
%% @equiv get_env(Scope, App, Key, [app_env])
get_env(Scope, App, Key) ->
Expand Down Expand Up @@ -933,10 +954,10 @@ monitor(Key, Type) when Type==info;
Type==standby ->
?CATCH_GPROC_ERROR(monitor1(Key, Type), [Key, Type]).

monitor1({T,g,_} = Key, Type) when T==n; T==a ->
monitor1({T,g,_} = Key, Type) when T==n; T==a; T==rc ->
?CHK_DIST,
gproc_dist:monitor(Key, Type);
monitor1({T,l,_} = Key, Type) when T==n; T==a ->
monitor1({T,l,_} = Key, Type) when T==n; T==a; T==rc ->
call({monitor, Key, self(), Type}, l);
monitor1(_, _) ->
?THROW_GPROC_ERROR(badarg).
Expand All @@ -950,10 +971,10 @@ monitor1(_, _) ->
demonitor(Key, Ref) ->
?CATCH_GPROC_ERROR(demonitor1(Key, Ref), [Key, Ref]).

demonitor1({T,g,_} = Key, Ref) when T==n; T==a ->
demonitor1({T,g,_} = Key, Ref) when T==n; T==a; T==rc ->
?CHK_DIST,
gproc_dist:demonitor(Key, Ref);
demonitor1({T,l,_} = Key, Ref) when T==n; T==a ->
demonitor1({T,l,_} = Key, Ref) when T==n; T==a; T==rc ->
call({demonitor, Key, Ref, self()}, l);
demonitor1(_, _) ->
?THROW_GPROC_ERROR(badarg).
Expand Down Expand Up @@ -1169,7 +1190,7 @@ reg_shared(Key) ->
?CATCH_GPROC_ERROR(reg_shared1(valid_key(Key)), [Key]).

%% @private
reg_shared1({T,_,_} = Key) when T==a; T==p; T==c ->
reg_shared1({T,_,_} = Key) when T==a; T==p; T==c; T==r ->
reg_shared(Key, default(Key)).

%% @spec reg_shared(Key::key(), Value) -> true
Expand Down Expand Up @@ -1247,7 +1268,7 @@ munreg(T, C, L) ->
munreg1(T, g, L) ->
?CHK_DIST,
gproc_dist:munreg(T, existing(T,g,L));
munreg1(T, l, L) when T==a; T==n ->
munreg1(T, l, L) when T==p; T==a; T==n; T==rc; T==r ->
if is_list(L) ->
call({munreg, T, l, existing(T,l,L)});
true ->
Expand All @@ -1259,9 +1280,9 @@ munreg1(_, _, _) ->
?THROW_GPROC_ERROR(badarg).

existing(T,Scope,L) ->
Keys = if T==p; T==c ->
Keys = if T==p; T==c; T==r ->
[{{T,Scope,K}, self()} || K <- L];
T==a; T==n ->
T==a; T==n; T==rc ->
[{{T,Scope,K}, T} || K <- L]
end,
_ = [case ets:member(?TAB, K) of
Expand Down Expand Up @@ -1829,17 +1850,18 @@ lookup_values({T,_,_} = Key) ->
update_counter(Key, Incr) ->
Pid = case Key of
{n,_,_} -> n;
{c,_,_} -> self()
{T,_,_} when T==c; T==r ->
self()
end,
?CATCH_GPROC_ERROR(update_counter1(Key, Pid, Incr), [Key, Incr]).

update_counter(Key, Pid, Incr) when is_pid(Pid);
Pid == shared; Pid == n ->
?CATCH_GPROC_ERROR(update_counter1(Key, Pid, Incr), [Key, Pid, Incr]).

update_counter1({T,l,_} = Key, Pid, Incr) when T==c; T==n ->
update_counter1({T,l,_} = Key, Pid, Incr) when T==c; T==r; T==n ->
gproc_lib:update_counter(Key, Incr, Pid);
update_counter1({T,g,_} = Key, Pid, Incr) when T==c; T==n ->
update_counter1({T,g,_} = Key, Pid, Incr) when T==c; T==r; T==n ->
?CHK_DIST,
gproc_dist:update_counter(Key, Pid, Incr);
update_counter1(_, _, _) ->
Expand Down Expand Up @@ -2302,7 +2324,7 @@ handle_call({reg_or_locate, {T,l,_} = Key, Val, P}, _, S) ->
{reply, {OtherPid, OtherValue}, S}
end;
handle_call({monitor, {T,l,_} = Key, Pid, Type}, _From, S)
when T==n; T==a ->
when T==n; T==a; T==rc ->
Ref = make_ref(),
Lookup = ets:lookup(?TAB, {Key, T}),
IsRegged = is_regged(Lookup),
Expand Down Expand Up @@ -2910,7 +2932,7 @@ scope(S) when S==l; S==g -> S.

type('_') -> '_';
type(all) -> '_';
type(T) when T==n; T==p; T==c; T==a -> T;
type(T) when T==n; T==p; T==c; T==a; T==r; T==rc -> T;
type(names) -> n;
type(props) -> p;
type(resources) -> r;
Expand Down Expand Up @@ -3092,7 +3114,7 @@ qlc_lookup_pid(Pid, Scope, Check) ->
[], ['$_']}]),
lists:flatmap(
fun({{_,{T,_,_}=K}, _}) ->
K2 = if T==n orelse T==a -> T;
K2 = if T==n orelse T==a orelse T==rc -> T;
true -> Pid
end,
case ets:lookup(?TAB, {K,K2}) of
Expand Down
48 changes: 33 additions & 15 deletions src/gproc_dist.erl
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ give_away({_,g,_} = Key, To) ->


update_counter({T,g,_} = Key, Pid, Incr) when is_integer(Incr), T==c;
is_integer(Incr), T==r;
is_integer(Incr), T==n ->
leader_call({update_counter, Key, Incr, Pid});
update_counter(_, _, _) ->
Expand Down Expand Up @@ -518,6 +519,7 @@ handle_leader_call({reg_or_locate, {n,g,_} = K, Value, P},
end;
handle_leader_call({update_counter, {T,g,_Ctr} = Key, Incr, Pid}, _From, S, _E)
when is_integer(Incr), T==c;
is_integer(Incr), T==r;
is_integer(Incr), T==n ->
try New = ets:update_counter(?TAB, {Key, Pid}, {3,Incr}),
RealPid = case Pid of
Expand Down Expand Up @@ -1012,13 +1014,14 @@ surrendered_1(Globs) ->
batch_update_counters(Cs) ->
batch_update_counters(Cs, [], []).

batch_update_counters([{{c,g,_} = Key, Pid, Incr}|T], Returns, Updates) ->
batch_update_counters([{{T,g,_} = Key, Pid, Incr}|Cs], Returns, Updates)
when T==c; T==n; T==r ->
case update_counter_g(Key, Incr, Pid) of
[{_,_,_} = A, {_, _, V} = C] ->
batch_update_counters(T, [{Key,Pid,V}|Returns], add_object(
batch_update_counters(Cs, [{Key,Pid,V}|Returns], add_object(
A, add_object(C, Updates)));
[{_, _, V} = C] ->
batch_update_counters(T, [{Key,Pid,V}|Returns], add_object(C, Updates))
batch_update_counters(Cs, [{Key,Pid,V}|Returns], add_object(C, Updates))
end;
batch_update_counters([], Returns, Updates) ->
{lists:reverse(Returns), Updates}.
Expand All @@ -1033,22 +1036,37 @@ add_object(Obj, []) ->



update_counter_g({c,g,_} = Key, Incr, Pid) when is_integer(Incr) ->
Res = ets:update_counter(?TAB, {Key, Pid}, {3,Incr}),
update_aggr_counter(Key, Incr, [{{Key,Pid},Pid,Res}]);
update_counter_g({c,g,_} = Key, {Incr, Threshold, SetValue}, Pid)
when is_integer(Incr), is_integer(Threshold), is_integer(SetValue) ->
[Prev, New] = ets:update_counter(?TAB, {Key, Pid},
update_counter_g({T,g,_} = Key, Incr, Pid) when is_integer(Incr), T==c;
is_integer(Incr), T==r;
is_integer(Incr), T==n ->
R = if T==n -> T;
true -> Pid
end,
Res = ets:update_counter(?TAB, {Key, R}, {3,Incr}),
update_aggr_counter(Key, Incr, [{{Key,R},Pid,Res}]);
update_counter_g({T,g,_} = Key, {Incr, Threshold, SetValue}, Pid)
when is_integer(Incr), is_integer(Threshold), is_integer(SetValue), T==c;
is_integer(Incr), is_integer(Threshold), is_integer(SetValue), T==r;
is_integer(Incr), is_integer(Threshold), is_integer(SetValue), T==n ->
R = if T==n -> T;
true -> Pid
end,
[Prev, New] = ets:update_counter(?TAB, {Key, R},
[{3, 0}, {3, Incr, Threshold, SetValue}]),
update_aggr_counter(Key, New - Prev, [{{Key,Pid},Pid,New}]);
update_counter_g({c,g,_} = Key, Ops, Pid) when is_list(Ops) ->
case ets:update_counter(?TAB, {Key, Pid},
update_aggr_counter(Key, New - Prev, [{{Key,R},Pid,New}]);
update_counter_g({T,g,_} = Key, Ops, Pid) when is_list(Ops), T==c;
is_list(Ops), T==r;
is_list(Ops), T==n ->
R = if T==n -> T;
true -> Pid
end,
case ets:update_counter(?TAB, {Key, R},
[{3, 0} | expand_ops(Ops)]) of
[_] ->
[];
[Prev | Rest] ->
[New | _] = lists:reverse(Rest),
update_aggr_counter(Key, New - Prev, [{Key, Pid, Rest}])
update_aggr_counter(Key, New - Prev, [{{Key,R}, Pid, Rest}])
end;
update_counter_g(_, _, _) ->
?THROW_GPROC_ERROR(badarg).
Expand All @@ -1064,11 +1082,11 @@ expand_ops([]) ->
expand_ops(_) ->
?THROW_GPROC_ERROR(badarg).

update_aggr_counter({n,_,_}, _) ->
[];
update_aggr_counter(Key, Incr) ->
update_aggr_counter(Key, Incr, []).

update_aggr_counter({T,g,_}, _Incr, Acc) when T==n; T==r ->
Acc;
update_aggr_counter({c,g,Ctr}, Incr, Acc) ->
Key = {{a,g,Ctr},a},
case ets:lookup(?TAB, Key) of
Expand Down
4 changes: 3 additions & 1 deletion src/gproc_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,8 @@ do_set_counter_value({_,C,N} = Key, Value, Pid) ->
Res.

update_counter({T,l,Ctr} = Key, Incr, Pid) when is_integer(Incr), T==c;
is_integer(Incr), T==n ->
is_integer(Incr), T==r;
is_integer(Incr), T==n ->
Res = ets:update_counter(?TAB, {Key, Pid}, {3,Incr}),
if T==c ->
update_aggr_counter(l, Ctr, Incr);
Expand All @@ -539,6 +540,7 @@ update_counter({T,l,Ctr} = Key, Incr, Pid) when is_integer(Incr), T==c;
Res;
update_counter({T,l,Ctr} = Key, {Incr, Threshold, SetValue}, Pid)
when is_integer(Incr), is_integer(Threshold), is_integer(SetValue), T==c;
is_integer(Incr), is_integer(Threshold), is_integer(SetValue), T==r;
is_integer(Incr), is_integer(Threshold), is_integer(SetValue), T==n ->
[Prev, New] = ets:update_counter(?TAB, {Key, Pid},
[{3, 0}, {3, Incr, Threshold, SetValue}]),
Expand Down
69 changes: 56 additions & 13 deletions test/gproc_dist_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ basic_tests(Ns) ->
?f(t_simple_ensure_other(Ns)),
?f(t_simple_reg_or_locate(Ns)),
?f(t_simple_counter(Ns)),
?f(t_simple_r_counter(Ns)),
?f(t_simple_n_counter(Ns)),
?f(t_aggr_counter(Ns)),
?f(t_awaited_aggr_counter(Ns)),
?f(t_simple_resource_count(Ns)),
Expand All @@ -78,6 +80,8 @@ basic_tests(Ns) ->
?f(t_awaited_resource_count(Ns)),
?f(t_resource_count_on_zero(Ns)),
?f(t_update_counters(Ns)),
?f(t_update_r_counters(Ns)),
?f(t_update_n_counters(Ns)),
?f(t_shared_counter(Ns)),
?f(t_prop(Ns)),
?f(t_mreg(Ns)),
Expand Down Expand Up @@ -216,6 +220,22 @@ t_simple_counter([H|_] = Ns) ->
?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 5)),
?assertMatch(ok, t_call(P, die)).

t_simple_r_counter([H|_] = Ns) ->
Ctr = ?T_RESOURCE,
P = t_spawn_reg(H, Ctr, 3),
?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 3)),
?assertMatch(5, t_call(P, {apply, gproc, update_counter, [Ctr, 2]})),
?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 5)),
?assertMatch(ok, t_call(P, die)).

t_simple_n_counter([H|_] = Ns) ->
Ctr = ?T_NAME,
P = t_spawn_reg(H, Ctr, 3),
?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 3)),
?assertMatch(5, t_call(P, {apply, gproc, update_counter, [Ctr, 2]})),
?assertMatch(ok, t_read_everywhere(Ctr, P, Ns, 5)),
?assertMatch(ok, t_call(P, die)).

t_shared_counter([H|_] = Ns) ->
Ctr = ?T_COUNTER,
P = t_spawn_reg_shared(H, Ctr, 3),
Expand Down Expand Up @@ -319,7 +339,6 @@ t_wild_key_in_resource([H1|_]) ->
?assertError({'DOWN', _, {badarg, _}},
t_call(P2, {apply, gproc, mreg, [r, g, [{Rw, 1}]]})).


t_awaited_resource_count([H1,H2|_] = Ns) ->
{r,g,Nm} = R = ?T_RESOURCE,
RC = {rc,g,Nm},
Expand Down Expand Up @@ -368,29 +387,53 @@ t_resource_count_on_zero([H1,H2|_] = Ns) ->
?assertMatch(ok, t_call(Pp, die)),
?assertMatch(ok, t_call(Prc, die)).

t_update_counters([H1,H2|_] = Ns) ->
{c,g,N1} = C1 = ?T_COUNTER,
A1 = {a,g,N1},
t_update_counters(Ns) ->
C1 = ?T_COUNTER,
C2 = ?T_COUNTER,
t_update_counters(C1, C1, C2, Ns).

t_update_r_counters(Ns) ->
C1 = ?T_RESOURCE,
C2 = ?T_RESOURCE,
t_update_counters(C1, C1, C2, Ns).

t_update_n_counters(Ns) ->
C1 = ?T_NAME,
C2 = ?T_NAME,
C3 = ?T_NAME,
t_update_counters(C1, C2, C3, Ns).

t_update_counters(C1, C12, C2, [H1,H2|_] = Ns) ->
{T,g,N1} = C1,
A1 = {a,g,N1},
P1 = t_spawn_reg(H1, C1, 2),
P12 = t_spawn_reg(H2, C1, 2),
P12 = t_spawn_reg(H2, C12, 2),
P2 = t_spawn_reg(H2, C2, 1),
Pa1 = t_spawn_reg(H2, A1),
Pa1 = if T==c -> t_spawn_reg(H2, A1);
true -> undefined
end,
?assertMatch(ok, t_read_everywhere(C1, P1, Ns, 2)),
?assertMatch(ok, t_read_everywhere(C1, P12, Ns, 2)),
?assertMatch(ok, t_read_everywhere(C12, P12, Ns, 2)),
?assertMatch(ok, t_read_everywhere(C2, P2, Ns, 1)),
?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 4)),
if T==c -> ?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 4));
true -> ok
end,
?assertMatch([{C1,P1, 3},
{C1,P12,4},
{C12,P12,4},
{C2,P2, 0}], t_call(P1, {apply, gproc, update_counters,
[g, [{C1,P1,1},{C1,P12,2},{C2,P2,{-2,0,0}}]]})),
[g, [{C1,P1,1},{C12,P12,2},{C2,P2,{-2,0,0}}]]})),
?assertMatch(ok, t_read_everywhere(C1, P1, Ns, 3)),
?assertMatch(ok, t_read_everywhere(C1, P12, Ns, 4)),
?assertMatch(ok, t_read_everywhere(C12, P12, Ns, 4)),
?assertMatch(ok, t_read_everywhere(C2, P2, Ns, 0)),
?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 7)),
if T==c -> ?assertMatch(ok, t_read_everywhere(A1, Pa1, Ns, 7));
true -> ok
end,
?assertMatch(ok, t_call(P1, die)),
?assertMatch(ok, t_call(P12, die)),
?assertMatch(ok, t_call(P2, die)).
?assertMatch(ok, t_call(P2, die)),
if T==c -> ?assertMatch(ok, t_call(Pa1, die));
true -> ok
end.

t_prop([H1,H2|_] = Ns) ->
{p, g, _} = P = ?T_PROP,
Expand Down
15 changes: 15 additions & 0 deletions test/gproc_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ reg_test_() ->
, ?_test(t_is_clean())
, {spawn, ?_test(?debugVal(t_update_counters()))}
, ?_test(t_is_clean())
, {spawn, ?_test(?debugVal(t_update_r_counter()))}
, ?_test(t_is_clean())
, {spawn, ?_test(?debugVal(t_update_n_counter()))}
, ?_test(t_is_clean())
, {spawn, ?_test(?debugVal(t_simple_prop()))}
, ?_test(t_is_clean())
, {spawn, ?_test(?debugVal(t_await()))}
Expand Down Expand Up @@ -446,6 +450,17 @@ t_update_counters() ->
end,
?assert(gproc:get_value({a,l,c1}) =:= 7).

t_update_r_counter() ->
K = {r,l,r1},
?assert(gproc:reg(K, 3) =:= true),
?assertEqual(5, gproc:update_counter(K, 2)),
?assert(gproc:get_value(K) =:= 5).

t_update_n_counter() ->
K = {n,l,n1},
?assert(gproc:reg(K, 3) =:= true),
?assertEqual(5, gproc:update_counter(K, 2)),
?assert(gproc:get_value(K) =:= 5).

t_simple_prop() ->
?assert(gproc:reg({p,l,prop}) =:= true),
Expand Down