当前位置:   article > 正文

浅看rabbitmq的mnesia部署

rabbitmq mnesia protocol negotiation failed. local version: {8,3}. remote ve

伸缩性:根据系统负载,可以在运行中过程中添加或者删除服务节点,改变系统处理规模。
mnesia是一个分布式数据库模块,由多个节点构成的数据库cluster,数据表的位置对应用是透明的。透过该特性,很容易构建出一个具有高伸缩性的系统。
rabbitmq是一个分布式的消息中间件,在mnesia-cluster的机制上可由多个节点共同构建。

rabbitmq在一个节点上初始化mnesia的过程概况如下:
1.启动mnesia,尝试连接到系统的其他节点上;
2.若无法连接到任何节点,表示该节点是系统中第一个启动的节点;
    2.1.在这种情况下可以检查数据库的老表,然后等待其他节点的连接;
3.若连接到一些节点,则这些节点会同步并合并schema表;
    3.1.在当前节点上创建schema表和其他数据表的副本后,该节点就和所连接的节点数据将保持同步和一致;

 

  1. %%rabbit_mnesia.erl
  2. init_db(ClusterNodes) ->
  3. case mnesia:change_config(extra_db_nodes, ClusterNodes -- [node()]) of
  4. end

 启动mnesia后连接到其他节点。

 

  1. %%rabbit_mnesia.erl
  2. case mnesia:change_config(extra_db_nodes, ClusterNodes -- [node()]) of
  3. {ok, []} ->

 无法连接到任何节点,则检查当前数据库目录下的老表是否正确。(数据库目录为空则建立新表)

 

  1. %%rabbit_mnesia.erl
  2. case mnesia:change_config(extra_db_nodes, ClusterNodes -- [node()]) of
  3. {ok, [_|_]} ->
  4. IsDiskNode = ClusterNodes == [] orelse %%ClusterNodes==[]主节点
  5. lists:member(node(), ClusterNodes),
  6. ok = wait_for_replicated_tables(),
  7. ok = create_local_table_copy(schema, disc_copies),
  8. ok = create_local_table_copies(case IsDiskNode of

成功连接到一些节点后,mnesia之间交换数据库元信息,并等待在当前节点上有磁盘副本(disc_copies)的表和cluster完成同步。
            ok = wait_for_replicated_tables(),
如果当前节点是数据存储节点,还要在该节点上建立一些表格的磁盘副本
            ok = create_local_table_copies(case IsDiskNode of

 

我做了些简单的实验来观察mnesia相互连接时的特性。

  1. (a@localhost)1> mnesia:create_schema([node()]).
  2. ok
  3. (a@localhost)2> mnesia:start().
  4. ok
  5. (a@localhost)3> mnesia:create_table(user, [{disc_copies, [node()]}]).
  6. {atomic,ok}
  7. (a@localhost)4> mnesia:info().
  8. ---> Processes holding locks <---
  9. ---> Processes waiting for locks <---
  10. ---> Participant transactions <---
  11. ---> Coordinator transactions <---
  12. ---> Uncertain transactions <---
  13. ---> Active tables <---
  14. user : with 0 records occupying 304 words of mem
  15. schema : with 2 records occupying 524 words of mem
  16. ===> System info in version "4.4.10", debug level = none <===
  17. opt_disc. Directory "/home/hwh/a" is used.
  18. use fallback at restart = false
  19. running db nodes = [a@localhost]
  20. stopped db nodes = []
  21. master node tables = []
  22. remote = []
  23. ram_copies = []
  24. disc_copies = [schema,user]
  25. disc_only_copies = []
  26. [{a@localhost,disc_copies}] = [schema,user]
  27. 3 transactions committed, 0 aborted, 0 restarted, 1 logged to disc
  28. 0 held locks, 0 in queue; 0 local transactions, 0 remote
  29. 0 transactions waits for other nodes: []
  30. ok

 a节点上有两个磁盘表,分别是schema和user。

 

  1. (b@localhost)1> mnesia:start().
  2. ok
  3. (b@localhost)2> mnesia:change_config(extra_db_nodes, ['a@localhost', 'b@localhost', 'c@localhost']--[node()]).
  4. {ok,[a@localhost]}

 b节点尝试连接a,c节点,最终连接上了正在运行的a节点。连接之后b节点是什么状态呢?

 

  1. (b@localhost)3> mnesia:info().
  2. ---> Processes holding locks <---
  3. ---> Processes waiting for locks <---
  4. ---> Participant transactions <---
  5. ---> Coordinator transactions <---
  6. ---> Uncertain transactions <---
  7. ---> Active tables <---
  8. schema : with 2 records occupying 533 words of mem
  9. ===> System info in version "4.4.10", debug level = none <===
  10. opt_disc. Directory "/home/hwh/b" is NOT used.
  11. use fallback at restart = false
  12. running db nodes = [a@localhost,b@localhost]
  13. stopped db nodes = []
  14. master node tables = []
  15. remote = [user]
  16. ram_copies = [schema]
  17. disc_copies = []
  18. disc_only_copies = []
  19. [{a@localhost,disc_copies}] = [user]
  20. [{a@localhost,disc_copies},{b@localhost,ram_copies}] = [schema]
  21. 4 transactions committed, 0 aborted, 0 restarted, 0 logged to disc
  22. 0 held locks, 0 in queue; 0 local transactions, 0 remote
  23. 0 transactions waits for other nodes: []
  24. ok

可以看到user表是remote的,在a节点上有磁盘副本,在b节点上没有任何类型的副本。schema表已经合并,分别存储在a,b节点上。
此时在b节点上就可操作user表,表的位置是透明的。

 

  1. (b@localhost)4> mnesia:change_table_copy_type(schema, node(), disc_copies).
  2. {atomic,ok}
  3. (b@localhost)5> mnesia:add_table_copy(user, node(), disc_copies).
  4. {atomic,ok}
  5. (b@localhost)6> mnesia:info().
  6. ---> Processes holding locks <---
  7. ---> Processes waiting for locks <---
  8. ---> Participant transactions <---
  9. ---> Coordinator transactions <---
  10. ---> Uncertain transactions <---
  11. ---> Active tables <---
  12. user : with 0 records occupying 304 words of mem
  13. schema : with 2 records occupying 542 words of mem
  14. ===> System info in version "4.4.10", debug level = none <===
  15. opt_disc. Directory "/home/hwh/b" is used.
  16. use fallback at restart = false
  17. running db nodes = [a@localhost,b@localhost]
  18. stopped db nodes = []
  19. master node tables = []
  20. remote = []
  21. ram_copies = []
  22. disc_copies = [schema,user]
  23. disc_only_copies = []
  24. [{a@localhost,disc_copies},{b@localhost,disc_copies}] = [schema,user]
  25. 6 transactions committed, 0 aborted, 0 restarted, 2 logged to disc
  26. 0 held locks, 0 in queue; 0 local transactions, 0 remote
  27. 0 transactions waits for other nodes: []
  28. ok

在b节点上建立schema表和user表的磁盘副本后,发现user表不再是remote属性了,可从本地直接读取。

 

先退出b节点,再退出a节点,然后只重启b节点。

  1. (b@localhost)1> mnesia:start().
  2. ok
  3. (b@localhost)2> mnesia:info().
  4. ---> Processes holding locks <---
  5. ---> Processes waiting for locks <---
  6. ---> Participant transactions <---
  7. ---> Coordinator transactions <---
  8. ---> Uncertain transactions <---
  9. ---> Active tables <---
  10. schema : with 2 records occupying 542 words of mem
  11. ===> System info in version "4.4.10", debug level = none <===
  12. opt_disc. Directory "/home/hwh/b" is used.
  13. use fallback at restart = false
  14. running db nodes = [b@localhost]
  15. stopped db nodes = [a@localhost]
  16. master node tables = []
  17. remote = []
  18. ram_copies = []
  19. disc_copies = [schema,user]
  20. disc_only_copies = []
  21. [] = [user]
  22. [{b@localhost,disc_copies}] = [schema]
  23. 2 transactions committed, 0 aborted, 0 restarted, 0 logged to disc
  24. 0 held locks, 0 in queue; 0 local transactions, 0 remote
  25. 0 transactions waits for other nodes: []
  26. ok
  27. (b@localhost)3> mnesia:dirty_read(user, key).
  28. ** exception exit: {aborted,{no_exists,[user,key]}}
  29. in function mnesia:abort/1

 这时看到user表此时是不可用的,因为a,b节点共同维护user表的一致性,但是b节点先退出,所以user表的最终状态由a节点决定。
启动a节点之后,user表就变成可用状态了。

(a@localhost)1> mnesia:start().
  1. (b@localhost)4> mnesia:info().
  2. ---> Processes holding locks <---
  3. ---> Processes waiting for locks <---
  4. ---> Participant transactions <---
  5. ---> Coordinator transactions <---
  6. ---> Uncertain transactions <---
  7. ---> Active tables <---
  8. user : with 0 records occupying 304 words of mem
  9. schema : with 2 records occupying 542 words of mem
  10. ===> System info in version "4.4.10", debug level = none <===
  11. opt_disc. Directory "/home/hwh/b" is used.
  12. use fallback at restart = false
  13. running db nodes = [a@localhost,b@localhost]
  14. stopped db nodes = []
  15. master node tables = []
  16. remote = []
  17. ram_copies = []
  18. disc_copies = [schema,user]
  19. disc_only_copies = []
  20. [{a@localhost,disc_copies},{b@localhost,disc_copies}] = [schema,user]
  21. 3 transactions committed, 0 aborted, 0 restarted, 0 logged to disc
  22. 0 held locks, 0 in queue; 0 local transactions, 0 remote
  23. 0 transactions waits for other nodes: []
  24. ok
 
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/457183
推荐阅读
相关标签
  

闽ICP备14008679号