Skip to content

Commit 2513da1

Browse files
authored
[dump] Optimized dump state cli and modified tests to not use common data (#2175)
#### Why I did **Optimize Dump State Cli:** With the optimizations made, an entire run of "dump state" will only create 1 `Sonicv2Connector `Object and only call `connect` method once per db. i.e one `DBConnector `Object per DB **Test Stability:** Since the dump state tests are based on data (key and field-value pairs) any changes to those tables will lead to failure in dump state tests. Thus migrated the data_sources for dump_state_test & match_engine_tests from mock_tables to dump_input/ thus making them more stable. #### How I did it #### How to verify it ``` vkarri@230fae5c3278:/sonic/src/sonic-utilities$ pytest-3 tests/dump_tests/ ========================================= test session starts ========================================== platform linux -- Python 3.9.2, pytest-6.0.2, py-1.10.0, pluggy-0.13.0 rootdir: /sonic/src/sonic-utilities/tests, configfile: pytest.ini plugins: pyfakefs-4.5.6, cov-2.10.1 collected 140 items tests/dump_tests/dump_state_test.py ............. [ 9%] tests/dump_tests/match_engine_test.py ................................. [ 32%] tests/dump_tests/module_tests/copp_test.py .......... [ 40%] tests/dump_tests/module_tests/evpn_test.py ...... [ 44%] tests/dump_tests/module_tests/fdb_test.py ............ [ 52%] tests/dump_tests/module_tests/interface_test.py .......... [ 60%] tests/dump_tests/module_tests/port_test.py ...... [ 64%] tests/dump_tests/module_tests/portchannel_member_test.py ... [ 66%] tests/dump_tests/module_tests/portchannel_test.py .... [ 69%] tests/dump_tests/module_tests/route_test.py .......... [ 76%] tests/dump_tests/module_tests/vlan_test.py ..................... [ 91%] tests/dump_tests/module_tests/vxlan_tunnel_map_test.py ..... [ 95%] tests/dump_tests/module_tests/vxlan_tunnel_test.py ....... [100%] ========================================= 140 passed in 1.04s ========================================== ``` #### Previous command output (if the output of a command-line utility has changed) #### New command output (if the output of a command-line utility has changed)
1 parent 9e310e5 commit 2513da1

28 files changed

+646
-230
lines changed

dump/main.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
from tabulate import tabulate
77
from sonic_py_common import multi_asic
88
from utilities_common.constants import DEFAULT_NAMESPACE
9+
from dump.match_infra import RedisSource, JsonSource, MatchEngine, CONN
910
from swsscommon.swsscommon import ConfigDBConnector
10-
from dump.match_infra import RedisSource, JsonSource, ConnectionPool
1111
from dump import plugins
1212

13-
1413
# Autocompletion Helper
1514
def get_available_modules(ctx, args, incomplete):
1615
return [k for k in plugins.dump_modules.keys() if incomplete in k]
@@ -29,8 +28,9 @@ def show_modules(ctx, param, value):
2928

3029

3130
@click.group()
32-
def dump():
33-
pass
31+
@click.pass_context
32+
def dump(ctx):
33+
ctx.obj = MatchEngine()
3434

3535

3636
@dump.command()
@@ -70,8 +70,7 @@ def state(ctx, module, identifier, db, table, key_map, verbose, namespace):
7070
else:
7171
os.environ["VERBOSE"] = "0"
7272

73-
ctx.module = module
74-
obj = plugins.dump_modules[module]()
73+
obj = plugins.dump_modules[module](ctx.obj)
7574

7675
if identifier == "all":
7776
ids = obj.get_all_args(namespace)
@@ -91,10 +90,10 @@ def state(ctx, module, identifier, db, table, key_map, verbose, namespace):
9190
if len(db) > 0:
9291
collected_info = filter_out_dbs(db, collected_info)
9392

94-
vidtorid = extract_rid(collected_info, namespace)
93+
vidtorid = extract_rid(collected_info, namespace, ctx.obj.conn_pool)
9594

9695
if not key_map:
97-
collected_info = populate_fv(collected_info, module, namespace)
96+
collected_info = populate_fv(collected_info, module, namespace, ctx.obj.conn_pool)
9897

9998
for id in vidtorid.keys():
10099
collected_info[id]["ASIC_DB"]["vidtorid"] = vidtorid[id]
@@ -104,8 +103,8 @@ def state(ctx, module, identifier, db, table, key_map, verbose, namespace):
104103
return
105104

106105

107-
def extract_rid(info, ns):
108-
r = RedisSource(ConnectionPool())
106+
def extract_rid(info, ns, conn_pool):
107+
r = RedisSource(conn_pool)
109108
r.connect("ASIC_DB", ns)
110109
vidtorid = {}
111110
vid_cache = {} # Cache Entries to reduce number of Redis Calls
@@ -146,19 +145,20 @@ def filter_out_dbs(db_list, collected_info):
146145
return collected_info
147146

148147

149-
def populate_fv(info, module, namespace):
148+
def populate_fv(info, module, namespace, conn_pool):
150149
all_dbs = set()
151150
for id in info.keys():
152151
for db_name in info[id].keys():
153152
all_dbs.add(db_name)
154153

155154
db_cfg_file = JsonSource()
156-
db_conn = ConnectionPool().initialize_connector(namespace)
157155
for db_name in all_dbs:
158156
if db_name == "CONFIG_FILE":
159157
db_cfg_file.connect(plugins.dump_modules[module].CONFIG_FILE, namespace)
160158
else:
161-
db_conn.connect(db_name)
159+
conn_pool.get(db_name, namespace)
160+
161+
db_conn = conn_pool.cache.get(namespace, {}).get(CONN, None)
162162

163163
final_info = {}
164164
for id in info.keys():

dump/match_infra.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
from sonic_py_common import multi_asic
88
from utilities_common.constants import DEFAULT_NAMESPACE
99

10+
# Constants
11+
CONN = "conn"
12+
CONN_TO = "connected_to"
13+
1014
EXCEP_DICT = {
1115
"INV_REQ": "Argument should be of type MatchRequest",
1216
"INV_DB": "DB provided is not valid",
@@ -249,12 +253,12 @@ def get(self, db_name, ns, update=False):
249253
""" Returns a SonicV2Connector Object and caches it for further requests """
250254
if ns not in self.cache:
251255
self.cache[ns] = {}
252-
self.cache[ns]["conn"] = self.initialize_connector(ns)
253-
self.cache[ns]["connected_to"] = set()
254-
if update or db_name not in self.cache[ns]["connected_to"]:
255-
self.cache[ns]["conn"].connect(db_name)
256-
self.cache[ns]["connected_to"].add(db_name)
257-
return self.cache[ns]["conn"]
256+
self.cache[ns][CONN] = self.initialize_connector(ns)
257+
self.cache[ns][CONN_TO] = set()
258+
if update or db_name not in self.cache[ns][CONN_TO]:
259+
self.cache[ns][CONN].connect(db_name)
260+
self.cache[ns][CONN_TO].add(db_name)
261+
return self.cache[ns][CONN]
258262

259263
def clear(self, namespace=None):
260264
if not namespace:
@@ -264,7 +268,7 @@ def clear(self, namespace=None):
264268

265269
def fill(self, ns, conn, connected_to):
266270
""" Update internal cache """
267-
self.cache[ns] = {'conn': conn, 'connected_to': set(connected_to)}
271+
self.cache[ns] = {CONN: conn, CONN_TO: set(connected_to)}
268272

269273

270274
class MatchEngine:
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"PORT_TABLE:Ethernet0": {
3+
"lanes": "33,34,35,36",
4+
"description": "ARISTA01T2:Ethernet3/1/1",
5+
"pfc_asym": "off",
6+
"mtu": "9100",
7+
"alias": "Ethernet1/1",
8+
"oper_status": "up",
9+
"admin_status": "up",
10+
"role": "Ext",
11+
"speed": "40000",
12+
"asic_port_name": "Eth0-ASIC0"
13+
}
14+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"ASIC_STATE:SAI_OBJECT_TYPE_SWITCH:oid:0x21000000000000": {
3+
"SAI_SWITCH_ATTR_INIT_SWITCH": "true",
4+
"SAI_SWITCH_ATTR_SRC_MAC_ADDRESS": "DE:AD:BE:EF:CA:FE"
5+
}
6+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"PORT|Ethernet0": {
3+
"admin_status": "up",
4+
"alias": "Ethernet1/1",
5+
"asic_port_name": "Eth0-ASIC0",
6+
"description": "ARISTA01T2:Ethernet3/1/1",
7+
"lanes": "33,34,35,36",
8+
"mtu": "9100",
9+
"pfc_asym": "off",
10+
"role": "Ext",
11+
"speed": "40000"
12+
}
13+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"TRANSCEIVER_INFO|Ethernet0": {
3+
"type": "QSFP28 or later",
4+
"vendor_rev": "AC",
5+
"serial": "MT1706FT02064",
6+
"manufacturer": "Mellanox",
7+
"model": "MFA1A00-C003",
8+
"vendor_oui": "00-02-c9",
9+
"vendor_date": "2017-01-13 ",
10+
"connector": "No separable connector",
11+
"encoding": "64B66B",
12+
"ext_identifier": "Power Class 3(2.5W max), CDR present in Rx Tx",
13+
"ext_rateselect_compliance": "QSFP+ Rate Select Version 1",
14+
"cable_type": "Length Cable Assembly(m)",
15+
"cable_length": "3",
16+
"specification_compliance": "{'10/40G Ethernet Compliance Code': '40G Active Cable (XLPPI)'}",
17+
"nominal_bit_rate": "255",
18+
"application_advertisement": "N/A"
19+
}
20+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"PORT_TABLE:Ethernet-BP256": {
3+
"oper_status": "up",
4+
"lanes": "61,62,63,64",
5+
"description": "ASIC0:Eth16-ASIC0",
6+
"pfc_asym": "off",
7+
"mtu": "9100",
8+
"alias": "Ethernet-BP256",
9+
"admin_status": "up",
10+
"speed": "40000",
11+
"asic_port_name": "Eth0-ASIC1"
12+
}
13+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"ASIC_STATE:SAI_OBJECT_TYPE_SWITCH:oid:0x21000000000000": {
3+
"SAI_SWITCH_ATTR_INIT_SWITCH": "true",
4+
"SAI_SWITCH_ATTR_SRC_MAC_ADDRESS": "DE:AD:BE:EF:CA:FF"
5+
}
6+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"PORT|Ethernet-BP256": {
3+
"admin_status": "up",
4+
"alias": "Ethernet-BP256",
5+
"asic_port_name": "Eth0-ASIC1",
6+
"description": "ASIC0:Eth16-ASIC0",
7+
"lanes": "61,62,63,64",
8+
"mtu": "9100",
9+
"pfc_asym": "off",
10+
"role": "Int",
11+
"speed": "40000"
12+
}
13+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"TRANSCEIVER_INFO|Ethernet-BP256": {
3+
"type": "QSFP28 or later",
4+
"vendor_rev": "AC",
5+
"serial": "MT1706FT02064",
6+
"manufacturer": "Mellanox",
7+
"model": "MFA1A00-C003",
8+
"vendor_oui": "00-02-c9",
9+
"vendor_date": "2017-01-13 ",
10+
"connector": "No separable connector",
11+
"encoding": "64B66B",
12+
"ext_identifier": "Power Class 3(2.5W max), CDR present in Rx Tx",
13+
"ext_rateselect_compliance": "QSFP+ Rate Select Version 1",
14+
"cable_type": "Length Cable Assembly(m)",
15+
"cable_length": "3",
16+
"specification_compliance": "{'10/40G Ethernet Compliance Code': '40G Active Cable (XLPPI)'}",
17+
"nominal_bit_rate": "255",
18+
"application_advertisement": "N/A"
19+
}
20+
}
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"PORT_TABLE:Ethernet176": {
3+
"index": "0",
4+
"lanes": "0",
5+
"alias": "etp45",
6+
"speed": "25000",
7+
"oper_status": "up",
8+
"pfc_asym": "off",
9+
"mtu": "9100",
10+
"fec": "rs",
11+
"admin_status": "up"
12+
},
13+
"PORT_TABLE:Ethernet160": {
14+
"index": "0",
15+
"lanes": "0",
16+
"alias": "etp41",
17+
"speed": "25000",
18+
"oper_status": "up",
19+
"pfc_asym": "off",
20+
"mtu": "9100",
21+
"fec": "rs",
22+
"admin_status": "up"
23+
},
24+
"PORT_TABLE:Ethernet164": {
25+
"index": "0",
26+
"lanes": "0",
27+
"alias": "etp42",
28+
"speed": "25000",
29+
"oper_status": "up",
30+
"pfc_asym": "off",
31+
"mtu": "9100",
32+
"fec": "rs",
33+
"admin_status": "up"
34+
},
35+
"PORT_TABLE:Ethernet0": {
36+
"index": "0",
37+
"lanes": "0",
38+
"alias": "Ethernet0",
39+
"description": "ARISTA01T2:Ethernet1",
40+
"speed": "25000",
41+
"oper_status": "down",
42+
"pfc_asym": "off",
43+
"mtu": "9100",
44+
"fec": "rs"
45+
},
46+
"PORT_TABLE:Ethernet200": {
47+
"index": "200",
48+
"lanes": "200,201,202,203",
49+
"alias": "Ethernet200",
50+
"description": "Ethernet200",
51+
"speed": "100000",
52+
"oper_status": "down",
53+
"fec": "rs",
54+
"mtu": "9100",
55+
"tpid": "0x8100",
56+
"pfc_asym": "off",
57+
"admin_status": "up"
58+
}
59+
}
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"ASIC_STATE:SAI_OBJECT_TYPE_HOSTIF:oid:0xd000000000a4d":{
3+
"SAI_HOSTIF_ATTR_TYPE" : "SAI_HOSTIF_TYPE_NETDEV",
4+
"SAI_HOSTIF_ATTR_OBJ_ID": "oid:0x100000000036a",
5+
"SAI_HOSTIF_ATTR_NAME" : "Ethernet176",
6+
"SAI_HOSTIF_ATTR_OPER_STATUS" : "true"
7+
},
8+
"ASIC_STATE:SAI_OBJECT_TYPE_PORT:oid:0x100000000036a": {
9+
"SAI_PORT_ATTR_ADMIN_STATE" : "true",
10+
"SAI_PORT_ATTR_SPEED" : "25000",
11+
"SAI_PORT_ATTR_MTU" : "9122"
12+
},
13+
"ASIC_STATE:SAI_OBJECT_TYPE_HOSTIF:oid:0xd000000000a49":{
14+
"SAI_HOSTIF_ATTR_TYPE" : "SAI_HOSTIF_TYPE_NETDEV",
15+
"SAI_HOSTIF_ATTR_OBJ_ID": "oid:0x10000000002e6",
16+
"SAI_HOSTIF_ATTR_NAME" : "Ethernet160",
17+
"SAI_HOSTIF_ATTR_OPER_STATUS" : "true"
18+
},
19+
"ASIC_STATE:SAI_OBJECT_TYPE_HOSTIF:oid:0xd000000000a4a":{
20+
"SAI_HOSTIF_ATTR_TYPE" : "SAI_HOSTIF_TYPE_NETDEV",
21+
"SAI_HOSTIF_ATTR_OBJ_ID": "oid:0x1000000000307",
22+
"SAI_HOSTIF_ATTR_OPER_STATUS" : "true"
23+
},
24+
"ASIC_STATE:SAI_OBJECT_TYPE_PORT:oid:0x1000000000307": {
25+
"SAI_PORT_ATTR_ADMIN_STATE" : "true",
26+
"SAI_PORT_ATTR_SPEED" : "25000",
27+
"SAI_PORT_ATTR_MTU" : "9122"
28+
},
29+
"ASIC_STATE:SAI_OBJECT_TYPE_SWITCH:oid:0x21000000000000": {
30+
"SAI_SWITCH_ATTR_INIT_SWITCH": "true",
31+
"SAI_SWITCH_ATTR_SRC_MAC_ADDRESS": "DE:AD:BE:EF:CA:FE"
32+
},
33+
"ASIC_STATE:SAI_OBJECT_TYPE_HOSTIF:oid:0xd00000000056d": {
34+
"SAI_HOSTIF_ATTR_NAME": "Ethernet0",
35+
"SAI_HOSTIF_ATTR_OBJ_ID": "oid:0x10000000004a4",
36+
"SAI_HOSTIF_ATTR_OPER_STATUS": "true",
37+
"SAI_HOSTIF_ATTR_TYPE": "SAI_HOSTIF_TYPE_NETDEV",
38+
"SAI_HOSTIF_ATTR_VLAN_TAG": "SAI_HOSTIF_VLAN_TAG_STRIP"
39+
},
40+
"ASIC_STATE:SAI_OBJECT_TYPE_PORT:oid:0x10000000004a4": {
41+
"NULL": "NULL",
42+
"SAI_PORT_ATTR_ADMIN_STATE": "true",
43+
"SAI_PORT_ATTR_MTU": "9122",
44+
"SAI_PORT_ATTR_SPEED": "100000"
45+
},
46+
"VIDTORID":{
47+
"oid:0xd00000000056d": "oid:0xd",
48+
"oid:0x10000000004a4": "oid:0x1690000000001"
49+
}
50+
}

0 commit comments

Comments
 (0)