emqx_modules_conf.erl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2021-2024 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. %% @doc The emqx-modules configuration interoperable interfaces
  17. -module(emqx_modules_conf).
  18. -behaviour(emqx_config_handler).
  19. -behaviour(emqx_config_backup).
  20. %% Load/Unload
  21. -export([
  22. load/0,
  23. unload/0
  24. ]).
  25. -export([
  26. topic_metrics/0,
  27. add_topic_metrics/1,
  28. remove_topic_metrics/1
  29. ]).
  30. %% config handlers
  31. -export([
  32. pre_config_update/3,
  33. post_config_update/5
  34. ]).
  35. %% Data backup
  36. -export([
  37. import_config/1
  38. ]).
  39. %%--------------------------------------------------------------------
  40. %% Load/Unload
  41. %%--------------------------------------------------------------------
  42. -spec load() -> ok.
  43. load() ->
  44. emqx_conf:add_handler([topic_metrics], ?MODULE).
  45. -spec unload() -> ok.
  46. unload() ->
  47. emqx_conf:remove_handler([topic_metrics]).
  48. %%--------------------------------------------------------------------
  49. %% Topic-Metrics
  50. %%--------------------------------------------------------------------
  51. -spec topic_metrics() -> [emqx_types:topic()].
  52. topic_metrics() ->
  53. lists:map(
  54. fun(#{topic := Topic}) -> Topic end,
  55. emqx:get_config([topic_metrics])
  56. ).
  57. -spec add_topic_metrics(emqx_types:topic()) ->
  58. {ok, emqx_types:topic()}
  59. | {error, term()}.
  60. add_topic_metrics(Topic) ->
  61. case cfg_update([topic_metrics], ?FUNCTION_NAME, Topic) of
  62. {ok, _} -> {ok, Topic};
  63. {error, Reason} -> {error, Reason}
  64. end.
  65. -spec remove_topic_metrics(emqx_types:topic()) ->
  66. ok
  67. | {error, term()}.
  68. remove_topic_metrics(Topic) ->
  69. case cfg_update([topic_metrics], ?FUNCTION_NAME, Topic) of
  70. {ok, _} -> ok;
  71. {error, Reason} -> {error, Reason}
  72. end.
  73. %%--------------------------------------------------------------------
  74. %% Data backup (Topic-Metrics)
  75. %%--------------------------------------------------------------------
  76. import_config(#{<<"topic_metrics">> := Topics}) ->
  77. case emqx_conf:update([topic_metrics], {merge_topics, Topics}, #{override_to => cluster}) of
  78. {ok, _} ->
  79. {ok, #{root_key => topic_metrics, changed => []}};
  80. Error ->
  81. {error, #{root_key => topic_metrics, reason => Error}}
  82. end;
  83. import_config(_RawConf) ->
  84. {ok, #{root_key => topic_metrics, changed => []}}.
  85. %%--------------------------------------------------------------------
  86. %% Config Handler
  87. %%--------------------------------------------------------------------
  88. -spec pre_config_update(
  89. list(atom()),
  90. emqx_config:update_request(),
  91. emqx_config:raw_config()
  92. ) ->
  93. {ok, emqx_config:update_request()} | {error, term()}.
  94. pre_config_update(_, {add_topic_metrics, Topic0}, RawConf) ->
  95. Topic = #{<<"topic">> => Topic0},
  96. case lists:member(Topic, RawConf) of
  97. true ->
  98. {error, already_existed};
  99. _ ->
  100. {ok, RawConf ++ [Topic]}
  101. end;
  102. pre_config_update(_, {remove_topic_metrics, Topic0}, RawConf) ->
  103. Topic = #{<<"topic">> => Topic0},
  104. case lists:member(Topic, RawConf) of
  105. true ->
  106. {ok, RawConf -- [Topic]};
  107. _ ->
  108. {error, not_found}
  109. end;
  110. pre_config_update(_, {merge_topics, NewConf}, OldConf) ->
  111. KeyFun = fun(#{<<"topic">> := T}) -> T end,
  112. MergedConf = emqx_utils:merge_lists(OldConf, NewConf, KeyFun),
  113. {ok, MergedConf};
  114. pre_config_update(_, NewConf, _OldConf) ->
  115. {ok, NewConf}.
  116. -spec post_config_update(
  117. list(atom()),
  118. emqx_config:update_request(),
  119. emqx_config:config(),
  120. emqx_config:config(),
  121. emqx_config:app_envs()
  122. ) ->
  123. ok | {ok, Result :: any()} | {error, Reason :: term()}.
  124. post_config_update(
  125. _,
  126. {add_topic_metrics, Topic},
  127. _NewConfig,
  128. _OldConfig,
  129. _AppEnvs
  130. ) ->
  131. case emqx_topic_metrics:register(Topic) of
  132. ok -> ok;
  133. {error, Reason} -> {error, Reason}
  134. end;
  135. post_config_update(
  136. _,
  137. {remove_topic_metrics, Topic},
  138. _NewConfig,
  139. _OldConfig,
  140. _AppEnvs
  141. ) ->
  142. case emqx_topic_metrics:deregister(Topic) of
  143. ok -> ok;
  144. {error, Reason} -> {error, Reason}
  145. end;
  146. post_config_update(_, _UpdateReq, NewConfig, OldConfig, _AppEnvs) ->
  147. #{
  148. removed := Removed,
  149. added := Added
  150. } = emqx_utils:diff_lists(NewConfig, OldConfig, fun(#{topic := T}) -> T end),
  151. Deregistered = [emqx_topic_metrics:deregister(T) || #{topic := T} <- Removed],
  152. Registered = [emqx_topic_metrics:register(T) || #{topic := T} <- Added],
  153. DeregisteredErrs = [Res || Res <- Deregistered, Res =/= ok, Res =/= {error, topic_not_found}],
  154. RegisteredErrs = [Res || Res <- Registered, Res =/= ok, Res =/= {error, already_existed}],
  155. case DeregisteredErrs ++ RegisteredErrs of
  156. [] -> ok;
  157. Errs -> {error, Errs}
  158. end.
  159. %%--------------------------------------------------------------------
  160. %% Private
  161. %%--------------------------------------------------------------------
  162. res({ok, Result}) -> {ok, Result};
  163. res({error, {pre_config_update, ?MODULE, Reason}}) -> {error, Reason};
  164. res({error, {post_config_update, ?MODULE, Reason}}) -> {error, Reason};
  165. res({error, Reason}) -> {error, Reason}.
  166. cfg_update(Path, Action, Params) ->
  167. res(
  168. emqx_conf:update(
  169. Path,
  170. {Action, Params},
  171. #{override_to => cluster}
  172. )
  173. ).