Skip to content

Commit e997e38

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 c88c94e commit e997e38

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

src/call_reply.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
* POSSIBILITY OF SUCH DAMAGE.
2828
*/
2929

30+
#include "script.h"
3031
#include "server.h"
32+
#include "valkeymodule.h"
3133
#include "call_reply.h"
3234

3335
#define REPLY_FLAG_ROOT (1 << 0)
@@ -102,7 +104,12 @@ static void callReplyError(void *ctx, const char *str, size_t len, const char *p
102104

103105
static void callReplySimpleStr(void *ctx, const char *str, size_t len, const char *proto, size_t proto_len) {
104106
CallReply *rep = ctx;
105-
callReplySetSharedData(rep, VALKEYMODULE_REPLY_STRING, proto, proto_len, 0);
107+
int type = VALKEYMODULE_REPLY_STRING;
108+
if (scriptIsRunning()) {
109+
/* In scripts, simple strings replies should not be converted to string replies. */
110+
type = VALKEYMODULE_REPLY_SIMPLE_STRING;
111+
}
112+
callReplySetSharedData(rep, type, proto, proto_len, 0);
106113
rep->len = len;
107114
rep->val.str = str;
108115
}
@@ -306,6 +313,7 @@ int callReplyType(CallReply *rep) {
306313

307314
/* Return reply string as buffer and len. Applicable to:
308315
* - VALKEYMODULE_REPLY_STRING
316+
* - VALKEYMODULE_REPLY_SIMPLE_STRING
309317
* - VALKEYMODULE_REPLY_ERROR
310318
*
311319
* The return value is borrowed from CallReply, so it must not be freed
@@ -316,7 +324,7 @@ int callReplyType(CallReply *rep) {
316324
*/
317325
const char *callReplyGetString(CallReply *rep, size_t *len) {
318326
callReplyParse(rep);
319-
if (rep->type != VALKEYMODULE_REPLY_STRING && rep->type != VALKEYMODULE_REPLY_ERROR) return NULL;
327+
if (rep->type != VALKEYMODULE_REPLY_STRING && rep->type != VALKEYMODULE_REPLY_SIMPLE_STRING && rep->type != VALKEYMODULE_REPLY_ERROR) return NULL;
320328
if (len) *len = rep->len;
321329
return rep->val.str;
322330
}

src/call_reply.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include "resp_parser.h"
3434

3535
typedef struct CallReply CallReply;
36-
typedef void (*ValkeyModuleOnUnblocked)(void *ctx, CallReply *reply, void *private_data);
3736

3837
CallReply *callReplyCreate(sds reply, list *deferred_error_list, void *private_data);
3938
CallReply *callReplyCreateError(sds reply, void *private_data);

src/module.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ typedef struct ValkeyModuleCommand ValkeyModuleCommand;
267267
* way depending on the function called on the reply structure. By default
268268
* only the type, proto and protolen are filled. */
269269
typedef struct CallReply ValkeyModuleCallReply;
270+
typedef void (*ValkeyModuleOnUnblocked)(void *ctx, ValkeyModuleCallReply *reply, void *private_data);
270271

271272
/* Structure to hold the module auth callback & the Module implementing it. */
272273
typedef struct ValkeyModuleAuthCtx {
@@ -6178,6 +6179,7 @@ ValkeyModuleString *VM_CreateStringFromCallReply(ValkeyModuleCallReply *reply) {
61786179
const char *str;
61796180
switch (callReplyType(reply)) {
61806181
case VALKEYMODULE_REPLY_STRING:
6182+
case VALKEYMODULE_REPLY_SIMPLE_STRING:
61816183
case VALKEYMODULE_REPLY_ERROR: str = callReplyGetString(reply, &len); return VM_CreateString(ctx, str, len);
61826184
case VALKEYMODULE_REPLY_INTEGER: {
61836185
char buf[64];

src/script.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
#ifndef __SCRIPT_H_
3131
#define __SCRIPT_H_
3232

33+
#include "sds.h"
34+
#include "monotonic.h"
35+
3336
/*
3437
* Script.c unit provides an API for functions and eval
3538
* to interact with the server. Interaction includes mostly
@@ -67,6 +70,8 @@
6770
#define SCRIPT_ALLOW_CROSS_SLOT (1ULL << 8) /* Indicate that the current script may access keys from multiple slots */
6871
typedef struct scriptRunCtx scriptRunCtx;
6972

73+
typedef struct client client;
74+
7075
/* This struct stores the necessary information to manage the execution of
7176
* scripts using EVAL and FCALL. */
7277
struct scriptRunCtx {

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)