Skip to content

dgigafox/consensus-ex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ConsensusEx

ConsensusEx is an Elixir implementation of a distributed system of nodes that agrees upon a single leader at any point in time.

Development

  • Install asdf version manager
  • Install Erlang and Elixir
asdf plugin-add erlang https://github.com/asdf-vm/asdf-erlang
asdf plugin-add elixir https://github.com/asdf-vm/asdf-elixir

asdf install
  • Install hex, rebar, phoenix and mix dependencies
mix local.hex --force
mix local.rebar --force
mix archive.install --force https://github.com/phoenixframework/archives/raw/master/phx_new.ez
mix deps.get

How to Run

Before running any node, you must first input your hostname in the etc/hosts of your machine that will serve as your DNS.

For example:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1       localhost
255.255.255.255 broadcasthost
::1             localhost
127.0.0.1       consensus.ex  #add this line

where consensus.ex is your chosen hostname. Note: Be sure to name your host with a dot and some extension (i.e. name.extension format) like the one above.

Now we can start our participating nodes. In the directory of the source code, run a node using name flag. Let us run 3 nodes for example.

$ iex --name darren@consensus.ex -S mix

$ iex --name shaye@consensus.ex -S mix

$ iex --name meadow@consensus.ex -S mix

No need to run any command to join the cluster.

Now we can ask each of them who is their leaders. By typing the following command on each node:

iex(darren@consensus.ex)> ConsensusEx.LeaderRegistry.get_leader()
:"[email protected]"

iex(meadow@consensus.ex)> ConsensusEx.LeaderRegistry.get_leader()
:"[email protected]"

iex(shaye@consensus.ex)4> ConsensusEx.LeaderRegistry.get_leader()
:"[email protected]"

To get the names of the nodes and their corresponding IDs you may type this command in any of the nodes

iex> alias ConsensusEx.Helpers.DistributedSystems
ConsensusEx.Helpers.DistributedSystems
iex> hostname = DistributedSystems.get_hostname(Node.self())
:"consensus.ex"
iex> DistributedSystems.get_connected_peers(hostname)
{:ok, [{'darren', 4839}, {'meadow', 5146}, {'shaye', 9138}]}

By trying to stop the iex process of the leader, we can see that the other nodes will try to elect another leader

iex(shaye@consensus.ex)3>
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
^Ciex(darren@consensus.ex)5> ConsensusEx.LeaderRegistry.get_leader()
:"[email protected]"

iex(meadow@consensus.ex)3> ConsensusEx.LeaderRegistry.get_leader()
:"[email protected]"

That's it! You may try to create 2 or more nodes and see the transition of leadership on each nodes.

Learn more

consensus-ex