Skip to content

Commit

Permalink
Merge branch 'master' into fix_deserialize_check
Browse files Browse the repository at this point in the history
  • Loading branch information
zbud-msft authored Sep 11, 2023
2 parents 6584de1 + 6a1ff52 commit 58c71ff
Show file tree
Hide file tree
Showing 5 changed files with 237 additions and 38 deletions.
41 changes: 19 additions & 22 deletions sonic-db-cli/sonic-db-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ string handleSingleOperation(
const string& netns,
const string& db_name,
const string& operation,
bool isTcpConn)
bool useUnixSocket)
{
shared_ptr<DBConnector> client;
auto host = SonicDBConfig::getDbHostname(db_name, netns);
string message = "Could not connect to Redis at " + host + ":";
try
{
auto db_id = SonicDBConfig::getDbId(db_name, netns);
if (!isTcpConn && db_name != "redis_chassis.server")
if (useUnixSocket && db_name != "redis_chassis.server")
{
auto db_socket = SonicDBConfig::getDbSock(db_name);
auto db_socket = SonicDBConfig::getDbSock(db_name, netns);
message += db_name + ": Connection refused";
client = make_shared<DBConnector>(db_id, db_socket, 0);
}
Expand Down Expand Up @@ -89,15 +89,15 @@ string handleSingleOperation(
int handleAllInstances(
const string& netns,
const string& operation,
bool isTcpConn)
bool useUnixSocket)
{
auto db_names = SonicDBConfig::getDbList(netns);
// Operate All Redis Instances in Parallel
// TODO: if one of the operations failed, it could fail quickly and not necessary to wait all other operations
list<future<string>> responses;
for (auto& db_name : db_names)
{
future<string> response = std::async(std::launch::async, handleSingleOperation, netns, db_name, operation, isTcpConn);
future<string> response = std::async(std::launch::async, handleSingleOperation, netns, db_name, operation, useUnixSocket);
responses.push_back(std::move(response));
}

Expand Down Expand Up @@ -133,22 +133,22 @@ int executeCommands(
const string& db_name,
vector<string>& commands,
const string& netns,
bool isTcpConn)
bool useUnixSocket)
{
shared_ptr<DBConnector> client = nullptr;
try
{
int db_id = SonicDBConfig::getDbId(db_name, netns);
if (isTcpConn)
if (useUnixSocket)
{
auto host = SonicDBConfig::getDbHostname(db_name, netns);
auto port = SonicDBConfig::getDbPort(db_name, netns);
client = make_shared<DBConnector>(db_id, host, port, 0);
auto db_socket = SonicDBConfig::getDbSock(db_name, netns);
client = make_shared<DBConnector>(db_id, db_socket, 0);
}
else
{
auto db_socket = SonicDBConfig::getDbSock(db_name);
client = make_shared<DBConnector>(db_id, db_socket, 0);
auto host = SonicDBConfig::getDbHostname(db_name, netns);
auto port = SonicDBConfig::getDbPort(db_name, netns);
client = make_shared<DBConnector>(db_id, host, port, 0);
}
}
catch (const exception& e)
Expand Down Expand Up @@ -279,19 +279,16 @@ int sonic_db_cli(
{
auto dbOrOperation = options.m_db_or_op;
auto netns = options.m_namespace;
bool isTcpConn = !options.m_unixsocket;
bool useUnixSocket = options.m_unixsocket;
// Load the database config for the namespace
if (!netns.empty())
{
initializeGlobalConfig();

// Use the unix domain connectivity if namespace not empty.
useUnixSocket = true;
}
else
{
// Use the tcp connectivity if namespace is local and unixsocket cmd_option is present.
isTcpConn = true;
netns = "";
}


if (options.m_cmd.size() != 0)
{
auto commands = options.m_cmd;
Expand All @@ -301,7 +298,7 @@ int sonic_db_cli(
initializeConfig();
}

return executeCommands(dbOrOperation, commands, netns, isTcpConn);
return executeCommands(dbOrOperation, commands, netns, useUnixSocket);
}
else if (dbOrOperation == "PING"
|| dbOrOperation == "SAVE"
Expand All @@ -317,7 +314,7 @@ int sonic_db_cli(
initializeConfig();
}

return handleAllInstances(netns, dbOrOperation, isTcpConn);
return handleAllInstances(netns, dbOrOperation, useUnixSocket);
}
catch (const exception& e)
{
Expand Down
32 changes: 16 additions & 16 deletions tests/cli_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ TEST(sonic_db_cli, test_cli_multi_ns_cmd)
char *args[7];
args[0] = "sonic-db-cli";
args[1] = "-n";
args[2] = "asic0";
args[2] = "asic2";
args[3] = "TEST_DB";

// set key to test DB
Expand Down Expand Up @@ -409,7 +409,7 @@ TEST(sonic_db_cli, test_cli_unix_socket_cmd)
args[0] = "sonic-db-cli";
args[1] = "-s";
args[2] = "-n";
args[3] = "asic0";
args[3] = "asic2";
args[4] = "TEST_DB";

// set key to test DB
Expand All @@ -431,7 +431,7 @@ TEST(sonic_db_cli, test_cli_eval_cmd)
char *args[11];
args[0] = "sonic-db-cli";
args[1] = "-n";
args[2] = "asic0";
args[2] = "asic2";
args[3] = "TEST_DB";

// run eval command: EVAL "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 k1 k2 v1 v2
Expand All @@ -450,33 +450,33 @@ TEST(sonic_db_cli, test_parallel_cmd) {
char *args[7];
args[0] = "sonic-db-cli";
args[1] = "-n";
args[2] = "asic0";
args[2] = "asic2";
args[4] = "SAVE";

auto db_names = swss::SonicDBConfig::getDbList("asic0");
auto db_names = swss::SonicDBConfig::getDbList("asic2");
for (auto& db_name : db_names)
{
generateTestData("asic0", const_cast<char*>(db_name.c_str()));
generateTestData("asic2", const_cast<char*>(db_name.c_str()));
}
db_names = swss::SonicDBConfig::getDbList("asic1");
db_names = swss::SonicDBConfig::getDbList("asic3");
for (auto& db_name : db_names)
{
generateTestData("asic1", const_cast<char*>(db_name.c_str()));
generateTestData("asic3", const_cast<char*>(db_name.c_str()));
}

// save 2 DBs and get save DB performance
auto begin_time = clock();
db_names = swss::SonicDBConfig::getDbList("asic0");
args[2] = "asic0";
db_names = swss::SonicDBConfig::getDbList("asic2");
args[2] = "asic2";
for (auto& db_name : db_names)
{
args[3] = const_cast<char*>(db_name.c_str());
optind = 0;
sonic_db_cli(5, args);
}

db_names = swss::SonicDBConfig::getDbList("asic1");
args[2] = "asic1";
db_names = swss::SonicDBConfig::getDbList("asic3");
args[2] = "asic3";
for (auto& db_name : db_names)
{
args[3] = const_cast<char*>(db_name.c_str());
Expand All @@ -487,15 +487,15 @@ TEST(sonic_db_cli, test_parallel_cmd) {
auto sequential_time = float( clock () - begin_time );

// prepare data
db_names = swss::SonicDBConfig::getDbList("asic0");
db_names = swss::SonicDBConfig::getDbList("asic2");
for (auto& db_name : db_names)
{
generateTestData("asic0", const_cast<char*>(db_name.c_str()));
generateTestData("asic2", const_cast<char*>(db_name.c_str()));
}
db_names = swss::SonicDBConfig::getDbList("asic1");
db_names = swss::SonicDBConfig::getDbList("asic3");
for (auto& db_name : db_names)
{
generateTestData("asic0", const_cast<char*>(db_name.c_str()));
generateTestData("asic3", const_cast<char*>(db_name.c_str()));
}

// save 2 DBs in parallel, and get save DB performance
Expand Down
97 changes: 97 additions & 0 deletions tests/redis_multi_db_ut_config/database_config2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"INSTANCES": {
"redis":{
"hostname" : "127.0.0.1",
"port": 6379,
"unix_socket_path": "/var/run/redis/redis.sock"
}
},
"DATABASES" : {
"APPL_DB" : {
"id" : 0,
"separator": ":",
"instance" : "redis"
},
"ASIC_DB" : {
"id" : 1,
"separator": ":",
"instance" : "redis"
},
"COUNTERS_DB" : {
"id" : 2,
"separator": ":",
"instance" : "redis"
},
"LOGLEVEL_DB" : {
"id" : 3,
"separator": ":",
"instance" : "redis"
},
"CONFIG_DB" : {
"id" : 4,
"separator": "|",
"instance" : "redis"
},
"PFC_WD_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"FLEX_COUNTER_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"STATE_DB" : {
"id" : 6,
"separator": "|",
"instance" : "redis"
},
"SNMP_OVERLAY_DB" : {
"id" : 7,
"separator": "|",
"instance" : "redis"
},
"RESTAPI_DB": {
"id": 8,
"separator": "|",
"instance": "redis"
},
"GB_ASIC_DB": {
"id": 9,
"separator": ":",
"instance": "redis"
},
"GB_COUNTERS_DB": {
"id": 10,
"separator": ":",
"instance": "redis"
},
"GB_FLEX_COUNTER_DB": {
"id": 11,
"separator": ":",
"instance": "redis"
},
"PROFILE_DB" : {
"id" : 12,
"separator": "|",
"instance" : "redis"
},
"STATE_DB2" : {
"id" : 13,
"separator": "|",
"instance" : "redis"
},
"APPL_STATE_DB" : {
"id" : 14,
"separator": ":",
"instance" : "redis"
},
"TEST_DB" : {
"id" : 15,
"separator": ":",
"instance" : "redis"
}
},
"VERSION" : "1.0"
}
97 changes: 97 additions & 0 deletions tests/redis_multi_db_ut_config/database_config3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
{
"INSTANCES": {
"redis":{
"hostname" : "127.0.0.1",
"port": 6379,
"unix_socket_path": "/var/run/redis/redis.sock"
}
},
"DATABASES" : {
"APPL_DB" : {
"id" : 0,
"separator": ":",
"instance" : "redis"
},
"ASIC_DB" : {
"id" : 1,
"separator": ":",
"instance" : "redis"
},
"COUNTERS_DB" : {
"id" : 2,
"separator": ":",
"instance" : "redis"
},
"LOGLEVEL_DB" : {
"id" : 3,
"separator": ":",
"instance" : "redis"
},
"CONFIG_DB" : {
"id" : 4,
"separator": "|",
"instance" : "redis"
},
"PFC_WD_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"FLEX_COUNTER_DB" : {
"id" : 5,
"separator": ":",
"instance" : "redis"
},
"STATE_DB" : {
"id" : 6,
"separator": "|",
"instance" : "redis"
},
"SNMP_OVERLAY_DB" : {
"id" : 7,
"separator": "|",
"instance" : "redis"
},
"RESTAPI_DB": {
"id": 8,
"separator": "|",
"instance": "redis"
},
"GB_ASIC_DB": {
"id": 9,
"separator": ":",
"instance": "redis"
},
"GB_COUNTERS_DB": {
"id": 10,
"separator": ":",
"instance": "redis"
},
"GB_FLEX_COUNTER_DB": {
"id": 11,
"separator": ":",
"instance": "redis"
},
"PROFILE_DB" : {
"id" : 12,
"separator": "|",
"instance" : "redis"
},
"STATE_DB2" : {
"id" : 13,
"separator": "|",
"instance" : "redis"
},
"APPL_STATE_DB" : {
"id" : 14,
"separator": ":",
"instance" : "redis"
},
"TEST_DB" : {
"id" : 15,
"separator": ":",
"instance" : "redis"
}
},
"VERSION" : "1.0"
}
Loading

0 comments on commit 58c71ff

Please sign in to comment.