From a2f303d7835eb7aec8db4249b3cbb300b1c5d4e2 Mon Sep 17 00:00:00 2001 From: Mia Altieri Date: Tue, 12 Dec 2023 22:01:01 +0000 Subject: [PATCH] mongos updates env var when scaling --- .../mongodb/v0/config_server_interface.py | 20 +++++++- src/charm.py | 50 ++++++------------- 2 files changed, 34 insertions(+), 36 deletions(-) diff --git a/lib/charms/mongodb/v0/config_server_interface.py b/lib/charms/mongodb/v0/config_server_interface.py index bf03fb0e2..4d6e86d49 100644 --- a/lib/charms/mongodb/v0/config_server_interface.py +++ b/lib/charms/mongodb/v0/config_server_interface.py @@ -86,7 +86,6 @@ def _on_relation_changed(self, event) -> None: # create user and set secrets for mongos relation self.charm.client_relations.oversee_users(None, None) - # TODO Future PR, use secrets if self.charm.unit.is_leader(): self.database_provides.update_relation_data( event.relation.id, @@ -98,6 +97,25 @@ def _on_relation_changed(self, event) -> None: }, ) + def update_config_server_db(self, event): + """Provides related mongos applications with new config server db.""" + if not self.pass_hook_checks(event): + logger.info("Skipping update_config_server_db: hook checks did not pass") + return + + config_server_db = self.generate_config_server_db() + + if not self.charm.unit.is_leader(): + return + + for relation in self.charm.model.relations[self.relation_name]: + self.database_provides.update_relation_data( + relation.id, + { + CONFIG_SERVER_DB_KEY: config_server_db, + }, + ) + def generate_config_server_db(self) -> str: """Generates the config server database for mongos to connect to.""" replica_set_name = self.charm.app.name diff --git a/src/charm.py b/src/charm.py index 3c1ee3ca2..288f5b953 100755 --- a/src/charm.py +++ b/src/charm.py @@ -414,14 +414,7 @@ def _on_relation_joined(self, event: RelationJoinedEvent) -> None: self._on_relation_handler(event) - # app relations should be made aware of the new set of hosts - try: - self.client_relations.update_app_relation_data() - self.config_server.update_mongos_hosts() - except PyMongoError as e: - logger.error("Deferring on updating app relation data since: error: %r", e) - event.defer() - return + self._update_related_hosts(event) def _on_relation_handler(self, event: RelationEvent) -> None: """Adds the unit as a replica to the MongoDB replica set. @@ -477,15 +470,6 @@ def _on_leader_elected(self, event: LeaderElectedEvent) -> None: self._update_hosts(event) - # app relations should be made aware of the new set of hosts - try: - self.client_relations.update_app_relation_data() - self.config_server.update_mongos_hosts() - except PyMongoError as e: - logger.error("Deferring on updating app relation data since: error: %r", e) - event.defer() - return - def _on_relation_departed(self, event: RelationDepartedEvent) -> None: """Remove peer from replica set if it wasn't able to remove itself. @@ -498,15 +482,6 @@ def _on_relation_departed(self, event: RelationDepartedEvent) -> None: self._update_hosts(event) - # app relations should be made aware of the new set of hosts - try: - self.client_relations.update_app_relation_data() - self.config_server.update_mongos_hosts() - except PyMongoError as e: - logger.error("Deferring on updating app relation data since: error: %r", e) - event.defer() - return - def _on_storage_detaching(self, event: StorageDetachingEvent) -> None: """Before storage detaches, allow removing unit to remove itself from the set. @@ -869,6 +844,19 @@ def _update_hosts(self, event: LeaderElectedEvent) -> None: self.process_unremoved_units(event) self.app_peer_data["replica_set_hosts"] = json.dumps(self._unit_ips) + self._update_related_hosts(event) + + def _update_related_hosts(self, event) -> None: + # app relations should be made aware of the new set of hosts + try: + self.client_relations.update_app_relation_data() + self.config_server.update_mongos_hosts() + self.cluster.update_config_server_db(event) + except PyMongoError as e: + logger.error("Deferring on updating app relation data since: error: %r", e) + event.defer() + return + def process_unremoved_units(self, event: LeaderElectedEvent) -> None: """Removes replica set members that are no longer running as a juju hosts.""" with MongoDBConnection(self.mongodb_config) as mongo: @@ -897,20 +885,12 @@ def _handle_reconfigure(self, event: UpdateStatusEvent): # remove any IPs that are no longer juju hosts & update app data. self._update_hosts(event) + # Add in any new IPs to the replica set. Relation handlers require a reference to # a unit. event.unit = self.unit self._on_relation_handler(event) - # app relations should be made aware of the new set of hosts - try: - self.client_relations.update_app_relation_data() - self.config_server.update_mongos_hosts() - except PyMongoError as e: - logger.error("Deferring on updating app relation data since: error: %r", e) - event.defer() - return - def _open_ports_tcp(self, ports: int) -> None: """Open the given port.