Fix race between join_cluster and ejabberd_mnesia (#2079)

This commit is contained in:
Christophe Romain 2017-12-21 14:07:29 +01:00
parent 32ad7d3761
commit a3083b5d71
1 changed files with 22 additions and 14 deletions

View File

@ -60,16 +60,15 @@ join(Node) ->
application:stop(mnesia), application:stop(mnesia),
mnesia:delete_schema([node()]), mnesia:delete_schema([node()]),
application:start(mnesia), application:start(mnesia),
mnesia:change_config(extra_db_nodes, [Node]), case mnesia:change_config(extra_db_nodes, [Node]) of
mnesia:change_table_copy_type(schema, node(), disc_copies), {ok, _} ->
spawn(fun() -> replicate_database(Node),
lists:foreach(fun(Table) -> wait_for_sync(infinity),
Type = ejabberd_cluster:call( application:stop(mnesia),
Node, mnesia, table_info, [Table, storage_type]), application:start(ejabberd);
mnesia:add_table_copy(Table, node(), Type) {error, Reason} ->
end, mnesia:system_info(tables)--[schema]) {error, Reason}
end), end;
application:start(ejabberd);
_ -> _ ->
{error, {no_ping, Node}} {error, {no_ping, Node}}
end. end.
@ -94,11 +93,11 @@ leave([], Node) ->
leave([Master|_], Node) -> leave([Master|_], Node) ->
application:stop(ejabberd), application:stop(ejabberd),
application:stop(mnesia), application:stop(mnesia),
ejabberd_cluster:call(Master, mnesia, del_table_copy, [schema, Node]),
spawn(fun() -> spawn(fun() ->
mnesia:delete_schema([node()]), rpc:call(Master, mnesia, del_table_copy, [schema, Node]),
erlang:halt(0) mnesia:delete_schema([node()]),
end), erlang:halt(0)
end),
ok. ok.
-spec node_id() -> binary(). -spec node_id() -> binary().
@ -130,6 +129,15 @@ subscribe(_) ->
%%%=================================================================== %%%===================================================================
%%% Internal functions %%% Internal functions
%%%=================================================================== %%%===================================================================
replicate_database(Node) ->
mnesia:change_table_copy_type(schema, node(), disc_copies),
lists:foreach(
fun(Table) ->
Type = ejabberd_cluster:call(Node, mnesia, table_info, [Table, storage_type]),
mnesia:add_table_copy(Table, node(), Type)
end, mnesia:system_info(tables)--[schema]).
-spec match_node_id(integer()) -> node(). -spec match_node_id(integer()) -> node().
match_node_id(I) -> match_node_id(I) ->
match_node_id(I, get_nodes()). match_node_id(I, get_nodes()).