mgwidmann

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

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.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement