Elector

Copyright © 2023 Anuar Alfetahe

Version: 0.3.0

Authors: Anuar Alfetahe (anuar.alfetahe@gmail.com).

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 runtime.

Features

Installation for Elixir application

Add {:elector, "~> 0.3.0"}` under the deps in the `mix.exs` file: ``` defp deps do [ {:elector, "~> 0.3.0"} ] end''

Next add elector under the extra_applications in the mix.exs file:
  def application do
    [
      extra_applications: [:elector],
      mod: {MyApp, []}
    ]
  end

Installation for Erlang application

Add elector to the deps in the rebar.config file: {deps, [{elector, {"elector", "0.3.0"}]}. Next 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.

Elector supports the following configurations:

election_delay - The delay in milliseconds before the new election starts. This value is used automatic election is triggered either by node join/leave or startup. Default value is 1 second(1000).

strategy_module - The module that is used for the election strategy implementation. Available options are: runtime_high_strategy` and `runtime_low_strategy. Feel free to write your own strategy module that implements the strategy_behaviour module. Default value is runtime_high_strategy which chooses the node with the highest runtime.

pre_election_hooks - A list of hooks/function calls that will be triggered before the node is starting the election. Expects a list of tuples with the following format: {Module, Function, Args}. Default value is [].

post_election_hooks - A list of hooks/function calls that will be triggered after the election process. Expects a list of tuples with the following format: {Module, Function, Args}. Default value is [].

quorum_size - The number of nodes(including the local node) that should be available in the cluster before the election process is started. D o not set it to 0 as it will disable the election process, leave empty or 1 if you want to run the election process even if there are no other nodes in the cluster. Default value is 1.

candidate_node - Boolean indicating if this node should be a candidate for the leader election. Default value is true.

hooks_execution - Atom indicating if the pre and post election hooks should be executed on the node starting the election process or on all nodes. The election process will be started automatically by the global commission process. The node who starts the commission process is not known and can be any node in the cluster. Available values are: local and global. Default value is global.

- automatic_elections - Boolean indicating if the automatic election process should be started when node joins or leaves the cluster. Default value is true.

Generated by EDoc