Skip to content

redisclient exists, hmset, hgetallordered function, warm restart table names and table getDbId() function#208

Merged
qiluo-msft merged 11 commits intosonic-net:masterfrom
jipanyang:warm_reboot_collab
Jul 20, 2018
Merged

redisclient exists, hmset, hgetallordered function, warm restart table names and table getDbId() function#208
qiluo-msft merged 11 commits intosonic-net:masterfrom
jipanyang:warm_reboot_collab

Conversation

@jipanyang
Copy link
Copy Markdown
Contributor

This is to add some swss common function support for warm reboot development:

<1> redisclient exists(), hmset(), hgetallordered() functions.
<2> warm restart table names in appDB and configDB
<3> getDbId() function for Table class

@qiluo-msft qiluo-msft requested review from jleveque, pavel-shirshov and qiluo-msft and removed request for jleveque and qiluo-msft July 17, 2018 19:21
@qiluo-msft qiluo-msft assigned lguohan and unassigned lguohan Jul 17, 2018
@qiluo-msft qiluo-msft requested a review from lguohan July 17, 2018 19:22
Copy link
Copy Markdown
Contributor

@pavel-shirshov pavel-shirshov left a comment

Choose a reason for hiding this comment

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

as comments

return r.getContext()->integer;
}

int64_t RedisClient::exists(const string &key)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it's better to use bool as return value

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

int64_t is the return value type from libhiredis for exists call.
We are giving one key as parameter here, using bool as return value seems reasonable. But is it a concern if we deviate from libhiredis as to the return value type?

Copy link
Copy Markdown
Contributor

@pavel-shirshov pavel-shirshov Jul 17, 2018

Choose a reason for hiding this comment

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

Hi Jipan,
I got your point, but I think in case of this library we have abstracted hiredis library and use swss-common instead. EXISTS returns 0 or 1 with Boolean semantics. So I think it's better to make it explicitly Boolean.
When I initially saw that your method return int64_t I checked redis documentation, because it was not clear for me why we need int64_t to return exists result. #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, will change to bool return.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

https://redis.io/commands/exists
Because redis command return integer. However, we don't need it.


In reply to: 203180025 [](ancestors = 203180025)

int64_t RedisClient::exists(const string &key)
{
RedisCommand sexists;
sexists.format("EXISTS %s", key.c_str());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think It would be faster to concatenate to strings than use .format her.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

.format is the redis library function, it is being used in almost all the exist swss-common redis related functions. I assume it should be fast and safe enough?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree with you. But I think concatenating two strings are faster than parse a format string.
So it's more a speed optimization.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

After checking it again, RedisCommand is swss-common class, format is the member function of it which in turn calls redisvFormatCommand() to populate the command data.

I saw comments like "Build the command string accordingly to protocol" in redisvFormatCommand(), probably it is safer to use format() here to avoid any potential issue.

auto it = vmap.begin();
while (it != vmap.end())
{
values.push_back(FieldValueTuple(it->first, it->second));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

,emplace_back(it->first, it->second)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok


vector<FieldValueTuple> values;
auto it = vmap.begin();
while (it != vmap.end())
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is probably better to use
for(const auto it: vmap) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok.

std::map<std::string, std::string> RedisClient::hgetallordered(const std::string &key)
{
RedisCommand sincr;
sincr.format("HGETALL %s", key.c_str());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

use string concatenation instead of format?


map<string, string> map;
for (unsigned int i = 0; i < ctx->elements; i += 2)
map[string(ctx->element[i]->str)] = string(ctx->element[i+1]->str);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

map.emplace(ctx->element[i]->str, ctx->element[i+1]->str) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok


void RedisClient::hmset(const string &key, const vector<FieldValueTuple> &values)
{
if (values.size() == 0)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if (values.empty()) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok


void RedisClient::hmset(const string &key, const std::map<std::string, std::string> &vmap)
{
if (vmap.size() == 0)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

if (vmap.empty())?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok

common/schema.h Outdated
#define PFC_WD_DB 5
#define FLEX_COUNTER_DB 5
#define STATE_DB 6
#define RESTORE_DB 7
Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

RESTORE_DB [](start = 8, length = 10)

Let's postpone schema changes into future PR. #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry, wrong push. Should be gone now.


int64_t RedisClient::exists(const string &key)
{
RedisCommand sexists;
Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

sexists [](start = 17, length = 7)

Let's use a different var name. #Closed

return r.getContext()->integer;
}

int64_t RedisClient::exists(const string &key)
Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

key [](start = 42, length = 3)

If the key contains any blank characters (including \t), there will be problem ignored. #Closed

Signed-off-by: Jipan Yang <[email protected]>
{
if (values.size() == 0)
return;

Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

You don't need this check. Already checked in RedisCommand::formatHMSET() #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ok, will remote the check in hmset()

RedisReply r(m_db, shmset, REDIS_REPLY_STATUS);
}

void RedisClient::hmset(const string &key, const std::map<std::string, std::string> &vmap)
Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

RedisClient::hmset [](start = 5, length = 18)

You can overload formatHMSET(), and call it. #Closed

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Not sure what change you mean here?

Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

You may define another RedisCommand::formatHMSET() function, and call it here. #Closed

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I will provide a PR.


In reply to: 203206068 [](ancestors = 203206068)

return map;
}

std::map<std::string, std::string> RedisClient::hgetallordered(const std::string &key)
Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

hgetallordered [](start = 48, length = 14)

Redis hash fields have no order. What is the use case? #Closed

Copy link
Copy Markdown
Contributor Author

@jipanyang jipanyang Jul 17, 2018

Choose a reason for hiding this comment

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

We have case to sort the field in string alphabetical order, instead of getting data into unordered_map<string, string> then sort them again outside, it is more efficient putting data directly into map<string, string>. #Closed

Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft Jul 17, 2018

Choose a reason for hiding this comment

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

This is a valid use case, however, it makes no sense to implement for every possible type of output container. And redis hash fields has no order by design.

You may implement a general function with inserter. Sample: http://www.cplusplus.com/forum/general/3194/


In reply to: 203201493 [](ancestors = 203201493)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I will provide a PR based on your PR.


In reply to: 203208567 [](ancestors = 203208567,203201493)

Copy link
Copy Markdown
Contributor

@qiluo-msft qiluo-msft left a comment

Choose a reason for hiding this comment

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

As comments.

@jipanyang jipanyang force-pushed the warm_reboot_collab branch from 9ff098a to b1f270e Compare July 20, 2018 00:01
bool RedisClient::exists(const string &key)
{
RedisCommand rexists;
if (key.find_first_of(" \t") != string::npos)
Copy link
Copy Markdown
Contributor

@pavel-shirshov pavel-shirshov Jul 20, 2018

Choose a reason for hiding this comment

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

Probably it's better to trim spaces and tabs from the key? #Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actually this input validation is to prevent any input like "key1 key2". Not for conveniently trimming.


In reply to: 204127076 [](ancestors = 204127076)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm good. Thank you for the explanations. I'd a comment explaining why do we need this validation here.

RedisReply r(m_db, shset, REDIS_REPLY_INTEGER);
}

void RedisClient::hmset(const string &key, const vector<FieldValueTuple> &values)
Copy link
Copy Markdown
Contributor

@pavel-shirshov pavel-shirshov Jul 20, 2018

Choose a reason for hiding this comment

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

implement both functions as a template? The code in them are identical #Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Agree. I will follow up.


In reply to: 204127551 [](ancestors = 204127551)

auto ctx = r.getContext();

unordered_map<string, string> map;
map<string, string> map;
Copy link
Copy Markdown
Contributor

@pavel-shirshov pavel-shirshov Jul 20, 2018

Choose a reason for hiding this comment

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

what it was changed to map? Do we need an order here? #Resolved

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It is not used and should be removed. My mistake and I will fix it.


In reply to: 204127846 [](ancestors = 204127846)

* Fix swig compile

Signed-off-by: Qi Luo <[email protected]>

* Fix: implement template function in header filese

Signed-off-by: Qi Luo <[email protected]>

* Remove unused variable

Signed-off-by: Qi Luo <[email protected]>

* Implement template hmset

Signed-off-by: Qi Luo <[email protected]>
@qiluo-msft qiluo-msft merged commit 9fa06da into sonic-net:master Jul 20, 2018
prgeor pushed a commit to prgeor/sonic-swss-common that referenced this pull request Feb 27, 2025
…erits from YCableBase required for Y-Cable API's in sonic-platform-daemons (sonic-net#208)

This PR adds support for YCable class required for platform-daemons to use the YCable API's for Broadcom.

Description
Basically a vendor specific implementation of abstract YCableBase class .
detailed design discussion can be found https://github.com/Azure/SONiC/pull/757/files

Motivation and Context
Required for transitioning to vendor agnostic API's to be called by xcvrd, so that all types of cables can be called.

How Has This Been Tested?
Ran the changes on Arista7050cx3 switch, making changes inside the container.


Signed-off-by: vaibhav-dahiya <[email protected]>
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.

4 participants