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

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

Merged
merged 11 commits into from
Jul 20, 2018

Conversation

jipanyang
Copy link
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

Signed-off-by: Jipan Yang <jipan.yang@alibaba-inc.com>
Signed-off-by: Jipan Yang <jipan.yang@alibaba-inc.com>
Signed-off-by: Jipan Yang <jipan.yang@alibaba-inc.com>
@qiluo-msft qiluo-msft requested review from qiluo-msft, jleveque and pavel-shirshov and removed request for qiluo-msft and jleveque 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
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

@@ -20,6 +20,14 @@ int64_t RedisClient::del(const string &key)
return r.getContext()->integer;
}

int64_t RedisClient::exists(const string &key)
Copy link
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Contributor Author

Choose a reason for hiding this comment

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

ok

@@ -35,6 +43,34 @@ void RedisClient::hset(const string &key, const string &field, const string &val
RedisReply r(m_db, shset, REDIS_REPLY_INTEGER);
}

void RedisClient::hmset(const string &key, const vector<FieldValueTuple> &values)
{
if (values.size() == 0)
Copy link
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
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
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
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
@@ -13,6 +13,7 @@ namespace swss {
#define PFC_WD_DB 5
#define FLEX_COUNTER_DB 5
#define STATE_DB 6
#define RESTORE_DB 7
Copy link
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
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.

@@ -20,6 +20,14 @@ int64_t RedisClient::del(const string &key)
return r.getContext()->integer;
}

int64_t RedisClient::exists(const string &key)
{
RedisCommand sexists;
Copy link
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

@@ -20,6 +20,14 @@ int64_t RedisClient::del(const string &key)
return r.getContext()->integer;
}

int64_t RedisClient::exists(const string &key)
Copy link
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 <jipan.yang@alibaba-inc.com>
{
if (values.size() == 0)
return;

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

@@ -57,6 +93,21 @@ unordered_map<string, string> RedisClient::hgetall(const string &key)
return map;
}

std::map<std::string, std::string> RedisClient::hgetallordered(const std::string &key)
Copy link
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
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
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
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
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.

Signed-off-by: Jipan Yang <jipan.yang@alibaba-inc.com>
Signed-off-by: Jipan Yang <jipan.yang@alibaba-inc.com>
qiluo-msft and others added 3 commits July 20, 2018 01:55
Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>
Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>
bool RedisClient::exists(const string &key)
{
RedisCommand rexists;
if (key.find_first_of(" \t") != string::npos)
Copy link
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
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
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.

@@ -35,6 +48,20 @@ void RedisClient::hset(const string &key, const string &field, const string &val
RedisReply r(m_db, shset, REDIS_REPLY_INTEGER);
}

void RedisClient::hmset(const string &key, const vector<FieldValueTuple> &values)
Copy link
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
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)

{
RedisCommand sincr;
sincr.format("HGETALL %s", key.c_str());
RedisReply r(m_db, sincr, REDIS_REPLY_ARRAY);

auto ctx = r.getContext();

unordered_map<string, string> map;
map<string, string> map;
Copy link
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
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 <qiluo-msft@users.noreply.github.com>

* Fix: implement template function in header filese

Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>

* Remove unused variable

Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>

* Implement template hmset

Signed-off-by: Qi Luo <qiluo-msft@users.noreply.github.com>
@qiluo-msft qiluo-msft merged commit 9fa06da into sonic-net:master Jul 20, 2018
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