Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

ntoorchi
Copy link

Currently getDbName() returns back an empty string as m_dbName is not populated when constructing dbConnector based on dbId. Here SonicDBConfig::getDbName method is added to find db name based on dbId and populating m_dbName in dbConnector constructor.

Signed-off-by: Niloofar Toorchi niloofar@opennetworking.org

@ghost
Copy link

ghost commented Mar 18, 2022

CLA assistant check
All CLA requirements met.

@ntoorchi
Copy link
Author

@bocon13 @rhalstea

@@ -287,6 +287,41 @@ int SonicDBConfig::getDbId(const string &dbName, const string &netns)
return getDbInfo(dbName, netns).dbId;
}

string SonicDBConfig::getDbName(int dbId, const string &netns)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a mostly copy-paste of SonicDBConfig::getSeparator. Can we refactor the two methods to call a common getDbInfo method that contains the common code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can extract the common part from getDbInfo and getDbName and call it as another method (getDbEntry). getSeparator, however, is using m_db_separator instead of m_db_info. m_db_info also has the information on separator, so it can use the same procedure as getDbName to find the separator based on dbId and then there would be no need to have duplicated information stored in m_db_separator.
@prsunny @qiluo-msft can you please review this PR?

Copy link
Contributor

@bocon13 bocon13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, this looks good to me. Thanks for putting it together!

@ntoorchi
Copy link
Author

@qiluo-msft please review

@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::unordered_map<std::string, SonicDBInfo> SonicDBConfig::getDbEntry(const std::string &netns)
const std::unordered_map<std::string, SonicDBInfo>& SonicDBConfig::getDbEntry(const std::string &netns)

We need to avoid copying the map.


SonicDBInfo& SonicDBConfig::getDbInfo(const std::string &dbName, const std::string &netns)
{
auto infos = getDbEntry(netns);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auto infos = getDbEntry(netns);
auto const& infos = getDbEntry(netns);

We want to make sure that we are taking a read-only reference.

}

SonicDBInfo& SonicDBConfig::getDbInfo(const std::string &dbName, const std::string &netns)
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
{
{
std::lock_guard<std::recursive_mutex> guard(m_db_info_mutex);
SWSS_LOG_ENTER();

Make sure to hold the lock while we are accessing the m_db_info structure. Also, the convention is to log entry into the method.

@@ -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)
{
string db_name;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

  1. hold the log
  2. log entry into the function
  3. make sure we have a reference to the entry map

@@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest renaming this to getDbEntryMap?

Also make sure the signature matches whatever we put in the .cc file

@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getDbName

It is possible that one dbId is mapped to multiple dbName. Like FLEX_COUNTER_DB and PFC_WD_DB are all 5.

Why we need this function?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getDbName(dbId) is needed to populate db_name when creating a DBConnector using a dbId (DBConnector::DBConnector(int dbId, const RedisContext& ctx)), which we call it in pins-infra at p4rt_app/p4rt.cc. Otherwise, DBConnector-> getDbName() returns an empty string. We might need to get the db name for logging purposes.
The majority of dbs have unique ID (as it is expected). Not sure why FLEX_COUNTER_DB and PFC_WD_DB have the same id, but I think their DBConnector can be created through dbName instead of dbId, and then there is no issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants