Skip to content

Commit 6d173f5

Browse files
committed
To fix the issue: show_techsupport & saidump errors during testbed testing
sonic-net#1387
1 parent 8d0f5eb commit 6d173f5

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed

saidump/saidump.cpp

100644100755
+39-28
Original file line numberDiff line numberDiff line change
@@ -502,37 +502,20 @@ static sai_status_t preProcessFile(const std::string file_name)
502502
return SAI_STATUS_SUCCESS;
503503
}
504504

505-
static sai_status_t dumpFromRedisRdbJson(const std::string file_name)
505+
static void traverseJson(const json & jsn)
506506
{
507507
SWSS_LOG_ENTER();
508-
509-
std::ifstream input_file(file_name);
510-
511-
if (!input_file.is_open())
512-
{
513-
SWSS_LOG_ERROR_AND_STDERR("The file %s does not exist for dumping from Redis RDB JSON file.", file_name.c_str());
514-
return SAI_STATUS_FAILURE;
515-
}
516-
517-
try
508+
if (jsn.is_object())
518509
{
519-
// Parse the JSON data from the file (validation)
520-
nlohmann::json jsonData;
521-
input_file >> jsonData;
522-
523-
SWSS_LOG_DEBUG("JSON file is valid.");
524-
525-
for (json::iterator it = jsonData.begin(); it != jsonData.end(); ++it)
510+
for (auto it = jsn.begin(); it != jsn.end(); ++it)
526511
{
527-
json jj_key = it.key();
528-
529-
std::string keystr = jj_key;
512+
std::string keystr = it.key();
530513
std::string item_name = keystr;
531514
size_t pos = keystr.find_first_of(":");
532515

533516
if (pos != std::string::npos)
534517
{
535-
if(ASIC_STATE_TABLE != keystr.substr(0, pos)) // filter out non ASIC_STATE
518+
if(ASIC_STATE_TABLE != keystr.substr(0, pos)) // filter out non "ASIC_STATE" items
536519
{
537520
continue;
538521
}
@@ -550,8 +533,7 @@ static sai_status_t dumpFromRedisRdbJson(const std::string file_name)
550533
}
551534

552535
std::cout << item_name << " " << std::endl;
553-
554-
json jj = it.value();
536+
json jsn_sub = it.value();
555537

556538
if (!it->is_object())
557539
{
@@ -560,11 +542,11 @@ static sai_status_t dumpFromRedisRdbJson(const std::string file_name)
560542

561543
TableMap map;
562544

563-
for (json::iterator itt = jj.begin(); itt != jj.end(); ++itt)
545+
for (auto it_sub = jsn_sub.begin(); it_sub != jsn_sub.end(); ++it_sub)
564546
{
565-
if (itt.key() != "NULL")
547+
if (it_sub.key() != "NULL")
566548
{
567-
map[itt.key()] = itt.value();
549+
map[it_sub.key()] = it_sub.value();
568550
}
569551
}
570552

@@ -577,10 +559,39 @@ static sai_status_t dumpFromRedisRdbJson(const std::string file_name)
577559
std::cout << str_indent << pad_string(field.first, max_len) << " : ";
578560
std::cout << field.second << std::endl;
579561
}
580-
581562
std::cout << std::endl;
582563
}
564+
}
565+
else if(jsn.is_array())
566+
{
567+
for (const auto& element : jsn)
568+
{
569+
if (element.is_object() || element.is_array())
570+
{
571+
traverseJson(element);
572+
}
573+
}
574+
}
575+
}
576+
577+
static sai_status_t dumpFromRedisRdbJson(const std::string file_name)
578+
{
579+
SWSS_LOG_ENTER();
580+
581+
std::ifstream input_file(file_name);
583582

583+
if (!input_file.is_open())
584+
{
585+
SWSS_LOG_ERROR_AND_STDERR("The file %s does not exist for dumping from Redis RDB JSON file.", file_name.c_str());
586+
return SAI_STATUS_FAILURE;
587+
}
588+
589+
try
590+
{
591+
// Parse the JSON data from the file (validation)
592+
json jsonData;
593+
input_file >> jsonData;
594+
traverseJson(jsonData);
584595
return SAI_STATUS_SUCCESS;
585596
}
586597
catch (std::exception &ex)

syncd/scripts/saidump.sh

+8-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ save_saidump_by_rdb()
1010
import json
1111
with open('$filepath') as json_file:
1212
data = json.load(json_file)
13-
print(data['INSTANCES']['redis']['hostname'], data['INSTANCES']['redis']['port'], data['INSTANCES']['redis']['unix_socket_path'])")
13+
print(data['INSTANCES']['redis']['hostname'], data['INSTANCES']['redis']['port'], data['INSTANCES']['redis']['unix_socket_path'])
14+
json_file.close()")
1415

1516
# split
1617
redis_config=(${redis_config// / })
@@ -19,19 +20,16 @@ with open('$filepath') as json_file:
1920
local redis_dir=`dirname ${redis_config[2]}`
2021
logger "saidump.sh: hostname:$hostname, port:$port, redis_dir:$redis_dir"
2122

22-
logger "saidump.sh: [1] Config Redis consistency directory."
23-
redis-cli -h $hostname -p $port CONFIG SET dir $redis_dir > /dev/null
23+
logger "saidump.sh: [1] Get the remote backups of RDB file."
24+
redis-cli -h $hostname -p $port --rdb $redis_dir/dump.rdb > /dev/null 2>&1
2425

25-
logger "saidump.sh: [2] SAVE."
26-
redis-cli -h $hostname -p $port SAVE > /dev/null
26+
logger "saidump.sh: [2] Run rdb-cli command to convert the dump files into JSON files."
27+
rdb-cli $redis_dir/dump.rdb json | tee $redis_dir/dump.json > /dev/null
2728

28-
logger "saidump.sh: [3] Run rdb command to convert the dump files into JSON files."
29-
rdb --command json $redis_dir/dump.rdb | tee $redis_dir/dump.json > /dev/null
30-
31-
logger "saidump.sh: [4] Run saidump -r to update the JSON files' format as same as the saidump before. Then we can get the saidump's result in standard output."
29+
logger "saidump.sh: [3] Run saidump -r to update the JSON files' format as same as the saidump before. Then we can get the saidump's result in standard output."
3230
saidump -r $redis_dir/dump.json -m 100
3331

34-
logger "saidump.sh: [5] Clear the temporary files."
32+
logger "saidump.sh: [4] Clear the temporary files."
3533
rm -f $redis_dir/dump.rdb
3634
rm -f $redis_dir/dump.json
3735
}

0 commit comments

Comments
 (0)