Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cluster deregistered side effects #1280

Merged
merged 11 commits into from
Apr 5, 2023

Conversation

CDimonaco
Copy link
Member

@CDimonaco CDimonaco commented Mar 23, 2023

Description

ClusterReadModel soft delete side effects.

We should exclude from the query in which is involved the ClusterReadModel, the soft-deleted entities.

This Schema is not involved in any association.

How was this tested?

Automated tests

@CDimonaco CDimonaco added the elixir Pull requests that update Elixir code label Mar 23, 2023
@CDimonaco CDimonaco self-assigned this Mar 23, 2023
@CDimonaco CDimonaco changed the base branch from main to deregistration March 23, 2023 17:05
@CDimonaco CDimonaco force-pushed the cluster_deregistered_side_effects branch from 2a619ca to 26d4bf7 Compare April 3, 2023 14:00
@CDimonaco CDimonaco marked this pull request as ready for review April 3, 2023 14:16
Copy link
Contributor

@arbulu89 arbulu89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I miss some more thorough test for the clusters use case honestly. For example, i think that the request_clusters_checks_execution condition is wrong, so the test would be helpful there.

Besides that, I think the HostRemovedFromCluster in the hosts projection is missing.
There, we should put the cluster_id of the host to nil.
I know that this cannot happen when you run the HostDeregistrationRequest, but if you deregister the cluster from the cli (or in the future with the delta deregistration), this scenario must be considered.

@CDimonaco
Copy link
Member Author

@arbulu89 I addressed the feedbacks but I don't really understand what do you mean here

Besides that, I think the HostRemovedFromCluster in the hosts projection is missing.
There, we should put the cluster_id of the host to nil.
I know that this cannot happen when you run the HostDeregistrationRequest, but if you deregister the cluster from the cli (or in the future with the delta deregistration), this scenario must be considered.

Copy link
Member

@fabriziosestito fabriziosestito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall great work! Some nitpick and I agree with @arbulu89 comment about the HostRemovedFromCluster projection.

@arbulu89
Copy link
Contributor

arbulu89 commented Apr 4, 2023

@arbulu89 I addressed the feedbacks but I don't really understand what do you mean here

Besides that, I think the HostRemovedFromCluster in the hosts projection is missing.
There, we should put the cluster_id of the host to nil.
I know that this cannot happen when you run the HostDeregistrationRequest, but if you deregister the cluster from the cli (or in the future with the delta deregistration), this scenario must be considered.

In one of the latest #1274 we have included the HostRemovedFromCluster event. This event happens when a host is removed from the cluster, that can lead to a total cluster deregistration, if it was the last host in the cluster. To host/cluster relationship in the frontend read-models is done with the cluster_id field in the host read model. So as far as we have removed the host, we should put this field to nil, so it reflects the real state of the host.

This is important if we have a standalone cluster de-registration (without de-registering the host itself).

At the end we need to revert what we do here: https://github.com/trento-project/web/blob/deregistration/lib/trento/application/projectors/host_projector.ex#L66

Copy link
Contributor

@rtorrero rtorrero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides from what's already been commented, it looks good @CDimonaco 👍

@CDimonaco
Copy link
Member Author

@arbulu89 @fabriziosestito Done, please review again

Copy link
Contributor

@arbulu89 arbulu89 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great!
I proposed other alternative, but it should do the same (as the test passes hehe)

},
fn multi ->
query =
Ecto.Query.from(h in HostReadModel,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this looks better:

fn multi ->
  changeset =
    HostReadModel.changeset(%HostReadModel{id: id, cluster_id: ""}, %{
      cluster_id: nil
    })

    Ecto.Multi.update(multi, :host, changeset)
  end

At the end you simply want to put the cluster_id to nil for this host

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes but no, I mean we don't know the order of the events, and we want to make sure to not update anything that is already soft delete, to mantain consistency and logic.

This guard will just make that happen. Right know I know that this could potentially be a no-op, but I don't really like the idea to pollute deleted entities :D

Anyway if you like it we can merge.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really think this could ever happen. You cannot de-register a host without de-registering it first from a cluster (and in any case, what you "say" about polluting would be reflecting the source of truth in that strange scenario XD)
I guess that the cost of doing an extra query is low.
Anyway, you already had my green light hehe

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arbulu89 @CDimonaco I think it's my fault on this one.
We cannot guarantee the order of the events, since HostRemovedFromCluster is emitted by the Cluster aggregate and might arrive after the HostDeregistered event which is emitted concurrently by the Host aggregate. I was confused and thought we were deleting the entry for good, therefore my suggestion was to use update_all.

I still prefer Xabi's solution since the host is just soft deleted so the read model can be found. It doesn't matter if we update a soft deleted read model, actually, we must update it to have a consistent entry if we then plan to roll back the deregistration.

@@ -129,6 +130,27 @@ defmodule Trento.HostProjectorTest do
refute_broadcast "host_details_updated", %{id: ^host_id, cluster_id: ^cluster_id}, 1000
end

test "should project a host without the cluster when HostRemovedFromCluster event is received and the host is not deregistered yet" do
insert(:cluster, id: cluster_id = Faker.UUID.v4())
Copy link
Contributor

@arbulu89 arbulu89 Apr 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need to insert this cluster

@CDimonaco
Copy link
Member Author

CDimonaco commented Apr 5, 2023 via email

@CDimonaco CDimonaco merged commit b768b8a into deregistration Apr 5, 2023
@CDimonaco CDimonaco deleted the cluster_deregistered_side_effects branch April 5, 2023 09:50
CDimonaco added a commit that referenced this pull request Apr 17, 2023
* Add deregistered_at field to Cluster read model

* ClusterDeregistered event projection

* Wip on clusters module, get clusters that are not soft deleted

* Clusters entrypoint tests

* Test broadcast of cluster deregistration in cluster projector

* Removed first from request_check_execution query

* Fix query parenthesis in request_clusters_checks_execution

* get_all_clusters, tested for deregistered and soft deleted clusters

* Handle HostRemovedFromCluster in host projection

* Cluster projector reorder of project clauses

* Host projector set the cluster to null when HostRemovedFromCluster
CDimonaco added a commit that referenced this pull request Apr 17, 2023
* Add deregistered_at field to Cluster read model

* ClusterDeregistered event projection

* Wip on clusters module, get clusters that are not soft deleted

* Clusters entrypoint tests

* Test broadcast of cluster deregistration in cluster projector

* Removed first from request_check_execution query

* Fix query parenthesis in request_clusters_checks_execution

* get_all_clusters, tested for deregistered and soft deleted clusters

* Handle HostRemovedFromCluster in host projection

* Cluster projector reorder of project clauses

* Host projector set the cluster to null when HostRemovedFromCluster
CDimonaco added a commit that referenced this pull request Apr 17, 2023
* Add deregistered_at field to Cluster read model

* ClusterDeregistered event projection

* Wip on clusters module, get clusters that are not soft deleted

* Clusters entrypoint tests

* Test broadcast of cluster deregistration in cluster projector

* Removed first from request_check_execution query

* Fix query parenthesis in request_clusters_checks_execution

* get_all_clusters, tested for deregistered and soft deleted clusters

* Handle HostRemovedFromCluster in host projection

* Cluster projector reorder of project clauses

* Host projector set the cluster to null when HostRemovedFromCluster
rtorrero pushed a commit that referenced this pull request Jun 16, 2023
* Add deregistered_at field to Cluster read model

* ClusterDeregistered event projection

* Wip on clusters module, get clusters that are not soft deleted

* Clusters entrypoint tests

* Test broadcast of cluster deregistration in cluster projector

* Removed first from request_check_execution query

* Fix query parenthesis in request_clusters_checks_execution

* get_all_clusters, tested for deregistered and soft deleted clusters

* Handle HostRemovedFromCluster in host projection

* Cluster projector reorder of project clauses

* Host projector set the cluster to null when HostRemovedFromCluster
rtorrero pushed a commit that referenced this pull request Jun 16, 2023
* Add deregistered_at field to Cluster read model

* ClusterDeregistered event projection

* Wip on clusters module, get clusters that are not soft deleted

* Clusters entrypoint tests

* Test broadcast of cluster deregistration in cluster projector

* Removed first from request_check_execution query

* Fix query parenthesis in request_clusters_checks_execution

* get_all_clusters, tested for deregistered and soft deleted clusters

* Handle HostRemovedFromCluster in host projection

* Cluster projector reorder of project clauses

* Host projector set the cluster to null when HostRemovedFromCluster
arbulu89 added a commit that referenced this pull request Jun 29, 2023
* Host deregistration events (#1245)

* Host deregistration commands

* Host deregistration events

* Fixing module attributes in host deregistration commands

* Fix deregistration commands namespace

* Deregistration side effects (#1252)

* Add deregistered_at to read model

Co-authored-by: Xabier Arbulu Insausti <xarbulu@suse.com>
Co-authored-by: Jamie Rodríguez <jamie.rodriguez@suse.com>

* Handle side effects from HostDeregistered event

Co-authored-by: Xabier Arbulu Insausti <xarbulu@suse.com>
Co-authored-by: Jamie Rodríguez <jamie.rodriguez@suse.com>

* Filter deregistered hosts from get_all_hosts

Co-authored-by: Xabier Arbulu Insausti <xarbulu@suse.com>
Co-authored-by: Jamie Rodríguez <jamie.rodriguez@suse.com>

* Fix tests

Co-authored-by: Xabier Arbulu Insausti <xarbulu@suse.com>
Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>

* Add test for filtering unregistered hosts

Co-authored-by: Xabier Arbulu Insausti <xarbulu@suse.com>
Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>

* Host Deregistration process manager and aggregate handling (#1249)

* Host aggregate handling of deregistration event

* Wip Deregistration process manager

* Process Managers supervisor

* Add Deregistration process manager test

* Fix credo and mispells

* Squashed Process manager state into process manager

* Routing deregistration commands to Host aggregate

* Fix deregistration process manager state with deftype

* Format router file

* Deregistration timestamp carried from Deregistration requested command

* Fix spell in process manager docstring

* Fix typo in test description

* Cluster and cluster host deregistration events/commands (#1274)

* Cluster and cluster host deregistration events/commands

* Address comments

* Remove leading space

* Fix Hosts query without the soft deleted hosts exclusion (#1282)

* Cluster registration also for nodes not DC (#1275)

* Cluster registration process happens also when the discovered node is
not a DC

* Address docs and testing review feedbacks

* Removed useless tests from cluster_tests

* Add host to cluster when a message from a dc host arrives for a cluster

Co-Authored-By: Fabrizio Sestito <fabrizio.sestito@suse.com>

* Add register cluster host test when the host is and is not a DC

---------

Co-authored-by: Fabrizio Sestito <fabrizio.sestito@suse.com>

* Cluster deregistration process manager & aggregate changes (#1278)

* Make Process Manager aware of cluster deregistration

Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>

* Handle cluster host deregistration in the cluster aggregate

Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>

* Route the DeregisterClusterHost to the cluster aggregate


---------

Co-authored-by: Jamie Rodriguez <jamie.rodriguez@suse.com>

* Cluster deregistered side effects (#1280)

* Add deregistered_at field to Cluster read model

* ClusterDeregistered event projection

* Wip on clusters module, get clusters that are not soft deleted

* Clusters entrypoint tests

* Test broadcast of cluster deregistration in cluster projector

* Removed first from request_check_execution query

* Fix query parenthesis in request_clusters_checks_execution

* get_all_clusters, tested for deregistered and soft deleted clusters

* Handle HostRemovedFromCluster in host projection

* Cluster projector reorder of project clauses

* Host projector set the cluster to null when HostRemovedFromCluster

* Correct typos (#1315)

* Database registration changes (#1326)

* Database sap system registration change

The database istance is register only if the sr is disabled or with sr
enabled but only primary instances

* Sap systems moduledoc updated

* Changed error when registering a non primary database for a sap system

* Updated moduledoc of sap system domain

* Refuse database registration changes

The registration is refused if the sap system does not exists
and the database has a secondary role

* Fix sap system domain tests

* Fix typos

* Format

* Add SAP System deregistration commands & events (#1314)



Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>

* Fix Deregistration process manager state validation

* Add application instance deregistration logic (#1357)



Co-authored-by: Jamie Rodríguez <jamie.rodriguez@suse.com>

* Application instances registration changes (#1358)

* Add SAP System de/registration logic to Process Manager (#1356)



Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>

* Add database instance deregistration logic (#1372)



Co-authored-by: Jamie Rodríguez <jamie.rodriguez@suse.com>
Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>

* Add SAP System deregistration side effects (#1387)


Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>

* Detect ascs/ers cluster type (#1392)

* Detect ascs/ers cluster type

* Update functions order to improve pattern matching

* Ascs ers type frontend (#1398)

* Add ascs/ers cluster type to the frontend

* Improve e2e testing of the clusters view

* Discover ASCS/ERS cluster SIDs (#1400)

* Discover ascs/ers cluster sids

* Add additional_sids to domain and side effects

* Add additional_sids to cluster api schema

* Handle `:database_not_registered` errors (#1405)

Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>

* Add polymorphic embed usage to deftype macro (#1411)

* Properly handle multiple errors returned (#1413)

* Display ASCS/ERS clusters SID in clusters view (#1403)

* Show additional sids in the clusters view

* Adapt e2e tests to check for sids

* Order get_all_clusters output by id

* Revert HeartbeatSucceeded to old name (#1429)

* Add deregistration websocket events (#1424)



Co-authored-by: Jamie Rodríguez <jamie.rodriguez@suse.com>
Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>

* Discover ASCS/ERS clusters health and initial details (#1422)

* Add nodes entry to the crm payload

* Add ascs/ers cluster details value objects

* Implement ascs/ers cluster health discovery

* Update cluster details to the polymorphic embed

* Add tests to the ascs/ers cluster health discovery

* Fix validate_required usage

* Set default value to additional_sids in cluster events (#1441)

* Cluster delta deregistration (#1386)

* Add get_cluster_id_by_host_id/1

* Add delta deregistration logic

* Add additional ascs/ers cluster details discovery (#1442)

* Add additional ascs/ers cluster details discovery

* Add comment to parse_ascs_ers_cluster_nodes

* Wrap long function in do/end

* Set false instead of nil in Enum.filter function

* Deregistration process manager retry the rollup errors from aggregates (#1473)

* Fix HostRemovedFromCluster concurrency issues (#1474)

* Basic deregistration leftovers (#1461)

* test

* Add deregistered_at field to Database

* Fix update of Application and Database when deregistering instances

* Put deregistered_at timestamp instead of nil sid on database deregistration

* Remove sid to nil when Sap system is deregistered

* Emit sap system deregistered when the database is deregistered

* Emit database deregistered event when no instances left and db is not
already deregistered

* Sap systems domain deregistration tests refactored

* format sap system domain

* Emit database deregistered event when they are instances only when the
db is not registered

* Removed redundant nil default to deregistered_at in database

* Fix mispell in sap system tests

* Add fake_sid util to sap system testing suite

* Sap system does not get deregistered a second time

* Changed test titles in sap system test, to remove "a second time"
misleading phrase

* Deregistration tombstoning rollup (#1425)

* Host tombstoning on host deregistration

* Add host tombstone rollup handling in stream event handler

* Cluster tombstoned event and lifespan when cluster is deregistered

* Cluster rollup triggered on Cluster tombstone event

* Move cluster tombstoned event to cluster domain folder

* Sap system aggregate emits tombstone event

* Sap system aggregate tombstoning test

* SapSystem tombstoned event triggers sap system rollup

* Removed tombstoned events from lifespan stop mechanism in all aggregates

* Formatted Cluster/Host tombstoned events

* Fix logging string in stream rollup event handler

* Addressing review feedbacks

* Credo format

* Change redirect plug to use a list of available versions (#1487)

* Change redirect plug to use a list of available versions

* Add sanity check for empty available api versions list

* Use Keyword.get to get the opts

* Enable openapi schema versioning (#1488)

* Convert the open api plug into a versinable macro

* Move V1 schemas to its own folder

* Update controllers to used versioned openapi

* Update router to pipethrough specific open api version

* Disable open api cache on dev

* Update controller tests to use new openapi versions

* Temporarily set openapi V1 version in CI docs generation

* Test paths generation in open api spec

* Add endpoint to deregister a host (#1450)



Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>

* Add V2 scope to /api/clusters endpoint (#1492)

* Add openapi v2 schemas

* Add clusters v2 endpoint controller and view

* Update pheonix router to add v2 endpoints

* Adapt clusters v1 schema and view to be backward compatible

* Use clusters v2 version on projector

* Remove deprecated code

* Use /api/v2/clusters endpoint in frontend

* Improve and fix some new schema implementations

* Aggregate clauses reorder (#1495)

* Reorder host aggregate clauses, host_id: nil clauses compacted

* Sap system clauses sap_system_id: nil, compacted

* Sap system delta deregistration (#1491)

* Fix cluster delta computation

* Add get_application_instances_by_host_id/1 and get_database_instances_by_host_id/1

* Add delta deregistration logic

* Identify sap systems ensa version (#1496)

* Identify sap systems ensa version

* Replace nil by no_ensa atom in EnsaVersion enum

* DatabaseDeregistered side effects (#1504)

* Add deregistered_at field to databases read model

* Handle database deregistered side effects

* Filtered deregistered databases in the usecases

* Make `HostReadModel.last_heartbeat_timestamp` a virtual field (#1505)

* Project removal of db/app instances to the read models (#1507)

* Project removal of db/app instances to the read models

* Reject commands when aggregate deregistered (#1513)

* Host aggregate reject commands when the host is deregistered

* Cluster aggregate reject all commands except registration when
deregistered

* Sap system aggregate rejects commands when deregistered

* Host aggregate rejects updates when deregistered

* Cluster aggregate rejects all commands when deregistered

* Fix host tests

* Ensa version domain (#1512)

* Add ensa_version to SapSystemReadModel

* Add SapSystemUpdated event and domain logic

* Add and update sap system domain tests

* Add SapSystemUpdated projection

* Add new ensa_version field in openapi schema

* Add test to check updated event is not emitted

* Update migration to set default value afterwards

* Create AscsErsClusterRole enum (#1520)

* Host restoration (#1517)

* Host restoration in Host aggregate

* Host restoration projector

* Addressing typo feedbacks

* Move host details update after dergistered_at guard clause

* Broadcast sap system updated (#1516)

* Broadcast SapSystemUpdated event

* Update sap systems in redux state upon update event

* Cluster restoration (#1523)

* Extract cluster update procedure to separate function

* Cluster restoration

* Cluster restored projection

* Addressing review feedbacks

* Addressing review feedbacks

* Move sagas functions to separate files according to adr 0009 (#1531)

* Update health summary endpoint (#1530)

* Update health summary service and view

* Update openapi schema and deprecate old fields

* Polish the code and handle unclustered systems

* Fix function specs

* Remove fixed sid usage from db instance creation in test

* Frontend instance removal on broadcast (#1522)

* Add sid to instance deregistration broadcast

* Remove instances from upon instance deregistration broadcast

* Apply various cleanups to the database state

* Use Repo.get! where possible

* Improve notification message

* Fix wrong test placement

* Add test for instances removal notify

* Fix sap system domain apply ordering issues (#1550)

* Move application instances (#1554)

* Add Deregistration modal (#1537)

* Allow rollup on deregistered aggregates (#1551)

* Complete tests on command acceptance/rejection (#1563)

* Sap system restoration (#1545)

* Database restoration in sap system aggregate

* Database restore projection

* Sap system restore domain

* Sap system restore projection

* Emit application instance registered or moved in restore

* Removed health multi change on sap system restoration

* Remove sap_system_id setting in database restored apply

* Removed sid from DatabaseRestored event

* Removed sid field from SapSytemRestored event

---------

Co-authored-by: Carmine Di Monaco <carmine.dimonaco@suse.com>
Co-authored-by: Rubén Torrero Marijnissen <rtorreromarijnissen@suse.com>
Co-authored-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
Co-authored-by: Jamie Rodriguez <jamie.rodriguez@suse.com>
Co-authored-by: Carmine Di Monaco <carmine.dimonaco@gmail.com>
Co-authored-by: Xabier Arbulu Insausti <xarbulu@suse.com>
Co-authored-by: Eugen Maksymenko <eugen.maksymenko@suse.com>
Co-authored-by: Jurgen Goldschmidt <jurgen.goldschmidt@suse.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
elixir Pull requests that update Elixir code
Development

Successfully merging this pull request may close these issues.

4 participants