sheharyarn / memento

Simple + Powerful interface to the Mnesia Distributed Database 💾
http://hexdocs.pm/memento/
MIT License
736 stars 23 forks source link

Distributed Memento (w/libcluster) #15

Closed mhsdef closed 5 years ago

mhsdef commented 5 years ago

@sheharyarn this is a real cool effort you've got here. 👏 Nice work.

How do I link up two nodes together using Memento? I don't see any change config or add table copy logic so far. Am I correct that I should drop out to :mnesia to do it?

Eg (w/two node RAM instances)... first node:

Memento.start
Memento.Table.create!(MyLovelyTable)
#:mnesia.wait_for_tables?

Second node:

Memento.start
:mnesia.change_config(:extra_db_nodes, Node.list())
:mnesia.add_table_copy(MyLovelyTable, node())
#:mnesia.wait_for_tables?
sheharyarn commented 5 years ago

Sorry for the late reply @hewsut. When running :mnesia in distributed mode with multiple nodes, you need to generate their schema (similar to persisting to disk - #3), but on each node:

You can create a helper method to do this for you:

defmodule MyApp do
  def setup do
    # Get a list of all nodes including current node
    nodes = [node() | Node.list()]

    # Stop Memento all nodes
    :rpc.multicall(nodes, Memento, :stop, [])

    # Create schema on all nodes
    Memento.Schema.create(nodes)

    # Start Memento all nodes
    :rpc.multicall(nodes, Memento, :start, [])

    # Create ram copies on all nodes
    Memento.Table.create!(YourTable, ram_copies: nodes)
  end
end

Just call setup/0 once on any node and it will create the schema and table copies for your on all nodes.

Note: Do make sure all nodes can see/talk to each other, i.e. they must share their erlang cookie.

mxgrn commented 2 months ago

@sheharyarn I tried this approach, got a question. Memento.info shows:

disc_copies        = [schema]

Doesn't it mean that schema is stored on disc? I expected everything to be in memory.

sheharyarn commented 2 months ago

@mxgrn That's what Memento.Schema.create/1 does. schema is different from other Mnesia/Memento tables. For multi-node distribution, even if it's just for ram_copies, schema still needs to be persisted to disk iirc (better to double-check in case I'm misremembering).

Further reading: https://www.erlang.org/doc/apps/mnesia/mnesia.html#create_schema/1

mxgrn commented 2 months ago

@sheharyarn Thanks. Though, I do see that mnesiac does not create schema on disc (see below for details), which is why I assumed it should also be possible to keep the schema in memory only (also, not seeing why it shouldn't be).

iex(ultramemory_prod@app2.local)1> :mnesia.info

...
ram_copies         = [games,schema]
...
mxgrn commented 2 months ago

I finally solved my issues with mnesiac, and can confirm that mnesia can run fully in RAM, no schema on disk is required.