Elector

example workflow

Description

Elector is an Erlang application that automatically detects all nodes inside the distributed Erlang cluster and chooses the leader node. The elections are started automatically when the Elector application is started or when a node joins or leaves the cluster. Elector also allows you to run pre- and post-election hooks that will be triggered when the election process is started and finished.

The default election strategy is to choose the node with the highest uptime.

Features

Configuration

Elector supports the following configurations:

Keep in mind to use the same configuration for all nodes in the cluster!

Guides

Installation for Elixir application

  1. Add {:elector, "~> 0.3.2"} under the deps in the mix.exs file:
defp deps do
    [
        {:elector, "~> 0.3.2"}
    ]
end
  1. Add elector under the extra_applications in the mix.exs file:
def application do
    [
        extra_applications: [:elector],
        mod: {MyApp, []}
    ]
end

Installation for Erlang application

  1. Add elector to the deps in the rebar.config file:
{deps, [{"elector", "0.3.2"}]}.
  1. Add elector to the applications list in the myapp.app.src file:
{applications, [elector]}.

Examples

Start election manually

Elixir

> alias :elector, as Elector
> Elector.elect_sync() # Start election synchronously
{:ok, :election_finished}
> Elector.elect() # Start election asynchronously
{:ok, :election_started}

Erlang

> elector:elect_sync(). % Start election synchronously
{ok, election_finished}
> elector:elect(). % Start election asynchronously
{ok, election_started}

Get current leader

Elixir

> alias :elector, as Elector
> Elector.get_leader()
{:ok, :example_node}

Erlang

> elector:get_leader().
{ok, example_node}

See the elector module for more.

https://hexdocs.pm/elector/

High level overview

Elector is a distributed application that utilizes the Erlang global module to spawn a singleton process named elector_commission. The commission process is responsible for starting the election process and keeping track of the nodes in the cluster. The election process is started by the commission process, which calls the elect/1 function from the strategy module. This function gathers the necessary information from the cluster and decides which node should become the leader.

Each node spawns the following local processes:

All processes are supervised by the elector_sup supervisor.

Setup Elector locally

Setup the elector locally and run the application:

Run tests:

Generate documentation: