From f93b47c8c886f496685d83c18d3d7c131f2bedd6 Mon Sep 17 00:00:00 2001 From: Bodong Yang <86948717+Bodong-Yang@users.noreply.github.com> Date: Sun, 1 Dec 2024 19:52:52 +0900 Subject: [PATCH] fix(backport v3.8.x): ota_proxy: fix disk space usage checker not working & backport #418 (#441) This PR fixes otaproxy's background disk space usage checker not working as expected due to checker thread exits early caused by raised exception, resulting in otaproxy LRU cache rotation not being triggered on soft disk space usage reached and otaproxy cache disabled on hard space usage reached. This PR also backports a minor fix PR #418 . Other changes include: 1. ota_proxy: increase soft limit to 90% disk usage, hard limit to 92% disk usage. --- src/ota_proxy/cache_streaming.py | 6 ++++-- src/ota_proxy/config.py | 4 ++-- src/ota_proxy/ota_cache.py | 10 +++++----- 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/ota_proxy/cache_streaming.py b/src/ota_proxy/cache_streaming.py index 48237dc0d..17e14b02f 100644 --- a/src/ota_proxy/cache_streaming.py +++ b/src/ota_proxy/cache_streaming.py @@ -335,6 +335,7 @@ async def cache_streaming( Raises: CacheStreamingFailed if any exception happens during retrieving. """ + _cache_write_gen = None try: _cache_write_gen = await tracker.start_provider(cache_meta) _cache_writer_failed = False @@ -370,8 +371,9 @@ async def cache_streaming( finally: # force terminate the generator in all condition at exit, this # can ensure the generator being gced after cache_streaming exits. - with contextlib.suppress(StopAsyncIteration): - await _cache_write_gen.athrow(StopAsyncIteration) + if _cache_write_gen: + with contextlib.suppress(StopAsyncIteration): + await _cache_write_gen.athrow(StopAsyncIteration) # remove the refs fd, tracker = None, None # type: ignore diff --git a/src/ota_proxy/config.py b/src/ota_proxy/config.py index a6c483877..c6b571adb 100644 --- a/src/ota_proxy/config.py +++ b/src/ota_proxy/config.py @@ -16,8 +16,8 @@ class Config: BASE_DIR = "/ota-cache" CHUNK_SIZE = 1 * 1024 * 1024 # 4MB - DISK_USE_LIMIT_SOFT_P = 70 # in p% - DISK_USE_LIMIT_HARD_P = 80 # in p% + DISK_USE_LIMIT_SOFT_P = 90 # in p% + DISK_USE_LIMIT_HARD_P = 92 # in p% DISK_USE_PULL_INTERVAL = 2 # in seconds # value is the largest numbers of files that # might need to be deleted for the bucket to hold a new entry diff --git a/src/ota_proxy/ota_cache.py b/src/ota_proxy/ota_cache.py index c68ff7731..531525cd9 100644 --- a/src/ota_proxy/ota_cache.py +++ b/src/ota_proxy/ota_cache.py @@ -248,7 +248,7 @@ def _background_check_free_space(self): self._storage_below_hard_limit_event.clear() break - current_used_p = disk_usage.used / disk_usage.total * 100 + current_used_p = int(disk_usage.used / disk_usage.total * 100) _previous_below_hard = self._storage_below_hard_limit_event.is_set() _current_below_hard = True @@ -266,13 +266,13 @@ def _background_check_free_space(self): # logging space monitoring result if _previous_below_hard and not _current_below_hard: logger.warning( - f"disk usage reached hard limit({current_used_p=:.1}%," - f"{cfg.DISK_USE_LIMIT_HARD_P:.1}%), cache disabled" + f"disk usage reached hard limit({current_used_p=}%," + f"{cfg.DISK_USE_LIMIT_HARD_P=}%), cache disabled" ) elif not _previous_below_hard and _current_below_hard: logger.info( - f"disk usage is below hard limit({current_used_p=:.1}%)," - f"{cfg.DISK_USE_LIMIT_SOFT_P:.1}%), cache enabled again" + f"disk usage is below hard limit({current_used_p=}%)," + f"{cfg.DISK_USE_LIMIT_SOFT_P=}%), cache enabled again" ) time.sleep(cfg.DISK_USE_PULL_INTERVAL)