-
Notifications
You must be signed in to change notification settings - Fork 261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Populating m_dbName in dbConnector constructor. #589
base: master
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -209,7 +209,7 @@ void SonicDBConfig::validateNamespace(const string &netns) | |||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
SonicDBInfo& SonicDBConfig::getDbInfo(const std::string &dbName, const std::string &netns) | ||||||||||||||
std::unordered_map<std::string, SonicDBInfo> SonicDBConfig::getDbEntry(const std::string &netns) | ||||||||||||||
{ | ||||||||||||||
std::lock_guard<std::recursive_mutex> guard(m_db_info_mutex); | ||||||||||||||
|
||||||||||||||
|
@@ -232,7 +232,12 @@ SonicDBInfo& SonicDBConfig::getDbInfo(const std::string &dbName, const std::stri | |||||||||||||
SWSS_LOG_ERROR("%s", msg.c_str()); | ||||||||||||||
throw out_of_range(msg); | ||||||||||||||
} | ||||||||||||||
auto& infos = foundNetns->second; | ||||||||||||||
return foundNetns->second; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
SonicDBInfo& SonicDBConfig::getDbInfo(const std::string &dbName, const std::string &netns) | ||||||||||||||
{ | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Make sure to hold the lock while we are accessing the |
||||||||||||||
auto infos = getDbEntry(netns); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We want to make sure that we are taking a read-only reference. |
||||||||||||||
auto foundDb = infos.find(dbName); | ||||||||||||||
if (foundDb == infos.end()) | ||||||||||||||
{ | ||||||||||||||
|
@@ -287,6 +292,20 @@ int SonicDBConfig::getDbId(const string &dbName, const string &netns) | |||||||||||||
return getDbInfo(dbName, netns).dbId; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
string SonicDBConfig::getDbName(int dbId, const string &netns) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like a mostly copy-paste of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can extract the common part from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||||||||||||
{ | ||||||||||||||
string db_name; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above.
|
||||||||||||||
auto infos = getDbEntry(netns); | ||||||||||||||
for ( auto it = infos.begin(); it != infos.end(); ++it ) { | ||||||||||||||
auto& db_info = it->second; | ||||||||||||||
if (db_info.dbId == dbId) { | ||||||||||||||
db_name = it->first; | ||||||||||||||
break; | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
return db_name; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
string SonicDBConfig::getSeparator(const string &dbName, const string &netns) | ||||||||||||||
{ | ||||||||||||||
return getDbInfo(dbName, netns).separator; | ||||||||||||||
|
@@ -510,6 +529,7 @@ void DBConnector::select(DBConnector *db) | |||||||||||||
DBConnector::DBConnector(const DBConnector &other) | ||||||||||||||
: RedisContext(other) | ||||||||||||||
, m_dbId(other.m_dbId) | ||||||||||||||
, m_dbName(other.m_dbName) | ||||||||||||||
ntoorchi marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
, m_namespace(other.m_namespace) | ||||||||||||||
{ | ||||||||||||||
select(this); | ||||||||||||||
|
@@ -518,6 +538,7 @@ DBConnector::DBConnector(const DBConnector &other) | |||||||||||||
DBConnector::DBConnector(int dbId, const RedisContext& ctx) | ||||||||||||||
: RedisContext(ctx) | ||||||||||||||
, m_dbId(dbId) | ||||||||||||||
, m_dbName(SonicDBConfig::getDbName(dbId)) | ||||||||||||||
, m_namespace(EMPTY_NAMESPACE) | ||||||||||||||
{ | ||||||||||||||
select(this); | ||||||||||||||
|
@@ -526,6 +547,7 @@ DBConnector::DBConnector(int dbId, const RedisContext& ctx) | |||||||||||||
DBConnector::DBConnector(int dbId, const string& hostname, int port, | ||||||||||||||
unsigned int timeout) | ||||||||||||||
: m_dbId(dbId) | ||||||||||||||
, m_dbName(SonicDBConfig::getDbName(dbId)) | ||||||||||||||
, m_namespace(EMPTY_NAMESPACE) | ||||||||||||||
{ | ||||||||||||||
struct timeval tv = {0, (suseconds_t)timeout * 1000}; | ||||||||||||||
|
@@ -537,6 +559,7 @@ DBConnector::DBConnector(int dbId, const string& hostname, int port, | |||||||||||||
|
||||||||||||||
DBConnector::DBConnector(int dbId, const string& unixPath, unsigned int timeout) | ||||||||||||||
: m_dbId(dbId) | ||||||||||||||
, m_dbName(SonicDBConfig::getDbName(dbId)) | ||||||||||||||
, m_namespace(EMPTY_NAMESPACE) | ||||||||||||||
{ | ||||||||||||||
struct timeval tv = {0, (suseconds_t)timeout * 1000}; | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ class SonicDBConfig | |
static void validateNamespace(const std::string &netns); | ||
static std::string getDbInst(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE); | ||
static int getDbId(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE); | ||
static std::string getDbName(int dbId, const std::string &netns = EMPTY_NAMESPACE); | ||
static std::string getSeparator(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE); | ||
static std::string getSeparator(int dbId, const std::string &netns = EMPTY_NAMESPACE); | ||
static std::string getSeparator(const DBConnector* db); | ||
|
@@ -96,6 +97,7 @@ class SonicDBConfig | |
std::unordered_map<std::string, RedisInstInfo> &inst_entry, | ||
std::unordered_map<std::string, SonicDBInfo> &db_entry, | ||
std::unordered_map<int, std::string> &separator_entry); | ||
static std::unordered_map<std::string, SonicDBInfo> getDbEntry(const std::string &netns = EMPTY_NAMESPACE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggest renaming this to Also make sure the signature matches whatever we put in the .cc file |
||
static SonicDBInfo& getDbInfo(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE); | ||
static RedisInstInfo& getRedisInfo(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE); | ||
}; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to avoid copying the map.