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.
This commit is contained in:
anamehra 2022-01-07 12:58:01 -08:00 committed by Judy Joseph
parent 5026e22333
commit 7c3dcfe0a7

View File

@ -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: