Syn (short for synonym) is a global process registry for Erlang.
Syn is a process registry that has the following features:
In any distributed system you are faced with a consistency challenge, which is often resolved by having one master arbiter performing all write operations (chosen with a mechanism of leader election), or through atomic transactions.
Syn was born for applications of the IoT field. In this context, Keys used to identify a process are often the physical object's unique identifier (for instance, its serial or mac address), and are therefore already defined and unique before hitting the system. The consistency challenge is less of a problem in this case, since the likelihood of concurrent incoming requests that would register processes with the same Key is extremely low and, in most cases, acceptable.
In addition, write speeds were a determining factor in the architecture of Syn.
Therefore, Availability has been chosen over Consistency and Syn is eventually consistent.
If you're using rebar, add syn
as a dependency in your project's rebar.config
file:
{syn, ".*", {git, "git://github.com/ostinelli/syn.git", "master"}}
Then, get and compile your dependencies:
$ rebar get-deps
$ rebar compile
Ensure to start Syn from your application. This can be done by either providing it as a dependency in your .app
file, or by starting it manually:
syn:start().
To register a process:
syn:register(Key, Pid) -> ok | {error, Error}.
Types:
Key = any()
Pid = pid()
Error = taken
To retrieve a Pid from a Key:
syn:find_by_key(Key) -> Pid | undefined.
Types:
Key = any()
Pid = pid()
To retrieve a Key from a Pid:
syn:find_by_pid(Pid) -> Key | undefined.
Types:
Pid = pid()
Key = any()
To unregister a previously registered Key:
syn:unregister(Key) -> ok | {error, Error}.
Types:
Key = any()
Error = undefined
To retrieve the count of total registered processes running in the cluster:
syn:count() -> non_neg_integer().
To retrieve the count of total registered processes running on a specific node:
syn:count(Node) -> non_neg_integer().
Types:
Node = atom()
Processes are automatically monitored and removed from the registry if they die.
Options can be set in the environment variable syn
. You're probably best off using an application configuration file (in releases, sys.config
):
{syn, [
%% define callback function
{process_exit_callback, [calback_module, callback_function]},
%% send a message to the discarded process (instead of kill)
{netsplit_send_message_to_process, shutdown}
]}
These options are explained here below.
The process_exit_callback
option allows you to specify the module
and the function
of the callback that will be triggered when a process exits. This callback will be called only on the node where the process was running.
The callback function is defined as:
CallbackFun = fun(Key, Pid, Reason) -> any().
Types:
Key = any()
Pid = pid()
Reason = any()
The Key
and Pid
are the ones of the process that exited with Reason
.
For instance, if you want to print a log when a process exited:
-module(my_callback).
callback_on_process_exit(Key, Pid, Reason) ->
error_logger:info_msg("Process with Key ~p and Pid ~p exited with reason ~p~n", [Key, Pid, Reason])
end.
Set it in the options:
{syn, [
%% define callback function
{process_exit_callback, [my_callback, callback_on_process_exit]}
]}
If you don't set this option, no callback will be triggered.
After a net split, when nodes reconnect, Syn will merge the data from all the nodes in the cluster.
If the same Key was used to register a process on different nodes during a netsplit, then there will be a conflict. By default, Syn will discard the processes running on the node the conflict is being resolved on, and will kill it by sending a kill
signal with exit(Pid, kill)
.
If this is not desired, you can set the netsplit_send_message_to_process
option to instruct Syn to send a message to the discarded process, so that you can trigger any actions on that process. In this case, the process will not be killed by Syn, and you'll have to decide what to do with it (for instance, a graceful shutdown).
For example, if you want the message shutdown
to be send to the discarded process you can set the option:
{syn, [
%% define callback function
{netsplit_send_message_to_process, shutdown}
]}
If you don't set this option, then the default will apply (i.e. sending the exit(Pid, kill)
signal).
Important Note: The conflict resolution method SHOULD be defined in the same way across all nodes of the cluster. Having different conflict resolution options on different nodes can have unexpected results.
Under the hood, Syn performs dirty reads and writes into a distributed in-memory Mnesia table, replicated across all the nodes of the cluster.
To automatically handle net splits, Syn implements a specialized and simplified version of the mechanisms used in Ulf Wiger's unsplit framework.
So you want to contribute? That's great! Please follow the guidelines below. It will make it easier to get merged in.
Before implementing a new feature, please submit a ticket to discuss what you intend to do. Your feature might already be in the works, or an alternative implementation might have already been discussed.
Do not commit to master in your fork. Provide a clean branch without merge commits. Every pull request should have its own topic branch. In this way, every additional adjustments to the original pull request might be done easily, and squashed with git rebase -i
. The updated branch will be visible in the same pull request, so there will be no need to open new pull requests when there are changes to be applied.
Ensure to include proper testing. To run Syn tests you simply have to be in the project's root directory and run:
$ make tests