From b1b30a0de6814a76b2bbd3ab7622f7d05cfdcceb Mon Sep 17 00:00:00 2001 From: Abram Date: Fri, 17 Nov 2023 14:41:42 +0100 Subject: [PATCH 1/5] Update - fix race condition in update_variant_parameterss db manager --- .../agenta_backend/services/db_manager.py | 49 ++++++++++++------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/agenta-backend/agenta_backend/services/db_manager.py b/agenta-backend/agenta_backend/services/db_manager.py index 248fb7e563..ebad1bc4f6 100644 --- a/agenta-backend/agenta_backend/services/db_manager.py +++ b/agenta-backend/agenta_backend/services/db_manager.py @@ -1082,26 +1082,41 @@ async def update_variant_parameters( assert parameters is not None, "parameters is missing" try: - logging.debug("Updating variant parameters") + import asyncio - # Update AppVariantDB parameters - app_variant_db.parameters = parameters + logging.debug("Updating variant parameters") - # Update associated ConfigDB parameters and versioning - config_db = app_variant_db.config - new_version = config_db.current_version + 1 - config_db.version_history.append( - ConfigVersionDB( - version=new_version, - parameters=config_db.parameters, - created_at=datetime.utcnow(), + async def save_variant_config(): + # Update associated ConfigDB parameters and versioning + config_db = app_variant_db.config + new_version = config_db.current_version + 1 + config_db.version_history.append( + ConfigVersionDB( + version=new_version, + parameters=config_db.parameters, + created_at=datetime.utcnow(), + ) ) - ) - config_db.current_version = new_version - config_db.parameters = parameters - # Save updated ConfigDB and AppVariantDB - await engine.save(config_db) - await engine.save(app_variant_db) + config_db.current_version = new_version + config_db.parameters = parameters + + # Save updated ConfigDB + await engine.save(config_db) + + # Create asynchronous task to run independently from the second coroutine + config_task = asyncio.create_task(save_variant_config()) + while True: + # check if the task is done -> resolves to True + if config_task.done(): + # Update AppVariantDB parameters + app_variant_db.parameters = parameters + + # Save updated ConfigDB and AppVariantDB + await engine.save(app_variant_db) + break + + # otherwise block for a moment + await asyncio.sleep(0.1) except Exception as e: logging.error(f"Issue updating variant parameters: {e}") From 2c0c93980aeeb71e1b2e1cb20080d946e183b2d6 Mon Sep 17 00:00:00 2001 From: Abram Date: Fri, 17 Nov 2023 14:42:18 +0100 Subject: [PATCH 2/5] Update - added await keyword to validate_image deployment manager --- agenta-backend/agenta_backend/routers/app_router.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agenta-backend/agenta_backend/routers/app_router.py b/agenta-backend/agenta_backend/routers/app_router.py index 21b527c7c7..8e8fa71f47 100644 --- a/agenta-backend/agenta_backend/routers/app_router.py +++ b/agenta-backend/agenta_backend/routers/app_router.py @@ -240,7 +240,7 @@ async def add_variant_from_image( status_code=500, detail="Image should have a tag starting with the registry name (agenta-server)", ) - elif deployment_manager.validate_image(image) is False: + elif await deployment_manager.validate_image(image) is False: raise HTTPException(status_code=404, detail="Image not found") try: From 6210874ea4507b095477ee033aebe7a6b19349cc Mon Sep 17 00:00:00 2001 From: Abram Date: Fri, 17 Nov 2023 14:56:45 +0100 Subject: [PATCH 3/5] Update - set block period to 0.3 seconds --- agenta-backend/agenta_backend/services/db_manager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/agenta-backend/agenta_backend/services/db_manager.py b/agenta-backend/agenta_backend/services/db_manager.py index ebad1bc4f6..7001a1c02f 100644 --- a/agenta-backend/agenta_backend/services/db_manager.py +++ b/agenta-backend/agenta_backend/services/db_manager.py @@ -1116,7 +1116,7 @@ async def save_variant_config(): break # otherwise block for a moment - await asyncio.sleep(0.1) + await asyncio.sleep(0.5) except Exception as e: logging.error(f"Issue updating variant parameters: {e}") From 9e89633480deca81161e17c6b3a090a2469a28c8 Mon Sep 17 00:00:00 2001 From: Abram Date: Fri, 17 Nov 2023 15:05:29 +0100 Subject: [PATCH 4/5] Update - make comment in update_variant_parameters db function clear --- agenta-backend/agenta_backend/services/db_manager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/agenta-backend/agenta_backend/services/db_manager.py b/agenta-backend/agenta_backend/services/db_manager.py index 7001a1c02f..ec2bff03f8 100644 --- a/agenta-backend/agenta_backend/services/db_manager.py +++ b/agenta-backend/agenta_backend/services/db_manager.py @@ -1103,7 +1103,7 @@ async def save_variant_config(): # Save updated ConfigDB await engine.save(config_db) - # Create asynchronous task to run independently from the second coroutine + # Creates and schedule an instance of the save_variant_config() coroutine task config_task = asyncio.create_task(save_variant_config()) while True: # check if the task is done -> resolves to True @@ -1116,7 +1116,7 @@ async def save_variant_config(): break # otherwise block for a moment - await asyncio.sleep(0.5) + await asyncio.sleep(0.3) except Exception as e: logging.error(f"Issue updating variant parameters: {e}") From 21db8045de7438a6dea901abc1570fbcfcbf56cd Mon Sep 17 00:00:00 2001 From: Abram Date: Fri, 17 Nov 2023 15:43:36 +0100 Subject: [PATCH 5/5] Update - remove busy-wait mechanism --- .../agenta_backend/services/db_manager.py | 46 ++++++------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/agenta-backend/agenta_backend/services/db_manager.py b/agenta-backend/agenta_backend/services/db_manager.py index ec2bff03f8..64b1340919 100644 --- a/agenta-backend/agenta_backend/services/db_manager.py +++ b/agenta-backend/agenta_backend/services/db_manager.py @@ -1082,41 +1082,23 @@ async def update_variant_parameters( assert parameters is not None, "parameters is missing" try: - import asyncio - logging.debug("Updating variant parameters") - async def save_variant_config(): - # Update associated ConfigDB parameters and versioning - config_db = app_variant_db.config - new_version = config_db.current_version + 1 - config_db.version_history.append( - ConfigVersionDB( - version=new_version, - parameters=config_db.parameters, - created_at=datetime.utcnow(), - ) + # Update associated ConfigDB parameters and versioning + config_db = app_variant_db.config + new_version = config_db.current_version + 1 + config_db.version_history.append( + ConfigVersionDB( + version=new_version, + parameters=config_db.parameters, + created_at=datetime.utcnow(), ) - config_db.current_version = new_version - config_db.parameters = parameters - - # Save updated ConfigDB - await engine.save(config_db) - - # Creates and schedule an instance of the save_variant_config() coroutine task - config_task = asyncio.create_task(save_variant_config()) - while True: - # check if the task is done -> resolves to True - if config_task.done(): - # Update AppVariantDB parameters - app_variant_db.parameters = parameters - - # Save updated ConfigDB and AppVariantDB - await engine.save(app_variant_db) - break - - # otherwise block for a moment - await asyncio.sleep(0.3) + ) + config_db.current_version = new_version + config_db.parameters = parameters + + # Save updated ConfigDB + await engine.save(config_db) except Exception as e: logging.error(f"Issue updating variant parameters: {e}")