Skip to content

Commit

Permalink
Allow partial nodes/1 impl (not all the cluster)
Browse files Browse the repository at this point in the history
  • Loading branch information
am-kantox committed Jan 17, 2025
1 parent cba9845 commit a88fcb9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
16 changes: 10 additions & 6 deletions lib/finitomata/distributed/cluster_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ defmodule Finitomata.ClusterInfo do
to preserve a determined consistency.
"""

@doc "Returns nodes available to select from"
@callback nodes :: [node()]
@doc "
Returns nodes available to select from for the given `Finitomata` instance.
If the instance is `false`, should return all the nodes for all instances."
@callback nodes(Finitomata.id()) :: [node()]

@doc "Returns the node “selected” for this particular `id`"
@callback whois(id :: term()) :: node() | nil
Expand All @@ -22,12 +25,13 @@ defmodule Finitomata.ClusterInfo do
do: :persistent_term.put(Finitomata.ClusterInfo, %{implementation: impl})

@doc "Delegates to the selected implementation of a cluster lookup"
def nodes(self? \\ false)
def nodes(false), do: impl().nodes()
def nodes(true), do: [node() | nodes(false)]
def nodes(id \\ false, self? \\ false)
def nodes(true, false), do: [node() | nodes(false, false)]
def nodes(id, false), do: impl().nodes(id)
def nodes(id, true), do: [node() | nodes(id, false)]

@doc "Delegates to the selected implementation of a cluster lookup"
def whois(id), do: impl().whois(id)
def whois(fini_id \\ false, id), do: impl().whois(fini_id, id)

defp impl do
Finitomata.ClusterInfo
Expand Down
8 changes: 4 additions & 4 deletions lib/finitomata/distributed/cluster_info/naive.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ defmodule Finitomata.ClusterInfo.Naive do
@behaviour Finitomata.ClusterInfo

@impl Finitomata.ClusterInfo
def nodes, do: Node.list()
def nodes(_fini_id \\ false), do: Node.list()

@impl Finitomata.ClusterInfo
def whois(id) do
{%{next: _next_fun, type: :exsss}, _} = :rand.seed(:exsss, term_to_seed(id))
Enum.random([node() | nodes()])
def whois(fini_id \\ false, id) do
{%{next: _next_fun, type: :exsss}, _} = :rand.seed(:exsss, term_to_seed({fini_id, id}))
Enum.random([node() | nodes(fini_id)])
end

@spec term_to_seed(term(), integer()) :: integer()
Expand Down
19 changes: 13 additions & 6 deletions lib/infinitomata.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ defmodule Infinitomata do
@behaviour Finitomata.ClusterInfo
@impl Finitomata.ClusterInfo
def nodes, do: HashRing.nodes(@ring) -- [node()]
def nodes(_fini_id), do: HashRing.nodes(@ring) -- [node()]
@impl Finitomata.ClusterInfo
def whois(id), do: HashRing.key_to_node(@ring, id)
def whois(_fini_id, id), do: HashRing.key_to_node(@ring, id)
end
```
"""
Expand Down Expand Up @@ -115,7 +115,14 @@ defmodule Infinitomata do

@doc since: "0.19.0"
@doc "Synchronizes the local `Infinitomata` instance with the cluster"
def synch(id \\ nil, nodes \\ ClusterInfo.nodes()) do
@spec synch(Finitomata.id(), [node()] | false) :: :ok
def synch(id \\ nil, nodes \\ false)

def synch(id, false) do
InfSup.synch(id, ClusterInfo.nodes(id), FinSup.infinitomata_name(id))
end

def synch(id, nodes) when is_list(nodes) do
InfSup.synch(id, nodes, FinSup.infinitomata_name(id))
end

Expand All @@ -126,12 +133,12 @@ defmodule Infinitomata do
@impl Finitomata.Supervisor
@spec start_fsm(Finitomata.id(), Finitomata.fsm_name(), module(), any()) ::
DynamicSupervisor.on_start_child()
def start_fsm(id \\ nil, target, implementation, payload) do
id = FinSup.infinitomata_name(id)
def start_fsm(fini_id \\ nil, target, implementation, payload) do
id = FinSup.infinitomata_name(fini_id)

case InfSup.get(id, target) do
nil ->
node = ClusterInfo.whois({id, target})
node = ClusterInfo.whois(fini_id, {id, target})
do_start_fsm(node == node(), node, id, target, implementation, payload)

%{node: node, pid: pid} ->
Expand Down

0 comments on commit a88fcb9

Please sign in to comment.