Skip to content

Commit

Permalink
mongos updates env var when scaling
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Dec 12, 2023
1 parent ec0c98e commit a2f303d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 36 deletions.
20 changes: 19 additions & 1 deletion lib/charms/mongodb/v0/config_server_interface.py
Original file line number Diff line number Diff line change
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

0 comments on commit a2f303d

Please sign in to comment.