From d0880a6cb6d3cca18f30cd2610eac9e342bbdf1d Mon Sep 17 00:00:00 2001 From: Mikhail Uvarov Date: Fri, 20 Aug 2021 11:38:25 +0200 Subject: [PATCH] Reset last_event_id once mongoose_domain_core starts This tells to the service, that it needs to read the complete database. But it assumes that the service process is down, so it would not overwrite the last_event_id we set here. To ensure that the service is down if the core is down, the core and service processes should be supervised with rest_for_one strategy. Which is in a separate PR. --- big_tests/tests/service_domain_db_SUITE.erl | 51 ++++++++++++++++++++- src/domain/mongoose_domain_core.erl | 1 + 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/big_tests/tests/service_domain_db_SUITE.erl b/big_tests/tests/service_domain_db_SUITE.erl index b8f34a588c..2d69b63c48 100644 --- a/big_tests/tests/service_domain_db_SUITE.erl +++ b/big_tests/tests/service_domain_db_SUITE.erl @@ -63,6 +63,7 @@ db_cases() -> [ db_out_of_sync_restarts_service, db_crash_on_initial_load_restarts_service, db_restarts_properly, + db_keeps_syncing_after_cluster_join, cli_can_insert_domain, cli_can_disable_domain, cli_can_enable_domain, @@ -242,7 +243,8 @@ setup_service(Opts, Config) -> Pairs1 = [{<<"example.cfg">>, <<"type1">>}, {<<"erlang-solutions.com">>, <<"type2">>}, {<<"erlang-solutions.local">>, <<"type2">>}], - CommonTypes = [<<"type1">>, <<"type2">>, <<"dbgroup">>, <<"dbgroup2">>, <<"cfggroup">>], + CommonTypes = [<<"type1">>, <<"type2">>, <<"test type">>, + <<"dbgroup">>, <<"dbgroup2">>, <<"cfggroup">>], Types2 = [<<"mim2only">>|CommonTypes], init_with(mim(), Pairs1, CommonTypes), init_with(mim2(), [], Types2), @@ -543,6 +545,28 @@ db_restarts_properly(_) -> end, mongoose_helper:wait_until(F, true, #{time_left => timer:seconds(15)}). +db_keeps_syncing_after_cluster_join(Config) -> + HostType = <<"test type">>, + %% GIVING mim1 and mim2 are not clustered. + %% Ask mim1 to join mim2's cluster + %% (and mongooseim application gets restarted on mim1) + leave_cluster(Config), + service_enabled(mim()), + ok = insert_domain(mim(), <<"example1.com">>, HostType), + ok = insert_domain(mim2(), <<"example2.com">>, HostType), + sync(), + %% Nodes don't have to be clustered to sync the domains. + assert_domains_are_equal(HostType), + %% WHEN Adding mim1 joins into a cluster + %% (and mongooseim application gets restarted on mim1) + join_cluster(Config), + service_enabled(mim()), + %% THEN Sync is successful + ok = insert_domain(mim(), <<"example3.com">>, HostType), + ok = insert_domain(mim2(), <<"example4.com">>, HostType), + sync(), + assert_domains_are_equal(HostType). + cli_can_insert_domain(Config) -> {"Added\n", 0} = ejabberdctl("insert_domain", [<<"example.db">>, <<"type1">>], Config), @@ -911,7 +935,9 @@ select_domain(Node, Domain) -> erase_database(Node) -> case mongoose_helper:is_rdbms_enabled(domain()) of - true -> rpc(Node, mongoose_domain_sql, erase_database, []); + true -> + prepare_test_queries(Node), + rpc(Node, mongoose_domain_sql, erase_database, []); false -> ok end. @@ -1101,3 +1127,24 @@ handler_opts(#{skip_auth := true}) -> []; handler_opts(_Params) -> [{password, <<"secret">>}, {username, <<"admin">>}]. + +leave_cluster(Config) -> + Cmd = "leave_cluster", + #{node := Node} = distributed_helper:mim(), + Args = ["--force"], + ejabberdctl_helper:ejabberdctl(Node, Cmd, Args, Config). + +join_cluster(Config) -> + Cmd = "join_cluster", + #{node := Node} = distributed_helper:mim(), + #{node := Node2} = distributed_helper:mim2(), + Args = ["--force", atom_to_list(Node2)], + ejabberdctl_helper:ejabberdctl(Node, Cmd, Args, Config). + +assert_domains_are_equal(HostType) -> + Domains1 = lists:sort(get_domains_by_host_type(mim(), HostType)), + Domains2 = lists:sort(get_domains_by_host_type(mim2(), HostType)), + case Domains1 == Domains2 of + true -> ok; + false -> ct:fail({Domains1, Domains2}) + end. diff --git a/src/domain/mongoose_domain_core.erl b/src/domain/mongoose_domain_core.erl index 3c5cce8533..fa194590db 100644 --- a/src/domain/mongoose_domain_core.erl +++ b/src/domain/mongoose_domain_core.erl @@ -130,6 +130,7 @@ get_start_args() -> %% gen_server callbacks %%-------------------------------------------------------------------- init([Pairs, AllowedHostTypes]) -> + service_domain_db:reset_last_event_id(), ets:new(?TABLE, [set, named_table, protected, {read_concurrency, true}]), ets:new(?HOST_TYPE_TABLE, [set, named_table, protected, {read_concurrency, true}]), insert_host_types(?HOST_TYPE_TABLE, AllowedHostTypes),