fix(retainer): change mnesia table storage types during update

https://emqx.atlassian.net/browse/EMQX-8650
This commit is contained in:
Thales Macedo Garitezi 2023-01-04 09:47:14 -03:00
parent f767db4d8f
commit 2f13bfd452
5 changed files with 71 additions and 9 deletions

View File

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

View File

@ -321,16 +321,23 @@ update_config(
OldConf
) ->
#{
backend := BackendCfg,
backend := #{
type := BackendType,
storage_type := StorageType
},
msg_clear_interval := ClearInterval
} = NewConf,
#{backend := OldBackendCfg} = OldConf,
StorageType = maps:get(type, BackendCfg),
OldStrorageType = maps:get(type, OldBackendCfg),
case OldStrorageType of
StorageType ->
#{
backend := #{
type := OldBackendType,
storage_type := OldStorageType
}
} = OldConf,
SameBackendType = BackendType =:= OldBackendType,
SameStorageType = StorageType =:= OldStorageType,
case SameBackendType andalso SameStorageType of
true ->
State#{
clear_timer := check_timer(
ClearTimer,
@ -338,7 +345,7 @@ update_config(
clear_expired
)
};
_ ->
false ->
State2 = disable_retainer(State),
enable_retainer(State2, NewConf)
end.

View File

@ -31,6 +31,7 @@ all() ->
emqx_common_test_helpers:all(?MODULE).
init_per_suite(Config) ->
emqx_common_test_helpers:clear_screen(),
application:load(emqx_conf),
ok = ekka:start(),
ok = mria_rlog:wait_for_shards([?CLUSTER_RPC_SHARD], infinity),
@ -219,6 +220,56 @@ t_lookup_and_delete(_) ->
ok = emqtt:disconnect(C1).
t_change_storage_type(_Config) ->
Path = api_path(["mqtt", "retainer"]),
{ok, ConfJson} = request_api(get, Path),
RawConf = emqx_json:decode(ConfJson, [return_maps]),
%% pre-conditions
?assertMatch(
#{
<<"backend">> := #{
<<"type">> := <<"built_in_database">>,
<<"storage_type">> := <<"ram">>
},
<<"enable">> := true
},
RawConf
),
?assertEqual(ram_copies, mnesia:table_info(?TAB_INDEX_META, storage_type)),
?assertEqual(ram_copies, mnesia:table_info(?TAB_MESSAGE, storage_type)),
?assertEqual(ram_copies, mnesia:table_info(?TAB_INDEX, storage_type)),
ChangedConf = emqx_map_lib:deep_merge(
RawConf,
#{
<<"backend">> =>
#{<<"storage_type">> => <<"disc">>}
}
),
{ok, UpdateResJson} = request_api(
put,
Path,
[],
auth_header_(),
ChangedConf
),
UpdatedRawConf = emqx_json:decode(UpdateResJson, [return_maps]),
?assertMatch(
#{
<<"backend">> := #{
<<"type">> := <<"built_in_database">>,
<<"storage_type">> := <<"disc">>
},
<<"enable">> := true
},
UpdatedRawConf
),
?assertEqual(disc_copies, mnesia:table_info(?TAB_INDEX_META, storage_type)),
?assertEqual(disc_copies, mnesia:table_info(?TAB_MESSAGE, storage_type)),
?assertEqual(disc_copies, mnesia:table_info(?TAB_INDEX, storage_type)),
ok.
%%--------------------------------------------------------------------
%% HTTP Request
%%--------------------------------------------------------------------

View File

@ -14,3 +14,5 @@
- Fix an issue where testing the GCP PubSub could leak memory, and an issue where its JWT token would fail to refresh a second time. [#9641](https://github.com/emqx/emqx/pull/9641)
- Fix the problem of data loss and bad match when the MySQL driver is disconnected [#9638](https://github.com/emqx/emqx/pull/9638).
- Fixed an issue where changing the storage type of the built-in database retainer would not take effect without restarting the node [#9676](https://github.com/emqx/emqx/pull/9676).

View File

@ -14,3 +14,5 @@
- 修复了测试GCP PubSub可能泄露内存的问题以及其JWT令牌第二次刷新失败的问题。 [#9640](https://github.com/emqx/emqx/pull/9640)
- 修复 MySQL 驱动断开连接时出现的数据丢失和匹配错误的问题 [#9638](https://github.com/emqx/emqx/pull/9638)。
- 修复了如果不重新启动节点,改变保留消息的存储类型将不会生效的问题 [#9676](https://github.com/emqx/emqx/pull/9676)。