syn_backbone.erl 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. %% ==========================================================================================================
  2. %% Syn - A global Process Registry and Process Group manager.
  3. %%
  4. %% The MIT License (MIT)
  5. %%
  6. %% Copyright (c) 2015 Roberto Ostinelli <roberto@ostinelli.net> and Neato Robotics, Inc.
  7. %%
  8. %% Permission is hereby granted, free of charge, to any person obtaining a copy
  9. %% of this software and associated documentation files (the "Software"), to deal
  10. %% in the Software without restriction, including without limitation the rights
  11. %% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. %% copies of the Software, and to permit persons to whom the Software is
  13. %% furnished to do so, subject to the following conditions:
  14. %%
  15. %% The above copyright notice and this permission notice shall be included in
  16. %% all copies or substantial portions of the Software.
  17. %%
  18. %% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. %% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. %% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. %% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. %% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. %% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. %% THE SOFTWARE.
  25. %% ==========================================================================================================
  26. -module(syn_backbone).
  27. %% API
  28. -export([initdb/0]).
  29. %% include
  30. -include("syn.hrl").
  31. %% ===================================================================
  32. %% API
  33. %% ===================================================================
  34. -spec initdb() -> ok | {error, any()}.
  35. initdb() ->
  36. %% ensure all nodes are added
  37. ClusterNodes = [node() | nodes()],
  38. mnesia:change_config(extra_db_nodes, ClusterNodes),
  39. %% create tables
  40. create_table(syn_registry_table, [
  41. {type, set},
  42. {ram_copies, ClusterNodes},
  43. {attributes, record_info(fields, syn_registry_table)},
  44. {index, [#syn_registry_table.pid]},
  45. {storage_properties, [{ets, [{read_concurrency, true}]}]}
  46. ]),
  47. create_table(syn_groups_table, [
  48. {type, bag},
  49. {ram_copies, ClusterNodes},
  50. {attributes, record_info(fields, syn_groups_table)},
  51. {index, [#syn_groups_table.pid]},
  52. {storage_properties, [{ets, [{read_concurrency, true}]}]}
  53. ]).
  54. create_table(TableName, Options) ->
  55. CurrentNode = node(),
  56. %% ensure table exists
  57. case mnesia:create_table(TableName, Options) of
  58. {atomic, ok} ->
  59. error_logger:info_msg("~p was successfully created", [TableName]),
  60. ok;
  61. {aborted, {already_exists, TableName}} ->
  62. %% table already exists, try to add current node as copy
  63. add_table_copy_to_current_node(TableName);
  64. {aborted, {already_exists, TableName, CurrentNode}} ->
  65. %% table already exists, try to add current node as copy
  66. add_table_copy_to_current_node(TableName);
  67. Other ->
  68. error_logger:error_msg("Error while creating ~p: ~p", [TableName, Other]),
  69. {error, Other}
  70. end.
  71. -spec add_table_copy_to_current_node(TableName :: atom()) -> ok | {error, any()}.
  72. add_table_copy_to_current_node(TableName) ->
  73. CurrentNode = node(),
  74. %% wait for table
  75. mnesia:wait_for_tables([TableName], 10000),
  76. %% add copy
  77. case mnesia:add_table_copy(TableName, CurrentNode, ram_copies) of
  78. {atomic, ok} ->
  79. error_logger:info_msg("Copy of ~p was successfully added to current node", [TableName]),
  80. ok;
  81. {aborted, {already_exists, TableName}} ->
  82. error_logger:info_msg("Copy of ~p is already added to current node", [TableName]),
  83. ok;
  84. {aborted, {already_exists, TableName, CurrentNode}} ->
  85. error_logger:info_msg("Copy of ~p is already added to current node", [TableName]),
  86. ok;
  87. {aborted, Reason} ->
  88. error_logger:error_msg("Error while creating copy of ~p: ~p", [TableName, Reason]),
  89. {error, Reason}
  90. end.