Skip to content

Commit 518c0a1

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 5247d6e commit 518c0a1

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
@@ -79,10 +79,36 @@ defmodule Edgehog.Devices.Reconciler do
7979
{:noreply, state}
8080
end
8181

82+
@impl GenServer
83+
def handle_info({:EXIT, _pid, {:name_conflict, {_name, _value}, _registry, _winning_pid}}, state) do
84+
_ =
85+
Logger.warning(
86+
"Received a :name_conflict signal from the outer space, maybe a netsplit occurred? Gracefully shutting down.",
87+
tag: "RPC exit"
88+
)
89+
90+
{:stop, :shutdown, state}
91+
end
92+
93+
@impl GenServer
94+
def handle_info({:EXIT, _pid, :shutdown}, state) do
95+
_ =
96+
Logger.warning(
97+
"Received a :shutdown signal from the outer space, maybe the supervisor is mad? Gracefully shutting down.",
98+
tag: "RPC exit"
99+
)
100+
101+
{:stop, :shutdown, state}
102+
end
103+
82104
defp spawn_reconciliation_task(tenant) do
83105
Task.Supervisor.async(Reconciler.Supervisor, fn ->
84106
Logger.info("Reconciling tenant #{tenant.slug}")
85107
Reconciler.Core.reconcile(tenant)
86108
end)
87109
end
110+
111+
defp name do
112+
{:via, Horde.Registry, {Edgehog.Devices.Reconciler.Registry, :auto}}
113+
end
88114
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)