fix: subscribe stats_collector after Mnesia tables are ready
CI / test (push) Failing after 13m10s
CI / deploy-ift (push) Has been skipped
CI / e2e-ift (push) Has been skipped
CI / deploy-stage (push) Has been skipped
CI / e2e-stage (push) Has been skipped

Backfill ran during init_tables before wait_for_tables, so dirty reads hit {no_exists,user} and crash-looped eventhub on IFT.
This commit is contained in:
2026-07-18 23:33:56 +03:00
parent fa07d91f4b
commit 31c2a1b7d7
4 changed files with 28 additions and 4 deletions
+2
View File
@@ -52,6 +52,8 @@ start_application() ->
ok = infra_mnesia:init_tables(), ok = infra_mnesia:init_tables(),
ok = infra_mnesia:wait_for_tables(), ok = infra_mnesia:wait_for_tables(),
ok = migration_engine:ensure_applied(), ok = migration_engine:ensure_applied(),
%% После wait + migrations: иначе backfill ловит {no_exists, user} на ещё не загруженных таблицах.
ok = stats_collector:subscribe(),
calendar_html_renderer:init_cache(), calendar_html_renderer:init_cache(),
application:ensure_all_started(cowboy), application:ensure_all_started(cowboy),
start_http(), % Пользовательский API (8080) start_http(), % Пользовательский API (8080)
+1 -1
View File
@@ -125,7 +125,7 @@ handle_call(init_tables, _From, State) ->
true -> ok true -> ok
end, end,
ok = create_indices(), ok = create_indices(),
ok = stats_collector:subscribe(), %% stats_collector:subscribe — после wait_for_tables + migrations (eventhub_app)
ok = start_cleanup_timer(), ok = start_cleanup_timer(),
{reply, ok, State}; {reply, ok, State};
+19 -2
View File
@@ -127,7 +127,18 @@ do_subscribe(State) ->
false -> stats_tops:rebuild() false -> stats_tops:rebuild()
end, end,
lists:foreach(fun(Table) -> lists:foreach(fun(Table) ->
mnesia:subscribe({table, Table, detailed}) case lists:member(Table, mnesia:system_info(tables)) of
true ->
case mnesia:wait_for_tables([Table], 60000) of
ok -> mnesia:subscribe({table, Table, detailed});
{timeout, _} ->
error_logger:warning_msg(
"stats_collector: skip subscribe ~p (wait timeout)~n", [Table])
end;
false ->
error_logger:warning_msg(
"stats_collector: skip subscribe ~p (no table)~n", [Table])
end
end, ?SUB_TABLES), end, ?SUB_TABLES),
State#state{ready = true, subscribed = true}. State#state{ready = true, subscribed = true}.
@@ -371,7 +382,13 @@ backfill_all() ->
safe_match(Table, Pattern) -> safe_match(Table, Pattern) ->
case lists:member(Table, mnesia:system_info(tables)) of case lists:member(Table, mnesia:system_info(tables)) of
true -> mnesia:dirty_match_object(Pattern); true ->
try mnesia:dirty_match_object(Pattern) of
Rows when is_list(Rows) -> Rows
catch
exit:{aborted, {no_exists, _}} -> [];
error:{aborted, {no_exists, _}} -> []
end;
false -> [] false -> []
end. end.
+6 -1
View File
@@ -81,7 +81,12 @@ ready() ->
foreach_table(Table, Pattern, Fun) -> foreach_table(Table, Pattern, Fun) ->
case lists:member(Table, mnesia:system_info(tables)) of case lists:member(Table, mnesia:system_info(tables)) of
true -> lists:foreach(Fun, mnesia:dirty_match_object(Pattern)); true ->
try lists:foreach(Fun, mnesia:dirty_match_object(Pattern))
catch
exit:{aborted, {no_exists, _}} -> ok;
error:{aborted, {no_exists, _}} -> ok
end;
false -> ok false -> ok
end. end.