[erlang-questions] Initializing mnesia
Fri Oct 9 12:36:53 CEST 2015
On 2015年10月9日 金曜日 10:45:34 T Ty wrote:
> It boils down to what presuppositions you want your application to have. I
> prefer splitting initialization of the working environment from starting
> the application.
> Meaning when starting my application I assume mnesia tables have been
> created and I don't have to try to be clever in my application code. If the
> tables aren't there application doesn't start.
> Since your application is being started via some shell or init script place
> testing for preconditions and creation of mnesia tables there instead.
> In the application you might want do to mnesia:wait_for_tables/2 and
> ultimately: If the tables aren't there application doesn't start.
This plays directly into my restart/zomg-crash/live-config strategy.
The environment is prepared.
Running things receive configuration data.
Running things (re)initialize based on the config data.
There may be more steps than this, and that sort of breakdown may exist at different levels in a largish system or service constellation (especially when it involves a lot of non-Erlang services), but as long as I keep this in mind when building a system I tend to not have much trouble envisioning what different parts of my supervision tree should look like, or what "safe state" looks like when things need restarting.
Incidentally it also makes testing different configurations easy, because configuration is conceptually REconfiguration, initialization is conceptually REinitializtion, and so on. When I've done things right I know that I can, for example, destructively update a database/external service/VM/whatever, and just let the orchestrating Erlang system's built-in crash/restart/reconfig process occur (at whatever level applies) -- to include whatever Mnesia or other data caches should do. This isn't an ideal way to manage production devops, perhaps, but in development its really nice to have all that stand itself up again without even executing a startup script again. It is also really nice to have confidence that your system would respond exactly that way in production were deep disaster to strike.
I know this brings the discussion to a level way above Mnesia initialization, but my point is that the basic idea of separating these different operational preparation steps provides a natural answer to the question "Where should I spin up Mnesia for the first/Nth time?"
More information about the erlang-questions