From cd83dbc75cf15f1c46037c6f72a0312eb0d8461a Mon Sep 17 00:00:00 2001 From: SuvarnaMeenakshi <50386592+SuvarnaMeenakshi@users.noreply.github.com> Date: Sat, 11 Jul 2020 09:34:11 -0700 Subject: [PATCH] [Namespace]: Fix SNMP AgentX socket connection timeout when using Namespace.get_all() (#140) * [Namespace]: Remove key exists check in dbs_get_all to reduce time taken. Signed-off-by: SuvarnaMeenakshi * Fix as per review comment. Signed-off-by: SuvarnaMeenakshi * Minor fix. Signed-off-by: SuvarnaMeenakshi * Connect to all required namespace dbs during init and periodic sync_d functions to reduce time taken in dbs_get_all. Signed-off-by: SuvarnaMeenakshi * Change test_mibs to use generic wrapper function added. The Namespace functions will be used for both single and multi-asic platforms. Signed-off-by: SuvarnaMeenakshi * Fix review comment. Signed-off-by: SuvarnaMeenakshi * Fix as per review comment. Signed-off-by: SuvarnaMeenakshi * Remove whitespace. Signed-off-by: SuvarnaMeenakshi * Fix review comment. Avoid modifying mutable object kwargs, do a shallow copy instead. Signed-off-by: SuvarnaMeenakshi --- src/sonic_ax_impl/mibs/__init__.py | 37 ++++++++++++++++++------------ tests/test_mibs.py | 6 ++--- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/sonic_ax_impl/mibs/__init__.py b/src/sonic_ax_impl/mibs/__init__.py index e9353861d..9ff5b8f61 100644 --- a/src/sonic_ax_impl/mibs/__init__.py +++ b/src/sonic_ax_impl/mibs/__init__.py @@ -190,9 +190,6 @@ def init_sync_d_interface_tables(db_conn): :return: tuple(if_name_map, if_id_map, oid_map, if_alias_map) """ - # Make sure we're connected to COUNTERS_DB - db_conn.connect(COUNTERS_DB) - # { if_name (SONiC) -> sai_id } # ex: { "Ethernet76" : "1000000000023" } if_name_map, if_id_map = port_util.get_interface_oid_map(db_conn) @@ -232,7 +229,6 @@ def init_sync_d_interface_tables(db_conn): .format(port_util.SONIC_ETHERNET_RE_PATTERN)) logger.warning("Port name map:\n" + pprint.pformat(if_name_map, indent=2)) - db_conn.connect(APPL_DB) if_alias_map = dict() @@ -259,7 +255,6 @@ def init_sync_d_lag_tables(db_conn): # { OID -> lag_name (SONiC) } oid_lag_name_map = {} - db_conn.connect(APPL_DB) lag_entries = db_conn.keys(APPL_DB, b"LAG_TABLE:*") @@ -294,9 +289,6 @@ def init_sync_d_queue_tables(db_conn): :return: tuple(port_queues_map, queue_stat_map) """ - # Make sure we're connected to COUNTERS_DB - db_conn.connect(COUNTERS_DB) - # { Port index : Queue index (SONiC) -> sai_id } # ex: { "1:2" : "1000000000023" } queue_name_map = db_conn.get_all(COUNTERS_DB, COUNTERS_QUEUE_NAME_MAP, blocking=True) @@ -457,8 +449,15 @@ def init_namespace_dbs(): db = SonicV2Connector(use_unix_socket_path=True, namespace=namespace) db_conn.append(db) + Namespace.connect_namespace_dbs(db_conn) return db_conn + @staticmethod + def connect_namespace_dbs(dbs): + list_of_dbs = [APPL_DB, COUNTERS_DB, CONFIG_DB, STATE_DB, ASIC_DB, SNMP_OVERLAY_DB] + for db_name in list_of_dbs: + Namespace.connect_all_dbs(dbs, db_name) + @staticmethod def connect_all_dbs(dbs, db_name): for db_conn in dbs: @@ -471,7 +470,6 @@ def dbs_keys(dbs, db_name, pattern='*'): """ result_keys=[] for db_conn in dbs: - db_conn.connect(db_name) keys = db_conn.keys(db_name, pattern) if keys is not None: result_keys.extend(keys) @@ -483,12 +481,18 @@ def dbs_get_all(dbs, db_name, _hash, *args, **kwargs): db get_all function executed on global and all namespace DBs. """ result = {} + # If there are multiple namespaces, _hash might not be + # present in all namespace, ignore if not present in a + # specfic namespace. + if len(dbs) > 1: + tmp_kwargs = kwargs.copy() + tmp_kwargs['blocking'] = False + else: + tmp_kwargs = kwargs for db_conn in dbs: - db_conn.connect(db_name) - if(db_conn.exists(db_name, _hash)): - ns_result = db_conn.get_all(db_name, _hash, *args, **kwargs) - if ns_result is not None: - result.update(ns_result) + ns_result = db_conn.get_all(db_name, _hash, *args, **tmp_kwargs) + if ns_result is not None: + result.update(ns_result) return result @staticmethod @@ -520,6 +524,7 @@ def init_namespace_sync_d_interface_tables(dbs): Ignore first global db to get interface tables if there are multiple namespaces. """ + Namespace.connect_namespace_dbs(dbs) for db_conn in Namespace.get_non_host_dbs(dbs): if_name_map_ns, \ if_alias_map_ns, \ @@ -547,6 +552,7 @@ def init_namespace_sync_d_lag_tables(dbs): Ignore first global db to get lag tables if there are multiple namespaces. """ + Namespace.connect_namespace_dbs(dbs) for db_conn in Namespace.get_non_host_dbs(dbs): lag_name_if_name_map_ns, \ if_name_lag_name_map_ns, \ @@ -554,7 +560,7 @@ def init_namespace_sync_d_lag_tables(dbs): lag_name_if_name_map.update(lag_name_if_name_map_ns) if_name_lag_name_map.update(if_name_lag_name_map_ns) oid_lag_name_map.update(oid_lag_name_map_ns) - + return lag_name_if_name_map, if_name_lag_name_map, oid_lag_name_map @staticmethod @@ -569,6 +575,7 @@ def init_namespace_sync_d_queue_tables(dbs): Ignore first global db to get queue tables if there are multiple namespaces. """ + Namespace.connect_namespace_dbs(dbs) for db_conn in Namespace.get_non_host_dbs(dbs): port_queues_map_ns, \ queue_stat_map_ns, \ diff --git a/tests/test_mibs.py b/tests/test_mibs.py index 3c28a89e5..578eb54ea 100644 --- a/tests/test_mibs.py +++ b/tests/test_mibs.py @@ -7,7 +7,7 @@ modules_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) sys.path.insert(0, os.path.join(modules_path, 'src')) -from sonic_ax_impl import mibs +from sonic_ax_impl.mibs import Namespace class TestGetNextPDU(TestCase): @classmethod @@ -16,11 +16,11 @@ def setUpClass(cls): tests.mock_tables.dbconnector.load_database_config() def test_init_sync_d_lag_tables(self): - db_conn = mibs.init_db() + db_conn = Namespace.init_namespace_dbs() lag_name_if_name_map, \ if_name_lag_name_map, \ - oid_lag_name_map = mibs.init_sync_d_lag_tables(db_conn) + oid_lag_name_map = Namespace.init_namespace_sync_d_lag_tables(db_conn) self.assertTrue(b"PortChannel04" in lag_name_if_name_map) self.assertTrue(lag_name_if_name_map[b"PortChannel04"] == [b"Ethernet124"])