|
| 1 | +defmodule Beacon do |
| 2 | + @moduledoc """ |
| 3 | + Distributed process group membership tracking. |
| 4 | + """ |
| 5 | + |
| 6 | + alias Beacon.Partition |
| 7 | + alias Beacon.Scope |
| 8 | + |
| 9 | + @type group :: any |
| 10 | + @type start_option :: {:partitions, pos_integer()} | {:broadcast_interval_in_ms, non_neg_integer()} |
| 11 | + |
| 12 | + @doc "Returns a supervisor child specification for a Beacon scope" |
| 13 | + def child_spec([scope]) when is_atom(scope), do: child_spec([scope, []]) |
| 14 | + def child_spec(scope) when is_atom(scope), do: child_spec([scope, []]) |
| 15 | + |
| 16 | + def child_spec([scope, opts]) when is_atom(scope) and is_list(opts) do |
| 17 | + %{ |
| 18 | + id: Beacon, |
| 19 | + start: {__MODULE__, :start_link, [scope, opts]}, |
| 20 | + type: :supervisor |
| 21 | + } |
| 22 | + end |
| 23 | + |
| 24 | + @doc """ |
| 25 | + Starts the Beacon supervision tree for `scope`. |
| 26 | +
|
| 27 | + Options: |
| 28 | +
|
| 29 | + * `:partitions` - number of partitions to use (default: number of schedulers online) |
| 30 | + * `:broadcast_interval_in_ms`: - interval in milliseconds to broadcast membership counts to other nodes (default: 5000 ms) |
| 31 | + * `:message_module` - module implementing `Beacon.Adapter` behaviour (default: `Beacon.Adapter.ErlDist`) |
| 32 | + """ |
| 33 | + @spec start_link(atom, [start_option]) :: Supervisor.on_start() |
| 34 | + def start_link(scope, opts \\ []) when is_atom(scope) do |
| 35 | + {partitions, opts} = Keyword.pop(opts, :partitions, System.schedulers_online()) |
| 36 | + broadcast_interval_in_ms = Keyword.get(opts, :broadcast_interval_in_ms) |
| 37 | + |
| 38 | + if not (is_integer(partitions) and partitions >= 1) do |
| 39 | + raise ArgumentError, |
| 40 | + "expected :partitions to be a positive integer, got: #{inspect(partitions)}" |
| 41 | + end |
| 42 | + |
| 43 | + if broadcast_interval_in_ms != nil and |
| 44 | + not (is_integer(broadcast_interval_in_ms) and broadcast_interval_in_ms > 0) do |
| 45 | + raise ArgumentError, |
| 46 | + "expected :broadcast_interval_in_ms to be a positive integer, got: #{inspect(broadcast_interval_in_ms)}" |
| 47 | + end |
| 48 | + |
| 49 | + Beacon.Supervisor.start_link(scope, partitions, opts) |
| 50 | + end |
| 51 | + |
| 52 | + @doc "Join pid to group in scope" |
| 53 | + @spec join(atom, any, pid) :: :ok | {:error, :not_local} |
| 54 | + def join(_scope, _group, pid) when is_pid(pid) and node(pid) != node(), do: {:error, :not_local} |
| 55 | + def join(scope, group, pid) when is_atom(scope) and is_pid(pid) do |
| 56 | + Partition.join(Beacon.Supervisor.partition(scope, group), group, pid) |
| 57 | + end |
| 58 | + |
| 59 | + @doc "Leave pid from group in scope" |
| 60 | + @spec leave(atom, group, pid) :: :ok |
| 61 | + def leave(scope, group, pid) when is_atom(scope) and is_pid(pid) do |
| 62 | + Partition.leave(Beacon.Supervisor.partition(scope, group), group, pid) |
| 63 | + end |
| 64 | + |
| 65 | + @doc "Get total members count per group in scope" |
| 66 | + @spec member_counts(atom) :: %{group => non_neg_integer} |
| 67 | + def member_counts(scope) when is_atom(scope) do |
| 68 | + remote_counts = Scope.member_counts(scope) |
| 69 | + |
| 70 | + scope |
| 71 | + |> local_member_counts() |
| 72 | + |> Map.merge(remote_counts, fn _k, v1, v2 -> v1 + v2 end) |
| 73 | + end |
| 74 | + |
| 75 | + @doc "Get total member count of group in scope" |
| 76 | + @spec member_count(atom, group) :: non_neg_integer |
| 77 | + def member_count(scope, group) do |
| 78 | + local_member_count(scope, group) + Scope.member_count(scope, group) |
| 79 | + end |
| 80 | + |
| 81 | + @doc "Get total member count of group in scope on specific node" |
| 82 | + @spec member_count(atom, group, node) :: non_neg_integer |
| 83 | + def member_count(scope, group, node) when node == node(), do: local_member_count(scope, group) |
| 84 | + def member_count(scope, group, node), do: Scope.member_count(scope, group, node) |
| 85 | + |
| 86 | + @doc "Get local members of group in scope" |
| 87 | + @spec local_members(atom, group) :: [pid] |
| 88 | + def local_members(scope, group) when is_atom(scope) do |
| 89 | + Partition.members(Beacon.Supervisor.partition(scope, group), group) |
| 90 | + end |
| 91 | + |
| 92 | + @doc "Get local member count of group in scope" |
| 93 | + @spec local_member_count(atom, group) :: non_neg_integer |
| 94 | + def local_member_count(scope, group) when is_atom(scope) do |
| 95 | + Partition.member_count(Beacon.Supervisor.partition(scope, group), group) |
| 96 | + end |
| 97 | + |
| 98 | + @doc "Get local members count per group in scope" |
| 99 | + @spec local_member_counts(atom) :: %{group => non_neg_integer} |
| 100 | + def local_member_counts(scope) when is_atom(scope) do |
| 101 | + Enum.reduce(Beacon.Supervisor.partitions(scope), %{}, fn partition_name, acc -> |
| 102 | + Map.merge(acc, Partition.member_counts(partition_name)) |
| 103 | + end) |
| 104 | + end |
| 105 | + |
| 106 | + @doc "Check if pid is a local member of group in scope" |
| 107 | + @spec local_member?(atom, group, pid) :: boolean |
| 108 | + def local_member?(scope, group, pid) when is_atom(scope) and is_pid(pid) do |
| 109 | + Partition.member?(Beacon.Supervisor.partition(scope, group), group, pid) |
| 110 | + end |
| 111 | + |
| 112 | + @doc "Get all local groups in scope" |
| 113 | + @spec local_groups(atom) :: [group] |
| 114 | + def local_groups(scope) when is_atom(scope) do |
| 115 | + Enum.flat_map(Beacon.Supervisor.partitions(scope), fn partition_name -> |
| 116 | + Partition.groups(partition_name) |
| 117 | + end) |
| 118 | + end |
| 119 | + |
| 120 | + @doc "Get local group count in scope" |
| 121 | + @spec local_group_count(atom) :: non_neg_integer |
| 122 | + def local_group_count(scope) when is_atom(scope) do |
| 123 | + Enum.sum_by(Beacon.Supervisor.partitions(scope), fn partition_name -> |
| 124 | + Partition.group_count(partition_name) |
| 125 | + end) |
| 126 | + end |
| 127 | + |
| 128 | + @doc "Get groups in scope" |
| 129 | + @spec groups(atom) :: [group] |
| 130 | + def groups(scope) when is_atom(scope) do |
| 131 | + remote_groups = Scope.groups(scope) |
| 132 | + |
| 133 | + scope |
| 134 | + |> local_groups() |
| 135 | + |> MapSet.new() |
| 136 | + |> MapSet.union(remote_groups) |
| 137 | + |> MapSet.to_list() |
| 138 | + end |
| 139 | + |
| 140 | + @doc "Get group count in scope" |
| 141 | + @spec group_count(atom) :: non_neg_integer |
| 142 | + def group_count(scope) when is_atom(scope) do |
| 143 | + remote_groups = Scope.groups(scope) |
| 144 | + |
| 145 | + scope |
| 146 | + |> local_groups() |
| 147 | + |> MapSet.new() |
| 148 | + |> MapSet.union(remote_groups) |
| 149 | + |> MapSet.size() |
| 150 | + end |
| 151 | +end |
0 commit comments