Skip to content

Commit f7a862c

Browse files
authored
Merge pull request #4362 from esl/domain-service-test-fix
Domain service test fix
2 parents 87ce067 + 85eb17d commit f7a862c

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

Diff for: big_tests/tests/service_domain_db_SUITE.erl

+4-1
Original file line numberDiff line numberDiff line change
@@ -206,9 +206,12 @@ init_per_suite(Config) ->
206206
erase_database(mim()),
207207
Config2 = ejabberd_node_utils:init(mim(), Config1),
208208
mongoose_helper:inject_module(?MODULE),
209+
%% If there are CI issues, use this to debug SQL queries:
210+
% mim_loglevel:enable_logging([mim, mim2, mim3], [{mongoose_domain_sql, debug}]),
209211
escalus:init_per_suite([{service_setup, per_testcase} | Config2]).
210212

211213
end_per_suite(Config) ->
214+
% mim_loglevel:disable_logging([mim, mim2, mim3], [{mongoose_domain_sql, debug}]),
212215
[restart_domain_core(Node) || Node <- all_nodes()],
213216
dynamic_services:restore_services(Config),
214217
domain_helper:insert_configured_domains(),
@@ -1209,7 +1212,7 @@ resume_service(Node) ->
12091212
ok = rpc(Node, sys, resume, [service_domain_db]).
12101213

12111214
sync_local(Node) ->
1212-
pong = rpc(Node, service_domain_db, sync_local, []).
1215+
pong = rpc(Node#{timeout => timer:seconds(30)}, service_domain_db, sync_local, []).
12131216

12141217
force_check_for_updates(Node) ->
12151218
ok = rpc(Node, service_domain_db, force_check_for_updates, []).

Diff for: src/domain/mongoose_domain_loader.erl

+6-3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ check_for_updates({Min, Max},
106106
check_for_updates(MinMax = {Min, Max},
107107
#{min_event_id := OldMin, max_event_id := OldMax})
108108
when is_integer(Min), is_integer(Max) ->
109+
put(debug_last_check_for_updates_minmax, MinMax),
109110
{MinEventId, MaxEventId} = limit_max_id(OldMax, MinMax, 1000),
110111
check_if_id_is_still_relevant(OldMax, MinEventId),
111112
NewGapsFromBelow =
@@ -134,7 +135,9 @@ check_for_updates(MinMax = {Min, Max},
134135
Ids = rows_to_ids(Rows),
135136
ids_to_gaps(FromId, MaxEventId, Ids)
136137
end,
137-
fix_gaps(NewGapsFromBelow ++ NewGapsFromThePage),
138+
Gaps = NewGapsFromBelow ++ NewGapsFromThePage,
139+
put(debug_last_gaps, list_to_tuple(Gaps)), %% Convert to a tuple so it could be printed
140+
fix_gaps(Gaps),
138141
State2 = #{min_event_id => MinEventId, max_event_id => MaxEventId},
139142
mongoose_loader_state:set(State2),
140143
case MaxEventId < Max of
@@ -263,7 +266,7 @@ fix_gaps(Gaps, Retries) when Retries > 0 ->
263266
%%
264267
%% There is no easy way to check for a reason.
265268
%%
266-
%% fix_gaps tries to insert_dummy_event with a gap event id.
269+
%% fix_gaps tries to insert_dummy_events with a gap event id.
267270
%% This makes the state of transaction for gap events obvious:
268271
%% - if this insert fails, this means the actual record finally
269272
%% appears and we can read it.
@@ -274,7 +277,7 @@ fix_gaps(Gaps, Retries) when Retries > 0 ->
274277
%%
275278
%% RDBMS servers do not overwrite data when INSERT operation is used.
276279
%% i.e. only one insert for a key succeeded.
277-
[catch mongoose_domain_sql:insert_dummy_event(Id) || Id <- Gaps],
280+
catch mongoose_domain_sql:insert_dummy_events(Gaps),
278281
%% The gaps should be filled at this point
279282
Rows = lists:append([mongoose_domain_sql:select_updates_between(Id, Id) || Id <- Gaps]),
280283
?LOG_WARNING(#{what => domain_fix_gaps, gaps => Gaps, rows => Rows}),

Diff for: src/domain/mongoose_domain_sql.erl

+27-10
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
select_updates_between/2,
2020
get_enabled_dynamic/0,
2121
delete_events_older_than/1,
22-
insert_dummy_event/1]).
22+
insert_dummy_events/1]).
2323

2424
%% interfaces only for integration tests
2525
-export([prepare_test_queries/0,
@@ -30,7 +30,8 @@
3030
-ignore_xref([erase_database/1, prepare_test_queries/0, get_enabled_dynamic/0,
3131
insert_full_event/2, insert_domain_settings_without_event/2]).
3232

33-
-import(mongoose_rdbms, [prepare/4, execute_successfully/3]).
33+
-import(mongoose_rdbms, [prepare/4]).
34+
-include("mongoose_logger.hrl").
3435

3536
-type event_id() :: non_neg_integer().
3637
-type domain() :: jid:lserver().
@@ -258,19 +259,24 @@ delete_events_older_than(Id) ->
258259
execute_successfully(Pool, domain_events_delete_older_than, [Id])
259260
end).
260261

261-
insert_dummy_event(EventId) ->
262-
insert_full_event(EventId, <<>>).
262+
insert_dummy_events(EventIds) ->
263+
insert_full_events([{EventId, <<>>} || EventId <- EventIds]).
263264

264265
insert_full_event(EventId, Domain) ->
266+
[Res] = insert_full_events([{EventId, Domain}]),
267+
Res.
268+
269+
insert_full_events(EventIdDomain) ->
265270
case mongoose_rdbms:db_type() of
266271
mssql ->
267-
insert_full_event_mssql(EventId, Domain);
272+
insert_full_events_mssql(EventIdDomain);
268273
_ ->
269274
Pool = get_db_pool(),
270-
execute_successfully(Pool, domain_insert_full_event, [EventId, Domain])
275+
[catch execute_successfully(Pool, domain_insert_full_event, [EventId, Domain])
276+
|| {EventId, Domain} <- EventIdDomain]
271277
end.
272278

273-
insert_full_event_mssql(EventId, Domain) ->
279+
insert_full_events_mssql(EventIdDomain) ->
274280
%% MSSQL does not allow to specify ids,
275281
%% that are supposed to be autoincremented, easily
276282
%% https://docs.microsoft.com/pl-pl/sql/t-sql/statements/set-identity-insert-transact-sql
@@ -281,7 +287,8 @@ insert_full_event_mssql(EventId, Domain) ->
281287
%% when trying to execute
282288
mongoose_rdbms:sql_query(Pool, <<"SET IDENTITY_INSERT domain_events ON">>),
283289
try
284-
execute_successfully(Pool, domain_insert_full_event, [EventId, Domain])
290+
[catch execute_successfully(Pool, domain_insert_full_event, [EventId, Domain])
291+
|| {EventId, Domain} <- EventIdDomain]
285292
after
286293
mongoose_rdbms:sql_query(Pool, <<"SET IDENTITY_INSERT domain_events OFF">>)
287294
end
@@ -372,16 +379,26 @@ transaction(F) ->
372379
%% (there is no logic, that would suffer by a restart of a transaction).
373380
transaction(_F, 0, Errors) ->
374381
{error, {db_error, Errors}};
375-
transaction(F, Retries, Errors) when Retries > 0 ->
382+
transaction(F, Tries, Errors) when Tries > 0 ->
376383
Pool = get_db_pool(),
377384
Result = rdbms_queries:sql_transaction(Pool, fun() -> F(Pool) end),
378385
case Result of
379386
{aborted, _} -> %% Restart any rolled back transaction
387+
put(debug_last_transaction_error, Result),
380388
timer:sleep(100), %% Small break before retry
381-
transaction(F, Retries - 1, [Result|Errors]);
389+
transaction(F, Tries - 1, [Result|Errors]);
382390
_ ->
391+
erase(debug_last_transaction_error),
383392
simple_result(Result)
384393
end.
385394

386395
simple_result({atomic, Result}) -> Result;
387396
simple_result(Other) -> {error, {db_error, Other}}.
397+
398+
execute_successfully(Pool, StatementName, Args) ->
399+
{Time, Res} = timer:tc(fun() -> mongoose_rdbms:execute_successfully(Pool, StatementName, Args) end),
400+
%% Convert args to tuple, because Erlang formats list as a string for domain_event_ids_between
401+
?LOG_DEBUG(#{what => domain_sql_execute,
402+
statement_name => StatementName, args => list_to_tuple(Args), result => Res,
403+
duration => round(Time / 1000)}),
404+
Res.

Diff for: src/domain/service_domain_db.erl

+8-2
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,18 @@ sync_local() ->
9595
true = is_pid(LocalPid),
9696
Nodes = [node(Pid) || Pid <- all_members()],
9797
%% Ping from all nodes in the cluster
98-
[pong = rpc:call(Node, ?MODULE, ping, [LocalPid]) || Node <- Nodes],
98+
{Results, []} = rpc:multicall(Nodes, ?MODULE, ping, [LocalPid]),
99+
[pong = Res || Res <- Results],
99100
pong.
100101

101102
-spec ping(pid()) -> pong.
102103
ping(Pid) ->
103-
gen_server:call(Pid, ping).
104+
try
105+
gen_server:call(Pid, ping, timer:seconds(15))
106+
catch Class:Reason:Stacktrace ->
107+
Info = rpc:pinfo(Pid, [current_stacktrace, dictionary]),
108+
erlang:raise(Class, {Reason, Info}, Stacktrace)
109+
end.
104110

105111
%% ---------------------------------------------------------------------------
106112
%% Server callbacks

0 commit comments

Comments
 (0)