From fa827eb794160a97a44b20ec62eb0cdacfdb421a Mon Sep 17 00:00:00 2001 From: Carmine Di Monaco Date: Thu, 22 Jun 2023 10:50:14 +0200 Subject: [PATCH] Allow rollup on deregistered aggregates (#1551) --- lib/trento/domain/cluster/cluster.ex | 20 +++++++++---------- lib/trento/domain/host/host.ex | 20 +++++++++---------- lib/trento/domain/sap_system/sap_system.ex | 16 +++++++-------- test/trento/domain/cluster/cluster_test.exs | 10 +++++++++- test/trento/domain/host/host_test.exs | 10 +++++++++- .../domain/sap_system/sap_system_test.exs | 15 ++------------ 6 files changed, 48 insertions(+), 43 deletions(-) diff --git a/lib/trento/domain/cluster/cluster.ex b/lib/trento/domain/cluster/cluster.ex index 9bbc561eda..64f46c3f00 100644 --- a/lib/trento/domain/cluster/cluster.ex +++ b/lib/trento/domain/cluster/cluster.ex @@ -247,6 +247,16 @@ defmodule Trento.Domain.Cluster do |> maybe_update_cluster(command) end + def execute( + %Cluster{cluster_id: cluster_id} = snapshot, + %RollUpCluster{} + ) do + %ClusterRollUpRequested{ + cluster_id: cluster_id, + snapshot: snapshot + } + end + def execute(%Cluster{deregistered_at: deregistered_at}, _) when not is_nil(deregistered_at), do: {:error, :cluster_not_registered} @@ -311,16 +321,6 @@ defmodule Trento.Domain.Cluster do |> Multi.execute(&maybe_emit_cluster_health_changed_event/1) end - def execute( - %Cluster{cluster_id: cluster_id} = snapshot, - %RollUpCluster{} - ) do - %ClusterRollUpRequested{ - cluster_id: cluster_id, - snapshot: snapshot - } - end - def execute( %Cluster{cluster_id: cluster_id} = cluster, %DeregisterClusterHost{ diff --git a/lib/trento/domain/host/host.ex b/lib/trento/domain/host/host.ex index b83d2286c6..d50b3cd08e 100644 --- a/lib/trento/domain/host/host.ex +++ b/lib/trento/domain/host/host.ex @@ -197,6 +197,16 @@ defmodule Trento.Domain.Host do ] end + def execute( + %Host{host_id: host_id} = snapshot, + %RollUpHost{} + ) do + %HostRollUpRequested{ + host_id: host_id, + snapshot: snapshot + } + end + def execute( %Host{deregistered_at: deregistered_at}, _ @@ -315,16 +325,6 @@ defmodule Trento.Domain.Host do } end - def execute( - %Host{host_id: host_id} = snapshot, - %RollUpHost{} - ) do - %HostRollUpRequested{ - host_id: host_id, - snapshot: snapshot - } - end - def execute( %Host{host_id: host_id}, %RequestHostDeregistration{requested_at: requested_at} diff --git a/lib/trento/domain/sap_system/sap_system.ex b/lib/trento/domain/sap_system/sap_system.ex index e856d935a8..dcc81cf94f 100644 --- a/lib/trento/domain/sap_system/sap_system.ex +++ b/lib/trento/domain/sap_system/sap_system.ex @@ -286,14 +286,6 @@ defmodule Trento.Domain.SapSystem do |> Multi.execute(&maybe_emit_sap_system_tombstoned_event/1) end - def execute( - %SapSystem{deregistered_at: deregistered_at}, - _ - ) - when not is_nil(deregistered_at) do - {:error, :sap_system_not_registered} - end - def execute( %SapSystem{sap_system_id: sap_system_id} = snapshot, %RollUpSapSystem{} @@ -304,6 +296,14 @@ defmodule Trento.Domain.SapSystem do } end + def execute( + %SapSystem{deregistered_at: deregistered_at}, + _ + ) + when not is_nil(deregistered_at) do + {:error, :sap_system_not_registered} + end + def apply( %SapSystem{sap_system_id: nil}, %DatabaseRegistered{ diff --git a/test/trento/domain/cluster/cluster_test.exs b/test/trento/domain/cluster/cluster_test.exs index 5e51ba6d85..9a58b70189 100644 --- a/test/trento/domain/cluster/cluster_test.exs +++ b/test/trento/domain/cluster/cluster_test.exs @@ -914,7 +914,6 @@ defmodule Trento.ClusterTest do commands_to_reject = [ %CompleteChecksExecution{}, %DeregisterClusterHost{}, - %RollUpCluster{}, %SelectChecks{}, %RegisterClusterHost{} ] @@ -923,6 +922,15 @@ defmodule Trento.ClusterTest do assert match?({:error, :cluster_not_registered}, aggregate_run(initial_events, command)), "Command #{inspect(command)} should be rejected by the aggregate" end + + commands_to_accept = [ + %RollUpCluster{} + ] + + for command <- commands_to_accept do + assert match?({:ok, _, _}, aggregate_run(initial_events, command)), + "Command #{inspect(command)} should be accepted by a deregistered cluster" + end end test "should emit the HostRemovedFromCluster event after a DeregisterClusterHost command and remove the host from the cluster aggregate state" do diff --git a/test/trento/domain/host/host_test.exs b/test/trento/domain/host/host_test.exs index 9106bf937e..c5e7e5ce56 100644 --- a/test/trento/domain/host/host_test.exs +++ b/test/trento/domain/host/host_test.exs @@ -744,7 +744,6 @@ defmodule Trento.HostTest do commands_to_reject = [ %DeregisterHost{}, %RequestHostDeregistration{}, - %RollUpHost{}, %UpdateHeartbeat{}, %UpdateProvider{}, %UpdateSlesSubscriptions{} @@ -753,6 +752,15 @@ defmodule Trento.HostTest do for command <- commands_to_reject do assert_error(initial_events, command, {:error, :host_not_registered}) end + + commands_to_accept = [ + %RollUpHost{} + ] + + for command <- commands_to_accept do + assert match?({:ok, _, _}, aggregate_run(initial_events, command)), + "Command #{inspect(command)} should be accepted by a deregistered host" + end end test "should emit the HostDeregistered and HostTombstoned events" do diff --git a/test/trento/domain/sap_system/sap_system_test.exs b/test/trento/domain/sap_system/sap_system_test.exs index 49890ae23e..23c5218e27 100644 --- a/test/trento/domain/sap_system/sap_system_test.exs +++ b/test/trento/domain/sap_system/sap_system_test.exs @@ -1899,25 +1899,14 @@ defmodule Trento.SapSystemTest do build(:register_database_instance_command), build(:register_application_instance_command), build(:deregister_database_instance_command, sap_system_id: sap_system_id), - build(:deregister_application_instance_command, sap_system_id: sap_system_id) + build(:deregister_application_instance_command, sap_system_id: sap_system_id), + build(:rollup_sap_system_command) ] for command <- commands_to_accept do assert match?({:ok, _, _}, aggregate_run(initial_events, command)), "Command #{inspect(command)} should be accepted by a deregistered SAP system" end - - commands_to_reject = [ - build(:rollup_sap_system_command) - ] - - for command <- commands_to_reject do - assert match?( - {:error, :sap_system_not_registered}, - aggregate_run(initial_events, command) - ), - "Command #{inspect(command)} should be rejected by a deregistered SAP system" - end end test "should deregister a Database and SAP system when the Primary database instance is removed" do