Skip to content

Commit

Permalink
[DPE-5235] add changes necessary for mongos-k8s ext connections (#483)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
MiaAltieri committed Sep 11, 2024
1 parent 1e823b3 commit cf68980
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/charms/mongodb/v1/mongodb_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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."""

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit cf68980

Please sign in to comment.