Skip to content

Removing Redis from internal lua function names and comments#288

Closed
parthpatel wants to merge 0 commit into
valkey-io:unstablefrom
parthpatel:unstable
Closed

Removing Redis from internal lua function names and comments#288
parthpatel wants to merge 0 commit into
valkey-io:unstablefrom
parthpatel:unstable

Conversation

@parthpatel

@parthpatel parthpatel commented Apr 10, 2024

Copy link
Copy Markdown
Contributor
  • Fixed a bug where 'server *' invocation from lua debugger was not working. Manually tested it as follows -
Before 

lua debugger> redis ping
<redis> ping
<reply> "+PONG"
lua debugger> server ping
<error> Unknown Redis Lua debugger command or wrong number of arguments.

After

% ./src/valkey-cli --ldb --eval /tmp/1
* Stopped at 1, stop reason = step over
-> 1   return redis.call('ping');
lua debugger> server ping
<redis> ping
<reply> "+PONG"
  • Functions in lua c files lack of documentation. Adding some documentation in this commit.
  • I removed Redis from function names in eval.c and script_lua.c files. I did not touch log messages in this commit. I also did not touch references to "RedisProtocol".

Comment thread src/script_lua.c Outdated
SERVER_API_NAME,
REDIS_API_NAME,
"__redis__err__handler", /* error handler for eval, currently located on globals.
"__server__err__handler", /* error handler for eval, currently located on globals.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'm not clear if this is a breaking change or not. I was fine just leaving it as redis for now. Since we want to backport this, I would like this PR to represent what we backport.

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.

From what I understand, this is only used to handle errors internally. It is not to be invoked by users anywhere. I changed it in both places, where it is registered and where it gets invoked from.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

People have complained about us touching globals in the past, even ones they weren't supposed to be using. I'm not sure if anyone would materially call this, but I would rather lean on the conservative side and not touch it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Google search returned just one hit on "__redis__err__handler" so I guess the risk is very low here. I think we can also consider aliasing __server__err__handler to __redis__err__handler when defining the handler function, if we are concerned about compat.

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.

I will alias them. If I can't then I will duplicate them.

@madolson Tables are definitely aliased because lua uses a reference for them. That's how, all the fields added after luaRegisterServerAPI to 'server' table magically appear under 'redis' table as well.

Comment thread src/eval.c Outdated

/* register debug commands */
lua_getglobal(lua,"redis");
/* register debug commands. we only need to add it under 'server' as 'redis' is effectively aliased to 'server' table at this point. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
/* register debug commands. we only need to add it under 'server' as 'redis' is effectively aliased to 'server' table at this point. */
/* register debug commands. We only need to add it under 'server' as 'redis' will be created as a copy of server later. */

I think this is closer to how it works.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

We also have 120 line length, and I think this barely spill over that.

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.

yeah, I was using Vim. I need to set this up in IntelliJ, it automatically splits larger lines. I will fix it for this PR manually.

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.

Let's not start a war about this but I believe antirez style is 80 chars width. That's also the standard for terminals. (Open up one a new terminal window you'll see.)

For multi-line comments I think it's nice to keep it around 80. It's easier to read. For long code lines, we can have a soft limit around 120 imo. (Let's not start a war about it though.)

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.

(In emacs press Alt-Q inside a comment to reflow it.)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah. The style the old Redis maintainers used was 120 max length, soft limit around 100, 80 encouraged for multi-line paragraphs with a lot of breaks. The only hard limit was 120.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

clang-format is still on my to-do list. After the "rebranding" work (which pretty much makes the code history discussion moot), I think we should just get out of the style discussion altogether and let the "machine" do the work automatically.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also a nitpick. This comment "we only need to add it under 'server' as 'redis' is effectively aliased to 'server' table at this point" seems to be more relevant to this PR than something we like to keep in the code for a longer time. The reference to the "server" table is pretty clear to me already. The aliasing of "server" to "redis" is a hack that's better be kept in the smallest possible scope.

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.

@PingXie I did not understand your suggestion. Do you want to modify the comment or remove it altogether? I like to have documentation around any code - temporary or not. We can remove the documentation when we remove the code - in this case 'redis' table from lua.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry I meant to say that we don't have to mention that "redis" and "server" are synonymous since that is covered in other part of the code already (like when you set up the "redis" alias for the "server" table).

Comment thread src/script_lua.c Outdated
/* Replace math.random and math.randomseed with custom implementations. */
lua_getglobal(lua,"math");

/* Add random and randomseed functions. */

@madolson madolson Apr 10, 2024

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
/* Add random and randomseed functions. */

I feel like this comment isn't saying anything, but I see similar comments exist throughout this function. So don't feel that strongly about it.

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.

yeah there were many such comments, so instead of removing I just made them a full sentence.

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.

removed.

@madolson madolson left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Generally, can you split this into two PRs.

  1. Strictly what we need to backport.
  2. Everything else. I think all your suggestions are pretty good, they will just be a bit annoying to backport since they are so widespread.

@parthpatel

Copy link
Copy Markdown
Contributor Author

Generally, can you split this into two PRs.

  1. Strictly what we need to backport.
  2. Everything else. I think all your suggestions are pretty good, they will just be a bit annoying to backport since they are so widespread.

Agreed. I will split it, so that the bugfix is easier to backport.

Comment thread src/eval.c Outdated
void ldbLogRedisReply(char *reply) {
void ldbLogServerReply(char *reply) {
sds log = sdsnew("<reply> ");
ldbRedisProtocolToHuman(&log,reply);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There are still a few "Redis" references in this file. Can we fix them all at once?

Suggested change
ldbRedisProtocolToHuman(&log,reply);
ldbServerProtocolToHuman(&log,reply);

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.

I thought we decided to keep references to RedisProtocol? Are we renaming the protocol from RESP to something else?

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.

The protocol is not called "server protocol". It doesn't say much.

I suggested earlier (another PR) that we keep it as RedisProtocol here.

If we insist on changing it, we can change Redis to RESP here, i.e. name the function to ldbRespToHuman or ldbRespProtocolToHuman.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yeah, I would vote updating it to RESP, which feels more accurate.

Comment thread src/script_lua.c Outdated
SERVER_API_NAME,
REDIS_API_NAME,
"__redis__err__handler", /* error handler for eval, currently located on globals.
"__server__err__handler", /* error handler for eval, currently located on globals.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Google search returned just one hit on "__redis__err__handler" so I guess the risk is very low here. I think we can also consider aliasing __server__err__handler to __redis__err__handler when defining the handler function, if we are concerned about compat.

Comment thread src/script_lua.c Outdated
lua_pushcfunction(lua,server_math_randomseed);
lua_settable(lua,-3);

/* overwrite value of global variable 'math' with this modified math table present on top of stack. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit - this line is pretty obvious IMO so probably not worth a code comment

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.

removed.

Comment thread src/script_lua.c Outdated
Comment on lines +1439 to +1444
/* In addition to registering server.call/pcall API, we will throw a customer message when a script accesses undefined global variable.
* LUA stored global variables in the global table, accessible to us on stack at virtual index = LUA_GLOBALSINDEX.
* We will set __index handler in global table's metatable to a custom C function to achieve this - handled by luaSetAllowListProtection.
* Refer to https://www.lua.org/pil/13.4.1.html for documentation on __index and https://www.lua.org/pil/contents.html#13 for documentation on metatables.
* We need to pass global table to lua invocations as parameters. To achieve this, lua_pushvalue invocation brings global variable table to the top of
* the stack by pushing value from global index onto the stack. And lua_pop invocation after luaSetAllowListProtection removes it - resetting the stack to its original state. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The comment LGTM.

I haven't counted but this seems going beyond 120 chars?

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.

yeah fixed it. There are still a lot of existing code lines in the file going beyond 120 characters, so we will need a more automated cleanup.

Comment thread src/script_lua.c Outdated
Comment thread src/eval.c Outdated

/* register debug commands */
lua_getglobal(lua,"redis");
/* register debug commands. we only need to add it under 'server' as 'redis' is effectively aliased to 'server' table at this point. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

clang-format is still on my to-do list. After the "rebranding" work (which pretty much makes the code history discussion moot), I think we should just get out of the style discussion altogether and let the "machine" do the work automatically.

Comment thread src/eval.c Outdated

/* register debug commands */
lua_getglobal(lua,"redis");
/* register debug commands. we only need to add it under 'server' as 'redis' is effectively aliased to 'server' table at this point. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also a nitpick. This comment "we only need to add it under 'server' as 'redis' is effectively aliased to 'server' table at this point" seems to be more relevant to this PR than something we like to keep in the code for a longer time. The reference to the "server" table is pretty clear to me already. The aliasing of "server" to "redis" is a hack that's better be kept in the smallest possible scope.

Comment thread src/eval.c Outdated
Comment thread src/eval.c Outdated
} else if (argc > 1 &&
(!strcasecmp(argv[0],"r") || !strcasecmp(argv[0],"redis"))) {
ldbRedis(lua,argv,argc);
(!strcasecmp(argv[0],"r") || !strcasecmp(argv[0],"redis") || !strcasecmp(argv[0],"server"))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

another nipick - should we consolidate all "redis"/"server" table references on "SERVER_API_NAME" and "REDIS_API_NAME"? since we have done the (indirection) work, might just continue with the pattern?

Comment thread src/script_lua.c Outdated
static int server_math_random (lua_State *L) {
/* the `%' avoids the (rare) case of r==1, and is needed also because on
some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
lua_Number r = (lua_Number)(serverLrand48()%REDIS_LRAND48_MAX) /

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Rename REDIS_LRAND48_MAX?

Suggested change
lua_Number r = (lua_Number)(serverLrand48()%REDIS_LRAND48_MAX) /
lua_Number r = (lua_Number)(serverLrand48()%VALKEY_LRAND48_MAX) /

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.

good catch. done.

@parthpatel parthpatel closed this Apr 11, 2024
madolson pushed a commit that referenced this pull request Apr 11, 2024
…all invocations. (#303)

* Tested it on local instance. This was originally part of
#288 but I am pushing this
separately, so that we can easily merge it into the upcoming release.

```
lua debugger> server ping
<redis> ping
<reply> "+PONG"
lua debugger> redis ping
<redis> ping
<reply> "+PONG"
```

* I also searched for lua debugger related unit tests to add coverage
for this but did not find any relevant test to modify. Leaving it at
that for now.

---------

Signed-off-by: Parth Patel <661497+parthpatel@users.noreply.github.com>
madolson pushed a commit that referenced this pull request Apr 11, 2024
…all invocations. (#303)

* Tested it on local instance. This was originally part of
#288 but I am pushing this
separately, so that we can easily merge it into the upcoming release.

```
lua debugger> server ping
<redis> ping
<reply> "+PONG"
lua debugger> redis ping
<redis> ping
<reply> "+PONG"
```

* I also searched for lua debugger related unit tests to add coverage
for this but did not find any relevant test to modify. Leaving it at
that for now.

---------

Signed-off-by: Parth Patel <661497+parthpatel@users.noreply.github.com>
@parthpatel

Copy link
Copy Markdown
Contributor Author

I accidentally closed this review while resetting the branch, new to github workflows. Reopening this PR. I will take care of the comments a little later in the day.

@parthpatel parthpatel reopened this Apr 11, 2024

@parthpatel parthpatel left a comment

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.

Ran make and make test. Also verified that the error handler still works.

parthp@88665a1dcdcb placeholderkv % ./src/valkey-cli --eval /tmp/1
(error) ERR user_script:1: Script attempted to access nonexistent global variable 'print' script: 08fa7f3474473aaa414ca70bc376721a5d4b087f, on @user_script:1.

Comment thread src/eval.c Outdated

/* register debug commands */
lua_getglobal(lua,"redis");
/* register debug commands. we only need to add it under 'server' as 'redis' is effectively aliased to 'server' table at this point. */

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.

@PingXie I did not understand your suggestion. Do you want to modify the comment or remove it altogether? I like to have documentation around any code - temporary or not. We can remove the documentation when we remove the code - in this case 'redis' table from lua.

Comment thread src/eval.c Outdated
void ldbLogRedisReply(char *reply) {
void ldbLogServerReply(char *reply) {
sds log = sdsnew("<reply> ");
ldbRedisProtocolToHuman(&log,reply);

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.

I thought we decided to keep references to RedisProtocol? Are we renaming the protocol from RESP to something else?

Comment thread src/script_lua.c Outdated
Comment on lines +1439 to +1444
/* In addition to registering server.call/pcall API, we will throw a customer message when a script accesses undefined global variable.
* LUA stored global variables in the global table, accessible to us on stack at virtual index = LUA_GLOBALSINDEX.
* We will set __index handler in global table's metatable to a custom C function to achieve this - handled by luaSetAllowListProtection.
* Refer to https://www.lua.org/pil/13.4.1.html for documentation on __index and https://www.lua.org/pil/contents.html#13 for documentation on metatables.
* We need to pass global table to lua invocations as parameters. To achieve this, lua_pushvalue invocation brings global variable table to the top of
* the stack by pushing value from global index onto the stack. And lua_pop invocation after luaSetAllowListProtection removes it - resetting the stack to its original state. */

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.

yeah fixed it. There are still a lot of existing code lines in the file going beyond 120 characters, so we will need a more automated cleanup.

Comment thread src/script_lua.c Outdated
Comment thread src/script_lua.c Outdated
static int server_math_random (lua_State *L) {
/* the `%' avoids the (rare) case of r==1, and is needed also because on
some systems (SunOS!) `rand()' may return a value larger than RAND_MAX */
lua_Number r = (lua_Number)(serverLrand48()%REDIS_LRAND48_MAX) /

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.

good catch. done.

Comment thread src/script_lua.c Outdated
/* Replace math.random and math.randomseed with custom implementations. */
lua_getglobal(lua,"math");

/* Add random and randomseed functions. */

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.

removed.

Comment thread src/script_lua.c Outdated
lua_pushcfunction(lua,server_math_randomseed);
lua_settable(lua,-3);

/* overwrite value of global variable 'math' with this modified math table present on top of stack. */

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.

removed.

Comment thread src/script_lua.c Outdated
SERVER_API_NAME,
REDIS_API_NAME,
"__redis__err__handler", /* error handler for eval, currently located on globals.
"__server__err__handler", /* error handler for eval, currently located on globals.

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.

I will alias them. If I can't then I will duplicate them.

@madolson Tables are definitely aliased because lua uses a reference for them. That's how, all the fields added after luaRegisterServerAPI to 'server' table magically appear under 'redis' table as well.

Comment thread src/eval.c Outdated
Comment thread src/script_lua.c Outdated
/* Reply to client 'c' converting the top element in the Lua stack to a
* server reply. As a side effect the element is consumed from the stack. */
static void luaReplyToRedisReply(client *c, client* script_client, lua_State *lua) {
static void luaReplyToServerReply(client *c, client* script_client, lua_State *lua) {

@zuiderkwast zuiderkwast Apr 12, 2024

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.

ServerReply = RESP?

Maybe we lost the meaning when renaming to ServerReply? Another option could be luaReplyToRespReply.

Nah... I don't think it's a source of confusion. Ignore this comment. :)

Comment thread src/script_lua.c Outdated
@madolson madolson changed the title Fixed a bug in lua debugger for 'server *' invocations and Removing Redis from internal function names. Removing Redis from internal lua function names and comments Apr 14, 2024
zuiderkwast pushed a commit to zuiderkwast/valkey that referenced this pull request Apr 17, 2024
…all invocations. (valkey-io#303)

* Tested it on local instance. This was originally part of
valkey-io#288 but I am pushing this
separately, so that we can easily merge it into the upcoming release.

```
lua debugger> server ping
<redis> ping
<reply> "+PONG"
lua debugger> redis ping
<redis> ping
<reply> "+PONG"
```

* I also searched for lua debugger related unit tests to add coverage
for this but did not find any relevant test to modify. Leaving it at
that for now.

---------

Signed-off-by: Parth Patel <661497+parthpatel@users.noreply.github.com>
PatrickJS pushed a commit to PatrickJS/placeholderkv that referenced this pull request Apr 24, 2024
…all invocations. (valkey-io#303)

* Tested it on local instance. This was originally part of
valkey-io#288 but I am pushing this
separately, so that we can easily merge it into the upcoming release.

```
lua debugger> server ping
<redis> ping
<reply> "+PONG"
lua debugger> redis ping
<redis> ping
<reply> "+PONG"
```

* I also searched for lua debugger related unit tests to add coverage
for this but did not find any relevant test to modify. Leaving it at
that for now.

---------

Signed-off-by: Parth Patel <661497+parthpatel@users.noreply.github.com>
@madolson

Copy link
Copy Markdown
Member

@parthpatel Are you still planning to work on this?

@hpatro

hpatro commented Jan 6, 2025

Copy link
Copy Markdown
Contributor

This is a closed PR. Removing from backport for 7.2.

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.

5 participants