From d153238f3d0c58fd3dfb791f1e58eae9e070979a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=AA=D7=9E=D7=A8=20=D7=9E=D7=9C=D7=99=D7=A7?= Date: Sun, 22 Sep 2024 16:31:37 +0300 Subject: [PATCH 1/4] lock integrations & relative changes --- .../Controller/LockController.py | 6 +- .../Integration/BucketIntegration.py | 4 +- .../Integration/LockIntegration.py | 98 +++++++++++++++++++ .../Service/Classes/LockService.py | 11 +-- 4 files changed, 106 insertions(+), 13 deletions(-) create mode 100644 Storage/NEW_KT_Storage/Integration/LockIntegration.py diff --git a/Storage/NEW_KT_Storage/Controller/LockController.py b/Storage/NEW_KT_Storage/Controller/LockController.py index f34069d5..d6d93523 100644 --- a/Storage/NEW_KT_Storage/Controller/LockController.py +++ b/Storage/NEW_KT_Storage/Controller/LockController.py @@ -5,8 +5,8 @@ class LockController: - def __init__(self, service: LockService): - self.service = service + def __init__(self): + self.service = LockService() def create_lock(self, bucket_key: str, lock_mode: str, amount: int, unit: str, object_key: str = '*'): return self.service.create_lock(bucket_key= bucket_key, object_key=object_key, lock_mode=lock_mode, amount=amount, unit=unit) @@ -21,7 +21,7 @@ def get_lock(self, lock_id: str): def is_object_updatable(self, bucket_key: str, object_key: str): return self.service.is_object_updatable(bucket_key, object_key) - def is_object_deleteable(self, bucket_key: str, object_key: str): + def is_object_deletable(self, bucket_key: str, object_key: str): return self.service.is_object_deletable(bucket_key, object_key) diff --git a/Storage/NEW_KT_Storage/Integration/BucketIntegration.py b/Storage/NEW_KT_Storage/Integration/BucketIntegration.py index b9c1446a..62c6f213 100644 --- a/Storage/NEW_KT_Storage/Integration/BucketIntegration.py +++ b/Storage/NEW_KT_Storage/Integration/BucketIntegration.py @@ -1,7 +1,7 @@ # demonstrate all object functionallity -print'''---------------------Start Of session----------------------''' +print('''---------------------Start Of session----------------------''') print(current_date_time) print('''{current_date_time} deonstration of object XXX start''') @@ -22,5 +22,5 @@ print('''{current_date_time} deonstration of object XXX ended successfully''') print(current_date_time) -print'''---------------------End Of session----------------------''' +print('''---------------------End Of session----------------------''') print(total_duration) diff --git a/Storage/NEW_KT_Storage/Integration/LockIntegration.py b/Storage/NEW_KT_Storage/Integration/LockIntegration.py new file mode 100644 index 00000000..514472d0 --- /dev/null +++ b/Storage/NEW_KT_Storage/Integration/LockIntegration.py @@ -0,0 +1,98 @@ +from datetime import datetime +import time +import os +import sys +sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..'))) +from Storage.NEW_KT_Storage.Controller.LockController import LockController + +# demonstrate lock functionality +print('''---------------------Start Of session----------------------''') +start_time_session = datetime.now() +print(f"{start_time_session} demonstration of object: Lock start") + +start_time = datetime.now() +lock_controller = LockController() + +# create lock +print() + +bucket_key = "exampleBucket" +object_key = "exampleObj111" +lock_mode = "all" +amount = 1 +unit = "h" +lock_id = f"{bucket_key}.{object_key}" + +print(f"{datetime.now()} start creating lock 'id: {lock_id}'") + +lock_controller.create_lock(bucket_key=bucket_key, object_key=object_key, lock_mode=lock_mode, amount=amount, unit=unit) +print(f"{datetime.now()} lock: '{lock_id}' created successfully") +end_time = datetime.now() +total_duration = end_time - start_time +print(f"total duration of create lock is: '{total_duration}'") +print() +time.sleep(20) + +# get lock +print() +start_time = datetime.now() +print(f"{start_time} start getting lock '{lock_id}'") +lock_example = lock_controller.get_lock(lock_id) +print(f"{datetime.now()} verify lock 'id: {lock_example.lock_id}, " + f"bucket_key: {lock_example.bucket_key}, object_key: {lock_example.object_key}, " + f"retain_until = {lock_example.retain_until}, lock_mode: {lock_example.lock_mode}'") + +end_time = datetime.now() +total_duration = end_time - start_time +print(f"total duration of get lock is: '{total_duration}'") +print() +time.sleep(20) + +# check if object is updatable +print() +start_time = datetime.now() +print(f"{start_time} start checking if object '{object_key}' in bucket '{bucket_key}' is updatable") +is_updatable = lock_controller.is_object_updatable(bucket_key=bucket_key, object_key=object_key) +print(f"{datetime.now()} object '{object_key}' in bucket '{bucket_key}' is updatable: {is_updatable}") +end_time = datetime.now() +total_duration = end_time - start_time +print(f"total duration of updatable check is: '{total_duration}'") +print() +time.sleep(20) + +# check if object is deletable +print() +start_time = datetime.now() +print(f"{start_time} start checking if object '{object_key}' in bucket '{bucket_key}' is deleteable") +is_deletable = lock_controller.is_object_deletable(bucket_key=bucket_key, object_key=object_key) +print(f"{datetime.now()} object '{object_key}' in bucket '{bucket_key}' is deleteable: {is_deletable}") +end_time = datetime.now() +total_duration = end_time - start_time +print(f"total duration of deletable check is: '{total_duration}'") +print() +time.sleep(20) + +# delete lock +print() +start_time = datetime.now() +print(f"{start_time} start deleting lock '{lock_id}'") +lock_controller.delete_lock(lock_id) +print(f"{datetime.now()} verify lock '{lock_id}' deleted by checking if it exists") +end_time = datetime.now() +total_duration = end_time - start_time +print(f"total duration of delete lock is: '{total_duration}'") + +# test deletion of lock by attempting to get it +print() +try: + lock_controller.get_lock(lock_id) +except Exception as e: + print(f"Error as expected: {e}") +print() + +# end of session +end_time_session = datetime.now() +print(f"{end_time_session} demonstration of object lock ended successfully") +total_duration_session = end_time_session - start_time_session +print(f"the total duration of session lock is: '{total_duration_session}'") +print('''---------------------End Of session----------------------''') diff --git a/Storage/NEW_KT_Storage/Service/Classes/LockService.py b/Storage/NEW_KT_Storage/Service/Classes/LockService.py index ac4fe51e..893bb885 100644 --- a/Storage/NEW_KT_Storage/Service/Classes/LockService.py +++ b/Storage/NEW_KT_Storage/Service/Classes/LockService.py @@ -20,7 +20,7 @@ def __init__(self): self.locks_IDs_list = SortedList([(lock.lock_id, lock.retain_until) for lock in existing_locks], key=lambda x: x[1]) # Initialize lock_map with existing locks-> lock_id : LockModel self.lock_map = {lock.lock_id: lock for lock in existing_locks} - + print('(Background) start_lock_cleanup_scheduler') self.start_lock_cleanup_scheduler() @@ -108,6 +108,7 @@ def remove_expired_locks(self): # Add the expired lock's ID to the list for deletion expired_locks.append(expired_lock_id) + print(f"(Background) Deleting expired locks: {expired_locks}") # Use delete_lock to remove for lock_id in expired_locks: self.delete_lock(lock_id) @@ -118,7 +119,7 @@ def start_lock_cleanup_scheduler(self): def run_cleanup(): while True: self.remove_expired_locks() - print("Lock cleanup completed.") + print("(Background) Lock cleanup completed.") time.sleep(10) # Check every minute cleanup_thread = threading.Thread(target=run_cleanup) @@ -151,9 +152,3 @@ def is_locked(self, bucket_key: str, object_key: str): lock_id = f"{bucket_key}.{object_key}" return lock_id in self.lock_map - - def print_locks_by_retain(self): - """Print the locks sorted by retain_until.""" - print("Locks sorted by retain_until:") - for idx, (lock_id, retain_until) in enumerate(self.locks_IDs_list): - print(f"Position {idx}: {lock_id}, Retain until: {retain_until}") From 86d249a10d321e3124610f7199271552546669f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=AA=D7=9E=D7=A8=20=D7=9E=D7=9C=D7=99=D7=A7?= Date: Sun, 22 Sep 2024 23:09:11 +0300 Subject: [PATCH 2/4] update print for background process --- Storage/NEW_KT_Storage/Integration/BucketIntegration.py | 5 ++--- Storage/NEW_KT_Storage/Integration/LockIntegration.py | 4 ++-- Storage/NEW_KT_Storage/Service/Classes/LockService.py | 2 +- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Storage/NEW_KT_Storage/Integration/BucketIntegration.py b/Storage/NEW_KT_Storage/Integration/BucketIntegration.py index 62c6f213..9c72185b 100644 --- a/Storage/NEW_KT_Storage/Integration/BucketIntegration.py +++ b/Storage/NEW_KT_Storage/Integration/BucketIntegration.py @@ -1,8 +1,7 @@ # demonstrate all object functionallity -print('''---------------------Start Of session----------------------''') -print(current_date_time) +print'''---------------------Start Of session----------------------'''print(current_date_time) print('''{current_date_time} deonstration of object XXX start''') @@ -23,4 +22,4 @@ print('''{current_date_time} deonstration of object XXX ended successfully''') print(current_date_time) print('''---------------------End Of session----------------------''') -print(total_duration) +print'''---------------------End Of session----------------------''' \ No newline at end of file diff --git a/Storage/NEW_KT_Storage/Integration/LockIntegration.py b/Storage/NEW_KT_Storage/Integration/LockIntegration.py index 514472d0..78c490ff 100644 --- a/Storage/NEW_KT_Storage/Integration/LockIntegration.py +++ b/Storage/NEW_KT_Storage/Integration/LockIntegration.py @@ -63,9 +63,9 @@ # check if object is deletable print() start_time = datetime.now() -print(f"{start_time} start checking if object '{object_key}' in bucket '{bucket_key}' is deleteable") +print(f"{start_time} start checking if object '{object_key}' in bucket '{bucket_key}' is deletable") is_deletable = lock_controller.is_object_deletable(bucket_key=bucket_key, object_key=object_key) -print(f"{datetime.now()} object '{object_key}' in bucket '{bucket_key}' is deleteable: {is_deletable}") +print(f"{datetime.now()} object '{object_key}' in bucket '{bucket_key}' is deletable: {is_deletable}") end_time = datetime.now() total_duration = end_time - start_time print(f"total duration of deletable check is: '{total_duration}'") diff --git a/Storage/NEW_KT_Storage/Service/Classes/LockService.py b/Storage/NEW_KT_Storage/Service/Classes/LockService.py index 893bb885..c411c89c 100644 --- a/Storage/NEW_KT_Storage/Service/Classes/LockService.py +++ b/Storage/NEW_KT_Storage/Service/Classes/LockService.py @@ -108,11 +108,11 @@ def remove_expired_locks(self): # Add the expired lock's ID to the list for deletion expired_locks.append(expired_lock_id) - print(f"(Background) Deleting expired locks: {expired_locks}") # Use delete_lock to remove for lock_id in expired_locks: self.delete_lock(lock_id) + print(f"Background process for completed successfully with {len(expired_locks)} expired locks removed.") def start_lock_cleanup_scheduler(self): """Start a background thread that checks for expired locks every minute.""" From 2e71b3c3e8ca2c1acb1d8a3a924a9724c6308c9c Mon Sep 17 00:00:00 2001 From: Tamar Malik <154598225+tamar1225@users.noreply.github.com> Date: Sun, 22 Sep 2024 23:22:25 +0300 Subject: [PATCH 3/4] Update first background print --- Storage/NEW_KT_Storage/Service/Classes/LockService.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Storage/NEW_KT_Storage/Service/Classes/LockService.py b/Storage/NEW_KT_Storage/Service/Classes/LockService.py index c411c89c..06b1e033 100644 --- a/Storage/NEW_KT_Storage/Service/Classes/LockService.py +++ b/Storage/NEW_KT_Storage/Service/Classes/LockService.py @@ -20,7 +20,7 @@ def __init__(self): self.locks_IDs_list = SortedList([(lock.lock_id, lock.retain_until) for lock in existing_locks], key=lambda x: x[1]) # Initialize lock_map with existing locks-> lock_id : LockModel self.lock_map = {lock.lock_id: lock for lock in existing_locks} - print('(Background) start_lock_cleanup_scheduler') + print("Background process: starting lock cleanup scheduler") self.start_lock_cleanup_scheduler() From 15080c97d92ad70c2d617682bf6b4eda2f3ff112 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D7=AA=D7=9E=D7=A8=20=D7=9E=D7=9C=D7=99=D7=A7?= Date: Sun, 22 Sep 2024 16:31:37 +0300 Subject: [PATCH 4/4] lock integrations & relative changes --- Storage/NEW_KT_Storage/Service/Classes/LockService.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Storage/NEW_KT_Storage/Service/Classes/LockService.py b/Storage/NEW_KT_Storage/Service/Classes/LockService.py index 06b1e033..23555980 100644 --- a/Storage/NEW_KT_Storage/Service/Classes/LockService.py +++ b/Storage/NEW_KT_Storage/Service/Classes/LockService.py @@ -108,6 +108,7 @@ def remove_expired_locks(self): # Add the expired lock's ID to the list for deletion expired_locks.append(expired_lock_id) + print(f"(Background) Deleting expired locks: {expired_locks}") # Use delete_lock to remove for lock_id in expired_locks: self.delete_lock(lock_id)