diff --git a/sonic-db-cli/sonic-db-cli.cpp b/sonic-db-cli/sonic-db-cli.cpp index 51b91633..f253d522 100755 --- a/sonic-db-cli/sonic-db-cli.cpp +++ b/sonic-db-cli/sonic-db-cli.cpp @@ -41,7 +41,7 @@ string handleSingleOperation( const string& netns, const string& db_name, const string& operation, - bool isTcpConn) + bool useUnixSocket) { shared_ptr client; auto host = SonicDBConfig::getDbHostname(db_name, netns); @@ -49,9 +49,9 @@ string handleSingleOperation( 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(db_id, db_socket, 0); } @@ -89,7 +89,7 @@ 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 @@ -97,7 +97,7 @@ int handleAllInstances( list> responses; for (auto& db_name : db_names) { - future response = std::async(std::launch::async, handleSingleOperation, netns, db_name, operation, isTcpConn); + future response = std::async(std::launch::async, handleSingleOperation, netns, db_name, operation, useUnixSocket); responses.push_back(std::move(response)); } @@ -133,22 +133,22 @@ int executeCommands( const string& db_name, vector& commands, const string& netns, - bool isTcpConn) + bool useUnixSocket) { shared_ptr 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(db_id, host, port, 0); + auto db_socket = SonicDBConfig::getDbSock(db_name, netns); + client = make_shared(db_id, db_socket, 0); } else { - auto db_socket = SonicDBConfig::getDbSock(db_name); - client = make_shared(db_id, db_socket, 0); + auto host = SonicDBConfig::getDbHostname(db_name, netns); + auto port = SonicDBConfig::getDbPort(db_name, netns); + client = make_shared(db_id, host, port, 0); } } catch (const exception& e) @@ -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; @@ -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" @@ -317,7 +314,7 @@ int sonic_db_cli( initializeConfig(); } - return handleAllInstances(netns, dbOrOperation, isTcpConn); + return handleAllInstances(netns, dbOrOperation, useUnixSocket); } catch (const exception& e) { diff --git a/tests/cli_ut.cpp b/tests/cli_ut.cpp index 39c80cee..bde4e3eb 100755 --- a/tests/cli_ut.cpp +++ b/tests/cli_ut.cpp @@ -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 @@ -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 @@ -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 @@ -450,24 +450,24 @@ 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(db_name.c_str())); + generateTestData("asic2", const_cast(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(db_name.c_str())); + generateTestData("asic3", const_cast(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(db_name.c_str()); @@ -475,8 +475,8 @@ TEST(sonic_db_cli, test_parallel_cmd) { 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(db_name.c_str()); @@ -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(db_name.c_str())); + generateTestData("asic2", const_cast(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(db_name.c_str())); + generateTestData("asic3", const_cast(db_name.c_str())); } // save 2 DBs in parallel, and get save DB performance diff --git a/tests/redis_multi_db_ut_config/database_config2.json b/tests/redis_multi_db_ut_config/database_config2.json new file mode 100644 index 00000000..e7cc0efb --- /dev/null +++ b/tests/redis_multi_db_ut_config/database_config2.json @@ -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" +} diff --git a/tests/redis_multi_db_ut_config/database_config3.json b/tests/redis_multi_db_ut_config/database_config3.json new file mode 100644 index 00000000..e7cc0efb --- /dev/null +++ b/tests/redis_multi_db_ut_config/database_config3.json @@ -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" +} diff --git a/tests/redis_multi_db_ut_config/database_global.json b/tests/redis_multi_db_ut_config/database_global.json index 3f66a74b..fa0879af 100644 --- a/tests/redis_multi_db_ut_config/database_global.json +++ b/tests/redis_multi_db_ut_config/database_global.json @@ -10,6 +10,14 @@ { "namespace" : "asic1", "include" : "../redis_multi_db_ut_config/database_config1.json" + }, + { + "namespace" : "asic2", + "include" : "../redis_multi_db_ut_config/database_config2.json" + }, + { + "namespace" : "asic3", + "include" : "../redis_multi_db_ut_config/database_config3.json" } ], "VERSION" : "1.0"