partition_supervisor (ex_stdlib v0.3.0)

View Source

PartitionSupervisor implementation inspired by Elixir's PartitionSupervisor.

A PartitionSupervisor is a supervisor that manages multiple partitions of the same child specification. It's designed for high-concurrency scenarios where you want to distribute work across multiple instances of the same process type.

Each partition runs as a separate DynamicSupervisor, and work is distributed across partitions using a hash function on a partition key.

Examples:

   % Start a partition supervisor with 4 partitions
   {ok, Pid} = partition_supervisor:start_link([
       {partitions, 4},
       {child_spec, #{id => worker, start => {worker, start_link, []}}}
   ]),
  
   % Start a child in a specific partition
   {ok, ChildPid} = partition_supervisor:start_child(Pid, user_123, [arg1, arg2]).

Summary

Functions

Returns a count of children in all partitions.

Returns a count of children in a specific partition.

Returns the partition index for the given partition key.

Returns the number of partitions in the supervisor.

Starts a child in the partition determined by the partition key.

Starts a child in the partition determined by the partition key with options.

Starts a PartitionSupervisor with the given options.

Starts a named PartitionSupervisor with the given options.

Synchronously stops the partition supervisor with reason normal.

Synchronously stops the partition supervisor with the given reason.

Synchronously stops the partition supervisor with the given reason and timeout. All partitions and their children are shut down first.

Terminates a child in the partition determined by the partition key.

Terminates a child in a specific partition.

Returns the PID of a child process by partition key.

Returns information about all children in all partitions.

Returns information about children in a specific partition.

Types

partition_index/0

-type partition_index() :: non_neg_integer().

partition_key/0

-type partition_key() :: term().

partition_options/0

-type partition_options() ::
          #{partitions := pos_integer(),
            child_spec := supervisor:child_spec(),
            hash_function => fun((partition_key()) -> non_neg_integer()),
            name => atom()}.

Functions

count_children(Supervisor)

-spec count_children(pid() | atom()) -> [{specs | active | supervisors | workers, non_neg_integer()}].

Returns a count of children in all partitions.

Returns a property list with counts of active, specs, supervisors, and workers.

count_children(Supervisor, PartitionIndex)

-spec count_children(pid() | atom(), partition_index()) ->
                        #{specs := non_neg_integer(),
                          active := non_neg_integer(),
                          supervisors := non_neg_integer(),
                          workers := non_neg_integer()}.

Returns a count of children in a specific partition.

Returns a property list with counts of active, specs, supervisors, and workers.

partition_for(Supervisor, PartitionKey)

-spec partition_for(pid() | atom(), partition_key()) -> partition_index().

Returns the partition index for the given partition key.

Uses the configured hash function to determine the partition.

partitions(Supervisor)

-spec partitions(pid() | atom()) -> pos_integer().

Returns the number of partitions in the supervisor.

start_child(Supervisor, PartitionKey, Args)

-spec start_child(pid() | atom(), partition_key(), [term()]) ->
                     {ok, pid()} | {ok, pid(), term()} | {error, term()}.

Starts a child in the partition determined by the partition key.

The partition is selected using the hash function on the partition key.

start_child(Supervisor, PartitionKey, Args, Options)

-spec start_child(pid() | atom(), partition_key(), [term()], [term()]) ->
                     {ok, pid()} | {ok, pid(), term()} | {error, term()}.

Starts a child in the partition determined by the partition key with options.

Additional options can be passed to modify the child spec.

start_link(Options)

-spec start_link(partition_options()) -> {ok, pid()} | {error, term()}.

Starts a PartitionSupervisor with the given options.

Options: - partitions: Number of partitions to create (required) - child_spec: Child specification for each partition (required) - hash_function: Custom hash function (optional, defaults to erlang:phash2/2)

start_link(Name, Options)

-spec start_link(atom(), partition_options()) -> {ok, pid()} | {error, term()}.

Starts a named PartitionSupervisor with the given options.

The supervisor will be registered under the given name.

stop(Supervisor)

-spec stop(pid() | atom()) -> ok.

Synchronously stops the partition supervisor with reason normal.

stop(Supervisor, Reason)

-spec stop(pid() | atom(), term()) -> ok.

Synchronously stops the partition supervisor with the given reason.

stop(Supervisor, Reason, Timeout)

-spec stop(pid() | atom(), term(), timeout()) -> ok.

Synchronously stops the partition supervisor with the given reason and timeout. All partitions and their children are shut down first.

terminate_child(Supervisor, ChildPid)

-spec terminate_child(pid() | atom(), pid()) -> ok | {error, term()}.

Terminates a child in the partition determined by the partition key.

The child is identified by its PID.

terminate_child(Supervisor, PartitionIndex, ChildPid)

-spec terminate_child(pid() | atom(), partition_index(), pid()) -> ok | {error, term()}.

Terminates a child in a specific partition.

The child is identified by its PID and the partition is specified directly.

whereis_name(Supervisor, PartitionKey)

-spec whereis_name(pid() | atom(), partition_key()) -> pid() | undefined.

Returns the PID of a child process by partition key.

This is useful for direct communication with processes in specific partitions.

which_children(Supervisor)

-spec which_children(pid() | atom()) ->
                        [{term(), pid() | undefined, worker | supervisor, [atom()] | dynamic}].

Returns information about all children in all partitions.

Returns a list of {Id, Child, Type, Modules} tuples for all children.

which_children(Supervisor, PartitionIndex)

-spec which_children(pid() | atom(), partition_index()) ->
                        [{term(), pid() | undefined, worker | supervisor, [atom()] | dynamic}].

Returns information about children in a specific partition.

Returns a list of {Id, Child, Type, Modules} tuples for children in the partition.