2

I have an application that I want to run distributed on two nodes (one main and one failover).

I have two config files for each node.
Now I am running my application on the two nodes using:

rebar3 shell --sname a --config a 
rebar3 shell --sname b --config b

The problem is that the application starts on both nodes since I suppose they are not pre-connected when I am issuing the rebar command.

Config files

[
    {kernel,[{distributed,[{ex_banking,['a@AdrianB-LAPTOP','b@Adrian-LAPTOP']}]},
             {sync_nodes_timeout,3000}]},
    {ex_banking,[{port,3000}]}].
[
    {kernel,[{distributed,[{ex_banking,['a@AdrianB-LAPTOP','b@Adrian-LAPTOP']}]},
             {sync_nodes_mandatory,['a@AdrianB-LAPTOP'},
             {sync_nodes_timeout,3000}]},
    {ex_banking,[{port,3000}]}].

What is the strategy to connect the nodes when/after using rebar shell? Is there any way to create scripts or something?

I just want the nodes connected but only the main one starting witht the application started,while the second one takes on when the first crashes.

greybeard
  • 2,249
  • 8
  • 30
  • 66
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152

1 Answers1

3

To connect two or more nodes, you need also set same cookies for each node. And then ping those nodes by net_adm:ping/1. Here is example(expected that terminals will start at the same time ):

Terminal #1:

$ ./rebar3 shell --sname a --setcookie test
Erlang/OTP 24 [erts-12.1.5] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

Eshell V12.1.5  (abort with ^G)
(a@pc)1> nodes().
[]

Terminal #2:

$ ./rebar3 shell --sname b --setcookie test
Erlang/OTP 24 [erts-12.1.5] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

Eshell V12.1.5  (abort with ^G)
(b@pc)1> nodes().
[]

As you see, when you run it - we don't have any nodes. Now lets try use net_adm:ping/1 to connect them. Terminal #1:

$ ./rebar3 shell --sname a --setcookie test
Erlang/OTP 24 [erts-12.1.5] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

Eshell V12.1.5  (abort with ^G)
(a@pc)1> nodes().
[]
(a@pc)2> net_adm:ping('b@pc').
pong
(a@pc)3> nodes().
[b@pc]

Terminal #2:

$ ./rebar3 shell --sname b --setcookie test
Erlang/OTP 24 [erts-12.1.5] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1]

Eshell V12.1.5  (abort with ^G)
(b@pc)1> nodes().
[a@pc]

Also, I suppose you can join to https://erlangforums.com/ - in the Erlang community forum you can find answers to any questions related to Erlang/OTP fast enough. P.S. The name and cookie you can set also by vm.args file.

vkatsuba
  • 1,411
  • 1
  • 7
  • 19
  • 1
    I am aware on how to connect two nodes when in the erlang shell as well as setting cookies. My question is more specifically towards how do you manage this when using `rebar` to start your distribute app and not `erl` – Bercovici Adrian Dec 29 '21 at 19:15
  • 1
    The `rebar` is a tool for build Erlang/OTP projects. This tool cannot join clusters by default as Erlang/OTP(and I think this is good since nodes are dynamically added and removed and this is correct since it is flexible) - for this case I suppose you need to create or custom bash script, `Makefile` or `escript` and also correct configure `vm.args` file for each of them. Same done in famous projects like `tanodb`, `riack`, `ejabberd` etc. – vkatsuba Dec 29 '21 at 19:48
  • 1
    Do you by chance have or know an example of such a script , and how to run it ? I have been checking alot of resources including the MAN , Adopting Erlang site , to no avail. – Bercovici Adrian Dec 29 '21 at 20:02
  • 1
    I suppose you can take a look to topic https://marianoguerra.github.io/little-riak-core-book/starting.html in this topic described bash file https://github.com/marianoguerra/tanodb/blob/master/config/admin_bin. Also you can take a look to https://github.com/processone/ejabberd/blob/master/src/ejabberd_cluster.erl behavior and https://github.com/processone/ejabberd/blob/master/src/ejabberd_admin.erl#L614. – vkatsuba Dec 29 '21 at 20:14
  • 1
    Also feel free to join to Erlang/OTP community in https://erlangforums.com/ and to Erlang Slack - https://erlef.org/slack-invite/erlanger ;-). In forum you can create topic with your issue and ask community help you create something what you need - folks from Erlang community are happy to participate in interesting open source projects ;-). – vkatsuba Dec 29 '21 at 20:34