mgwidmann

mgwidmann

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?

First 2 of 2 Posts Switch mode

mgwidmann

mgwidmann OP

I found this but I am not sure how to fix it, it seems to indicate the dynamic supervisor is not aware of the other node:

iex(nodea@127.0.0.1)4> Horde.Cluster.members(MyApp.Registry)
[
  {MyApp.Registry, :"nodeb@127.0.0.1"},
  {MyApp.Registry, :"nodea@127.0.0.1"}
]
iex(nodea@127.0.0.1)5> Horde.Cluster.members(MyApp.ClusterWideSupervisor)
[{MyApp.ClusterWideSupervisor, :"nodea@127.0.0.1"}]
mgwidmann

mgwidmann OP

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement