From 30074b804d5733013f51e35627c59015f57d8398 Mon Sep 17 00:00:00 2001 From: Dragomir Penev <6687393+dragomirp@users.noreply.github.com> Date: Mon, 8 Jul 2024 13:48:38 +0300 Subject: [PATCH] [DPE-4772] Subordinate scale up (#180) * Subordinate scale up * Add application level flag for subordinated provider * Rework remote unit and flag collection * Update lib/charms/data_platform_libs/v0/data_interfaces.py Co-authored-by: Marcelo Henrique Neppel * Bump patch version --------- Co-authored-by: Marcelo Henrique Neppel --- .../data_platform_libs/v0/data_interfaces.py | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/charms/data_platform_libs/v0/data_interfaces.py b/lib/charms/data_platform_libs/v0/data_interfaces.py index 59a97226..74b7f4f7 100644 --- a/lib/charms/data_platform_libs/v0/data_interfaces.py +++ b/lib/charms/data_platform_libs/v0/data_interfaces.py @@ -331,7 +331,7 @@ def _on_topic_requested(self, event: TopicRequestedEvent): # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 37 +LIBPATCH = 38 PYDEPS = ["ops>=2.0.0"] @@ -2606,6 +2606,16 @@ def set_version(self, relation_id: int, version: str) -> None: """ self.update_relation_data(relation_id, {"version": version}) + def set_subordinated(self, relation_id: int) -> None: + """Raises the subordinated flag in the application relation databag. + + The flag will be used to evaluate additional checks before emitting provider events. + + Args: + relation_id: the identifier for a particular relation. + """ + self.update_relation_data(relation_id, {"subordinated": "true"}) + class DatabaseProviderEventHandlers(EventHandlers): """Provider-side of the database relation handlers.""" @@ -2842,6 +2852,24 @@ def _on_relation_created_event(self, event: RelationCreatedEvent) -> None: def _on_relation_changed_event(self, event: RelationChangedEvent) -> None: """Event emitted when the database relation has changed.""" + is_subordinate = False + remote_unit_data = None + for key in event.relation.data.keys(): + 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" + + if is_subordinate: + # Check that provider units have joined. + if not remote_unit_data: + logger.debug("No provider units are available.") + return + + if remote_unit_data.get("state") != "ready": + logger.debug("Subordinate provider unit not ready.") + return + # Check which data has changed to emit customs events. diff = self._diff(event)