mgwidmann
Horde Node Down DynamicSupervisor Not Restarting Child on Another Node
I need to have a cluster wide monitor which will produce an output (hard coded) if a certain set of processes goes down at any time. I am using Horde’s DynamicSupervisor to start this child process on whichever node boots first like so in my application tree:
Code has the main application name changed to MyApp to remove my project’s name.
Supervisor.start_link(
[
{Cluster.Supervisor, [Application.get_env(:libcluster, :topologies), [name: MyApp.ClusterSupervisor]]},
{Horde.Registry, name: MyApp.Registry, keys: :unique, members: :auto},
{Task.Supervisor, name: MyApp.TaskSupervisor, strategy: :one_for_one},
{Horde.DynamicSupervisor, name: MyApp.ClusterWideSupervisor, strategy: :one_for_one, distribution_strategy: Horde.UniformRandomDistribution}
|
children ++ [ # At the end of the tree
%{
id: MyApp.StartCrashResponder,
start: {Task, :start_link, [__MODULE__, :start_crash_responder, []]},
restart: :temporary, # The task start_link is temporary, not my dynamic process
type: :worker
}
]
],
opts
)
Then that function which the task runs to start up the dynamic child:
def start_crash_responder() do
result = Horde.DynamicSupervisor.start_child(
MyApp.ClusterWideSupervisor,
%{
id: "crash-responder", # Hard coded so theres only ever one
start: {MyApp.Monitoring.CrashResponder, :start_link, [@monitored_processes]}, # Their registered names (which is their module names)
restart: :permanent # This should cause it to be restarted on hard or graceful node down
}
)
case result do
{:ok, _pid} ->
nil
{:error, {:already_started, pid}} ->
Logger.info("CrashResponder already online: #{inspect(pid)}")
MyApp.Monitoring.CrashResponder.update()
{:error, reason} ->
Logger.error("Error starting CrashResponder: #{inspect(reason)}")
end
result
end
The CrashResponder is a GenServer that receives a list of names in its start_link and in its handle_continue callback will look them up in the registry and call Process.monitor on each. It then handles the down messages and will produce an output if one goes down to alert the user the system is unstable and may not have processed something and may require trying again. If a node goes offline, the CrashResponder should be located on another node somewhere in the cluster. Its not a big deal if a duplicate is online for a short period of time or even produces a double output on rare occasions, just so long as at least 1 is running somewhere in the cluster.
When I boot two processes locally on my machine, one CrashResponder comes online and both nodes recognize that its a singleton:
Node A:
11:44:20.944 service=CrashResponder [info] Online! #PID<0.259.0>
11:44:20.944 service=CrashResponder [info] Continue completed!
Node B:
11:44:24.804 [info] CrashResponder already online: #PID<27461.259.0>
Then when I do :init.stop() or CTRL-C twice (doesn’t matter), the node goes down but node B does not restart the CrashResponder. When I look it up in the registry, its no longer registered. I can’t figure out what I’ve done incorrectly. Any ideas where to look?
Marked As Solved
mgwidmann
Ok I fixed it but I’m not sure why it was necessary, and I don’t see anything mentioning needing to do this in the docs as far as I have come across.
Adding this to my supervision tree after the DynamicSupervisor fixed the problem:
{Horde.NodeListener, MyApp.ClusterWideSupervisor}
This ensures that the DynamicSupervisor is updated with node add and remove events.
Digging through the code, the real issue seems to be that the members option defaults to [] instead of :auto whereas I had the Horde.Registry already set to :auto (which also has the same default). Using memers: :auto fixes the problem, I was not aware it was needed in both places.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









