[erlang-questions] Support for newcomers and the popularity of Erlang
Wojciech Knapik
wmknapik@REDACTED
Tue Apr 3 17:22:52 CEST 2012
On Tue, Apr 03, 2012 at 03:39:22AM +0200, Wojciech Knapik wrote:
> On Fri, Mar 30, 2012 at 02:08:39AM -0700, Ulf Wiger wrote:
>
> > You can start mnesia with {extra_db_nodes, KnownNodes]}. This will
> > establish a ram-only schema on the current node, containing all
> > meta-data about the database (assuming KnownNodes contains actual
> > schema nodes). After starting, you can call
> > mnesia:change_table_copy_type(schema, node(), disc_copies), to make
> > the schema on the current node persistent.
>
> Ok, here's what I came up with.
> This function is called from gen_server's init callback.
> The code is run on multiple identical nodes (code-wise).
> Any hints/comments/suggestions would be appreciated.
I added some code to limit the number of replicas to ?MAX_REPLICAS.
I'd appreciate it if someone could tell me if this code is ok, or if I'm
doing something fundamentally wrong or in a non-erlangy way ;]
setup_database() ->
DBBootstrap = fun() ->
mnesia:change_table_copy_type(schema, node(), disc_copies),
mnesia:create_table(?TABLE_NAME, [{attributes, record_info(fields, ?TABLE_NAME)}, {disc_copies, [node()]}])
end,
mnesia:start([{extra_db_nodes, nodes()}]),
case lists:member(?TABLE_NAME, mnesia:system_info(tables)) of
true ->
case lists:member(node(), mnesia:table_info(?TABLE_NAME, active_replicas)) of
true -> ok;
false ->
case length(mnesia:system_info(db_nodes)) of
N when N > ?MAX_REPLICAS -> ok;
_ -> mnesia:change_table_copy_type(schema, node(), disc_copies),
mnesia:add_table_copy(?TABLE_NAME, node(), disc_copies)
end
end;
false ->
case global:trans({db_bootstrap, self()}, DBBootstrap, [node()|nodes()], 0) of
aborted -> mnesia:wait_for_tables([?TABLE_NAME], 5000),
setup_database();
{aborted, Reason} -> erlang:error(Reason);
_ -> ok
end
end.
More information about the erlang-questions
mailing list