Skip to content

Commit

Permalink
Move ns_mnesia:ensure_table into the gen_server
Browse files Browse the repository at this point in the history
We were having crashes from trying to create tables while shutting
down.

Change-Id: I5dc2f951abc6036558d040890e821ed34a838594
Reviewed-on: http://review.northscale.com/3184
Reviewed-by: Aliaksey Kandratsenka <alkondratenko@gmail.com>
Tested-by: Aliaksey Kandratsenka <alkondratenko@gmail.com>
  • Loading branch information
Sean Lynch authored and alk committed Oct 11, 2010
1 parent 62a41eb commit 6821142
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/ns_mnesia.erl
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -49,24 +49,7 @@ delete_schema() ->
%% @doc Make sure table exists and has a copy on this node, creating it or %% @doc Make sure table exists and has a copy on this node, creating it or
%% adding a copy if it does not. %% adding a copy if it does not.
ensure_table(TableName, Opts) -> ensure_table(TableName, Opts) ->
try mnesia:table_info(TableName, disc_copies) of gen_server:call(?MODULE, {ensure_table, TableName, Opts}).
Nodes when is_list(Nodes) ->
case lists:member(node(), Nodes) of
true ->
ok;
false ->
?log_info("Creating local copy of ~p",
[TableName]),
{atomic, ok} = mnesia:add_table_copy(
TableName, node(), disc_copies)
end
catch exit:{aborted, {no_exists, _, _}} ->
{atomic, ok} =
mnesia:create_table(
TableName,
Opts ++ [{disc_copies, [node()]}]),
?log_info("Created table ~p", [TableName])
end.




%% @doc Back up the database in preparation for a node rename. %% @doc Back up the database in preparation for a node rename.
Expand Down Expand Up @@ -115,6 +98,27 @@ handle_call(prepare_rename, _From, State) ->
Reply = mnesia:backup(Pre), Reply = mnesia:backup(Pre),
{reply, Reply, State}; {reply, Reply, State};


handle_call({ensure_table, TableName, Opts}, _From, State) ->
try mnesia:table_info(TableName, disc_copies) of
Nodes when is_list(Nodes) ->
case lists:member(node(), Nodes) of
true ->
ok;
false ->
?log_info("Creating local copy of ~p",
[TableName]),
{atomic, ok} = mnesia:add_table_copy(
TableName, node(), disc_copies)
end
catch exit:{aborted, {no_exists, _, _}} ->
{atomic, ok} =
mnesia:create_table(
TableName,
Opts ++ [{disc_copies, [node()]}]),
?log_info("Created table ~p", [TableName])
end,
{reply, ok, State};

handle_call(Request, From, State) -> handle_call(Request, From, State) ->
?log_warning("Unexpected call from ~p: ~p", [From, Request]), ?log_warning("Unexpected call from ~p: ~p", [From, Request]),
{reply, unhandled, State}. {reply, unhandled, State}.
Expand Down

0 comments on commit 6821142

Please sign in to comment.