Skip to content

Commit

Permalink
lazily cache lagMembers
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh352 committed Jan 15, 2025
1 parent 01c4c16 commit a08cb4f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
56 changes: 34 additions & 22 deletions cfgmgr/portmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "exec.h"
#include "shellcmd.h"
#include <swss/redisutility.h>
#include <unordered_map>

using namespace std;
using namespace swss;
Expand All @@ -22,30 +23,40 @@ PortMgr::PortMgr(DBConnector *cfgDb, DBConnector *appDb, DBConnector *stateDb, c
{
}

bool PortMgr::isLagMember(const std::string &alias, const vector<string> &lagMembers)
bool PortMgr::isLagMember(const std::string &alias, std::unordered_map<std::string, int> &lagMembers)
{
/* Lag members are cached in doTask() as an optimization for high port
* count switches.
* vector<string> lagMembers;
* m_cfgLagMemberTable.getKeys(lagMembers);
/* Lag members are lazily loaded on the first call to isLagMember and cached
* within a variable inside of doTask() for future calls.
*/

for (auto key: keys)
if (lagMembers.empty())
{
auto tokens = tokenize(key, config_db_key_delimiter);
auto lag = tokens[0];
auto member = tokens[1];

if (alias == member)
vector<string> keys;
m_cfgLagMemberTable.getKeys(keys);
for (auto key: keys)
{
return true;
auto tokens = tokenize(key, config_db_key_delimiter);
std::string member = tokens[1];
if (!member.empty()) {
lagMembers[member] = 1;
}
}

/* placeholder to state we already read lagmembers even though there are
* none */
if (lagMembers.empty()) {
lagMembers["none"] = 1;
}
}

if (lagMembers.find(alias) != lagMembers.end())
{
return true;
}

return false;
}

bool PortMgr::setPortMtu(const string &alias, const string &mtu, const vector<string> &lagMembers)
bool PortMgr::setPortMtu(const string &alias, const string &mtu, std::unordered_map<std::string, int> &lagMembers)
{
stringstream cmd;
string res, cmd_str;
Expand All @@ -66,7 +77,7 @@ bool PortMgr::setPortMtu(const string &alias, const string &mtu, const vector<st
SWSS_LOG_WARN("Setting mtu to alias:%s netdev failed with cmd:%s, rc:%d, error:%s", alias.c_str(), cmd_str.c_str(), ret, res.c_str());
return false;
}
else if (isLagMember(lagMembers, alias))
else if (isLagMember(alias, lagMembers))
{
// Catch user improperly specified an MTU on the PortChannel
SWSS_LOG_WARN("Setting mtu to alias:%s which is a member of a PortChannel is invalid", alias.c_str());
Expand Down Expand Up @@ -162,18 +173,19 @@ void PortMgr::doTask(Consumer &consumer)
{
SWSS_LOG_ENTER();

/* Variable to lazily cache lag members upon first call into isLagMember(). We
* don't want to always query for lag members if not needed, and we also don't
* want to query it for each call to isLagMember() which may be on every port.
*/
std::unordered_map<std::string, int> lagMembers;

auto table = consumer.getTableName();
if (table == CFG_SEND_TO_INGRESS_PORT_TABLE_NAME)
{
doSendToIngressPortTask(consumer);
return;
}

/* Cache lag members outside of task iteration as optimization for large
* port count devices rather than doing it within isLagMember() */
vector<string> lagMembers;
m_cfgLagMemberTable.getKeys(lagMembers);

auto it = consumer.m_toSync.begin();
while (it != consumer.m_toSync.end())
{
Expand Down Expand Up @@ -229,7 +241,7 @@ void PortMgr::doTask(Consumer &consumer)
field_values.emplace_back("admin_status", admin_status);
}

if (mtu.empty() && !isLagMember(lagMembers, alias))
if (mtu.empty() && !isLagMember(alias, lagMembers))
{
mtu = DEFAULT_MTU_STR;
field_values.emplace_back("mtu", mtu);
Expand Down Expand Up @@ -257,7 +269,7 @@ void PortMgr::doTask(Consumer &consumer)
if (!mtu.empty())
{
SWSS_LOG_NOTICE("Configure %s MTU to %s", alias.c_str(), mtu.c_str());
setPortMtu(lagMembers, alias, mtu);
setPortMtu(alias, mtu, lagMembers);
}

if (!admin_status.empty())
Expand Down
4 changes: 2 additions & 2 deletions cfgmgr/portmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ class PortMgr : public Orch
void doSendToIngressPortTask(Consumer &consumer);
bool writeConfigToAppDb(const std::string &alias, const std::string &field, const std::string &value);
bool writeConfigToAppDb(const std::string &alias, std::vector<FieldValueTuple> &field_values);
bool setPortMtu(const std::string &alias, const std::string &mtu, const vector<string> &lagMembers);
bool setPortMtu(const std::string &alias, const std::string &mtu, std::unordered_map<std::string, int> &lagMembers);
bool setPortAdminStatus(const std::string &alias, const bool up);
bool isPortStateOk(const std::string &alias);
bool isLagMember(const std::string &alias, const vector<string> &lagMembers);
bool isLagMember(const std::string &alias, std::unordered_map<std::string, int> &lagMembers);
};

}

0 comments on commit a08cb4f

Please sign in to comment.