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

[DPE-4772] Subordinate scale up #274

Merged
merged 9 commits into from
Jul 8, 2024
Merged

[DPE-4772] Subordinate scale up #274

merged 9 commits into from
Jul 8, 2024

Conversation

dragomirp
Copy link
Contributor

@dragomirp dragomirp commented Jul 3, 2024

Implements a synchronisation mechanism so that scaled up units will only trigger database_created when pgbouncer is ready to accept connections.

Copy link

codecov bot commented Jul 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 71.13%. Comparing base (3c076f6) to head (8dfbb6b).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #274      +/-   ##
==========================================
+ Coverage   70.96%   71.13%   +0.16%     
==========================================
  Files           7        7              
  Lines        1195     1202       +7     
  Branches      210      211       +1     
==========================================
+ Hits          848      855       +7     
  Misses        269      269              
  Partials       78       78              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Comment on lines 146 to 149
if self.unit_databag.get("auth_file_set", "false") == "true" and self.unit_databag.get(
"backend_relation"
):
self.charm.client_relation.set_ready()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

At this point we should have a backend relation, rendered user list and config, so unit should be ready

@@ -168,6 +179,7 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
return

self.charm.render_pgb_config(reload_pgbouncer=True)
self.set_ready()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Setting the leader ready immediately, so we don't have to wait in a single unit cluster.

@@ -107,6 +111,9 @@ def _on_database_requested(self, event: DatabaseRequestedEvent) -> None:
Deferrals:
- If backend relation is not fully initialised
"""
rel_id = event.relation.id
self.database_provides.set_subordinated(rel_id)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Set the app level flag. There's other synchronisation for existing units, so it should be safe even if not immediately set.

@@ -181,6 +181,7 @@ def _on_database_created(self, event: DatabaseCreatedEvent) -> None:
self.charm.render_pgb_config(reload_pgbouncer=True)
self.charm.render_prometheus_service()
self.charm.update_status()
self.charm.client_relation.set_ready()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We should have the peer data and secret here already, so no need to retrigger the peer changed hook.

Copy link
Member

@marceloneppel marceloneppel left a comment

Choose a reason for hiding this comment

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

LGTM!

@dragomirp dragomirp marked this pull request as ready for review July 5, 2024 15:10
def set_ready(self) -> None:
"""Marks the unit as ready for all database relations."""
for relation in self.model.relations[self.relation_name]:
relation.data[self.charm.unit].update({"state": "ready"})
Copy link
Member

Choose a reason for hiding this comment

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

Could we use a self.database_provides.update_relation_data(relation.id, {"state": "ready"}) helper from data_interfaces here?
I prefer to avoid direct modification of databag.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

update_relation_data() should be setting app level data, we need this in the unit databag. We'll need another helper for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Possibly I can use _update_relation_data_without_secrets() but that's private.

Copy link
Contributor

Choose a reason for hiding this comment

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

No, we can't use any of those. Cross-charm relations (incl. all related internal functions) are restricted to app data.

Only peer relations support interface functions to access unit data at the moment.

@dragomirp dragomirp requested a review from delgod July 5, 2024 17:00
if isinstance(key, Unit) and not key.name.startswith(self.charm.app.name):
remote_unit_data = event.relation.data[key]
elif isinstance(key, Application) and key.name != self.charm.app.name:
is_subordinate = event.relation.data[key].get("subordinated") == "true"
Copy link
Contributor

Choose a reason for hiding this comment

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

Huh, this feels kinda hacky... I get the rationale, but I think we should have a proper "frame" around this.

The DatabaseProviderEventHander -- up to this point was not supposed to directly dig in databags.

We are reading an manipulating relation data via the DatabaseProviderEventHander.relation_data... Which --as for now-- strictly mean app level data.

We can alter the existing workflows to be ready to handle unit level data as well.
However that should go alongside considerations similar to the existing ones across the module (not just direct access in various parts of the code).

I could go with this solution as a temporary one, and I'm glad to volunteer to introduce the missing concept of handling unit level data in the libs. At that point this code will be cleaned up.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let's keep it as is for the time being.

@dragomirp dragomirp merged commit c4fdfb9 into main Jul 8, 2024
52 checks passed
@dragomirp dragomirp deleted the dpe-4772-scale-up branch July 8, 2024 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants