HammerActually

HammerActually

Unexpected behaviour with mnesia - not actually durable?

I’m observing behavior with mnesia that is unexpected by me and could use some help understanding what’s going on. I’ve tested this with erlang 21.0.1 and 21.2.2, and elixir 1.7.4.

Given two scripts:

write.exs

IO.puts "Creating schema..."
:ok = :mnesia.create_schema([node()])
IO.puts "Starting mnesia..."
:ok = :mnesia.start()
IO.puts "Creating table..."
{:atomic, :ok} = :mnesia.create_table(:example, [{:disc_only_copies, [node()]}, type: :set, attributes: [:name, :value]])
IO.puts "Setting :foo = 1..."
{:atomic, :ok} = :mnesia.transaction(fn -> :mnesia.write({:example, :foo, 1}) end)
IO.puts "Looking up :foo..."
:mnesia.transaction(fn -> :mnesia.read({:example, :foo}) end) |> IO.inspect
IO.puts "Done."

read.exs

IO.puts "Starting mnesia..."
:ok = :mnesia.start()
IO.puts "Waiting until table is loaded..."
:mnesia.wait_for_tables([:example], 1_000)
IO.puts "Looking up :foo..."
:mnesia.transaction(fn -> :mnesia.read({:example, :foo}) end) |> IO.inspect
IO.puts "Done."

When I run these scripts:

rm -rf Mnesia.nonode@nohost; mix run write.exs; mix run read.exs

I observe the following:

Creating schema...
Starting mnesia...
Creating table...
Setting :foo = 1...
Looking up :foo...
{:atomic, [{:example, :foo, 1}]}
Done.
Starting mnesia...
Waiting until table is loaded...
dets: file "Mnesia.nonode@nohost/example.DAT" not properly closed, repairing ...
Looking up :foo...
{:atomic, []}
Done.

The primary behavior that is unexpected to me is that writing to a disc only table inside a transaction doesn’t seem to ensure that the record is actually written to disc before moving on after the transaction. ie, it’s not actually durable. I do observe the dets warning that the table file was not properly closed, which I presume is because the script terminated abruptly (though normally).

If I explicitly call :mnesia.stop before the end of the write.exs script, the record is actually persisted and successfully read by the read.exs script, however I expect that I should be able to trust that once the transaction returns, the record is safely persisted. I can include :mnesia.stop in my shutdown procedures, but it does not appear that it’s actually durable in the case where the application crashes or is halted via kill -9.

If instead of stopping :mnesia I sleep for a few seconds at the end of write.exs, the dets warning is still emitted but the record is successfully found. So it does eventually write the record to disc without an explicit :mnesia.stop, but just not before the :mnesia.transaction call returns, which very clearly to me seems to conflict with what is said here: http://erlang.org/doc/apps/mnesia/Mnesia_chap4.html#durability

I’ve observed the same behavior with :disc_copies and using :mnesia.sync_transactionas well, just to be thorough. The following is the output from:mnesia.system_info(:all)`, in case it’s useful. It’s all default values afaik. (paths have been truncated by me)

[
  access_module: :mnesia,
  auto_repair: true,
  backend_types: [:ram_copies, :disc_copies, :disc_only_copies],
  backup_module: :mnesia_backup,
  checkpoints: [],
  db_nodes: [:nonode@nohost],
  debug: :none,
  directory: 'Mnesia.nonode@nohost',
  dump_log_load_regulation: false,
  dump_log_time_threshold: 180000,
  dump_log_update_in_place: true,
  dump_log_write_threshold: 1000,
  event_module: :mnesia_event,
  extra_db_nodes: [],
  fallback_activated: false,
  held_locks: [],
  ignore_fallback_at_startup: false,
  fallback_error_function: {:mnesia, :lkill},
  is_running: :yes,
  local_tables: [:schema, :example],
  lock_queue: [],
  log_version: '4.3',
  master_node_tables: [],
  max_wait_for_decision: :infinity,
  protocol_version: {8, 3},
  running_db_nodes: [:nonode@nohost],
  schema_location: :opt_disc,
  schema_version: {2, 0},
  subscribers: [#PID<0.141.0>],
  tables: [:schema, :example],
  transaction_commits: 2,
  transaction_failures: 0,
  transaction_log_writes: 0,
  transaction_restarts: 0,
  transactions: [],
  use_dir: true,
  core_dir: false,
  no_table_loaders: 2,
  dc_dump_limit: 4,
  send_compressed: 0,
  version: '4.15.5'
]

Most Liked

tty

tty

Like most other dbases Mnesia’s disc_only_copies / disc_copies writes to a write-ahead-log. The abrupt termination didn’t allow it to sync to disc properly causing the error you see.

Unfortunately there is not really much you can do to avoid this situation. Even the “robust” Oracle ended up with a very corrupted dbase after dripping water from condensation fried the server.

In general a kill -15 should be attempted first to allow the application a chance to cleanup before a kill -9.

HammerActually

HammerActually

After originally posting here, I dove into the Erlang source code and posted on the Erlang bug tracker: https://bugs.erlang.org/browse/ERL-831

I’m pretty far removed from this issue at the moment, so may not have much further context to share, but I believe between this thread and the one in the bug tracker, everything I observed has been captured, in case it’s useful to anyone. Looking at the Erlang source code is likely provide a lot of clarity around where any blocking actually occurs, and echoing what has been said by a few folks in a few different places, it isn’t always what you might assume or expect in regards to durability, based on the documentation.

Where Next?

Popular in Questions Top

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
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement