Skip to content

Commit

Permalink
Remove an extra Deployment preload during after_join (#1618)
Browse files Browse the repository at this point in the history
- Preload the deployment, along with its firmware and archive, at the start of `after_join`
- Preload all the required bits again in `Deployments.set_deployment`
- And only use `:force` if the deployment has changed.
- This approach saves 100ms during `after_join`, which is over 15% of the total time.
  • Loading branch information
joshk authored Oct 29, 2024
1 parent a46a73e commit e7aeaf9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
19 changes: 10 additions & 9 deletions lib/nerves_hub/deployments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule NervesHub.Deployments do
alias NervesHub.AuditLogs
alias NervesHub.Deployments.Deployment
alias NervesHub.Deployments.InflightDeploymentCheck
alias NervesHub.Devices
alias NervesHub.Devices.Device
alias NervesHub.Products.Product
alias NervesHub.Repo
Expand Down Expand Up @@ -498,25 +499,25 @@ defmodule NervesHub.Deployments do

[deployment] ->
device
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:deployment_id, deployment.id)
|> Repo.update!()
|> Repo.preload([:deployment])
|> Devices.update_deployment(deployment)
|> preload_with_firmware_and_archive(true)

[deployment | _] ->
Logger.debug(
"More than one deployment matches for #{device.identifier}, setting to the first"
)

device
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:deployment_id, deployment.id)
|> Repo.update!()
|> Repo.preload([:deployment])
|> Devices.update_deployment(deployment)
|> preload_with_firmware_and_archive(true)
end
end

def set_deployment(device) do
Repo.preload(device, [:deployment])
preload_with_firmware_and_archive(device)
end

def preload_with_firmware_and_archive(device, force \\ false) do
Repo.preload(device, [deployment: [:archive, :firmware]], force: force)
end
end
7 changes: 7 additions & 0 deletions lib/nerves_hub/devices.ex
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,13 @@ defmodule NervesHub.Devices do

def matches_deployment?(_, _), do: false

def update_deployment(device, deployment) do
device
|> Ecto.Changeset.change()
|> Ecto.Changeset.put_change(:deployment_id, deployment.id)
|> Repo.update!()
end

@spec failure_threshold_met?(Device.t(), Deployment.t()) :: boolean()
def failure_threshold_met?(%Device{} = device, %Deployment{} = deployment) do
Enum.count(device.update_attempts) >= deployment.device_failure_threshold
Expand Down
13 changes: 8 additions & 5 deletions lib/nerves_hub_web/channels/device_channel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ defmodule NervesHubWeb.DeviceChannel do
end

def handle_info({:after_join, params}, %{assigns: %{device: device}} = socket) do
device =
device
|> Devices.verify_deployment()
|> Deployments.set_deployment()
|> deployment_preload()
device = maybe_update_deployment(device)

maybe_send_public_keys(device, socket, params)

Expand Down Expand Up @@ -476,6 +472,13 @@ defmodule NervesHubWeb.DeviceChannel do
:ok
end

defp maybe_update_deployment(device) do
device
|> Deployments.preload_with_firmware_and_archive()
|> Devices.verify_deployment()
|> Deployments.set_deployment()
end

defp log_to_sentry(device, message, extra \\ %{}) do
Sentry.Context.set_tags_context(%{
device_identifier: device.identifier,
Expand Down

0 comments on commit e7aeaf9

Please sign in to comment.