Skip to content

Commit 13cf0b7

Browse files
committed
chore(backend): move services to horde
Moves relevant registries and supervisors in the application tree to allow edgehog to scale horizontally. This is done only with registries and supervsors where it makes sens to chare the load: - `Containers.Reconciler`, where the process spawns per-tenant and talks with the DB. - `Tenant.Reconciler`, where tasks and processes are again spawned per-tenant. - `Devices.Reconciler` is moved to a single process managed trough a horde process to make it single per-cluster. Signed-off-by: Luca Zaninotto <luca.zaninotto@secomind.com>
1 parent 7536ff3 commit 13cf0b7

File tree

4 files changed

+31
-4
lines changed

4 files changed

+31
-4
lines changed

backend/lib/edgehog/application.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ defmodule Edgehog.Application do
6767
# Start the Tenant Reconciler Supervisor
6868
{Edgehog.Tenants.Reconciler.Supervisor, tenant_to_trigger_url_fun: tenant_to_trigger_url_fun},
6969
# Start Containers reconciler
70-
{Registry, keys: :unique, name: Edgehog.Containers.Reconciler.Registry},
70+
{Horde.Registry, keys: :unique, name: Edgehog.Containers.Reconciler.Registry},
71+
{Horde.Registry, keys: :unique, name: Edgehog.Devices.Reconciler.Registry},
7172
# Start the Endpoint (http/https)
7273
Endpoint,
7374
# Fetch Astarte devices

backend/lib/edgehog/containers/reconciler/reconciler.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ defmodule Edgehog.Containers.Reconciler do
144144
end
145145

146146
defp name(tenant) do
147-
{:via, Registry, {Edgehog.Containers.Reconciler.Registry, tenant.tenant_id}}
147+
{:via, Horde.Registry, {Edgehog.Containers.Reconciler.Registry, tenant.tenant_id}}
148148
end
149149

150150
defp do_register_device(device, state) do

backend/lib/edgehog/devices/reconciler/reconciler.ex

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ defmodule Edgehog.Devices.Reconciler do
2828
require Logger
2929

3030
def start_link(opts \\ []) do
31-
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
31+
GenServer.start_link(__MODULE__, opts, name: name())
3232
end
3333

3434
@impl GenServer
@@ -67,10 +67,36 @@ defmodule Edgehog.Devices.Reconciler do
6767
{:noreply, state}
6868
end
6969

70+
@impl GenServer
71+
def handle_info({:EXIT, _pid, {:name_conflict, {_name, _value}, _registry, _winning_pid}}, state) do
72+
_ =
73+
Logger.warning(
74+
"Received a :name_conflict signal from the outer space, maybe a netsplit occurred? Gracefully shutting down.",
75+
tag: "RPC exit"
76+
)
77+
78+
{:stop, :shutdown, state}
79+
end
80+
81+
@impl GenServer
82+
def handle_info({:EXIT, _pid, :shutdown}, state) do
83+
_ =
84+
Logger.warning(
85+
"Received a :shutdown signal from the outer space, maybe the supervisor is mad? Gracefully shutting down.",
86+
tag: "RPC exit"
87+
)
88+
89+
{:stop, :shutdown, state}
90+
end
91+
7092
defp spawn_reconciliation_task(tenant) do
7193
Task.Supervisor.async(Reconciler.Supervisor, fn ->
7294
Logger.info("Reconciling tenant #{tenant.slug}")
7395
Reconciler.Core.reconcile(tenant)
7496
end)
7597
end
98+
99+
defp name do
100+
{:via, Horde.Registry, {Edgehog.Devices.Reconciler.Registry, :auto}}
101+
end
76102
end

backend/test/edgehog_web/controllers/astarte_trigger_controller_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ defmodule EdgehogWeb.Controllers.AstarteTriggerControllerTest do
313313
timestamp = utc_now_second()
314314
event = disconnection_trigger(device.device_id, timestamp)
315315

316-
Registry.register(Edgehog.Containers.Reconciler.Registry, tenant.tenant_id, self())
316+
Horde.Registry.register(Edgehog.Containers.Reconciler.Registry, tenant.tenant_id, self())
317317

318318
stub(DeviceStatusMock, :get, fn _client, _device_id -> flunk() end)
319319
assert conn |> post(path, event) |> response(200)

0 commit comments

Comments
 (0)