From 791c9092d9be14cddc802b32d3f306f506ff4ff9 Mon Sep 17 00:00:00 2001 From: jumao Date: Wed, 13 Sep 2023 11:57:49 -0400 Subject: [PATCH] =?UTF-8?q?*=20[saidump]=20=E2=80=A2=20Saidump=20for=20DNX?= =?UTF-8?q?-SAI=20https://github.com/sonic-net/sonic-buildimage/issues/135?= =?UTF-8?q?61?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Solution and modification: To use the Redis-db SAVE option to save the snapshot of DB each time and recover later, instead of looping through each entry in the table and saving it. (1) Updated platform/broadcom/docker-syncd-brcm-dnx/Dockerfile.j2, install Python library rdbtools into the syncd containter. (2) Updated sonic-buildimage/src/sonic-sairedis/saidump/saidump.cpp, add a new option -r, which updates the rdbtools's output-JSON files' format. (3) Updated sonic-buildimage/build_debian.sh, to add a new script file: files/scripts/saidump.sh into the host. This shell file does the below steps: For each ASIC0, such as ASIC0, 1. Save the Redis data. sudo sonic-db-cli -n asic$1 SAVE > /dev/null 2. Move dump files to /var/run/redisX/ docker exec database$1 sh -c "mv /var/lib/redis/dump.rdb /var/run/redis$1/" 3. Run rdb command to convert the dump files into JSON files docker exec syncd$1 sh -c "rdb --command json /var/run/redis$1/dump.rdb | tee /var/run/redis$1/dump.json > /dev/null" 4. Run saidump -r to update the JSON files' format as same as the saidump before. Then we can get the saidump result in standard output. docker exec syncd$1 sh -c "saidump -r /var/run/redis$1/dump.json -m 100" 5. clear sudo rm -f /var/run/redis$1/dump.rdb sudo rm -f /var/run/redis$1/dump.json (4) Update sonic-buildimage/src/sonic-utilities/scripts/generate_dump, to check the asic db size and if it is larger than xxx entries, then do with REDIS SAVE, otherwise, to do with old method: looping through each entry of Redis DB. --- saidump/saidump.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/saidump/saidump.cpp b/saidump/saidump.cpp index f0a9bc282..a23415a1d 100644 --- a/saidump/saidump.cpp +++ b/saidump/saidump.cpp @@ -14,7 +14,7 @@ extern "C" { #include "swss/table.h" #include "meta/sai_serialize.h" #include "sairediscommon.h" -#include "swss/json.hpp" +#include #include @@ -125,7 +125,7 @@ CmdOptions handleCmdLine(int argc, char **argv) } options.rdbJSonSizeLimit = result * 1024 * 1024; - SWSS_LOG_NOTICE("Configure the RDB JSON MAX size to %llu MB", options.rdbJSonSizeLimit / 1024 / 1024); + SWSS_LOG_NOTICE("Configure the RDB JSON MAX size to %llu MB", options.rdbJSonSizeLimit / 1024 / 1024); break; @@ -449,7 +449,7 @@ void dumpGraph(const TableDump& td) #define SWSS_LOG_ERROR_AND_STDERR(format, ...) { fprintf(stderr, format"\n", ##__VA_ARGS__); SWSS_LOG_ERROR(format, ##__VA_ARGS__); } /** - * @brief Preprocess the input JSON file to make sure it's a valid JSON file for Nlohmann JSON library. + * @brief Process the input JSON file to make sure it's a valid JSON file for the JSON library. */ static sai_status_t preProcessFile(const std::string file_name) { @@ -465,9 +465,10 @@ static sai_status_t preProcessFile(const std::string file_name) input_file.seekg(0, std::ios::end); // Move to the end of the file uint64_t file_size = input_file.tellg(); // Get the current position + SWSS_LOG_NOTICE("Get %s's size %ld Bytes, limit: %ld MB.", file_name.c_str(), file_size, g_cmdOptions.rdbJSonSizeLimit / 1024 / 1024); if (file_size >= g_cmdOptions.rdbJSonSizeLimit) - { + { SWSS_LOG_ERROR_AND_STDERR("Get %s's size failure or its size %ld >= %ld MB.", file_name.c_str(), file_size, g_cmdOptions.rdbJSonSizeLimit / 1024 / 1024); return SAI_STATUS_FAILURE; } @@ -488,7 +489,7 @@ static sai_status_t preProcessFile(const std::string file_name) return SAI_STATUS_FAILURE; } - //Romove the 1st and last char to make sure its format is same as previous saidump's output + //Remove the 1st and last char to make sure its format is same as previous output if (content.size() >= 2 && content[0] == '[' && content[content.length()-1] == ']') { outputFile << content.substr(1, content.size()-2); @@ -583,11 +584,11 @@ static sai_status_t dumpFromRedisRdbJson(const std::string file_name) return SAI_STATUS_SUCCESS; } catch (std::exception &ex) - { + { SWSS_LOG_ERROR_AND_STDERR("JSON file %s is invalid.", file_name.c_str()); SWSS_LOG_ERROR_AND_STDERR("JSON parsing error: %s.", ex.what()); } - + return SAI_STATUS_FAILURE; }