Permalink
Browse files

Move ns_mnesia:ensure_table into the gen_server

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...
seanlynch authored and alk committed Oct 11, 2010
1 parent 62a41eb commit 6821142cd91e3792c1317acf2bd2cbb656160776
Showing with 22 additions and 18 deletions.
  1. +22 −18 src/ns_mnesia.erl
View
@@ -49,24 +49,7 @@ delete_schema() ->
%% @doc Make sure table exists and has a copy on this node, creating it or
%% adding a copy if it does not.
ensure_table(TableName, Opts) ->
- 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.
+ gen_server:call(?MODULE, {ensure_table, TableName, Opts}).
%% @doc Back up the database in preparation for a node rename.
@@ -115,6 +98,27 @@ handle_call(prepare_rename, _From, State) ->
Reply = mnesia:backup(Pre),
{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) ->
?log_warning("Unexpected call from ~p: ~p", [From, Request]),
{reply, unhandled, State}.

0 comments on commit 6821142

Please sign in to comment.