Skip to content

Commit 82be3c4

Browse files
committed
Cached vidtorid extract and made connpool optim related changes
Signed-off-by: Vivek Reddy Karri <[email protected]>
1 parent bf4ea75 commit 82be3c4

File tree

2 files changed

+24
-24
lines changed

2 files changed

+24
-24
lines changed

dump/main.py

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from dump.match_infra import RedisSource, JsonSource
2-
import plugins
31
import os
42
import sys
53
import json
@@ -8,9 +6,8 @@
86
from tabulate import tabulate
97
from sonic_py_common import multi_asic
108
from utilities_common.constants import DEFAULT_NAMESPACE
11-
from utilities_common.general import load_db_config
12-
13-
sys.path.append(os.path.dirname(__file__))
9+
from dump.match_infra import RedisSource, JsonSource, ConnectionPool
10+
from dump import plugins
1411

1512

1613
# Autocompletion Helper
@@ -59,9 +56,6 @@ def state(ctx, module, identifier, db, table, key_map, verbose, namespace):
5956
click.echo("Namespace option is not valid for a single-ASIC device")
6057
ctx.exit()
6158

62-
# Load database config files
63-
load_db_config()
64-
6559
if multi_asic.is_multi_asic() and (namespace != DEFAULT_NAMESPACE and namespace not in multi_asic.get_namespace_list()):
6660
click.echo("Namespace option is not valid. Choose one of {}".format(multi_asic.get_namespace_list()))
6761
ctx.exit()
@@ -107,29 +101,33 @@ def state(ctx, module, identifier, db, table, key_map, verbose, namespace):
107101

108102

109103
def extract_rid(info, ns):
110-
r = RedisSource()
104+
r = RedisSource(ConnectionPool())
111105
r.connect("ASIC_DB", ns)
112106
vidtorid = {}
107+
vid_cache = {} # Cache Entries to reduce number of Redis Calls
113108
for arg in info.keys():
114-
mp = get_v_r_map(r, info[arg])
109+
mp = get_v_r_map(r, info[arg], vid_cache)
115110
if mp:
116111
vidtorid[arg] = mp
117112
return vidtorid
118113

119114

120-
def get_v_r_map(r, single_dict):
115+
def get_v_r_map(r, single_dict, vid_cache):
121116
v_r_map = {}
122-
asic_obj_ptrn = r"ASIC_STATE:.*:oid:0x\w{1,14}"
117+
asic_obj_ptrn = "ASIC_STATE:.*:oid:0x\w{1,14}"
123118

124-
if "ASIC_DB" in single_dict and "keys" in single_dict["ASIC_DB"]:
125-
for redis_key in single_dict["ASIC_DB"]["keys"]:
119+
if "ASIC_DB" in single_dict and 'keys' in single_dict["ASIC_DB"]:
120+
for redis_key in single_dict["ASIC_DB"]['keys']:
126121
if re.match(asic_obj_ptrn, redis_key):
127122
matches = re.findall(r"oid:0x\w{1,14}", redis_key)
128123
if matches:
129124
vid = matches[0]
130-
v_r_map[vid] = r.hget("ASIC_DB", "VIDTORID", vid)
131-
if not v_r_map[vid]:
132-
v_r_map[vid] = "Real ID Not Found"
125+
if vid in vid_cache:
126+
rid = vid_cache[vid]
127+
else:
128+
rid = r.hget("ASIC_DB", "VIDTORID", vid)
129+
vid_cache[vid] = rid
130+
v_r_map[vid] = rid if rid else "Real ID Not Found"
133131
return v_r_map
134132

135133

@@ -150,14 +148,13 @@ def populate_fv(info, module, namespace):
150148
for db_name in info[id].keys():
151149
all_dbs.add(db_name)
152150

153-
db_dict = {}
151+
db_cfg_file = JsonSource()
152+
db_conn = ConnectionPool().initialize_connector(namespace)
154153
for db_name in all_dbs:
155154
if db_name is "CONFIG_FILE":
156-
db_dict[db_name] = JsonSource()
157-
db_dict[db_name].connect(plugins.dump_modules[module].CONFIG_FILE, namespace)
155+
db_cfg_file.connect(plugins.dump_modules[module].CONFIG_FILE, namespace)
158156
else:
159-
db_dict[db_name] = RedisSource()
160-
db_dict[db_name].connect(db_name, namespace)
157+
db_conn.connect(db_name)
161158

162159
final_info = {}
163160
for id in info.keys():
@@ -167,7 +164,11 @@ def populate_fv(info, module, namespace):
167164
final_info[id][db_name]["keys"] = []
168165
final_info[id][db_name]["tables_not_found"] = info[id][db_name]["tables_not_found"]
169166
for key in info[id][db_name]["keys"]:
170-
final_info[id][db_name]["keys"].append({key: db_dict[db_name].get(db_name, key)})
167+
if db_name is "CONFIG_FILE":
168+
fv = db_dict[db_name].get(db_name, key)
169+
else:
170+
fv = db_conn.get_all(db_name, key)
171+
final_info[id][db_name]["keys"].append({key: fv})
171172

172173
return final_info
173174

tests/dump_tests/dump_state_test.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from utilities_common.db import Db
1111
import traceback
1212
from utilities_common.constants import DEFAULT_NAMESPACE
13-
from .module_tests import mock_sonicv2connector
1413

1514

1615
def compare_json_output(exp_json, rec, exclude_paths=None):

0 commit comments

Comments
 (0)