Skip to content

Commit

Permalink
add more logs
Browse files Browse the repository at this point in the history
  • Loading branch information
MiaAltieri committed Sep 29, 2023
1 parent 659f22d commit 587f3e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
4 changes: 3 additions & 1 deletion lib/charms/mongodb/v0/mongos.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,9 @@ def add_shard(self, shard_name, shard_hosts, shard_port=27017):
shard_hosts = ",".join(shard_hosts)
shard_url = f"{shard_name}/{shard_hosts}"
# TODO Future PR raise error when number of shards currently adding are higher than the
# number of secondaries on the primary shard
# number of secondaries on the primary shard. This will be challenging, as there is no
# MongoDB command to retrieve the primary shard. Will likely need to be done via
# mongosh

if shard_name in self.get_shard_members():
logger.info("Skipping adding shard %s, shard is already in cluster", shard_name)
Expand Down
29 changes: 19 additions & 10 deletions lib/charms/mongodb/v0/shards_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from charms.mongodb.v0.users import MongoDBUser, OperatorUser
from ops.charm import CharmBase, RelationBrokenEvent
from ops.framework import Object
from ops.model import BlockedStatus, MaintenanceStatus, WaitingStatus
from ops.model import ActiveStatus, BlockedStatus, MaintenanceStatus, WaitingStatus
from tenacity import RetryError, Retrying, stop_after_delay, wait_fixed

from config import Config
Expand All @@ -32,7 +32,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 1
LIBPATCH = 2
KEYFILE_KEY = "key-file"
HOSTS_KEY = "hosts"
OPERATOR_PASSWORD_KEY = MongoDBUser.get_password_key_name_for_user(OperatorUser.get_username())
Expand Down Expand Up @@ -137,13 +137,22 @@ def add_shards(self, departed_shard_id):
# TODO Future PR, limit number of shards add at a time, based on the number of
# replicas in the primary shard
for shard in relation_shards - cluster_shards:
shard_hosts = self._get_shard_hosts(shard)
if not len(shard_hosts):
logger.info("host info for shard %s not yet added, skipping", shard)
continue

logger.info("Adding shard: %s ", shard)
mongo.add_shard(shard, shard_hosts)
try:
shard_hosts = self._get_shard_hosts(shard)
if not len(shard_hosts):
logger.info("host info for shard %s not yet added, skipping", shard)
continue

self.charm.unit.status = MaintenanceStatus(
f"Adding shard {shard} to config-server"
)
logger.info("Adding shard: %s ", shard)
mongo.add_shard(shard, shard_hosts)
except PyMongoError as e:
logger.error("Failed to add shard %s to the config server, error=%r", shard, e)
raise

self.charm.unit.status = ActiveStatus("")

def _update_relation_data(self, relation_id: int, data: dict) -> None:
"""Updates a set of key-value pairs in the relation.
Expand Down Expand Up @@ -246,7 +255,7 @@ def _on_relation_changed(self, event):
)
return

# send hosts to mongos to be added to the cluster
# send shard hosts to config-server mongos, so that shard can be added to the cluster.
self._update_relation_data(
event.relation.id,
{HOSTS_KEY: json.dumps(self.charm._unit_ips)},
Expand Down

0 comments on commit 587f3e5

Please sign in to comment.