Skip to content

Commit

Permalink
[DPE-3085] - Provide mongos with update config db (#315)
Browse files Browse the repository at this point in the history
## Issue
When replicas are added to the config DB the related mongos charm should
get an updated connection string

## Future PRs
Adopt this change on `mongos` and add an integration tests
  • Loading branch information
MiaAltieri authored Dec 13, 2023
1 parent 911eff7 commit 9070c93
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 37 deletions.
22 changes: 20 additions & 2 deletions lib/charms/mongodb/v0/config_server_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 5
LIBPATCH = 6


class ClusterProvider(Object):
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
50 changes: 15 additions & 35 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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.
Expand Down
31 changes: 31 additions & 0 deletions tests/unit/test_config_server_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,34 @@ def is_config_mock_call(*args):
self.harness.set_leader(False)
self.harness.add_relation_unit(relation_id, "mongos/2")
self.harness.charm.cluster.database_provides.update_relation_data.assert_not_called()

def test_update_rel_data_failed_hook_checks(self):
"""Tests that no relation data is set when the cluster is not ready."""

def is_not_config_mock_call(*args):
assert args == ("config-server",)
return False

self.harness.charm.app_peer_data["db_initialised"] = "True"

# fails due to being run on non-config-server
self.harness.charm.is_role = is_not_config_mock_call
self.harness.charm.cluster.update_config_server_db(mock.Mock())
self.harness.charm.cluster.database_provides.update_relation_data = mock.Mock()
self.harness.charm.cluster.database_provides.update_relation_data.assert_not_called()

# fails because db has not been initialized
del self.harness.charm.app_peer_data["db_initialised"]

def is_config_mock_call(*args):
assert args == ("config-server",)
return True

self.harness.charm.is_role = is_config_mock_call
self.harness.charm.cluster.update_config_server_db(mock.Mock())
self.harness.charm.cluster.database_provides.update_relation_data.assert_not_called()

# fails because not leader
self.harness.set_leader(False)
self.harness.charm.cluster.update_config_server_db(mock.Mock())
self.harness.charm.cluster.database_provides.update_relation_data.assert_not_called()

0 comments on commit 9070c93

Please sign in to comment.