Skip to content

Commit

Permalink
Cache connection handles to prevent duplicate (#9636)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
anamehra authored and judyjoseph committed Jan 9, 2022
1 parent 5026e22 commit 7c3dcfe
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/sonic-py-common/sonic_py_common/multi_asic.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
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
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 7c3dcfe

Please sign in to comment.