From 7c3dcfe0a79857e846e5b0683c422bf5c62a0cf8 Mon Sep 17 00:00:00 2001 From: anamehra <54692434+anamehra@users.noreply.github.com> Date: Fri, 7 Jan 2022 12:58:01 -0800 Subject: [PATCH] Cache connection handles to prevent duplicate (#9636) On a multi-asic Supervisor card, running commands like 'show interface counter' opens a confid_db connection per namespace per interface which results in many duplicate connections exceeding the allowed open file handles. This causes the command to fail. Caching the connections to prevent duplicate handles. --- src/sonic-py-common/sonic_py_common/multi_asic.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/sonic-py-common/sonic_py_common/multi_asic.py b/src/sonic-py-common/sonic_py_common/multi_asic.py index a43275fd1b..662c01800e 100644 --- a/src/sonic-py-common/sonic_py_common/multi_asic.py +++ b/src/sonic-py-common/sonic_py_common/multi_asic.py @@ -27,6 +27,10 @@ DEFAULT_NAMESPACE = '' PORT_ROLE = 'role' +# Dictionary to cache config_db connection handle per namespace +# to prevent duplicate connections from being opened +config_db_handle = {} + def connect_config_db_for_ns(namespace=DEFAULT_NAMESPACE): """ The function connects to the config DB for a given namespace and @@ -237,7 +241,9 @@ def get_all_namespaces(): if is_multi_asic(): for asic in range(num_asics): namespace = "{}{}".format(ASIC_NAME_PREFIX, asic) - config_db = connect_config_db_for_ns(namespace) + if namespace not in config_db_handle: + config_db_handle[namespace] = connect_config_db_for_ns(namespace) + config_db = config_db_handle[namespace] metadata = config_db.get_table('DEVICE_METADATA') if metadata['localhost']['sub_role'] == FRONTEND_ASIC_SUB_ROLE: