Skip to content

Commit

Permalink
fix globals scope (2600hz#2861)
Browse files Browse the repository at this point in the history
  • Loading branch information
lazedo authored and fenollp committed Dec 4, 2016
1 parent 4521e53 commit 05ffbf4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 5 deletions.
1 change: 1 addition & 0 deletions core/kazoo/include/kz_types.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@
,version :: api_binary() | '_'
,channels = 0 :: non_neg_integer() | '_'
,registrations = 0 :: non_neg_integer() | '_'
,globals = [] :: kz_proplist() | '$1' | '_'
}).

-type kz_node() :: #kz_node{}.
Expand Down
1 change: 1 addition & 0 deletions core/kazoo_amqp/src/api/kapi_nodes.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
,<<"WhApps">>, <<"Used-Memory">>, <<"Processes">>
,<<"Ports">>, <<"Version">>, <<"Channels">>
,<<"Registrations">>, <<"Zone">>
,<<"Globals">>
]).
-define(ADVERTISE_VALUES, [{<<"Event-Category">>, <<"nodes">>}
,{<<"Event-Name">>, <<"advertise">>}
Expand Down
6 changes: 3 additions & 3 deletions core/kazoo_globals/src/kazoo_globals_sup.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
]]
).

-define(CHILDREN, [?WORKER('kz_nodes')
,?WORKER('kz_globals')
,?SUPER('kz_global_proxies_sup')
-define(CHILDREN, [?WORKER('kz_globals')
,?WORKER_ARGS('kazoo_etsmgr_srv', ?ETSMGR_ARGS)
,?WORKER('kazoo_globals_init')
,?WORKER('kz_nodes')
,?SUPER('kz_global_proxies_sup')
]).

%% ===================================================================
Expand Down
9 changes: 9 additions & 0 deletions core/kazoo_globals/src/kz_global.erl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
,all_globals_by_pid/2
,all_globals_by_node/2
,all_dead_pids/1
,stats/1

,from_jobj/2
,update_with_pid_ref/2, update_with_pid_ref/3
Expand Down Expand Up @@ -105,6 +106,14 @@ all_names(Table) ->
MatchSpec = [{#kz_global{name = '$1', _ = '_'} ,[],['$1']}],
ets:select(Table, MatchSpec).

-spec stats(ets:tab()) -> kz_proplist().
stats(Table) ->
MatchSpec = [{#kz_global{state = '$1', _ = '_'} ,[],['$1']}],
lists:foldl(fun(State, Props) ->
V = props:get_integer_value(State, Props, 0) + 1,
props:set_value(State, V, Props)
end, [], ets:select(Table, MatchSpec)).

-spec all_globals_by_pid(ets:tab(), pid()) -> globals().
all_globals_by_pid(Table, Pid) ->
MatchSpec = [{#kz_global{pid = Pid, _ = '_'} ,[],['$_']}],
Expand Down
13 changes: 11 additions & 2 deletions core/kazoo_globals/src/kz_globals.erl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
,registered/0
,reconcile/0
,is_ready/0
,stats/0
]).

-export([start_link/0
Expand Down Expand Up @@ -166,6 +167,14 @@ register_name(Name, Pid) ->
registered() ->
kz_global:all_names(?TAB_NAME).

-spec stats() -> kz_proplist().
stats() ->
case kz_global:stats(?TAB_NAME) of
[] -> [{'total', 0}];
Stats -> Stats
end.


-spec unregister_name(kz_global:name()) -> 'ok'.
unregister_name(Name) ->
case where(Name) of
Expand Down Expand Up @@ -881,7 +890,7 @@ amqp_call_timeout(_Seconds) ->

-spec amqp_call_scope() -> fun() | 'undefined'.
amqp_call_scope() ->
Count = kz_nodes:whapp_count('kazoo_globals', 'true'),
Count = kz_nodes:globals_scope(),
{StartTime, _} = statistics('wall_clock'),
amqp_call_scope(Count, StartTime).

Expand All @@ -891,7 +900,7 @@ amqp_call_scope(_N, Seconds)
lager:debug("system running for less than 2 minutes, attempting to collect 10 responses from kazoo_globals"),
amqp_call_scope_fun(10);
amqp_call_scope(N, _Seconds) ->
amqp_call_scope_fun(N - 1).
amqp_call_scope_fun(N).

-spec amqp_call_scope_fun(integer()) -> fun() | 'undefined'.
amqp_call_scope_fun(0) -> 'undefined';
Expand Down
28 changes: 28 additions & 0 deletions core/kazoo_globals/src/kz_nodes.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
]).
-export([local_zone/0]).
-export([whapp_zones/1, whapp_zone_count/1]).
-export([globals_scope/0]).

-export([init/1
,handle_call/3
Expand Down Expand Up @@ -116,6 +117,20 @@ is_up(Node) ->
[_] -> 'true'
end.

-spec globals_scope() -> integer().
globals_scope() ->
MatchSpec = [{#kz_node{globals='$1'
,zone='$2'
,_ = '_'
}
,[{'andalso'
,{'=/=', '$1', []}
,{'=/=', '$2', {'const', local_zone()}}
}]
,['$1']
}],
length(ets:select(?MODULE, MatchSpec)).

-spec whapp_count(text()) -> integer().
whapp_count(Whapp) ->
whapp_count(Whapp, 'false').
Expand Down Expand Up @@ -237,6 +252,7 @@ print_node_status(#kz_node{zone=NodeZone
,used_memory=UsedMemory
,broker=Broker
,kapps=Whapps
,globals=Globals
}=Node
,Zone
) ->
Expand All @@ -253,6 +269,7 @@ print_node_status(#kz_node{zone=NodeZone

io:format("Broker : ~s~n", [Broker]),

_ = maybe_print_globals(Globals),
_ = maybe_print_kapps(Whapps),
_ = maybe_print_media_servers(Node),

Expand All @@ -264,6 +281,13 @@ maybe_print_zone(Zone, Zone) when Zone =/= <<"local">> ->
maybe_print_zone(NodeZone, _Zone) ->
io:format("Zone : ~s~n", [NodeZone]).

-spec maybe_print_globals(kz_proplist()) -> 'ok'.
maybe_print_globals([]) -> 'ok';
maybe_print_globals(Globals) ->
io:format("Globals :"),
lists:foreach(fun({K,V}) -> io:format(" ~s (~B)",[K, V]) end, Globals),
io:format("~n").

-spec maybe_print_kapps(kz_proplist()) -> 'ok'.
maybe_print_kapps(Whapps) ->
case lists:sort(fun compare_apps/2, Whapps) of
Expand Down Expand Up @@ -579,6 +603,7 @@ create_node(Heartbeat, #state{zone=Zone
,ports=length(erlang:ports())
,version=Version
,zone=Zone
,globals=kz_globals:stats()
}).

-spec normalize_amqp_uri(ne_binary()) -> ne_binary().
Expand Down Expand Up @@ -658,6 +683,7 @@ advertise_payload(#kz_node{expires=Expires
,channels=Channels
,registrations=Registrations
,zone=Zone
,globals=Globals
}) ->
props:filter_undefined(
[{<<"Expires">>, kz_util:to_binary(Expires)}
Expand All @@ -670,6 +696,7 @@ advertise_payload(#kz_node{expires=Expires
,{<<"Channels">>, Channels}
,{<<"Registrations">>, Registrations}
,{<<"Zone">>, kz_util:to_binary(Zone)}
,{<<"Globals">>, kz_json:from_list(Globals)}
| kz_api:default_headers(?APP_NAME, ?APP_VERSION)
]).

Expand Down Expand Up @@ -699,6 +726,7 @@ from_json(JObj, State) ->
,registrations=kz_json:get_integer_value(<<"Registrations">>, JObj, 0)
,broker=get_amqp_broker(JObj)
,zone=get_zone(JObj, State)
,globals=kz_json:to_proplist(kz_json:get_value(<<"Globals">>, JObj, kz_json:new()))
}.

-spec kapps_from_json(api_terms()) -> kapps_info().
Expand Down

0 comments on commit 05ffbf4

Please sign in to comment.