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
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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








