Skip to content

Commit 792e8f9

Browse files
committed
Add SIMPLE_STRING reply type to the Module API
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>
1 parent 16ba309 commit 792e8f9

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

src/call_reply.c

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#define REPLY_FLAG_ROOT (1 << 0)
3434
#define REPLY_FLAG_PARSED (1 << 1)
3535
#define REPLY_FLAG_RESP3 (1 << 2)
36+
#define REPLY_FLAG_SIMPLE_STRING (1 << 3)
3637

3738
/* --------------------------------------------------------
3839
* An opaque struct used to parse a RESP protocol reply and
@@ -102,7 +103,9 @@ static void callReplyError(void *ctx, const char *str, size_t len, const char *p
102103

103104
static void callReplySimpleStr(void *ctx, const char *str, size_t len, const char *proto, size_t proto_len) {
104105
CallReply *rep = ctx;
105-
callReplySetSharedData(rep, VALKEYMODULE_REPLY_STRING, proto, proto_len, 0);
106+
int type = rep->flags & REPLY_FLAG_SIMPLE_STRING ? VALKEYMODULE_REPLY_SIMPLE_STRING
107+
: VALKEYMODULE_REPLY_STRING;
108+
callReplySetSharedData(rep, type, proto, proto_len, 0);
106109
rep->len = len;
107110
rep->val.str = str;
108111
}
@@ -306,6 +309,7 @@ int callReplyType(CallReply *rep) {
306309

307310
/* Return reply string as buffer and len. Applicable to:
308311
* - VALKEYMODULE_REPLY_STRING
312+
* - VALKEYMODULE_REPLY_SIMPLE_STRING
309313
* - VALKEYMODULE_REPLY_ERROR
310314
*
311315
* The return value is borrowed from CallReply, so it must not be freed
@@ -316,7 +320,9 @@ int callReplyType(CallReply *rep) {
316320
*/
317321
const char *callReplyGetString(CallReply *rep, size_t *len) {
318322
callReplyParse(rep);
319-
if (rep->type != VALKEYMODULE_REPLY_STRING && rep->type != VALKEYMODULE_REPLY_ERROR) return NULL;
323+
if (rep->type != VALKEYMODULE_REPLY_STRING &&
324+
rep->type != VALKEYMODULE_REPLY_SIMPLE_STRING &&
325+
rep->type != VALKEYMODULE_REPLY_ERROR) return NULL;
320326
if (len) *len = rep->len;
321327
return rep->val.str;
322328
}
@@ -563,3 +569,13 @@ CallReply *callReplyCreateError(sds reply, void *private_data) {
563569
listAddNodeTail(deferred_error_list, sdsnew(err_buff));
564570
return callReplyCreate(err_buff, deferred_error_list, private_data);
565571
}
572+
573+
/* Enable the parsing of simple strings flag.
574+
*
575+
* This flag enforces the distinction between simple strings and bulk strings
576+
* when parsing the CallReply.
577+
*/
578+
void enableParseSimpleStringFlag(CallReply *rep) {
579+
serverAssert(!(rep->flags & REPLY_FLAG_PARSED));
580+
rep->flags |= REPLY_FLAG_SIMPLE_STRING;
581+
}

src/call_reply.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,6 @@ int callReplyIsResp3(CallReply *rep);
5656
list *callReplyDeferredErrorList(CallReply *rep);
5757
void freeCallReply(CallReply *rep);
5858
CallReply *callReplyCreatePromise(void *private_data);
59+
void enableParseSimpleStringFlag(CallReply *rep);
5960

6061
#endif /* SRC_CALL_REPLY_H_ */

src/module.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,9 @@ static CallReply *moduleParseReply(client *c, ValkeyModuleCtx *ctx) {
911911
listDelNode(c->reply, listFirst(c->reply));
912912
}
913913
CallReply *reply = callReplyCreate(proto, c->deferred_reply_errors, ctx);
914+
if (ctx && ctx->flags & VALKEYMODULE_CTX_SCRIPT_EXECUTION) {
915+
enableParseSimpleStringFlag(reply);
916+
}
914917
c->deferred_reply_errors = NULL; /* now the responsibility of the reply object. */
915918
return reply;
916919
}
@@ -6179,6 +6182,7 @@ ValkeyModuleString *VM_CreateStringFromCallReply(ValkeyModuleCallReply *reply) {
61796182
const char *str;
61806183
switch (callReplyType(reply)) {
61816184
case VALKEYMODULE_REPLY_STRING:
6185+
case VALKEYMODULE_REPLY_SIMPLE_STRING:
61826186
case VALKEYMODULE_REPLY_ERROR: str = callReplyGetString(reply, &len); return VM_CreateString(ctx, str, len);
61836187
case VALKEYMODULE_REPLY_INTEGER: {
61846188
char buf[64];

src/valkeymodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ typedef long long ustime_t;
112112
#define VALKEYMODULE_REPLY_VERBATIM_STRING 10
113113
#define VALKEYMODULE_REPLY_ATTRIBUTE 11
114114
#define VALKEYMODULE_REPLY_PROMISE 12
115+
#define VALKEYMODULE_REPLY_SIMPLE_STRING 13
115116

116117
/* Postponed array length. */
117118
#define VALKEYMODULE_POSTPONED_ARRAY_LEN -1 /* Deprecated, please use VALKEYMODULE_POSTPONED_LEN */

0 commit comments

Comments
 (0)