-
Notifications
You must be signed in to change notification settings - Fork 940
Add SIMPLE_STRING reply type to the Module API #2804
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
base: unstable
Are you sure you want to change the base?
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## unstable #2804 +/- ##
============================================
+ Coverage 72.40% 72.41% +0.01%
============================================
Files 128 128
Lines 70270 70280 +10
============================================
+ Hits 50880 50895 +15
+ Misses 19390 19385 -5
🚀 New features to boost your workflow:
|
zuiderkwast
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Yeah I suppose we need this in some form. See comment below.
| static void callReplySimpleStr(void *ctx, const char *str, size_t len, const char *proto, size_t proto_len) { | ||
| CallReply *rep = ctx; | ||
| callReplySetSharedData(rep, VALKEYMODULE_REPLY_STRING, proto, proto_len, 0); | ||
| int type = VALKEYMODULE_REPLY_STRING; | ||
| if (scriptIsRunning()) { | ||
| /* In scripts, simple strings replies should not be converted to string replies. */ | ||
| type = VALKEYMODULE_REPLY_SIMPLE_STRING; | ||
| } | ||
| callReplySetSharedData(rep, type, proto, proto_len, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need this logic to keep backward compatibility with existing modules and with lua scripts?
- Most modules receive all string replies as bulk strings from VM_Call, including the PONG reply.
- Modules implementing a scripting engine need to be aware that if they use
ValkeyModule_Callfrom a scripting engine callback (i.e. when a script is running) the +PONG reply is a simple string. - Scripts that call module-implemented commands that in turn call
ValkeyModule_Call, now the module will receive a simple string in this scenario? Let's MYMOD.PING is a module command that is just a proxy to the built-in PING command usingValkeyModule_Call("PING"). If it's called from a client asMYMOD.PINGthe module receives a bulk string PONG, but if it's called from a script likeEVAL 'server.call("MYMOD.PING")' 0, the module receives the +PONG as a simple string, just becausescriptIsRunning()is true in this case. It's not intended, right?
Do we need to keep track of which client is doing what here?
Should we instead do something like a module capability or flag to ValkeyModule_Call that enables simple string replies for the particular ValkeyModule_Call regardless of script is running?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. We don't want the mymod.ping to behave differently when it's called within a script.
I think having a new flag for the ValkeyModule_Call is the way to go, and is more robust than relying solely on the scriptIsRunning() function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm going to open a PR that introduces the new flag. And then after merging the flag PR, I update this PR to use the flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, good. Are there more reply type differences a module may want to be aware of? If we add them at the same time, we can use the same flag to enable all the more detailed reply types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the only reply type missing for Lua to work as a module.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, but more generically, if a module wants to simulate the behaviour of any existing reply, is there anything more missing here...
Looking at what we have for VM_ReplyWithXXX, the only thing I can see missing is the difference between "null array" and "null bulk string" that is present in RESP 2. A null array is encoded as an array with length -1 and a null string as a string with length -1.
/* Reply to the client with a null array, simply null in RESP3,
* null array in RESP2.
*
* Note: In RESP3 there's no difference between Null reply and
* NullArray reply, so to prevent ambiguity it's better to avoid
* using this API and use ValkeyModule_ReplyWithNull instead.
*
* The function always returns VALKEYMODULE_OK. */
int VM_ReplyWithNullArray(ValkeyModuleCtx *ctx) {
client *c = moduleGetReplyClient(ctx);
if (c == NULL) return VALKEYMODULE_OK;
addReplyNullArray(c);
return VALKEYMODULE_OK;
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not care about different kinds of nulls. 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Opened #2818. Instead of a new VM_Call flag, I decided to add a new ValkeyModuleContext flag, since it's the context that is used all around.
e997e38 to
afe04ed
Compare
afe04ed to
8e493ff
Compare
The new module context flag `VALKEYMODULE_CTX_SCRIPT_EXECUTION` denotes that the module API function is being called in the context of a scripting engine execution. Signed-off-by: Ricardo Dias <ricardo.dias@percona.com>
792e8f9 to
b190ad2
Compare
This commit adds the `SIMPLE_STRING` reply type to the Module API, which is required for scripts to process correctly the reply type of commands called inside scripts. Before this change, commands like `PING` or `SET`, which return `"OK"` as a simple string reply, would be returned as string replies to scripts. To allow the support of the Lua engine as an external module, we need to distinguish between simple string and string replies to keep backward compatibility. Signed-off-by: Ricardo Dias <ricardo.dias@percona.com>
b190ad2 to
0481b6f
Compare
This commit adds the
SIMPLE_STRINGreply type to the Module API, which is required for scripts to process correctly the reply type of commands called inside scripts.Before this change, commands like
PINGorSET, which return"OK"as a simple string reply, would be returned as string reply to scripts. To allow the support of the Lua engine as an external module, we need to distinguish between simple string and string replies to keep backward compatibility.