View Source ProcessHub.Strategy.Redundancy.Replication (ProcessHub v0.4.0-beta)
The replication strategy allows for multiple instances of a process to be started
across the cluster. The number of instances is determined by the replication_factor
option.
The replication strategy also comes with a replication_model option, which determines
which instances of the process are active and which are passive.
Imagine a scenario where you have a process that is responsible for handling streams of
data. You may want to have multiple instances of this process running across the cluster,
but only one of them should be active at any given time and insert data into the database.
The passive instances of the process should be ready to take over if the active instance
fails.
Replication strategy selects the master/active node based on the
child_idandchild_nodesarguments. Thechild_idis converted to a charlist and summed up. The sum is then used to calculate the index of the master node in thechild_nodeslist. This ensures that the same master node is selected for the samechild_idandchild_nodesarguments.
This strategy also allows replication on all nodes in the cluster. This is done by setting
the replication_factor option to :cluster_size.
ProcessHub can notify the process when it is active or passive by sending a :redundancy_signal.
Using the :redundancy_signal optionWhen using the
:redundancy_signaloption, make sure that the processes are handling the message{:process_hub, :redundancy_signal, mode}, where themodevariable is either:activeor:passive.
Example GenServer process handling the :redundancy_signal:
def handle_info({:process_hub, :redundancy_signal, mode}, state) do
# Update the state with the new mode and do something differently.
{:noreply, Map.put(state, :replication_mode, mode)}
end
Summary
Types
@type t() :: %ProcessHub.Strategy.Redundancy.Replication{ redundancy_signal: :none | :active | :passive | :all, replication_factor: pos_integer() | :cluster_size, replication_model: :active_active | :active_passive }
Replication strategy options.
replication_factor- The number of instances of a single process across the cluster. This option can be positive integer orcluster_sizeatom to replicate process on all nodes. Default value is2.replication_model- This option determines the mode in which the instances of processes are started. Default value is:active_active.:active_active- All instances of a process across the cluster are equal.:active_passive- Only one instance of a process across the cluster is active; the rest are passive. Remaining replicas are started as passive processes.
redundancy_signal- This option determines when a process should be notified of its replication mode. Default value is:none.:none- No notifications are sent.:active_active- Only active processes are notified.:active_passive- Only passive processes are notified.:all- All processes are notified.
Functions
@spec handle_post_start(struct(), ProcessHub.Hub.t(), [ {ProcessHub.child_id(), pid(), [node()]} ]) :: :ok
@spec handle_post_update( struct(), ProcessHub.Hub.t(), {[{ProcessHub.child_id(), [node()], keyword()}], {:up | :down, node()}} ) :: :ok