From 78e6711f25dae927f129022b6c095d36133f3f24 Mon Sep 17 00:00:00 2001 From: David Garcia Date: Thu, 5 May 2022 09:46:06 +0200 Subject: [PATCH] Add deleting lock to K8sJujuConnector - Minor fix in delete_lock - bug 2018 Change-Id: Ic2f66eeee7967f0d7789428abb4b288c7bb90f36 Signed-off-by: David Garcia --- n2vc/k8s_juju_conn.py | 29 +++++++++++++++++++++-------- n2vc/n2vc_juju_conn.py | 5 +++-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/n2vc/k8s_juju_conn.py b/n2vc/k8s_juju_conn.py index b695536..29e6c1d 100644 --- a/n2vc/k8s_juju_conn.py +++ b/n2vc/k8s_juju_conn.py @@ -77,6 +77,7 @@ class K8sJujuConnector(K8sConnector): db_uri = EnvironConfig(prefixes=["OSMLCM_", "OSMMON_"]).get("database_uri") self._store = MotorStore(db_uri) self.loading_libjuju = asyncio.Lock(loop=self.loop) + self.uninstall_locks = {} self.log.debug("K8S Juju connector initialized") # TODO: Remove these commented lines: @@ -496,18 +497,30 @@ class K8sJujuConnector(K8sConnector): """ self.log.debug("[uninstall] Destroying model") - libjuju = await self._get_libjuju(kwargs.get("vca_id")) - await libjuju.destroy_model(kdu_instance, total_timeout=3600) + will_not_delete = False + if kdu_instance not in self.uninstall_locks: + self.uninstall_locks[kdu_instance] = asyncio.Lock(loop=self.loop) + delete_lock = self.uninstall_locks[kdu_instance] + + while delete_lock.locked(): + will_not_delete = True + await asyncio.sleep(0.1) + + if will_not_delete: + self.log.info("Model {} deleted by another worker.".format(kdu_instance)) + return True + + try: + async with delete_lock: + libjuju = await self._get_libjuju(kwargs.get("vca_id")) - # self.log.debug("[uninstall] Model destroyed and disconnecting") - # await controller.disconnect() + await libjuju.destroy_model(kdu_instance, total_timeout=3600) + finally: + self.uninstall_locks.pop(kdu_instance) + self.log.debug(f"[uninstall] Model {kdu_instance} destroyed") return True - # TODO: Remove these commented lines - # if not self.authenticated: - # self.log.debug("[uninstall] Connecting to controller") - # await self.login(cluster_uuid) async def exec_primitive( self, diff --git a/n2vc/n2vc_juju_conn.py b/n2vc/n2vc_juju_conn.py index 9ed225d..c470b1b 100644 --- a/n2vc/n2vc_juju_conn.py +++ b/n2vc/n2vc_juju_conn.py @@ -814,8 +814,9 @@ class N2VCJujuConnector(N2VCConnector): will_not_delete = False if namespace not in self.delete_namespace_locks: self.delete_namespace_locks[namespace] = asyncio.Lock(loop=self.loop) + delete_lock = self.delete_namespace_locks[namespace] - while self.delete_namespace_locks[namespace].locked(): + while delete_lock.locked(): will_not_delete = True await asyncio.sleep(0.1) @@ -824,7 +825,7 @@ class N2VCJujuConnector(N2VCConnector): return try: - async with self.delete_namespace_locks[namespace]: + async with delete_lock: libjuju = await self._get_libjuju(vca_id) # check arguments -- 2.25.1