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

Producer/Consumer table binary support #801

Merged
merged 14 commits into from
Jul 13, 2023

Conversation

Pterosaur
Copy link
Contributor

@Pterosaur Pterosaur commented Jun 26, 2023

We want to make the Producer/Consumer table can support binary messages, The native C string (char *) will be replaced to pointers and its lengths in all paths.
Meanwhile, the Python interfaces of SWIG can only handle the type, str, with UTF-8. So, we need to specialize the SWIG interfaces from bytes of Python to string of C++.

Signed-off-by: Ze Gan <ganze718@gmail.com>
Signed-off-by: Ze Gan <ganze718@gmail.com>
Signed-off-by: Ze Gan <ganze718@gmail.com>
Signed-off-by: Ze Gan <ganze718@gmail.com>
Signed-off-by: Ze Gan <ganze718@gmail.com>
@Pterosaur Pterosaur changed the title Redis binary support Producer/Consumer table binary support Jun 28, 2023
@Pterosaur Pterosaur marked this pull request as ready for review June 28, 2023 13:49
common/luatable.cpp Outdated Show resolved Hide resolved
common/redisapi.h Outdated Show resolved Hide resolved
common/luatable.cpp Outdated Show resolved Hide resolved
@qiluo-msft
Copy link
Contributor

qiluo-msft commented Jun 29, 2023

void RedisCommand::formatArgv(int argc, const char **argv, const size_t *argvlen)

I see a common pattern when using this function. Better to create an overload function, and keep this as is.

void RedisCommand::formatArgv(int argc, iterator<string>& argv)

In reply to: 1612539336


In reply to: 1612539336


In reply to: 1612539336


Refers to: common/rediscommand.cpp:39 in 466a131. [](commit_id = 466a131, deletion_comment = False)

@Pterosaur Pterosaur force-pushed the redis_binary_support branch 2 times, most recently from 98be4ca to 837e19d Compare July 7, 2023 02:15
Signed-off-by: Ze Gan <ganze718@gmail.com>
@Pterosaur
Copy link
Contributor Author

void RedisCommand::formatArgv(int argc, const char **argv, const size_t *argvlen)

I see a common pattern when using this function. Better to create an overload function, and keep this as is.

void RedisCommand::formatArgv(int argc, iterator<string>& argv)

Refers to: common/rediscommand.cpp:39 in 466a131. [](commit_id = 466a131, deletion_comment = False)

I found an existing API, void RedisCommand::format(const vector<string> &commands), which can meet our requirement, so directly leverage it.

@Pterosaur Pterosaur requested a review from qiluo-msft July 7, 2023 15:36

void reset(char *str = nullptr, int len = 0);

const char *getStr() const;
Copy link
Contributor

@qiluo-msft qiluo-msft Jul 8, 2023

Choose a reason for hiding this comment

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

getStr

If you align the function names with RedisCommand, then RedisCommand could be a subclass of RedisString #Closed

@@ -124,12 +113,12 @@ void RedisCommand::formatDEL(const std::string& key)

const char *RedisCommand::c_str() const
Copy link
Contributor

@qiluo-msft qiluo-msft Jul 8, 2023

Choose a reason for hiding this comment

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

c_str

In your new code, m_str may contains \0. Then it is super dangerous to expose c_str to outside. Do you want to deprecate this function? #Closed

Copy link
Contributor Author

@Pterosaur Pterosaur Jul 9, 2023

Choose a reason for hiding this comment

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

I don't think so.

  1. Because we have already provided the function len to get the length, so \0 will be handled.
  2. As the STL string's convention, const char * doesn't matter to the `\0'.
  3. If we don't expose the const char * instead of expose a std::string, which is safer but MAY introduce one extra deep copy which is unnecessary.

This reverts commit bcf697e.
@Pterosaur Pterosaur requested a review from qiluo-msft July 9, 2023 03:17
qiluo-msft
qiluo-msft previously approved these changes Jul 10, 2023
Signed-off-by: Ze Gan <ganze718@gmail.com>
@@ -64,12 +66,17 @@ class RedisCommand {
/* Format DEL key command */
void formatDEL(const std::string& key);

int appendTo(redisContext *ctx) const;

std::string printable_string() const;
Copy link
Contributor

Choose a reason for hiding this comment

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

printable_string

As this repo's practice, we implement const std::string to_string() const member function.

Copy link
Contributor

Choose a reason for hiding this comment

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

Or if you think the semantics are different, you may follow the function naming convention toPrintableString

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -166,4 +166,61 @@ static inline std::string binary_to_hex(const void *buffer, size_t length)
return s;
}

static inline std::string binary_to_printable(const void *buffer, size_t length)
Copy link
Contributor

@qiluo-msft qiluo-msft Jul 12, 2023

Choose a reason for hiding this comment

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

binary_to_printable

Will you implement a reverse function? #WontFix


std::string printable_string() const;

private:
Copy link
Contributor

@qiluo-msft qiluo-msft Jul 12, 2023

Choose a reason for hiding this comment

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

private

This library may be used by unknown app. Let's be safe to mark it as deprecated for a while. This will serve the same purpose to fail sonic related build.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not available, because functions, c_str and len, have been used in other functions of this class.

Signed-off-by: Ze Gan <ganze718@gmail.com>
Signed-off-by: Ze Gan <ganze718@gmail.com>
qiluo-msft
qiluo-msft previously approved these changes Jul 12, 2023
Signed-off-by: Ze Gan <ganze718@gmail.com>
@Pterosaur Pterosaur merged commit 62e8d80 into sonic-net:master Jul 13, 2023
13 checks passed
saiarcot895 added a commit to saiarcot895/sonic-swss-common that referenced this pull request Jul 28, 2023
PR sonic-net#801 added some code to convert binary strings to be printable
strings. However, one code path used the raw value of `len` without
sanity checking as the length of the string, when the `length()`
function should have been used instead.

Sample syncd backtrace:

```

(gdb) print *this
$22 = {temp = 0x55feeaced250 "*3\r\n$6\r\nSCRIPT\r\n$4\r\nLOAD\r\n$863\r\nlocal keys = redis.call('KEYS', KEYS[1])\nlocal n = table.getn(keys)\n\nfor i = 1, n do\n   if KEYS[2] == \"\" and KEYS[3] == \"1\" then\n      redis.call('DEL', keys[i])  \n   e"..., len = -355539936}
```

Fix this by using the `length()` function.

Signed-off-by: Saikrishna Arcot <sarcot@microsoft.com>
SviatoslavBoichuk pushed a commit to SviatoslavBoichuk/sonic-swss-common that referenced this pull request Sep 7, 2023
We want to make the Producer/Consumer table can support binary messages, The native C string (char *) will be replaced to pointers and its lengths in all paths.
Meanwhile, the Python interfaces of SWIG can only handle the type, str, with UTF-8. So, we need to specialize the SWIG interfaces from bytes of Python to string of C++.
---------

Signed-off-by: Ze Gan <ganze718@gmail.com>
Co-authored-by: Qi Luo <qiluo-msft@users.noreply.github.com>
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