Relrin

Relrin

Creating Mnesia tables with disc_copies option

Hello, everyone

I’m trying to create a Mnesia table with using the mnesiam package, but can’t understand what the issue is happening during creation.

The table is very simple and doesn’t contain something special:

defmodule Matchmaking.Model.LobbyState do
  alias :mnesia, as: Mnesia

  @table Matchmaking.Model.LobbyState
  @attributes [
    :id,              # UUID
    :dump             # Shared state in JSON as string
  ]

  def init_store() do
    Mnesia.create_table(
      @table,
      [
        type: :set,
        disc_copies: [Node.self()],
        record_name: @table,
        attributes: @attributes
      ]
    )
  end

  def copy_store() do
    Mnesia.add_table_copy(@table, Node.self(), :disc_copies)
  end
end

If I’m trying to call the init_store/0 manually, then it returns the following record:

iex(1)> Matchmaking.Model.LobbyState.init_store()
{aborted: {:bad_type, Matchmaking.Model.LobbyState, :disc_copies, :nonode@nohost}

and :mnesia.system_info returns this:

iex(2)> :mnesia.system_info
===> System info in version "4.15.3", debug level = none <===
opt_disc. Directory "/app/mnesia_disc_dump" is NOT used.
use fallback at restart = false
running db nodes   = [nonode@nohost]
stopped db nodes   = [] 
master node tables = []
remote             = []
ram_copies         = ['Elixir.Matchmaking.Model.ActiveUser',schema]
disc_copies        = []
disc_only_copies   = []
[{nonode@nohost,ram_copies}] = [schema,'Elixir.Matchmaking.Model.ActiveUser']
3 transactions committed, 7 aborted, 0 restarted, 0 logged to disc
0 held locks, 0 in queue; 0 local transactions, 0 remote
0 transactions waits for other nodes: []
:yes

Finding the answer to this issue led to the adding a variable in configs (for using disc_copies tables you will need to define a directory for storing dumps). However, an attempt to specify a directory in config.exc :

config :mnesia, :dir, System.get_env("MNESIA_DUMP_DIRECTORY") || '/app/mnesia_disc_dump'

with rights for writing/reading/executing didn’t help me. The expected table still isn’t created and returns the same error as was recently mentioned.

Anybody had faced with the same issue before? How it can be solved?

Most Liked Switch mode

neneboe

neneboe

I was encountering a similar issue trying to use Mnesia for persistent sessions with Pow. I have a full breakdown of the situation, the problems I encountered, and my solution here.

To summarize, I am using Edeliver/Distillery, and so I had added mnesia to my extra_applications, and this was starting :mnesia with the default schema in memory, then Pow was trying to create another schema on the same node, but with :disk_copies enabled. This lead to the same :bad_type error as @Relrin was getting.

To fix this, I removed mnesia from :extra_applications, and instead put it in :included_applications, so that Mnesia is available to my app in production, but no default schema is created.

danschultzer

danschultzer

Pow Core Team

It’s probably not an issue with mnesia, but just with :included_applications not being used in releases in Elixir 1.9.0 (fixed in 1.9.1): 1.9.0 mix release doesn't work well with included_application · Issue #9163 · elixir-lang/elixir · GitHub

Mnesia has been working pretty well for me personally. Just wished the documentation and error messages was better.

rvirding

rvirding

Creator of Erlang

A first question: did you create a schema before starting mnesia and creating your table? When you create a schema you create a directory structure where all mnesia data is kept. Disc copies of tables are kept in this directory. If you don’t explicitly create a schema then everything is kept in RAM so creating a disc doesn’t seem reasonable.

You must create the schema before starting mnesia and you only have to create the schema the first time, after that it will be reused.

Where Next?

Trending in Questions Top

jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
silverdr
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated! To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead. Sta...
New
saveman71
Hello ! We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
michallepicki
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
type1fool
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
akoutmos
@hugobarauna and I (Alex Koutmos) have been hard at work on writing a book on Nerves that takes you from simply blinking LEDs to building...
New

We're in Beta

About us Mission Statement