Merge pull request #8911 from lafirest/fix/retainer_start_error

fix(retainer): fix that EMQX can't start when the retainer is disabled
This commit is contained in:
lafirest 2022-09-09 10:14:34 +08:00 committed by GitHub
commit 67945515c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 9 deletions

View File

@ -8,6 +8,7 @@
* Speed up dispatching of shared subscription messages in a cluster [#8893](https://github.com/emqx/emqx/pull/8893) * Speed up dispatching of shared subscription messages in a cluster [#8893](https://github.com/emqx/emqx/pull/8893)
* Fix the extra / prefix when CoAP gateway parsing client topics. [#8658](https://github.com/emqx/emqx/pull/8658) * Fix the extra / prefix when CoAP gateway parsing client topics. [#8658](https://github.com/emqx/emqx/pull/8658)
* Speed up updating the configuration, When some nodes in the cluster are down. [#8857](https://github.com/emqx/emqx/pull/8857) * Speed up updating the configuration, When some nodes in the cluster are down. [#8857](https://github.com/emqx/emqx/pull/8857)
* Fix that EMQX can't start when the retainer is disabled [#8911](https://github.com/emqx/emqx/pull/8911)
## Enhancements ## Enhancements

View File

@ -2,7 +2,7 @@
{application, emqx_retainer, [ {application, emqx_retainer, [
{description, "EMQX Retainer"}, {description, "EMQX Retainer"},
% strict semver, bump manually! % strict semver, bump manually!
{vsn, "5.0.4"}, {vsn, "5.0.5"},
{modules, []}, {modules, []},
{registered, [emqx_retainer_sup]}, {registered, [emqx_retainer_sup]},
{applications, [kernel, stdlib, emqx]}, {applications, [kernel, stdlib, emqx]},

View File

@ -348,16 +348,12 @@ enable_retainer(
#{context_id := ContextId} = State, #{context_id := ContextId} = State,
#{ #{
msg_clear_interval := ClearInterval, msg_clear_interval := ClearInterval,
backend := BackendCfg, backend := BackendCfg
flow_control := FlowControl
} }
) -> ) ->
NewContextId = ContextId + 1, NewContextId = ContextId + 1,
Context = create_resource(new_context(NewContextId), BackendCfg), Context = create_resource(new_context(NewContextId), BackendCfg),
load(Context), load(Context),
emqx_limiter_server:add_bucket(
?APP, internal, maps:get(batch_deliver_limiter, FlowControl, undefined)
),
State#{ State#{
enable := true, enable := true,
context_id := NewContextId, context_id := NewContextId,
@ -373,7 +369,6 @@ disable_retainer(
} = State } = State
) -> ) ->
unload(), unload(),
emqx_limiter_server:del_bucket(?APP, internal),
ok = close_resource(Context), ok = close_resource(Context),
State#{ State#{
enable := false, enable := false,

View File

@ -18,6 +18,8 @@
-behaviour(application). -behaviour(application).
-include("emqx_retainer.hrl").
-export([ -export([
start/2, start/2,
stop/1 stop/1
@ -25,8 +27,19 @@
start(_Type, _Args) -> start(_Type, _Args) ->
ok = emqx_retainer_mnesia_cli:load(), ok = emqx_retainer_mnesia_cli:load(),
init_bucket(),
emqx_retainer_sup:start_link(). emqx_retainer_sup:start_link().
stop(_State) -> stop(_State) ->
ok = emqx_retainer_mnesia_cli:unload(), ok = emqx_retainer_mnesia_cli:unload(),
delete_bucket(),
ok. ok.
init_bucket() ->
#{flow_control := FlowControl} = emqx:get_config([retainer]),
emqx_limiter_server:add_bucket(
?APP, internal, maps:get(batch_deliver_limiter, FlowControl, undefined)
).
delete_bucket() ->
emqx_limiter_server:del_bucket(?APP, internal).

View File

@ -31,14 +31,16 @@ all() ->
[ [
{group, mnesia_without_indices}, {group, mnesia_without_indices},
{group, mnesia_with_indices}, {group, mnesia_with_indices},
{group, mnesia_reindex} {group, mnesia_reindex},
{group, test_disable_then_start}
]. ].
groups() -> groups() ->
[ [
{mnesia_without_indices, [sequence], common_tests()}, {mnesia_without_indices, [sequence], common_tests()},
{mnesia_with_indices, [sequence], common_tests()}, {mnesia_with_indices, [sequence], common_tests()},
{mnesia_reindex, [sequence], [t_reindex]} {mnesia_reindex, [sequence], [t_reindex]},
{test_disable_then_start, [sequence], [test_disable_then_start]}
]. ].
common_tests() -> common_tests() ->
@ -624,6 +626,19 @@ t_get_basic_usage_info(_Config) ->
?assertEqual(#{retained_messages => 5}, emqx_retainer:get_basic_usage_info()), ?assertEqual(#{retained_messages => 5}, emqx_retainer:get_basic_usage_info()),
ok. ok.
%% test whether the app can start normally after disabling emqx_retainer
%% fix: https://github.com/emqx/emqx/pull/8911
test_disable_then_start(_Config) ->
emqx_retainer:update_config(#{<<"enable">> => false}),
?assertNotEqual([], gproc_pool:active_workers(emqx_retainer_dispatcher)),
ok = application:stop(emqx_retainer),
timer:sleep(100),
?assertEqual([], gproc_pool:active_workers(emqx_retainer_dispatcher)),
ok = application:ensure_started(emqx_retainer),
timer:sleep(100),
?assertNotEqual([], gproc_pool:active_workers(emqx_retainer_dispatcher)),
ok.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Helper functions %% Helper functions
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------