From cf689805618d35bf3e8f4e14362b81eb9f7d688e Mon Sep 17 00:00:00 2001 From: Mia Altieri <32723809+MiaAltieri@users.noreply.github.com> Date: Wed, 11 Sep 2024 16:46:49 +0200 Subject: [PATCH] [DPE-5235] add changes necessary for mongos-k8s ext connections (#483) ## Issue mongos k8s charm can have errors while retrieving hosts, due to race conditions in units ## Solution library should be flexible to handle this scenario --- lib/charms/mongodb/v1/mongodb_provider.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/charms/mongodb/v1/mongodb_provider.py b/lib/charms/mongodb/v1/mongodb_provider.py index ad8c7170..2fc78b25 100644 --- a/lib/charms/mongodb/v1/mongodb_provider.py +++ b/lib/charms/mongodb/v1/mongodb_provider.py @@ -31,7 +31,7 @@ # Increment this PATCH version before using `charmcraft publish-lib` or reset # to 0 if you are raising the major API version -LIBPATCH = 12 +LIBPATCH = 13 logger = logging.getLogger(__name__) REL_NAME = "database" @@ -49,6 +49,10 @@ deleted — key that were deleted.""" +class FailedToGetHostsError(Exception): + """Raised when charm fails to retrieve hosts.""" + + class MongoDBProvider(Object): """In this class, we manage client database relations.""" @@ -145,7 +149,11 @@ def _on_relation_event(self, event): try: self.oversee_users(departed_relation_id, event) - except PyMongoError as e: + except (PyMongoError, FailedToGetHostsError) as e: + # Failed to get hosts error is unique to mongos-k8s charm. In other charms we do not + # foresee issues to retrieve hosts. However in external mongos-k8s, the leader can + # attempt to retrieve hosts while non-leader units are still enabling node port + # resulting in an exception. logger.error("Deferring _on_relation_event since: error=%r", e) event.defer() return