Skip to content

Commit

Permalink
[syncd] Use lua script to update db when using bulk api (sonic-net#690)
Browse files Browse the repository at this point in the history
* [syncd] Use lua script to update db when using bulk api

* [syncd] Handle multi keys create with empty attribute list

* [syncd] Fix emplace back on non existing object

* [syncd] Update redis client to use corrected del method name
  • Loading branch information
kcudnik authored Nov 6, 2020
1 parent 797197c commit 584ce03
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 43 deletions.
74 changes: 74 additions & 0 deletions syncd/RedisClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,38 @@ void RedisClient::removeTempAsicObject(
m_dbAsic->del(key);
}

void RedisClient::removeAsicObjects(
_In_ const std::vector<std::string>& keys)
{
SWSS_LOG_ENTER();

std::vector<std::string> prefixKeys;

// we need to rewrite keys to add table prefix
for (const auto& key: keys)
{
prefixKeys.push_back((ASIC_STATE_TABLE ":") + key);
}

m_dbAsic->del(prefixKeys);
}

void RedisClient::removeTempAsicObjects(
_In_ const std::vector<std::string>& keys)
{
SWSS_LOG_ENTER();

std::vector<std::string> prefixKeys;

// we need to rewrite keys to add table prefix
for (const auto& key: keys)
{
prefixKeys.push_back((TEMP_PREFIX ASIC_STATE_TABLE ":") + key);
}

m_dbAsic->del(prefixKeys);
}

void RedisClient::setAsicObject(
_In_ const sai_object_meta_key_t& metaKey,
_In_ const std::string& attr,
Expand Down Expand Up @@ -551,6 +583,48 @@ void RedisClient::createTempAsicObject(
}
}

void RedisClient::createAsicObjects(
_In_ const std::unordered_map<std::string, std::vector<swss::FieldValueTuple>>& multiHash)
{
SWSS_LOG_ENTER();

std::unordered_map<std::string, std::vector<std::pair<std::string, std::string>>> hash;

// we need to rewrite hash to add table prefix
for (const auto& kvp: multiHash)
{
hash[(ASIC_STATE_TABLE ":") + kvp.first] = kvp.second;

if (kvp.second.size() == 0)
{
hash[(ASIC_STATE_TABLE ":") + kvp.first].emplace_back(std::make_pair<std::string, std::string>("NULL","NULL"));
}
}

m_dbAsic->hmset(hash);
}

void RedisClient::createTempAsicObjects(
_In_ const std::unordered_map<std::string, std::vector<swss::FieldValueTuple>>& multiHash)
{
SWSS_LOG_ENTER();

std::unordered_map<std::string, std::vector<std::pair<std::string, std::string>>> hash;

// we need to rewrite hash to add table prefix
for (const auto& kvp: multiHash)
{
hash[(TEMP_PREFIX ASIC_STATE_TABLE ":") + kvp.first] = kvp.second;

if (kvp.second.size() == 0)
{
hash[(TEMP_PREFIX ASIC_STATE_TABLE ":") + kvp.first].emplace_back(std::make_pair<std::string, std::string>("NULL","NULL"));
}
}

m_dbAsic->hmset(hash);
}

void RedisClient::setVidAndRidMap(
_In_ const std::unordered_map<sai_object_id_t, sai_object_id_t>& map)
{
Expand Down
12 changes: 12 additions & 0 deletions syncd/RedisClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ namespace syncd
void removeTempAsicObject(
_In_ const sai_object_meta_key_t& metaKey);

void removeAsicObjects(
_In_ const std::vector<std::string>& keys);

void removeTempAsicObjects(
_In_ const std::vector<std::string>& keys);

void createAsicObject(
_In_ const sai_object_meta_key_t& metaKey,
_In_ const std::vector<swss::FieldValueTuple>& attrs);
Expand All @@ -93,6 +99,12 @@ namespace syncd
_In_ const sai_object_meta_key_t& metaKey,
_In_ const std::vector<swss::FieldValueTuple>& attrs);

void createAsicObjects(
_In_ const std::unordered_map<std::string, std::vector<swss::FieldValueTuple>>& multiHash);

void createTempAsicObjects(
_In_ const std::unordered_map<std::string, std::vector<swss::FieldValueTuple>>& multiHash);

void setVidAndRidMap(
_In_ const std::unordered_map<sai_object_id_t, sai_object_id_t>& map);

Expand Down
83 changes: 40 additions & 43 deletions syncd/Syncd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,10 @@ void Syncd::syncUpdateRedisBulkQuadEvent(

const std::string strObjectType = sai_serialize_object_type(objectType);

std::unordered_map<std::string, std::vector<swss::FieldValueTuple>> multiHash;

std::vector<std::string> keys;

for (size_t idx = 0; idx < statuses.size(); idx++)
{
sai_status_t status = statuses[idx];
Expand All @@ -1992,64 +1996,57 @@ void Syncd::syncUpdateRedisBulkQuadEvent(
continue;
}

auto& objectId = objectIds.at(idx);

auto& values = strAttributes.at(idx);

auto key = strObjectType + ":" + objectId;

sai_object_meta_key_t metaKey;
sai_deserialize_object_meta_key(key, metaKey);
auto key = strObjectType + ":" + objectIds.at(idx);

const bool initView = isInitViewMode();
keys.push_back(key);

switch (api)
{
case SAI_COMMON_API_BULK_CREATE:
multiHash[key] = strAttributes.at(idx);
}

{
if (initView)
m_client->createTempAsicObject(metaKey, values);
else
m_client->createAsicObject(metaKey, values);
const bool initView = isInitViewMode();

break;
}
switch (api)
{
case SAI_COMMON_API_BULK_CREATE:

case SAI_COMMON_API_BULK_REMOVE:
{
if (initView)
m_client->createTempAsicObjects(multiHash);
else
m_client->createAsicObjects(multiHash);

{
if (initView)
m_client->removeTempAsicObject(metaKey);
else
m_client->removeAsicObject(metaKey);
break;
}

break;
}
case SAI_COMMON_API_BULK_REMOVE:

case SAI_COMMON_API_BULK_SET:
{
if (initView)
m_client->removeTempAsicObjects(keys);
else
m_client->removeAsicObjects(keys);

{
auto& first = values.at(0);
break;
}

auto& attr = fvField(first);
auto& value = fvValue(first);
case SAI_COMMON_API_BULK_SET:

if (initView)
m_client->setTempAsicObject(metaKey, attr, value);
else
m_client->setAsicObject(metaKey, attr, value);
{
// SET is the same as create
if (initView)
m_client->createTempAsicObjects(multiHash);
else
m_client->createAsicObjects(multiHash);

break;
}
break;
}

case SAI_COMMON_API_GET:
break; // ignore get since get is not modifying db
case SAI_COMMON_API_GET:
break; // ignore get since get is not modifying db

default:
default:

SWSS_LOG_THROW("api %d is not supported", api);
}
SWSS_LOG_THROW("api %d is not supported", api);
}

timer.stop();
Expand Down

0 comments on commit 584ce03

Please sign in to comment.