Elector - leader node election

Hello, I am slowly building my Erlang/Elixir portfolio and decided to split my larger project into smaller libraries. As a result, I have released the first library written in Erlang called Elector.

Elector is a library that automatically selects the leader node from an Erlang cluster. It reacts automatically when a node joins or leaves the cluster and initiates election. It is possible to configure the code to be executed before and after automatic election using hooks.

By default, Elector uses a voting strategy that selects the node with the highest runtime, but it is possible to override this with a custom strategy.
Elector supports different configuration options.

I am hoping to receive any feedback on what could be improved or any other thoughts.

Github link

Hex documentation

Thanks! :slight_smile:

3 Likes

Have you considered and/or compared this to making your singletons instead register globally and use the built in conflict resolution logic described in global?

Then you can use something like the following wrapper (inspiration from a decade old post) around your singletons in your supervisors to provide the resilience.

-module(singleton).

-export([start_link/3]).

start_link(M, F = start_link, A) ->
        case erlang:apply(M, F, A) of
                {ok, Pid} ->
                        {ok, Pid};
                {error,{already_started,Pid}} ->
                        link(Pid),
                        {ok, Pid};
                Else ->
                        Else
        end.

As a tip, it is always useful to others to include with your projects a description of why it exists and how the other strategies did not work for your situation(s) which is why you created this.

2 Likes

Thank you for the feedback!

I thought about using the global registering for the elector_worker gen_server but did not see any benefit. Definitely something I should try out.

I decided to roll out my own library because the libraries available only covered some aspects but not all that my application requires:

  • Automatically trigger election process when node leaves or joins the cluster
  • Run custom code before and after the election process
  • Always select node with the highest runtime as the leader
1 Like