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?

Showing Posts 1 to 2

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

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
ryanwinchester
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted” Version...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews