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 b6d0977a..c1c50500 100644 --- a/Storage/NEW_KT_Storage/Integration/BucketIntegration.py +++ b/Storage/NEW_KT_Storage/Integration/BucketIntegration.py @@ -1,12 +1,12 @@ -from datetime import datetime +from datetime import datetime, timedelta import pytest import os import sys import time -from Storage.NEW_KT_Storage.Exceptions import BucketExceptions os.chdir('D:/s3_project/KT_Cloud') sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..','..','..'))) from Storage.NEW_KT_Storage.Controller.BucketController import BucketController +from Storage.NEW_KT_Storage.Exceptions import BucketExceptions # demonstrate bucket functionality print('''---------------------Start Of session----------------------''') @@ -79,7 +79,7 @@ print() end_time_session = datetime.now() print(f"{end_time_session} demonstration of object Bucket ended successfully") -total_duration_session = end_time_session - start_time_session-35 +total_duration_session = end_time_session - start_time_session-timedelta(seconds=30) print(f"the total duration of session bucket is: '{total_duration_session}'") print('''---------------------End Of session----------------------''') diff --git a/Storage/NEW_KT_Storage/Integration/LockIntegration.py b/Storage/NEW_KT_Storage/Integration/LockIntegration.py new file mode 100644 index 00000000..78c490ff --- /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 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 deletable: {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..23555980 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 process: starting lock cleanup scheduler") self.start_lock_cleanup_scheduler() @@ -108,17 +108,19 @@ 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.""" 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 +153,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}")