Clause
← All documentation

mnesia

Clause's mnesia application exports the OTP 29 interface of mnesia (mnesia-4.27) and mnesia_frag_hash. In detail:

Every other documented function works. See the function inventory and differences.

Design

mnesia is written in Erlang over ets, dets and disk_log, in its own structure. Where programs can observe something, it keeps OTP's shape:

The design keeps OTP's formats, so that a later step can add more nodes.

Processes

application:start(mnesia), or mnesia:start/0,1, starts this tree (clause_mnesia_app):

Each child has OTP's name, order and modules, as supervisor:which_children/1 shows them.

mnesia_monitor does four things:

Queries from other nodes

Another node's mnesia, OTP's or Clause's, calls OTP's internal module names on this node. Clause provides those modules, with the functions other nodes call and OTP's answers (mnesia's files and protocol); their other functions are declined:

create_schema/1 and delete_schema/1 ask each node through these queries, so they work with OTP nodes too.

Connecting nodes

Clause's mnesia connects to other nodes' mnesia, OTP's or Clause's, as OTP's does (mnesia's files and protocol):

Each node keeps the outcomes of the transactions it took part in (transactions), and a disc node logs them until each disc node taking part is certain (disc nodes); a decision another node sends in {decisions, ...} is not taken yet.

Merging schemas

change_config(extra_db_nodes, Nodes), and a start with the extra_db_nodes parameter, merge this node's schema with those of the nodes that run mnesia, OTP's or Clause's, as OTP's does (mnesia's files and protocol):

Disc nodes

Nodes whose schemas are on disc, OTP's or Clause's, are created and start together as OTP's (mnesia's files and protocol):

Global variables

mnesia_gvar is a public set of {Name, Value} rows, as in OTP.

Master nodes are {master_nodes, {tab, Tab}, Nodes} rows of the mnesia_decision table, as in OTP.

A missing row gives OTP's answer:

Configuration

The documented configuration parameters are read when mnesia starts. Each comes from the application environment, or else from its documented default. A value of the wrong type stops the start with {bad_config, Name, Value}.

mnesia:start/1 takes {Name, Value} pairs and sets them before starting. A bad value returns {error, {bad_type, Name, Value}}, as in OTP.

system_info/1 answers the configuration items even when mnesia is not running. Other items then answer as OTP's do:

Schema

The schema lives in RAM, or on disc when mnesia's directory holds one (see disc storage).

create_table/2 checks a definition in OTP's order, with OTP's reasons (clause_mnesia_schema):

  1. Option keys: an unknown key, or anything that is not a {Key, Value} pair, gives badarg. A repeated key gives {badarg, Tab, "Duplicate keys", Keys}.
  2. storage_properties: ets and dets options that mnesia sets itself are refused.
  3. Existing table: already_exists.
  4. The definition's checks:
    • node lists, then the table name, the type and record_name;
    • attributes: at least two atoms, never snmp, none twice;
    • index positions: after the key, bag or ordered;
    • local_content, access_mode, majority, the SNMP structure, user_properties, cookie and version.
  5. Disc nodes: a disc node that is not running gives not_active. Disc copies on this node with a RAM schema give {bad_type, Tab, disc_copies, Node}.

Behavior that follows OTP's:

delete_table/1 removes the table, its index tables and its rows. wait_for_tables/2 waits until each table has a replica to read here, and returns {timeout, Tables} for the rest.

Schema changes

Each change takes OTP's locks: the schema's write lock first, then the table's write lock where OTP takes it, which also aborts with {no_exists, Tab} for a table that is read_only or has no replica here. Then come OTP's checks: {no_exists, {Tab, cstruct}} for an unknown table, {no_exists, Tab} without an active replica, and the definition's checks of create_table/2 for the changed definition. Each change gives the definition its next major version.

Disc storage

Disc storage keeps OTP's files in mnesia's directory, the dir parameter (default Mnesia.<node()>) (clause_mnesia_disc):

FileContents
FALLBACK.BUPthe backup that create_schema/1,2 writes, installed at the next start
schema.DATa dets table of the schema's {schema, Tab, CreateList} records
LATEST.LOGa disk_log of #commit{} records with each commit's disc writes, {master_nodes, Tab, Nodes} entries, and the decisions and other nodes' mnesia_up and mnesia_down records of disc nodes, after a #log_header{}
PREVIOUS.LOGthe log a dump is moving into the tables' files
DECISION_TAB.LOGthe {decision_list, Decisions} that each dump finding entries in the log writes: the transaction serial {trans_tid, serial, N}, the master nodes and the other nodes seen going down
Tab.DCD, Tab.DCLa disc_copies table's records, and its writes since the DCD file was written
Tab.DAT, Tab_Pos.DATa disc_only_copies table's dets table, and the dets table of its index at Pos

The disc schema

Transaction log

Storage types

change_table_copy_type/3 moves a replica between storage types:

Disc-only tables

A disc_only_copies table works on its dets table, as OTP's:

Dirty access

The dirty functions run in the calling process and work on the table's ets table:

Transactions

A transaction runs in the calling process (clause_mnesia_tm). Its activity is {AccessModule, {tid, Counter, Pid}, #tidstore{}} under mnesia_activity_state in the process dictionary, as in OTP:

select/4 and select_reverse/4 return OTP's #mnesia_select{} continuation. In a transaction with writes to the table, each chunk merges them as OTP's does, chunk boundaries included.

Locks

mnesia_locker keeps read and write locks on records, whole tables ({Tab, '______WHOLETABLE_____'}) and global items (clause_mnesia_locker):

Activities

activity/2,3,4, async_dirty/1,2, sync_dirty/1,2 and ets/1,2 run a fun with mnesia's functions mapped as in OTP (clause_mnesia_access):

Indexes

Each index is an ordered_set table of {{Value, Key}} (clause_mnesia_index):

Events

mnesia_subscr keeps the subscriptions (clause_mnesia_subscr). It links each subscriber and drops its subscriptions when it exits.

Text files

clause_mnesia_text reads and writes OTP's text files: a {tables, [{Tab, Options}]} term, then records named after their tables.

Query tables

table/1,2 give qlc a table (clause_mnesia_qlc):

The report

clause_mnesia_info prints OTP's report.

Checkpoints

A checkpoint keeps its tables as they were when it was activated (clause_mnesia_checkpoint).

Backups

clause_mnesia_backup writes and reads backups through a backup module, by default mnesia_backup (mnesia_backup), which keeps OTP's callbacks and its disk_log file.

Restoring

restore/2 restores tables from a backup in one schema transaction and returns {atomic, Tabs}:

Fallbacks

Fragmented tables

A table with frag_properties is split into fragments: the first is the table itself, fragment N the table Tab_fragN (mnesia_frag).

SNMP

A table with an snmp structure, [{key, Types}] given to create_table/2 or snmp_open_table/2, keeps an ordered index of its rows by SNMP row index, as OTP's mnesia_snmp_hook does (clause_mnesia_snmp):

Tests