diff options
| author | Arnaud Cogoluègnes <acogoluegnes@gmail.com> | 2016-12-12 17:22:09 +0100 |
|---|---|---|
| committer | Arnaud Cogoluègnes <acogoluegnes@gmail.com> | 2016-12-12 17:22:09 +0100 |
| commit | 9966bf207df0dd6999ff237f92de1c3731f6fc84 (patch) | |
| tree | 4b8afd4f88b902f3bdd2c4dcd8f43e1ac6096a0a /src | |
| parent | 4d3d1705e835e2bad9b82ff10bbaee6f8130a2a9 (diff) | |
| download | rabbitmq-server-git-9966bf207df0dd6999ff237f92de1c3731f6fc84.tar.gz | |
Improve global runtime parameters support
References rabbitmq/rabbitmq-mqtt#73
Diffstat (limited to 'src')
| -rw-r--r-- | src/rabbit_control_main.erl | 29 | ||||
| -rw-r--r-- | src/rabbit_runtime_parameters.erl | 62 |
2 files changed, 87 insertions, 4 deletions
diff --git a/src/rabbit_control_main.erl b/src/rabbit_control_main.erl index 8c245892b7..c4106aa88a 100644 --- a/src/rabbit_control_main.erl +++ b/src/rabbit_control_main.erl @@ -70,6 +70,10 @@ {clear_parameter, [?VHOST_DEF]}, {list_parameters, [?VHOST_DEF]}, + set_global_parameter, + clear_global_parameter, + list_global_parameters, + {set_policy, [?VHOST_DEF, ?PRIORITY_DEF, ?APPLY_TO_DEF]}, {clear_policy, [?VHOST_DEF]}, {list_policies, [?VHOST_DEF]}, @@ -527,6 +531,20 @@ action(clear_parameter, Node, [Component, Key], Opts, Inform) -> list_to_binary(Component), list_to_binary(Key)]); +action(set_global_parameter, Node, [Key, Value], _Opts, Inform) -> + Inform("Setting global runtime parameter ~p to ~p", [Key, Value]), + rpc_call( + Node, rabbit_runtime_parameters, set_global, + [evaluate_input_as_term(Key), rabbit_data_coercion:to_binary(Value)] + ); + +action(clear_global_parameter, Node, [Key], _Opts, Inform) -> + Inform("Clearing global runtime parameter ~p", [Key]), + rpc_call( + Node, rabbit_runtime_parameters, clear_global, + [evaluate_input_as_term(Key)] + ); + action(set_policy, Node, [Key, Pattern, Defn], Opts, Inform) -> Msg = "Setting policy ~p for pattern ~p to ~p with priority ~p", VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)), @@ -623,6 +641,11 @@ action(list_parameters, Node, [], Opts, Inform, Timeout) -> call(Node, {rabbit_runtime_parameters, list_formatted, [VHostArg]}, rabbit_runtime_parameters:info_keys(), Timeout); +action(list_global_parameters, Node, [], _Opts, Inform, Timeout) -> + Inform("Listing global runtime parameters", []), + call(Node, {rabbit_runtime_parameters, list_global_formatted, []}, + rabbit_runtime_parameters:global_info_keys(), Timeout); + action(list_policies, Node, [], Opts, Inform, Timeout) -> VHostArg = list_to_binary(proplists:get_value(?VHOST_OPT, Opts)), Inform("Listing policies", []), @@ -978,3 +1001,9 @@ alarms_by_node(Name) -> {_, As} = lists:keyfind(alarms, 1, Status), {Name, As} end. + +evaluate_input_as_term(Input) -> + {ok,Tokens,_EndLine} = erl_scan:string(Input ++ "."), + {ok,AbsForm} = erl_parse:parse_exprs(Tokens), + {value,TermValue,_Bs} = erl_eval:exprs(AbsForm, erl_eval:new_bindings()), + TermValue.
\ No newline at end of file diff --git a/src/rabbit_runtime_parameters.erl b/src/rabbit_runtime_parameters.erl index 97f78da8ba..8f5cc2c9c3 100644 --- a/src/rabbit_runtime_parameters.erl +++ b/src/rabbit_runtime_parameters.erl @@ -53,7 +53,9 @@ list_component/1, list/2, list_formatted/1, list_formatted/3, lookup/3, value/3, value/4, info_keys/0, clear_component/1]). --export([set_global/2, value_global/1, value_global/2]). +-export([set_global/2, value_global/1, value_global/2, + list_global/0, list_global_formatted/0, + global_info_keys/0, clear_global/1]). %%---------------------------------------------------------------------------- @@ -108,10 +110,17 @@ set(_, <<"policy">>, _, _, _) -> set(VHost, Component, Name, Term, User) -> set_any(VHost, Component, Name, Term, User). +set_global(Name, Term) when is_atom(Name) -> + %% for cluster_name + set_global0(Name, Term); + set_global(Name, Term) -> + set_global0({global, Name}, Term). + +set_global0(Name, Term) -> mnesia_update(Name, Term), event_notify(parameter_set, none, global, [{name, Name}, - {value, Term}]), + {value, Term}]), ok. format_error(L) -> @@ -167,6 +176,25 @@ clear(_, <<"policy">> , _) -> clear(VHost, Component, Name) -> clear_any(VHost, Component, Name). +clear_global(Key) -> + Notify = fun() -> + event_notify(parameter_set, none, global, [{name, Key}]), + ok + end, + case value_global(Key) of + not_found -> + {error_string, "Parameter does not exist"}; + _ -> + F = fun () -> + ok = mnesia:delete(?TABLE, {global, Key}, write) + end, + ok = rabbit_misc:execute_mnesia_transaction(F), + case mnesia:is_transaction() of + true -> Notify; + false -> Notify() + end + end. + clear_component(Component) -> case rabbit_runtime_parameters:list_component(Component) of [] -> @@ -234,9 +262,20 @@ list(VHost, Component) -> Comp =/= <<"policy">> orelse Component =:= <<"policy">>] end). +list_global() -> + mnesia:async_dirty( + fun () -> + Match = #runtime_parameters{key = {global, '_'}, + _ = '_'}, + [p(P) || P <- mnesia:match_object(?TABLE, Match, read)] + end). + list_formatted(VHost) -> [pset(value, format(pget(value, P)), P) || P <- list(VHost)]. +list_global_formatted() -> + [pset(value, format(pget(value, P)), P) || P <- list_global()]. + list_formatted(VHost, Ref, AggregatorPid) -> rabbit_control_misc:emitting_map( AggregatorPid, Ref, @@ -251,8 +290,17 @@ lookup(VHost, Component, Name) -> value(VHost, Comp, Name) -> value0({VHost, Comp, Name}). value(VHost, Comp, Name, Def) -> value0({VHost, Comp, Name}, Def). -value_global(Key) -> value0(Key). -value_global(Key, Default) -> value0(Key, Default). +value_global(Key) when is_atom(Key) -> + %% for cluster_name + value0(Key); +value_global(Key) -> + value0({global, Key}). + +value_global(Key, Default) when is_atom(Key) -> + %% for cluster_name + value0(Key, Default); +value_global(Key, Default) -> + value0({global, Key}, Default). value0(Key) -> case lookup0(Key, rabbit_misc:const(not_found)) of @@ -289,10 +337,16 @@ p(#runtime_parameters{key = {VHost, Component, Name}, value = Value}) -> [{vhost, VHost}, {component, Component}, {name, Name}, + {value, Value}]; + +p(#runtime_parameters{key = {global, Name}, value = Value}) -> + [{name, Name}, {value, Value}]. info_keys() -> [component, name, value]. +global_info_keys() -> [name, value]. + %%--------------------------------------------------------------------------- lookup_component(Component) -> |
