From 8684564bb5cdf75ddefafb61ea94db8529530e88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20J=C3=BAnior?= Date: Wed, 18 Aug 2021 18:52:19 -0300 Subject: [PATCH 1/2] adding get cached info validation --- networkapi/distributedlock/__init__.py | 3 +++ networkapi/distributedlock/memcachedlock.py | 5 +++++ networkapi/util/geral.py | 3 +++ 3 files changed, 11 insertions(+) diff --git a/networkapi/distributedlock/__init__.py b/networkapi/distributedlock/__init__.py index 388237095..692988f40 100644 --- a/networkapi/distributedlock/__init__.py +++ b/networkapi/distributedlock/__init__.py @@ -117,3 +117,6 @@ def __enter__(self): def __exit__(self, type, value, traceback): _debug('releasing lock %s' % self.key) self.lock.release() + + def get_cached_data(self): + return self.lock.get_cached_data() diff --git a/networkapi/distributedlock/memcachedlock.py b/networkapi/distributedlock/memcachedlock.py index 8f29f45a0..ad3e426d3 100644 --- a/networkapi/distributedlock/memcachedlock.py +++ b/networkapi/distributedlock/memcachedlock.py @@ -69,3 +69,8 @@ def release(self): else: log.warning( "I've no lock to release. Increase TIMEOUT of lock operations") + + def get_cached_data(self): + value = self.client.get(self.key) + + return value if value else None diff --git a/networkapi/util/geral.py b/networkapi/util/geral.py index d364f104f..e8d68342f 100644 --- a/networkapi/util/geral.py +++ b/networkapi/util/geral.py @@ -72,6 +72,9 @@ def create_lock_with_blocking(locks_name): for lock_name in locks_name: try: lock = distributedlock(lock_name, blocking=False) + if lock.get_cached_data(): + log.info('Get cached lock data for {}. Disabling it and creating a new lock'.format(lock_name)) + lock.__exit__('', '', '') lock.__enter__() locks_list.append(lock) except LockNotAcquiredError: From 84ee17e41b286c46bf16b4c95817706297b8100e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20J=C3=BAnior?= Date: Wed, 18 Aug 2021 21:02:28 -0300 Subject: [PATCH 2/2] bypass instance id validation --- networkapi/util/geral.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/networkapi/util/geral.py b/networkapi/util/geral.py index e8d68342f..2d79ab50e 100644 --- a/networkapi/util/geral.py +++ b/networkapi/util/geral.py @@ -72,8 +72,12 @@ def create_lock_with_blocking(locks_name): for lock_name in locks_name: try: lock = distributedlock(lock_name, blocking=False) + # TODO: This is a temporary solution for a high needed change. In the future we need to validate + # why destroy_lock in models/create_v3 didn't work some times + if lock.get_cached_data(): log.info('Get cached lock data for {}. Disabling it and creating a new lock'.format(lock_name)) + lock.lock.instance_id = lock.get_cached_data() lock.__exit__('', '', '') lock.__enter__() locks_list.append(lock)