Skip to content

Commit

Permalink
Extract cluster supervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
sleipnir committed Nov 27, 2023
1 parent 99a4047 commit aec4d91
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 78 deletions.
95 changes: 95 additions & 0 deletions lib/spawn/cluster/cluster_supervisor.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
defmodule Spawn.Cluster.ClusterSupervisor do
@moduledoc false
use Supervisor
require Logger

alias Actors.Config.PersistentTermConfig, as: Config

def start_link(opts) do
Supervisor.start_link(__MODULE__, opts,
name: String.to_atom("#{String.capitalize(Config.get(:app_name))}.Cluster")
)
end

@impl true
def init(opts) do
children = [
cluster_supervisor(opts)
]

Supervisor.init(children, strategy: :one_for_one)
end

defp cluster_supervisor(opts) do
cluster_strategy = Config.get(:proxy_cluster_strategy)

topologies =
case cluster_strategy do
"epmd" ->
get_epmd_strategy(opts)

"gossip" ->
get_gossip_strategy(opts)

"kubernetes-dns" ->
get_k8s_dns_strategy(opts)

_ ->
Logger.warning("Invalid Topology")
end

if topologies && Code.ensure_compiled(Cluster.Supervisor) do
Logger.debug("Cluster topology #{inspect(topologies)}")

{Cluster.Supervisor,
[
topologies,
[name: String.to_atom("#{String.capitalize(Config.get(:app_name))}.${__MODULE__}")]
]}
end
end

defp get_epmd_strategy(_opts) do
[
proxy: [
strategy: Cluster.Strategy.Epmd,
config: [
hosts: [
:"[email protected]",
:"[email protected]",
:"[email protected]",
:"[email protected]",
:"[email protected]",
:"[email protected]",
:"[email protected]"
]
]
]
]
end

defp get_gossip_strategy(_opts) do
[
proxy: [
strategy: Cluster.Strategy.Gossip,
config: [
reuseaddr: Config.get(:proxy_cluster_gossip_reuseaddr_address),
multicast_addr: Config.get(:proxy_cluster_gossip_multicast_address),
broadcast_only: Config.get(:proxy_cluster_gossip_broadcast_only)
]
]
]
end

defp get_k8s_dns_strategy(_opts),
do: [
proxy: [
strategy: Elixir.Cluster.Strategy.Kubernetes.DNS,
config: [
service: Config.get(:proxy_headless_service),
application_name: "spawn",
polling_interval: Config.get(:proxy_cluster_polling_interval)
]
]
]
end
81 changes: 3 additions & 78 deletions lib/spawn/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ defmodule Spawn.Supervisor do

def start_link(opts) do
Supervisor.start_link(__MODULE__, opts,
name: String.to_atom("#{String.capitalize(Config.get(:app_name))}.Cluster"),
name: __MODULE__,
shutdown: @shutdown_timeout_ms
)
end

def child_spec(opts) do
id = String.to_atom("#{String.capitalize(Config.get(:app_name))}.Cluster")

%{
id: id,
id: __MODULE__,
start: {__MODULE__, :start_link, [opts]}
}
end
Expand All @@ -27,7 +25,7 @@ defmodule Spawn.Supervisor do
def init(opts) do
children =
[
cluster_supervisor(opts),
{Spawn.Cluster.ClusterSupervisor, []},
{Spawn.Cache.LookupCache, []},
Spawn.Cluster.StateHandoff.ManagerSupervisor.child_spec(opts),
Spawn.Cluster.Node.Registry.child_spec()
Expand All @@ -53,77 +51,4 @@ defmodule Spawn.Supervisor do
|> List.flatten()
end
end

defp cluster_supervisor(opts) do
cluster_strategy = Config.get(:proxy_cluster_strategy)

topologies =
case cluster_strategy do
"epmd" ->
get_epmd_strategy(opts)

"gossip" ->
get_gossip_strategy(opts)

"kubernetes-dns" ->
get_k8s_dns_strategy(opts)

_ ->
Logger.warning("Invalid Topology")
end

if topologies && Code.ensure_compiled(Cluster.Supervisor) do
Logger.debug("Cluster topology #{inspect(topologies)}")

{Cluster.Supervisor,
[
topologies,
[name: String.to_atom("#{String.capitalize(Config.get(:app_name))}.${__MODULE__}")]
]}
end
end

defp get_epmd_strategy(_opts) do
[
proxy: [
strategy: Cluster.Strategy.Epmd,
config: [
hosts: [
:"[email protected]",
:"[email protected]",
:"[email protected]",
:"[email protected]",
:"[email protected]",
:"[email protected]",
:"[email protected]"
]
]
]
]
end

defp get_gossip_strategy(_opts) do
[
proxy: [
strategy: Cluster.Strategy.Gossip,
config: [
reuseaddr: Config.get(:proxy_cluster_gossip_reuseaddr_address),
multicast_addr: Config.get(:proxy_cluster_gossip_multicast_address),
broadcast_only: Config.get(:proxy_cluster_gossip_broadcast_only)
]
]
]
end

defp get_k8s_dns_strategy(_opts),
do: [
proxy: [
strategy: Elixir.Cluster.Strategy.Kubernetes.DNS,
config: [
service: Config.get(:proxy_headless_service),
application_name: "spawn",
polling_interval: Config.get(:proxy_cluster_polling_interval)
]
]
]
end

0 comments on commit aec4d91

Please sign in to comment.