分散のテスト

落書きですみません。元ネタは下記の URL から。

単に少し書き直した程度でほとんど代わっていません。あ、transaction めんどくさくて使ってませんね。

===================
Mnesia
===================

分散 DB を扱う

.. warning:: @localhost の部分は環境に依存します

spam::

  $ erl -name spam -setcookie secret
  Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

  Eshell V5.7.4  (abort with ^G)
  (spam@localhost)1>


eggs::

  $ erl -sname eggs -setcookie secret
  Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

  Eshell V5.7.4  (abort with ^G)
  (eggs@localhost)1> 

spam(成功)::

  (spam@localhost)1> net_adm:ping('eggs@localhost').
  pong

spam(失敗)::

  (spam@localhost)2> net_adm:ping('eggs@localhost').
  pang

spam::

  (spam@localhost)3> mnesia:create_schema(['spam@localhost']).
  ok
  (spam@localhost)4> mnesia:start().
  ok

eggs::

  (egg@localhost)1> mnesia:start().
  ok
  (egg@localhost)2> mnesia:change_config(extra_db_nodes, ['spam@localhost']).
  {ok,['spam@localhost']}

spam::

  (spam@localhost)5> rd(store, {key, value1, value2, value3}).
  store
  (spam@localhost)7> mnesia:create_table(store, [{ram_copies, ['spam@localhost', 'eggs@slocalhost']}, {attributes, record_info(fields, store)}]).
  {atomic,ok}

eggs::

  (egg@localhost)3> mnesia:info().
  ---> Processes holding locks <--- 
  ---> Processes waiting for locks <--- 
  ---> Participant transactions <--- 
  ---> Coordinator transactions <---
  ---> Uncertain transactions <--- 
  ---> Active tables <--- 
  schema         : with 2        records occupying 557      words of mem
  ===> System info in version "4.4.12", debug level = none <===
  use fallback at restart = false
  running db nodes   = ['spam@localhost','egg@localhost']
  stopped db nodes   = [] 
  master node tables = []
  remote             = [store]
  ram_copies         = [schema]
  disc_copies        = []
  disc_only_copies   = []
  [{'egg@localhost',ram_copies},{'spam@localhost',disc_copies}] = [schema]
  [{'spam@localhost',ram_copies}] = [store]
  4 transactions committed, 0 aborted, 0 restarted, 0 logged to disc
  0 held locks, 0 in queue; 0 local transactions, 0 remote
  0 transactions waits for other nodes: []
  ok

spam::

  (spam@localhost)8> mnesia:dirty_write(#store{key=a, value1=10}).
  ok

eggs::


  (egg@localhost)5> rd(store, {key, value1, value2, value3}).
  store
  (egg@localhost)7> mnesia:dirty_read(store, a).     
  [#store{key = a,value1 = 10,value2 = undefined,
          value3 = undefined}]

spam::

  (spam@localhost)11> mnesia:dirty_delete(store, a).
  ok

eggs::

  (egg@localhost)8> mnesia:dirty_read(store, a).
  []

bacon::

  % erl -name bacon -setcookie secret
  Erlang R13B03 (erts-5.7.4) [source] [64-bit] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]

  Eshell V5.7.4  (abort with ^G)
  (bacon@localhost)1> mnesia:start().
  ok
  (bacon@localhost)2> rd(store, {key, value1, value2, value3}).
  store
  (bacon@localhost)3> mnesia:change_config(extra_db_nodes,['spam@localhost', 'eggs@localhost']).
  {ok,['spam@localhost']}

spam::

  (spam@localhost)12> mnesia:dirty_write(#store{key=b, value1=20}). 
  ok

eggs::

  (egg@localhost)9> mnesia:dirty_read(store, b).
  [#store{key = b,value1 = 20,value2 = undefined,
          value3 = undefined}]

bacon::

  (bacon@localhost)4> mnesia:dirty_read(store, b).
  [#store{key = b,value1 = 20,value2 = undefined,
          value3 = undefined}]