emqx_cluster_rpc_handler.erl 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 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. -module(emqx_cluster_rpc_handler).
  17. -behaviour(gen_server).
  18. -include_lib("emqx/include/logger.hrl").
  19. -include("emqx_conf.hrl").
  20. -export([start_link/0, start_link/2]).
  21. -export([
  22. init/1,
  23. handle_call/3,
  24. handle_cast/2,
  25. handle_info/2,
  26. terminate/2,
  27. code_change/3
  28. ]).
  29. start_link() ->
  30. MaxHistory = emqx_conf:get(["node", "cluster_call", "max_history"], 100),
  31. CleanupMs = emqx_conf:get(["node", "cluster_call", "cleanup_interval"], 5 * 60 * 1000),
  32. start_link(MaxHistory, CleanupMs).
  33. start_link(MaxHistory, CleanupMs) ->
  34. State = #{max_history => MaxHistory, cleanup_ms => CleanupMs, timer => undefined},
  35. gen_server:start_link(?MODULE, [State], []).
  36. %%%===================================================================
  37. %%% Spawning and gen_server implementation
  38. %%%===================================================================
  39. init([State]) ->
  40. erlang:process_flag(trap_exit, true),
  41. {ok, ensure_timer(State)}.
  42. handle_call(Req, _From, State) ->
  43. ?SLOG(error, #{msg => "unexpected_call", call => Req}),
  44. {reply, ignored, State}.
  45. handle_cast(Msg, State) ->
  46. ?SLOG(error, #{msg => "unexpected_msg", cast => Msg}),
  47. {noreply, State}.
  48. handle_info({timeout, TRef, del_stale_mfa}, State = #{timer := TRef, max_history := MaxHistory}) ->
  49. case mria:transaction(?CLUSTER_RPC_SHARD, fun del_stale_mfa/1, [MaxHistory]) of
  50. {atomic, ok} -> ok;
  51. Error -> ?SLOG(error, #{msg => "del_stale_cluster_rpc_mfa_error", error => Error})
  52. end,
  53. {noreply, ensure_timer(State), hibernate};
  54. handle_info(Info, State) ->
  55. ?SLOG(error, #{msg => "unexpected_info", info => Info}),
  56. {noreply, State}.
  57. terminate(_Reason, #{timer := TRef}) ->
  58. emqx_misc:cancel_timer(TRef).
  59. code_change(_OldVsn, State, _Extra) ->
  60. {ok, State}.
  61. %%--------------------------------------------------------------------
  62. %% Internal functions
  63. %%--------------------------------------------------------------------
  64. ensure_timer(State = #{cleanup_ms := Ms}) ->
  65. State#{timer := emqx_misc:start_timer(Ms, del_stale_mfa)}.
  66. %% @doc Keep the latest completed 100 records for querying and troubleshooting.
  67. del_stale_mfa(MaxHistory) ->
  68. DoneId =
  69. mnesia:foldl(
  70. fun(Rec, Min) -> min(Rec#cluster_rpc_commit.tnx_id, Min) end,
  71. infinity,
  72. ?CLUSTER_COMMIT
  73. ),
  74. delete_stale_mfa(mnesia:last(?CLUSTER_MFA), DoneId, MaxHistory).
  75. delete_stale_mfa('$end_of_table', _DoneId, _Count) ->
  76. ok;
  77. delete_stale_mfa(CurrId, DoneId, Count) when CurrId > DoneId ->
  78. delete_stale_mfa(mnesia:prev(?CLUSTER_MFA, CurrId), DoneId, Count);
  79. delete_stale_mfa(CurrId, DoneId, Count) when Count > 0 ->
  80. delete_stale_mfa(mnesia:prev(?CLUSTER_MFA, CurrId), DoneId, Count - 1);
  81. delete_stale_mfa(CurrId, DoneId, Count) when Count =< 0 ->
  82. mnesia:delete(?CLUSTER_MFA, CurrId, write),
  83. delete_stale_mfa(mnesia:prev(?CLUSTER_MFA, CurrId), DoneId, Count - 1).