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

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?

Popular in Questions Top

mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
dotdotdotPaul
Okay, I’m having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I’m sure I’...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement