Skip to content

Commit

Permalink
supervise ns_config_events subscription in ns_bucket_sup. MB-4400
Browse files Browse the repository at this point in the history
So that restarts of ns_config_events for whatever reason does not
impact our ability to create/delete buckets.

Change-Id: I44a19053e72e35d9ec8d0cdc1db21901640fe134
Reviewed-on: http://review.couchbase.org/10636
Tested-by: Aliaksey Kandratsenka <alkondratenko@gmail.com>
Reviewed-by: Aliaksey Artamonau <aliaksiej.artamonau@gmail.com>
  • Loading branch information
Aliaksey Kandratsenka authored and alk committed Nov 5, 2011
1 parent 0b44f9f commit 61b51b5
Showing 1 changed file with 28 additions and 18 deletions.
46 changes: 28 additions & 18 deletions src/ns_bucket_sup.erl
Expand Up @@ -33,28 +33,38 @@ start_link() ->

%% supervisor callbacks

-define(SUBSCRIPTION_SPEC_NAME, buckets_observing_subscription).

init([]) ->
ns_pubsub:subscribe(
ns_config_events,
fun (Event, State) ->
case Event of
{buckets, L} ->
Buckets = ns_bucket:node_bucket_names(node(),
proplists:get_value(configs, L, [])),
work_queue:submit_work(ns_bucket_worker,
fun () ->
update_childs(Buckets)
end);
_ -> ok
end,
State
end, undefined),
SubscriptionChild = {?SUBSCRIPTION_SPEC_NAME,
{misc, start_event_link, [fun subscribe_on_config_events/0]},
permanent, 1000, worker, []},
{ok, {{one_for_one, 3, 10},
lists:flatmap(fun child_specs/1,
ns_bucket:node_bucket_names(node()))}}.
[SubscriptionChild]}}.

%% Internal functions

ns_config_event_handler_body({buckets, RawBuckets}, State) ->
Buckets = ns_bucket:node_bucket_names(node(),
proplists:get_value(configs, RawBuckets, [])),
?log_debug("~nRawBuckets: ~p", [RawBuckets]),
?log_debug("~nBuckets: ~p", [Buckets]),
work_queue:submit_work(ns_bucket_worker,
fun () ->
update_childs(Buckets)
end),
State;
ns_config_event_handler_body(_, _State) ->
ignored.


subscribe_on_config_events() ->
ns_pubsub:subscribe(
ns_config_events,
fun ns_config_event_handler_body/2, undefined),
undefined = ns_config_event_handler_body({buckets, [{configs, ns_bucket:get_buckets()}]}, undefined).


%% @private
%% @doc The child specs for each bucket.
child_specs(Bucket) ->
Expand All @@ -68,7 +78,7 @@ update_childs(Buckets) ->
OldSpecs = supervisor:which_children(?MODULE),
RunningIds = [element(1, X) || X <- OldSpecs],
ToStart = NewIds -- RunningIds,
ToStop = RunningIds -- NewIds,
ToStop = (RunningIds -- NewIds) -- [?SUBSCRIPTION_SPEC_NAME],
lists:foreach(fun (StartId) ->
Tuple = lists:keyfind(StartId, 1, NewSpecs),
true = is_tuple(Tuple),
Expand Down

0 comments on commit 61b51b5

Please sign in to comment.