-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #816 from Annopaolo/delete-device
Support device deletion
- Loading branch information
Showing
45 changed files
with
2,812 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...arte_data_updater_plant/lib/astarte_data_updater_plant/data_updater/deletion_scheduler.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# | ||
# This file is part of Astarte. | ||
# | ||
# Copyright 2023 SECO Mind Srl | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
defmodule Astarte.DataUpdater.DeletionScheduler do | ||
@moduledoc """ | ||
This module sends messages to start deletion to a | ||
Astarte.DataUpdater.Server. When a deletion notice | ||
is received, the Server will start the device deletion | ||
procedure, write the dup_start_ack to db, and | ||
synchronously acknowledge it to the Scheduler. | ||
""" | ||
use GenServer | ||
|
||
alias Astarte.DataUpdaterPlant.DataUpdater.Queries | ||
alias Astarte.DataUpdaterPlant.DataUpdater | ||
alias Astarte.DataUpdaterPlant.Config | ||
alias Astarte.Core.Device | ||
|
||
require Logger | ||
|
||
# TODO expose this via config | ||
@reconciliation_timeout :timer.minutes(5) | ||
def start_link(_args) do | ||
GenServer.start_link(__MODULE__, [], name: __MODULE__) | ||
end | ||
|
||
def init(_args) do | ||
# TODO: manually start_device_deletion!() when needed | ||
schedule_next_device_deletion() | ||
{:ok, %{}} | ||
end | ||
|
||
def handle_info(:delete_devices, state) do | ||
_ = Logger.debug("Reconciling devices for whom deletion shall begin") | ||
|
||
start_device_deletion!() | ||
schedule_next_device_deletion() | ||
{:noreply, state} | ||
end | ||
|
||
defp start_device_deletion! do | ||
retrieve_devices_to_delete!() | ||
|> Enum.each(fn %{realm_name: realm_name, encoded_device_id: encoded_device_id} -> | ||
timestamp = now_us_x10_timestamp() | ||
# This must be a call, as we want to be sure this was completed | ||
:ok = DataUpdater.start_device_deletion(realm_name, encoded_device_id, timestamp) | ||
end) | ||
end | ||
|
||
defp schedule_next_device_deletion do | ||
Process.send_after(self(), :delete_devices, @reconciliation_timeout) | ||
end | ||
|
||
defp retrieve_devices_to_delete! do | ||
realms = Queries.retrieve_realms!() | ||
|
||
for %{"realm_name" => realm_name} <- realms, | ||
%{"device_id" => device_id} <- | ||
Queries.retrieve_devices_waiting_to_start_deletion!(realm_name), | ||
encoded_device_id = Device.encode_device_id(device_id), | ||
should_handle_data_from_device?(realm_name, encoded_device_id) do | ||
_ = | ||
Logger.debug("Retrieved device to delete", | ||
tag: "device_to_delete", | ||
realm_name: realm_name, | ||
device_id: encoded_device_id | ||
) | ||
|
||
%{realm_name: realm_name, encoded_device_id: encoded_device_id} | ||
end | ||
end | ||
|
||
defp should_handle_data_from_device?(realm_name, encoded_device_id) do | ||
# TODO extract a function from Astarte.DataUpdaterPlant.AMQPDataConsumer | ||
# This is the same sharding algorithm used in astarte_vmq_plugin | ||
# Make sure they stay in sync | ||
queue_index = | ||
{realm_name, encoded_device_id} | ||
|> :erlang.phash2(Config.data_queue_total_count!()) | ||
|
||
queue_index >= Config.data_queue_range_start!() and | ||
queue_index <= Config.data_queue_range_end!() | ||
end | ||
|
||
# TODO this is copied from astarte_vmq_plugin | ||
defp now_us_x10_timestamp do | ||
DateTime.utc_now() | ||
|> DateTime.to_unix(:microsecond) | ||
|> Kernel.*(10) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.