Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions common/c-api/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ SWSSResult SWSSSonicDBConfig_initialize(const char *path) {
SWSSTry(SonicDBConfig::initialize(path));
}

SWSSResult SWSSSonicDBConfig_initializeGlobalConfig(const char *path) {
SWSSTry(SonicDBConfig::initializeGlobalConfig(path));
SWSSResult SWSSSonicDBConfig_initializeGlobalConfig(const char *path, uint8_t ignore_nonexistent) {
SWSSTry(SonicDBConfig::initializeGlobalConfig(path, ignore_nonexistent));
}

SWSSResult SWSSDBConnector_new_tcp(int32_t dbId, const char *hostname, uint16_t port,
Expand Down
2 changes: 1 addition & 1 deletion common/c-api/dbconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extern "C" {

SWSSResult SWSSSonicDBConfig_initialize(const char *path);

SWSSResult SWSSSonicDBConfig_initializeGlobalConfig(const char *path);
SWSSResult SWSSSonicDBConfig_initializeGlobalConfig(const char *path, uint8_t ignore_nonexistent);

typedef struct SWSSDBConnectorOpaque *SWSSDBConnector;

Expand Down
25 changes: 19 additions & 6 deletions common/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <fstream>
#include <nlohmann/json.hpp>
#include <set>
#include <unistd.h>
#include "logger.h"

#include "common/dbconnector.h"
Expand All @@ -22,8 +23,14 @@ using namespace swss;
void SonicDBConfig::parseDatabaseConfig(const string &file,
std::map<std::string, RedisInstInfo> &inst_entry,
std::unordered_map<std::string, SonicDBInfo> &db_entry,
std::unordered_map<int, std::string> &separator_entry)
std::unordered_map<int, std::string> &separator_entry,
bool ignore_nonexistent)
{
if (ignore_nonexistent && access(file.c_str(), F_OK) == -1) {
SWSS_LOG_NOTICE("Sonic database config file doesn't exist at %s\n", file.c_str());
return;
}

ifstream i(file);
if (i.good())
{
Expand Down Expand Up @@ -75,7 +82,7 @@ void SonicDBConfig::parseDatabaseConfig(const string &file,
}
}

void SonicDBConfig::initializeGlobalConfig(const string &file)
void SonicDBConfig::initializeGlobalConfig(const string &file, bool ignore_nonexistent)
{
std::string dir_name;
std::lock_guard<std::recursive_mutex> guard(m_db_info_mutex);
Expand Down Expand Up @@ -128,10 +135,16 @@ void SonicDBConfig::initializeGlobalConfig(const string &file)
continue;
}

parseDatabaseConfig(local_file, inst_entry, db_entry, separator_entry);
m_inst_info[key] = inst_entry;
m_db_info[key] = db_entry;
m_db_separator[key] = separator_entry;
parseDatabaseConfig(local_file, inst_entry, db_entry, separator_entry, ignore_nonexistent);
// all the entries are empty, then don't add them to the map. It may happen if the included
// config doesn't exist. For example, dash-ha container will only mount the redis instance
// config file for the dpu it is managing.
if (!inst_entry.empty() || !db_entry.empty() || !separator_entry.empty())
{
m_inst_info[key] = inst_entry;
m_db_info[key] = db_entry;
m_db_separator[key] = separator_entry;
}

if(key.isEmpty())
{
Expand Down
11 changes: 6 additions & 5 deletions common/dbconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ struct SonicDBKey
* In our design, we allow multiple containers to share a same namespace.
* So, this combination of container name and namespace is used to uniquely identify a DB instance.
* If the namespace is empty, it means the DB instance is running in the default(host) namespace.
* If the container name is empty, it for adapting the old design that only one DB instance is
* If the container name is empty, it for adapting the old design that only one DB instance is
* running in a namespace.
*/
std::string containerName;
Expand Down Expand Up @@ -99,13 +99,13 @@ class SonicDBConfig
%}
#endif

static void initializeGlobalConfig(const std::string &file = DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
static void initializeGlobalConfig(const std::string &file = DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE, bool ignore_nonexistent = false);
#if defined(SWIG) && defined(SWIGPYTHON)
%pythoncode %{
## TODO: the python function and C++ one is not on-par
@staticmethod
def load_sonic_global_db_config(global_db_file_path=DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE, namespace=None):
SonicDBConfig.initializeGlobalConfig(global_db_file_path)
def load_sonic_global_db_config(global_db_file_path=DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE, namespace=None, ignore_nonexistent=False):
SonicDBConfig.initializeGlobalConfig(global_db_file_path, ignore_nonexistent)
%}
#endif
static void reset();
Expand Down Expand Up @@ -157,7 +157,8 @@ class SonicDBConfig
static void parseDatabaseConfig(const std::string &file,
std::map<std::string, RedisInstInfo> &inst_entry,
std::unordered_map<std::string, SonicDBInfo> &db_entry,
std::unordered_map<int, std::string> &separator_entry);
std::unordered_map<int, std::string> &separator_entry,
bool ignore_nonexistent = false);
static RedisInstInfo& getRedisInfo(const std::string &dbName, const SonicDBKey &key);
static SonicDBInfo& getDbInfo(const std::string &dbName, const SonicDBKey &key);
};
Expand Down
4 changes: 2 additions & 2 deletions crates/swss-common-testing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ pub fn sonic_db_config_init_for_test() {
fs::write("/tmp/db_config_test.json", DB_CONFIG_JSON).unwrap();
fs::write("/tmp/db_global_config_test.json", DB_GLOBAL_CONFIG_JSON).unwrap();
sonic_db_config_initialize("/tmp/db_config_test.json").unwrap();
sonic_db_config_initialize_global("/tmp/db_global_config_test.json").unwrap();
sonic_db_config_initialize_global("/tmp/db_global_config_test.json", false).unwrap();
fs::remove_file("/tmp/db_config_test.json").unwrap();
fs::remove_file("/tmp/db_global_config_test.json").unwrap();
*sonic_db_init = true;
Expand All @@ -206,7 +206,7 @@ fn config_db_config_init_for_test(sock_str: &str) {
fs::write("/tmp/db_config_test.json", db_config_json).unwrap();
fs::write("/tmp/db_global_config_test.json", DB_GLOBAL_CONFIG_JSON).unwrap();
sonic_db_config_initialize("/tmp/db_config_test.json").unwrap();
sonic_db_config_initialize_global("/tmp/db_global_config_test.json").unwrap();
sonic_db_config_initialize_global("/tmp/db_global_config_test.json", false).unwrap();
fs::remove_file("/tmp/db_config_test.json").unwrap();
fs::remove_file("/tmp/db_global_config_test.json").unwrap();
*config_db_init = true;
Expand Down
4 changes: 2 additions & 2 deletions crates/swss-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ pub fn sonic_db_config_initialize(path: &str) -> Result<(), Exception> {
}

/// Rust wrapper around `swss::SonicDBConfig::initializeGlobalConfig`.
pub fn sonic_db_config_initialize_global(path: &str) -> Result<(), Exception> {
pub fn sonic_db_config_initialize_global(path: &str, ignore_nonexistent: bool) -> Result<(), Exception> {
let path = cstr(path);
unsafe { swss_try!(bindings::SWSSSonicDBConfig_initializeGlobalConfig(path.as_ptr())) }
unsafe { swss_try!(bindings::SWSSSonicDBConfig_initializeGlobalConfig(path.as_ptr(), ignore_nonexistent as u8)) }
}

/// Trait for objects that can be stored in a Sonic DB table.
Expand Down
25 changes: 24 additions & 1 deletion tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ using namespace swss;
string existing_file = "./tests/redis_multi_db_ut_config/database_config.json";
string nonexisting_file = "./tests/redis_multi_db_ut_config/database_config_nonexisting.json";
string global_existing_file = "./tests/redis_multi_db_ut_config/database_global.json";

string global_with_invalid_include = "./tests/redis_multi_db_ut_config/database_global_with_invalid_include.json";
#define TEST_DB "APPL_DB"
#define TEST_NAMESPACE "asic0"
#define INVALID_NAMESPACE "invalid"
Expand Down Expand Up @@ -55,6 +55,29 @@ class SwsscommonEnvironment : public ::testing::Environment {
EXPECT_TRUE(strstr(e.what(), "Initialize global DB config using API SonicDBConfig::initializeGlobalConfig"));
}

// Test the global SonicDBConfig::initializeGlobalConfig with non-existing include
SonicDBConfig::initializeGlobalConfig(global_with_invalid_include, true);
cout<<"INIT: load global db config file with invalid include, isGlobalInit = "<<SonicDBConfig::isGlobalInit()<<endl;
EXPECT_TRUE(SonicDBConfig::isGlobalInit());
vector<SonicDBKey> db_keys = SonicDBConfig::getDbKeys();

// Extract containerName from SonicDBKey in db_keys and store in vector<string>
vector<string> cn_actual;
for (const auto& key : db_keys) {
cn_actual.push_back(key.containerName);
}

sort (cn_actual.begin(), cn_actual.end());
vector<string> cn_expected = {"", "dpu0", "dpu2"};
// verify the non-existent include is skipped
EXPECT_EQ(cn_actual.size(), cn_expected.size());
EXPECT_TRUE(std::equal(cn_expected.begin(), cn_expected.end(), cn_actual.begin()));
// reset SonicDBConfig, init should be false
SonicDBConfig::reset();
cout<<"RESET: isInit = "<<SonicDBConfig::isInit()<<endl;
EXPECT_FALSE(SonicDBConfig::isInit());
EXPECT_FALSE(SonicDBConfig::isGlobalInit());

// load local global file, init should be true
SonicDBConfig::initializeGlobalConfig(global_existing_file);
cout<<"INIT: load global db config file, isInit = "<<SonicDBConfig::isGlobalInit()<<endl;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"INCLUDES" : [
{
"include" : "database_config.json"
},
{
"container_name" : "dpu0",
"include" : "../redis_multi_db_ut_config/database_config4.json"
},
{
"container_name" : "dpu1",
"include" : "../redis_multi_db_ut_config/non_existent.json"
},
{
"container_name" : "dpu2",
"include" : "../redis_multi_db_ut_config/database_config5.json"
}
],
"VERSION" : "1.0"
}
Loading