Skip to content

Commit

Permalink
Revert "DBConnector classes to understand the namespace. (#364)"
Browse files Browse the repository at this point in the history
This reverts commit c0ba527.
  • Loading branch information
judyjoseph authored Jul 27, 2020
1 parent c0ba527 commit 9c2449e
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 554 deletions.
250 changes: 59 additions & 191 deletions common/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,17 @@ using namespace std;

namespace swss {

void SonicDBConfig::parseDatabaseConfig(const string &file,
std::unordered_map<std::string, RedisInstInfo> &inst_entry,
std::unordered_map<std::string, SonicDBInfo> &db_entry,
std::unordered_map<int, std::string> &separator_entry)
void SonicDBConfig::initialize(const string &file)
{

SWSS_LOG_ENTER();

if (m_init)
{
SWSS_LOG_ERROR("SonicDBConfig already initialized");
throw runtime_error("SonicDBConfig already initialized");
}

ifstream i(file);
if (i.good())
{
Expand All @@ -34,7 +40,7 @@ void SonicDBConfig::parseDatabaseConfig(const string &file,
string socket = it.value().at("unix_socket_path");
string hostname = it.value().at("hostname");
int port = it.value().at("port");
inst_entry[instName] = {socket, hostname, port};
m_inst_info[instName] = {socket, {hostname, port}};
}

for (auto it = j["DATABASES"].begin(); it!= j["DATABASES"].end(); it++)
Expand All @@ -43,12 +49,12 @@ void SonicDBConfig::parseDatabaseConfig(const string &file,
string instName = it.value().at("instance");
int dbId = it.value().at("id");
string separator = it.value().at("separator");
db_entry[dbName] = {instName, dbId, separator};
m_db_info[dbName] = {instName, dbId, separator};

separator_entry.emplace(dbId, separator);
m_db_separator.emplace(dbId, separator);
}
m_init = true;
}

catch (domain_error& e)
{
SWSS_LOG_ERROR("key doesn't exist in json object, NULL value has no iterator >> %s\n", e.what());
Expand All @@ -67,152 +73,32 @@ void SonicDBConfig::parseDatabaseConfig(const string &file,
}
}

void SonicDBConfig::initializeGlobalConfig(const string &file)
{
std::string local_file, dir_name, ns_name;
std::unordered_map<std::string, SonicDBInfo> db_entry;
std::unordered_map<std::string, RedisInstInfo> inst_entry;
std::unordered_map<int, std::string> separator_entry;

SWSS_LOG_ENTER();

if (m_global_init)
{
SWSS_LOG_ERROR("SonicDBConfig Global config is already initialized");
return;
}


ifstream i(file);
if (i.good())
{
local_file = dir_name = std::string();

// Get the directory name from the file path given as input.
std::string::size_type pos = file.rfind("/");
if( pos != std::string::npos)
{
dir_name = file.substr(0,pos+1);
}

try
{
json j;
i >> j;

for (auto& element : j["INCLUDES"])
{
local_file.append(dir_name);
local_file.append(element["include"]);

if(element["namespace"].empty())
{
// If database_config.json is already initlized via SonicDBConfig::initialize
// skip initializing it here again.
if(m_init)
{
local_file.clear();
continue;
}
ns_name = EMPTY_NAMESPACE;
}
else
{
ns_name = element["namespace"];
}

parseDatabaseConfig(local_file, inst_entry, db_entry, separator_entry);
m_inst_info[ns_name] = inst_entry;
m_db_info[ns_name] = db_entry;
m_db_separator[ns_name] = separator_entry;

inst_entry.clear();
db_entry.clear();
separator_entry.clear();
local_file.clear();
}
}

catch (domain_error& e)
{
SWSS_LOG_ERROR("key doesn't exist in json object, NULL value has no iterator >> %s\n", e.what());
throw runtime_error("key doesn't exist in json object, NULL value has no iterator >> " + string(e.what()));
}
catch (exception &e)
{
SWSS_LOG_ERROR("Sonic database config file syntax error >> %s\n", e.what());
throw runtime_error("Sonic database config file syntax error >> " + string(e.what()));
}
}
else
{
SWSS_LOG_ERROR("Sonic database global config file doesn't exist at %s\n", file.c_str());
throw runtime_error("Sonic database global config file doesn't exist at " + file);
}

// Set it as the global config file is already parsed and init done.
m_global_init = true;

// Make regular init also done
m_init = true;
}

void SonicDBConfig::initialize(const string &file, const string &nameSpace)
{
std::unordered_map<std::string, SonicDBInfo> db_entry;
std::unordered_map<std::string, RedisInstInfo> inst_entry;
std::unordered_map<int, std::string> separator_entry;

SWSS_LOG_ENTER();

if (m_init)
{
SWSS_LOG_ERROR("SonicDBConfig already initialized");
throw runtime_error("SonicDBConfig already initialized");
}

// namespace string is empty, use the file given as input to parse.
if(nameSpace.empty())
{
parseDatabaseConfig(file, inst_entry, db_entry, separator_entry);
m_inst_info[EMPTY_NAMESPACE] = inst_entry;
m_db_info[EMPTY_NAMESPACE] = db_entry;
m_db_separator[EMPTY_NAMESPACE] = separator_entry;
}
else
// namespace is not empty, use DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE.
initializeGlobalConfig();

// Set it as the config file is already parsed and init done.
m_init = true;
}

string SonicDBConfig::getDbInst(const string &dbName, const string &nameSpace)
string SonicDBConfig::getDbInst(const string &dbName)
{
if (!m_init)
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
return m_db_info[nameSpace].at(dbName).instName;
initialize();
return m_db_info.at(dbName).instName;
}

int SonicDBConfig::getDbId(const string &dbName, const string &nameSpace)
int SonicDBConfig::getDbId(const string &dbName)
{
if (!m_init)
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
return m_db_info[nameSpace].at(dbName).dbId;
initialize();
return m_db_info.at(dbName).dbId;
}

string SonicDBConfig::getSeparator(const string &dbName, const string &nameSpace)
string SonicDBConfig::getSeparator(const string &dbName)
{
if (!m_init)
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
return m_db_info[nameSpace].at(dbName).separator;
initialize();
return m_db_info.at(dbName).separator;
}

string SonicDBConfig::getSeparator(int dbId, const string &nameSpace)
string SonicDBConfig::getSeparator(int dbId)
{
if (!m_init)
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
return m_db_separator[nameSpace].at(dbId);
initialize();
return m_db_separator.at(dbId);
}

string SonicDBConfig::getSeparator(const DBConnector* db)
Expand All @@ -223,61 +109,42 @@ string SonicDBConfig::getSeparator(const DBConnector* db)
}

string dbName = db->getDbName();
string nameSpace = db->getNamespace();
if (dbName.empty())
{
return getSeparator(db->getDbId(), nameSpace);
return getSeparator(db->getDbId());
}
else
{
return getSeparator(dbName, nameSpace);
return getSeparator(dbName);
}
}

string SonicDBConfig::getDbSock(const string &dbName, const string &nameSpace)
string SonicDBConfig::getDbSock(const string &dbName)
{
if (!m_init)
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
return m_inst_info[nameSpace].at(getDbInst(dbName)).unixSocketPath;
initialize();
return m_inst_info.at(getDbInst(dbName)).first;
}

string SonicDBConfig::getDbHostname(const string &dbName, const string &nameSpace)
string SonicDBConfig::getDbHostname(const string &dbName)
{
if (!m_init)
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
return m_inst_info[nameSpace].at(getDbInst(dbName)).hostname;
initialize();
return m_inst_info.at(getDbInst(dbName)).second.first;
}

int SonicDBConfig::getDbPort(const string &dbName, const string &nameSpace)
int SonicDBConfig::getDbPort(const string &dbName)
{
if (!m_init)
initialize(DEFAULT_SONIC_DB_CONFIG_FILE, nameSpace);
return m_inst_info[nameSpace].at(getDbInst(dbName)).port;
}

vector<string> SonicDBConfig::getNamespaces()
{
vector<string> list;

if (!m_global_init)
initializeGlobalConfig();

// This API returns back non-empty namespaces.
for (auto it = m_inst_info.cbegin(); it != m_inst_info.cend(); ++it) {
if(!((it->first).empty()))
list.push_back(it->first);
}

return list;
initialize();
return m_inst_info.at(getDbInst(dbName)).second.second;
}

constexpr const char *SonicDBConfig::DEFAULT_SONIC_DB_CONFIG_FILE;
constexpr const char *SonicDBConfig::DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE;
unordered_map<string, unordered_map<string, RedisInstInfo>> SonicDBConfig::m_inst_info;
unordered_map<string, unordered_map<string, SonicDBInfo>> SonicDBConfig::m_db_info;
unordered_map<string, unordered_map<int, string>> SonicDBConfig::m_db_separator;
unordered_map<string, pair<string, pair<string, int>>> SonicDBConfig::m_inst_info;
unordered_map<string, SonicDBInfo> SonicDBConfig::m_db_info;
unordered_map<int, string> SonicDBConfig::m_db_separator;
bool SonicDBConfig::m_init = false;
bool SonicDBConfig::m_global_init = false;

constexpr const char *DBConnector::DEFAULT_UNIXSOCKET;

Expand All @@ -297,8 +164,7 @@ DBConnector::~DBConnector()

DBConnector::DBConnector(int dbId, const string& hostname, int port,
unsigned int timeout) :
m_dbId(dbId),
m_namespace(EMPTY_NAMESPACE)
m_dbId(dbId)
{
struct timeval tv = {0, (suseconds_t)timeout * 1000};

Expand All @@ -315,8 +181,7 @@ DBConnector::DBConnector(int dbId, const string& hostname, int port,
}

DBConnector::DBConnector(int dbId, const string& unixPath, unsigned int timeout) :
m_dbId(dbId),
m_namespace(EMPTY_NAMESPACE)
m_dbId(dbId)
{
struct timeval tv = {0, (suseconds_t)timeout * 1000};

Expand All @@ -327,31 +192,30 @@ DBConnector::DBConnector(int dbId, const string& unixPath, unsigned int timeout)

if (m_conn->err)
throw system_error(make_error_code(errc::address_not_available),
"Unable to connect to redis (unix-socket)");
"Unable to connect to redis (unixs-socket)");

select(this);
}

DBConnector::DBConnector(const string& dbName, unsigned int timeout, bool isTcpConn, const string& nameSpace)
: m_dbId(SonicDBConfig::getDbId(dbName, nameSpace))
DBConnector::DBConnector(const string& dbName, unsigned int timeout, bool isTcpConn)
: m_dbId(SonicDBConfig::getDbId(dbName))
, m_dbName(dbName)
, m_namespace(nameSpace)
{
struct timeval tv = {0, (suseconds_t)timeout * 1000};

if (timeout)
{
if (isTcpConn)
m_conn = redisConnectWithTimeout(SonicDBConfig::getDbHostname(dbName, nameSpace).c_str(), SonicDBConfig::getDbPort(dbName, nameSpace), tv);
m_conn = redisConnectWithTimeout(SonicDBConfig::getDbHostname(dbName).c_str(), SonicDBConfig::getDbPort(dbName), tv);
else
m_conn = redisConnectUnixWithTimeout(SonicDBConfig::getDbSock(dbName, nameSpace).c_str(), tv);
m_conn = redisConnectUnixWithTimeout(SonicDBConfig::getDbSock(dbName).c_str(), tv);
}
else
{
if (isTcpConn)
m_conn = redisConnect(SonicDBConfig::getDbHostname(dbName, nameSpace).c_str(), SonicDBConfig::getDbPort(dbName, nameSpace));
m_conn = redisConnect(SonicDBConfig::getDbHostname(dbName).c_str(), SonicDBConfig::getDbPort(dbName));
else
m_conn = redisConnectUnix(SonicDBConfig::getDbSock(dbName, nameSpace).c_str());
m_conn = redisConnectUnix(SonicDBConfig::getDbSock(dbName).c_str());
}

if (m_conn->err)
Expand All @@ -376,15 +240,19 @@ string DBConnector::getDbName() const
return m_dbName;
}

string DBConnector::getNamespace() const
{
return m_namespace;
}

DBConnector *DBConnector::newConnector(unsigned int timeout) const
{
DBConnector *ret;
ret = new DBConnector(getDbName(), timeout, (getContext()->connection_type == REDIS_CONN_TCP), getNamespace());
if (getContext()->connection_type == REDIS_CONN_TCP)
ret = new DBConnector(getDbId(),
getContext()->tcp.host,
getContext()->tcp.port,
timeout);
else
ret = new DBConnector(getDbId(),
getContext()->unix_sock.path,
timeout);
ret->m_dbName = m_dbName;
return ret;
}

Expand Down
Loading

0 comments on commit 9c2449e

Please sign in to comment.