From 6d0f6fd27a5986676849a97a039c2b6beb106c34 Mon Sep 17 00:00:00 2001 From: kshcheku Date: Wed, 21 Oct 2026 14:29:21 +0100 Subject: [PATCH] Cache icurl() results within a run to avoid redundant duplicate API calls Several checks independently query the same APIC class/DN, causing identical queries to be fetched multiple times in the same run. Add a per-run cache keyed on (apitype, query, page_size), guarded by a per-query lock so concurrent callers of the same query wait for the first fetch instead of all hitting the APIC, while distinct queries still run fully in parallel. Failed fetches are not cached. No change to check logic or results; verified with full test suite and live fabric runs (identical PASS/FAIL/MANUAL/N/A/ERROR summaries before and after). Fixes #485 --- aci-preupgrade-validation-script.py | 20 +++++++++++++++++++- tests/conftest.py | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/aci-preupgrade-validation-script.py b/aci-preupgrade-validation-script.py index 284e09b8..e2da7021 100644 --- a/aci-preupgrade-validation-script.py +++ b/aci-preupgrade-validation-script.py @@ -1745,7 +1745,7 @@ def _icurl(apitype, query, page=0, page_size=100000): return data -def icurl(apitype, query, page_size=100000): +def _icurl_all_pages(apitype, query, page_size=100000): total_imdata = [] total_cnt = 999999 page = 0 @@ -1762,6 +1762,24 @@ def icurl(apitype, query, page_size=100000): return total_imdata +_icurl_cache = {} +_icurl_locks = {} +_icurl_locks_lock = threading.Lock() + + +def icurl(apitype, query, page_size=100000): + # Checks run in parallel threads and several issue identical queries. Successful + # results are cached for the run; a per-query lock makes concurrent callers of the + # same query wait for the first one instead of all hitting the APIC at once. + cache_key = (apitype, query, page_size) + with _icurl_locks_lock: + key_lock = _icurl_locks.setdefault(cache_key, threading.Lock()) + with key_lock: + if cache_key not in _icurl_cache: + _icurl_cache[cache_key] = _icurl_all_pages(apitype, query, page_size) + return _icurl_cache[cache_key] + + def run_cmd(cmd, splitlines=True): """ Run a shell command. diff --git a/tests/conftest.py b/tests/conftest.py index 348c7d70..654395a3 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -119,6 +119,7 @@ def _mock_icurl(apitype, query, page=0, page_size=100000): return data monkeypatch.setattr(script, "_icurl", _mock_icurl) + monkeypatch.setattr(script, "_icurl_cache", {}) @pytest.fixture