What is the best way to detect the conflict of mnesia schemas?

Sergey Samokhin prikrutil@REDACTED
Tue Jun 9 00:56:52 CEST 2009


I'm trying to create a cluster based on mnesia. And there is a problem
with connecting a node (which may have schema previously stored on
disk) to a cluster.

Schema stored on disk by mnesia may contain one schema whereas node
I'm trying to connect to by mnesia:change_config(extra_db_nodes,
[Node]) another one. How should I detect this problem and say the user
that schemas are incompatible? mnesia:start() doesn't return anything
like {error, schemas_conflict} which could be used to exactly detect
such a problem. Also I didn't see any useful events sent to

One way to detect it is to compare schema's cookies before mnesia is
started. Unfortunatelly, I can't get such a cookie until mnesia is
started. And as soon as mnesia is started, error may happen if there
are two different schemas.

Let's suppose that emulator is started by "erl -kernel auto_dist_connect never":

mnesia:start(), % there won't be any auto-connects even if schema
contains info about another nodes

Cookie = mnesia:table_info(schema, cookie), % will fail if mnesia isn't started

Node = another_node@REDACTED, % The node I want to connect to

% <skipped> Here I'm checking if Cookie is the same as Node has

% If cookies are the same, I would like to connect:
application:set_env(kernel, auto_dist_connect, always),
mnesia:change_config(extra_db_nodes, [Node])

% If cookies are different, I want to crash:

I've found that changing the kernel option "auto_dist_connect" by calling:

application:set_env(kernel, auto_dist_connect, Type), where Type =
once | never | always

...works fine.

Is it safe to change this option on the fly without restarting an emulator?

Of course, schema stored on disk by mnesia may contain information
about nodes which no longer have the same schema (they may have been
taken away from one cluster and put into another one). In this case
one can compare the current schema with all the schemas from nodes
obtained by mnesia:system_info(db_nodes). It's look too tricky...

What do you use to detect such a problems? Do you let mnesia crash
when starting (in this case, how do you detect this problem and write
error messages to a log)?

Sergey Samokhin

More information about the erlang-questions mailing list