2024-03-21 09:30:47 -04:00
|
|
|
// Copyright (c) 2016, Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
// SPDX-FileCopyrightText: 2024 Redict Contributors
|
|
|
|
// SPDX-FileCopyrightText: 2024 Salvatore Sanfilippo <antirez at gmail dot com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
2016-08-03 04:23:03 -04:00
|
|
|
|
2024-03-21 05:49:18 -04:00
|
|
|
#include "redictmodule.h"
|
2016-08-03 12:10:11 -04:00
|
|
|
#include <string.h>
|
2021-09-09 04:03:05 -04:00
|
|
|
#include <stdlib.h>
|
2016-08-03 12:10:11 -04:00
|
|
|
|
|
|
|
/* --------------------------------- Helpers -------------------------------- */
|
|
|
|
|
|
|
|
/* Return true if the reply and the C null term string matches. */
|
|
|
|
int TestMatchReply(RedisModuleCallReply *reply, char *str) {
|
|
|
|
RedisModuleString *mystr;
|
|
|
|
mystr = RedisModule_CreateStringFromCallReply(reply);
|
|
|
|
if (!mystr) return 0;
|
|
|
|
const char *ptr = RedisModule_StringPtrLen(mystr,NULL);
|
|
|
|
return strcmp(ptr,str) == 0;
|
|
|
|
}
|
2016-08-03 04:23:03 -04:00
|
|
|
|
|
|
|
/* ------------------------------- Test units ------------------------------- */
|
|
|
|
|
2016-08-03 12:10:11 -04:00
|
|
|
/* TEST.CALL -- Test Call() API. */
|
|
|
|
int TestCall(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
2016-10-07 07:10:29 -04:00
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
2016-08-03 12:10:11 -04:00
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
RedisModule_Call(ctx,"DEL","c","mylist");
|
|
|
|
RedisModuleString *mystr = RedisModule_CreateString(ctx,"foo",3);
|
|
|
|
RedisModule_Call(ctx,"RPUSH","csl","mylist",mystr,(long long)1234);
|
|
|
|
reply = RedisModule_Call(ctx,"LRANGE","ccc","mylist","0","-1");
|
|
|
|
long long items = RedisModule_CallReplyLength(reply);
|
|
|
|
if (items != 2) goto fail;
|
|
|
|
|
|
|
|
RedisModuleCallReply *item0, *item1;
|
|
|
|
|
|
|
|
item0 = RedisModule_CallReplyArrayElement(reply,0);
|
|
|
|
item1 = RedisModule_CallReplyArrayElement(reply,1);
|
|
|
|
if (!TestMatchReply(item0,"foo")) goto fail;
|
|
|
|
if (!TestMatchReply(item1,"1234")) goto fail;
|
|
|
|
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"OK");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ERR");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
Unified Lua and modules reply parsing and added RESP3 support to RM_Call (#9202)
## Current state
1. Lua has its own parser that handles parsing `reds.call` replies and translates them
to Lua objects that can be used by the user Lua code. The parser partially handles
resp3 (missing big number, verbatim, attribute, ...)
2. Modules have their own parser that handles parsing `RM_Call` replies and translates
them to RedisModuleCallReply objects. The parser does not support resp3.
In addition, in the future, we want to add Redis Function (#8693) that will probably
support more languages. At some point maintaining so many parsers will stop
scaling (bug fixes and protocol changes will need to be applied on all of them).
We will probably end up with different parsers that support different parts of the
resp protocol (like we already have today with Lua and modules)
## PR Changes
This PR attempt to unified the reply parsing of Lua and modules (and in the future
Redis Function) by introducing a new parser unit (`resp_parser.c`). The new parser
handles parsing the reply and calls different callbacks to allow the users (another
unit that uses the parser, i.e, Lua, modules, or Redis Function) to analyze the reply.
### Lua API Additions
The code that handles reply parsing on `scripting.c` was removed. Instead, it uses
the resp_parser to parse and create a Lua object out of the reply. As mentioned
above the Lua parser did not handle parsing big numbers, verbatim, and attribute.
The new parser can handle those and so Lua also gets it for free.
Those are translated to Lua objects in the following way:
1. Big Number - Lua table `{'big_number':'<str representation for big number>'}`
2. Verbatim - Lua table `{'verbatim_string':{'format':'<verbatim format>', 'string':'<verbatim string value>'}}`
3. Attribute - currently ignored and not expose to the Lua parser, another issue will be open to decide how to expose it.
Tests were added to check resp3 reply parsing on Lua
### Modules API Additions
The reply parsing code on `module.c` was also removed and the new resp_parser is used instead.
In addition, the RedisModuleCallReply was also extracted to a separate unit located on `call_reply.c`
(in the future, this unit will also be used by Redis Function). A nice side effect of unified parsing is
that modules now also support resp3. Resp3 can be enabled by giving `3` as a parameter to the
fmt argument of `RM_Call`. It is also possible to give `0`, which will indicate an auto mode. i.e, Redis
will automatically chose the reply protocol base on the current client set on the RedisModuleCtx
(this mode will mostly be used when the module want to pass the reply to the client as is).
In addition, the following RedisModuleAPI were added to allow analyzing resp3 replies:
* New RedisModuleCallReply types:
* `REDISMODULE_REPLY_MAP`
* `REDISMODULE_REPLY_SET`
* `REDISMODULE_REPLY_BOOL`
* `REDISMODULE_REPLY_DOUBLE`
* `REDISMODULE_REPLY_BIG_NUMBER`
* `REDISMODULE_REPLY_VERBATIM_STRING`
* `REDISMODULE_REPLY_ATTRIBUTE`
* New RedisModuleAPI:
* `RedisModule_CallReplyDouble` - getting double value from resp3 double reply
* `RedisModule_CallReplyBool` - getting boolean value from resp3 boolean reply
* `RedisModule_CallReplyBigNumber` - getting big number value from resp3 big number reply
* `RedisModule_CallReplyVerbatim` - getting format and value from resp3 verbatim reply
* `RedisModule_CallReplySetElement` - getting element from resp3 set reply
* `RedisModule_CallReplyMapElement` - getting key and value from resp3 map reply
* `RedisModule_CallReplyAttribute` - getting a reply attribute
* `RedisModule_CallReplyAttributeElement` - getting key and value from resp3 attribute reply
* New context flags:
* `REDISMODULE_CTX_FLAGS_RESP3` - indicate that the client is using resp3
Tests were added to check the new RedisModuleAPI
### Modules API Changes
* RM_ReplyWithCallReply might return REDISMODULE_ERR if the given CallReply is in resp3
but the client expects resp2. This is not a breaking change because in order to get a resp3
CallReply one needs to specifically specify `3` as a parameter to the fmt argument of
`RM_Call` (as mentioned above).
Tests were added to check this change
### More small Additions
* Added `debug set-disable-deny-scripts` that allows to turn on and off the commands no-script
flag protection. This is used by the Lua resp3 tests so it will be possible to run `debug protocol`
and check the resp3 parsing code.
Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Yossi Gottlieb <yossigo@gmail.com>
2021-08-04 09:28:07 -04:00
|
|
|
int TestCallResp3Attribute(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "attrib"); /* 3 stands for resp 3 reply */
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_STRING) goto fail;
|
|
|
|
|
|
|
|
/* make sure we can not reply to resp2 client with resp3 (it might be a string but it contains attribute) */
|
|
|
|
if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
|
|
|
|
|
|
|
|
if (!TestMatchReply(reply,"Some real reply following the attribute")) goto fail;
|
|
|
|
|
|
|
|
reply = RedisModule_CallReplyAttribute(reply);
|
|
|
|
if (!reply || RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ATTRIBUTE) goto fail;
|
|
|
|
/* make sure we can not reply to resp2 client with resp3 attribute */
|
|
|
|
if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
|
|
|
|
if (RedisModule_CallReplyLength(reply) != 1) goto fail;
|
|
|
|
|
|
|
|
RedisModuleCallReply *key, *val;
|
|
|
|
if (RedisModule_CallReplyAttributeElement(reply,0,&key,&val) != REDISMODULE_OK) goto fail;
|
|
|
|
if (!TestMatchReply(key,"key-popularity")) goto fail;
|
|
|
|
if (RedisModule_CallReplyType(val) != REDISMODULE_REPLY_ARRAY) goto fail;
|
|
|
|
if (RedisModule_CallReplyLength(val) != 2) goto fail;
|
|
|
|
if (!TestMatchReply(RedisModule_CallReplyArrayElement(val, 0),"key:123")) goto fail;
|
|
|
|
if (!TestMatchReply(RedisModule_CallReplyArrayElement(val, 1),"90")) goto fail;
|
|
|
|
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"OK");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ERR");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestGetResp(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
int flags = RedisModule_GetContextFlags(ctx);
|
|
|
|
|
|
|
|
if (flags & REDISMODULE_CTX_FLAGS_RESP3) {
|
|
|
|
RedisModule_ReplyWithLongLong(ctx, 3);
|
|
|
|
} else {
|
|
|
|
RedisModule_ReplyWithLongLong(ctx, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestCallRespAutoMode(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
RedisModule_Call(ctx,"DEL","c","myhash");
|
|
|
|
RedisModule_Call(ctx,"HSET","ccccc","myhash", "f1", "v1", "f2", "v2");
|
|
|
|
/* 0 stands for auto mode, we will get the reply in the same format as the client */
|
|
|
|
reply = RedisModule_Call(ctx,"HGETALL","0c" ,"myhash");
|
|
|
|
RedisModule_ReplyWithCallReply(ctx, reply);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestCallResp3Map(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
RedisModule_Call(ctx,"DEL","c","myhash");
|
|
|
|
RedisModule_Call(ctx,"HSET","ccccc","myhash", "f1", "v1", "f2", "v2");
|
|
|
|
reply = RedisModule_Call(ctx,"HGETALL","3c" ,"myhash"); /* 3 stands for resp 3 reply */
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_MAP) goto fail;
|
|
|
|
|
|
|
|
/* make sure we can not reply to resp2 client with resp3 map */
|
|
|
|
if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
|
|
|
|
|
|
|
|
long long items = RedisModule_CallReplyLength(reply);
|
|
|
|
if (items != 2) goto fail;
|
|
|
|
|
|
|
|
RedisModuleCallReply *key0, *key1;
|
|
|
|
RedisModuleCallReply *val0, *val1;
|
|
|
|
if (RedisModule_CallReplyMapElement(reply,0,&key0,&val0) != REDISMODULE_OK) goto fail;
|
|
|
|
if (RedisModule_CallReplyMapElement(reply,1,&key1,&val1) != REDISMODULE_OK) goto fail;
|
|
|
|
if (!TestMatchReply(key0,"f1")) goto fail;
|
|
|
|
if (!TestMatchReply(key1,"f2")) goto fail;
|
|
|
|
if (!TestMatchReply(val0,"v1")) goto fail;
|
|
|
|
if (!TestMatchReply(val1,"v2")) goto fail;
|
|
|
|
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"OK");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ERR");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestCallResp3Bool(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "true"); /* 3 stands for resp 3 reply */
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_BOOL) goto fail;
|
|
|
|
/* make sure we can not reply to resp2 client with resp3 bool */
|
|
|
|
if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
|
|
|
|
|
|
|
|
if (!RedisModule_CallReplyBool(reply)) goto fail;
|
|
|
|
reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "false"); /* 3 stands for resp 3 reply */
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_BOOL) goto fail;
|
|
|
|
if (RedisModule_CallReplyBool(reply)) goto fail;
|
|
|
|
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"OK");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ERR");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestCallResp3Null(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "null"); /* 3 stands for resp 3 reply */
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_NULL) goto fail;
|
|
|
|
|
|
|
|
/* make sure we can not reply to resp2 client with resp3 null */
|
|
|
|
if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
|
|
|
|
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"OK");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ERR");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestCallReplyWithNestedReply(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
RedisModule_Call(ctx,"DEL","c","mylist");
|
|
|
|
RedisModule_Call(ctx,"RPUSH","ccl","mylist","test",(long long)1234);
|
|
|
|
reply = RedisModule_Call(ctx,"LRANGE","ccc","mylist","0","-1");
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) goto fail;
|
|
|
|
if (RedisModule_CallReplyLength(reply) < 1) goto fail;
|
|
|
|
RedisModuleCallReply *nestedReply = RedisModule_CallReplyArrayElement(reply, 0);
|
|
|
|
|
|
|
|
RedisModule_ReplyWithCallReply(ctx,nestedReply);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ERR");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestCallReplyWithArrayReply(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
RedisModule_Call(ctx,"DEL","c","mylist");
|
|
|
|
RedisModule_Call(ctx,"RPUSH","ccl","mylist","test",(long long)1234);
|
|
|
|
reply = RedisModule_Call(ctx,"LRANGE","ccc","mylist","0","-1");
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) goto fail;
|
|
|
|
|
|
|
|
RedisModule_ReplyWithCallReply(ctx,reply);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ERR");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestCallResp3Double(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "double"); /* 3 stands for resp 3 reply */
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_DOUBLE) goto fail;
|
|
|
|
|
|
|
|
/* make sure we can not reply to resp2 client with resp3 double*/
|
|
|
|
if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
|
|
|
|
|
|
|
|
double d = RedisModule_CallReplyDouble(reply);
|
2021-08-04 14:33:38 -04:00
|
|
|
/* we compare strings, since comparing doubles directly can fail in various architectures, e.g. 32bit */
|
|
|
|
char got[30], expected[30];
|
2022-07-18 03:56:26 -04:00
|
|
|
snprintf(got, sizeof(got), "%.17g", d);
|
|
|
|
snprintf(expected, sizeof(expected), "%.17g", 3.141);
|
2021-08-04 14:33:38 -04:00
|
|
|
if (strcmp(got, expected) != 0) goto fail;
|
Unified Lua and modules reply parsing and added RESP3 support to RM_Call (#9202)
## Current state
1. Lua has its own parser that handles parsing `reds.call` replies and translates them
to Lua objects that can be used by the user Lua code. The parser partially handles
resp3 (missing big number, verbatim, attribute, ...)
2. Modules have their own parser that handles parsing `RM_Call` replies and translates
them to RedisModuleCallReply objects. The parser does not support resp3.
In addition, in the future, we want to add Redis Function (#8693) that will probably
support more languages. At some point maintaining so many parsers will stop
scaling (bug fixes and protocol changes will need to be applied on all of them).
We will probably end up with different parsers that support different parts of the
resp protocol (like we already have today with Lua and modules)
## PR Changes
This PR attempt to unified the reply parsing of Lua and modules (and in the future
Redis Function) by introducing a new parser unit (`resp_parser.c`). The new parser
handles parsing the reply and calls different callbacks to allow the users (another
unit that uses the parser, i.e, Lua, modules, or Redis Function) to analyze the reply.
### Lua API Additions
The code that handles reply parsing on `scripting.c` was removed. Instead, it uses
the resp_parser to parse and create a Lua object out of the reply. As mentioned
above the Lua parser did not handle parsing big numbers, verbatim, and attribute.
The new parser can handle those and so Lua also gets it for free.
Those are translated to Lua objects in the following way:
1. Big Number - Lua table `{'big_number':'<str representation for big number>'}`
2. Verbatim - Lua table `{'verbatim_string':{'format':'<verbatim format>', 'string':'<verbatim string value>'}}`
3. Attribute - currently ignored and not expose to the Lua parser, another issue will be open to decide how to expose it.
Tests were added to check resp3 reply parsing on Lua
### Modules API Additions
The reply parsing code on `module.c` was also removed and the new resp_parser is used instead.
In addition, the RedisModuleCallReply was also extracted to a separate unit located on `call_reply.c`
(in the future, this unit will also be used by Redis Function). A nice side effect of unified parsing is
that modules now also support resp3. Resp3 can be enabled by giving `3` as a parameter to the
fmt argument of `RM_Call`. It is also possible to give `0`, which will indicate an auto mode. i.e, Redis
will automatically chose the reply protocol base on the current client set on the RedisModuleCtx
(this mode will mostly be used when the module want to pass the reply to the client as is).
In addition, the following RedisModuleAPI were added to allow analyzing resp3 replies:
* New RedisModuleCallReply types:
* `REDISMODULE_REPLY_MAP`
* `REDISMODULE_REPLY_SET`
* `REDISMODULE_REPLY_BOOL`
* `REDISMODULE_REPLY_DOUBLE`
* `REDISMODULE_REPLY_BIG_NUMBER`
* `REDISMODULE_REPLY_VERBATIM_STRING`
* `REDISMODULE_REPLY_ATTRIBUTE`
* New RedisModuleAPI:
* `RedisModule_CallReplyDouble` - getting double value from resp3 double reply
* `RedisModule_CallReplyBool` - getting boolean value from resp3 boolean reply
* `RedisModule_CallReplyBigNumber` - getting big number value from resp3 big number reply
* `RedisModule_CallReplyVerbatim` - getting format and value from resp3 verbatim reply
* `RedisModule_CallReplySetElement` - getting element from resp3 set reply
* `RedisModule_CallReplyMapElement` - getting key and value from resp3 map reply
* `RedisModule_CallReplyAttribute` - getting a reply attribute
* `RedisModule_CallReplyAttributeElement` - getting key and value from resp3 attribute reply
* New context flags:
* `REDISMODULE_CTX_FLAGS_RESP3` - indicate that the client is using resp3
Tests were added to check the new RedisModuleAPI
### Modules API Changes
* RM_ReplyWithCallReply might return REDISMODULE_ERR if the given CallReply is in resp3
but the client expects resp2. This is not a breaking change because in order to get a resp3
CallReply one needs to specifically specify `3` as a parameter to the fmt argument of
`RM_Call` (as mentioned above).
Tests were added to check this change
### More small Additions
* Added `debug set-disable-deny-scripts` that allows to turn on and off the commands no-script
flag protection. This is used by the Lua resp3 tests so it will be possible to run `debug protocol`
and check the resp3 parsing code.
Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Yossi Gottlieb <yossigo@gmail.com>
2021-08-04 09:28:07 -04:00
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"OK");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ERR");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestCallResp3BigNumber(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "bignum"); /* 3 stands for resp 3 reply */
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_BIG_NUMBER) goto fail;
|
|
|
|
|
|
|
|
/* make sure we can not reply to resp2 client with resp3 big number */
|
|
|
|
if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
|
|
|
|
|
|
|
|
size_t len;
|
|
|
|
const char* big_num = RedisModule_CallReplyBigNumber(reply, &len);
|
|
|
|
RedisModule_ReplyWithStringBuffer(ctx,big_num,len);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ERR");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestCallResp3Verbatim(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
reply = RedisModule_Call(ctx,"DEBUG","3cc" ,"PROTOCOL", "verbatim"); /* 3 stands for resp 3 reply */
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_VERBATIM_STRING) goto fail;
|
|
|
|
|
|
|
|
/* make sure we can not reply to resp2 client with resp3 verbatim string */
|
|
|
|
if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
|
|
|
|
|
|
|
|
const char* format;
|
|
|
|
size_t len;
|
|
|
|
const char* str = RedisModule_CallReplyVerbatim(reply, &len, &format);
|
|
|
|
RedisModuleString *s = RedisModule_CreateStringPrintf(ctx, "%.*s:%.*s", 3, format, (int)len, str);
|
|
|
|
RedisModule_ReplyWithString(ctx,s);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ERR");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int TestCallResp3Set(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
|
|
|
RedisModule_Call(ctx,"DEL","c","myset");
|
|
|
|
RedisModule_Call(ctx,"sadd","ccc","myset", "v1", "v2");
|
|
|
|
reply = RedisModule_Call(ctx,"smembers","3c" ,"myset"); // N stands for resp 3 reply
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_SET) goto fail;
|
|
|
|
|
|
|
|
/* make sure we can not reply to resp2 client with resp3 set */
|
|
|
|
if (RedisModule_ReplyWithCallReply(ctx, reply) != REDISMODULE_ERR) goto fail;
|
|
|
|
|
|
|
|
long long items = RedisModule_CallReplyLength(reply);
|
|
|
|
if (items != 2) goto fail;
|
|
|
|
|
|
|
|
RedisModuleCallReply *val0, *val1;
|
|
|
|
|
|
|
|
val0 = RedisModule_CallReplySetElement(reply,0);
|
|
|
|
val1 = RedisModule_CallReplySetElement(reply,1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The order of elements on sets are not promised so we just
|
|
|
|
* veridy that the reply matches one of the elements.
|
|
|
|
*/
|
|
|
|
if (!TestMatchReply(val0,"v1") && !TestMatchReply(val0,"v2")) goto fail;
|
|
|
|
if (!TestMatchReply(val1,"v1") && !TestMatchReply(val1,"v2")) goto fail;
|
|
|
|
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"OK");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ERR");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
/* TEST.STRING.APPEND -- Test appending to an existing string object. */
|
|
|
|
int TestStringAppend(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
2016-10-07 07:10:29 -04:00
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
RedisModuleString *s = RedisModule_CreateString(ctx,"foo",3);
|
|
|
|
RedisModule_StringAppendBuffer(ctx,s,"bar",3);
|
|
|
|
RedisModule_ReplyWithString(ctx,s);
|
|
|
|
RedisModule_FreeString(ctx,s);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TEST.STRING.APPEND.AM -- Test append with retain when auto memory is on. */
|
|
|
|
int TestStringAppendAM(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
2016-10-07 07:10:29 -04:00
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleString *s = RedisModule_CreateString(ctx,"foo",3);
|
|
|
|
RedisModule_RetainString(ctx,s);
|
2021-09-23 08:00:37 -04:00
|
|
|
RedisModule_TrimStringAllocation(s); /* Mostly NOP, but exercises the API function */
|
2016-08-03 04:23:03 -04:00
|
|
|
RedisModule_StringAppendBuffer(ctx,s,"bar",3);
|
|
|
|
RedisModule_ReplyWithString(ctx,s);
|
|
|
|
RedisModule_FreeString(ctx,s);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
2023-02-28 12:38:58 -05:00
|
|
|
/* TEST.STRING.TRIM -- Test we trim a string with free space. */
|
|
|
|
int TestTrimString(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
RedisModuleString *s = RedisModule_CreateString(ctx,"foo",3);
|
|
|
|
char *tmp = RedisModule_Alloc(1024);
|
|
|
|
RedisModule_StringAppendBuffer(ctx,s,tmp,1024);
|
|
|
|
size_t string_len = RedisModule_MallocSizeString(s);
|
|
|
|
RedisModule_TrimStringAllocation(s);
|
|
|
|
size_t len_after_trim = RedisModule_MallocSizeString(s);
|
2023-03-07 02:06:58 -05:00
|
|
|
|
|
|
|
/* Determine if using jemalloc memory allocator. */
|
|
|
|
RedisModuleServerInfoData *info = RedisModule_GetServerInfo(ctx, "memory");
|
|
|
|
const char *field = RedisModule_ServerInfoGetFieldC(info, "mem_allocator");
|
|
|
|
int use_jemalloc = !strncmp(field, "jemalloc", 8);
|
|
|
|
|
|
|
|
/* Jemalloc will reallocate `s` from 2k to 1k after RedisModule_TrimStringAllocation(),
|
|
|
|
* but non-jemalloc memory allocators may keep the old size. */
|
|
|
|
if ((use_jemalloc && len_after_trim < string_len) ||
|
|
|
|
(!use_jemalloc && len_after_trim <= string_len))
|
|
|
|
{
|
2023-02-28 12:38:58 -05:00
|
|
|
RedisModule_ReplyWithSimpleString(ctx, "OK");
|
|
|
|
} else {
|
|
|
|
RedisModule_ReplyWithError(ctx, "String was not trimmed as expected.");
|
|
|
|
}
|
2023-03-07 02:06:58 -05:00
|
|
|
RedisModule_FreeServerInfo(ctx, info);
|
2023-02-28 12:38:58 -05:00
|
|
|
RedisModule_Free(tmp);
|
|
|
|
RedisModule_FreeString(ctx,s);
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-21 05:30:38 -04:00
|
|
|
/* TEST.STRING.PRINTF -- Test string formatting. */
|
|
|
|
int TestStringPrintf(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
if (argc < 3) {
|
|
|
|
return RedisModule_WrongArity(ctx);
|
|
|
|
}
|
2019-03-19 07:11:37 -04:00
|
|
|
RedisModuleString *s = RedisModule_CreateStringPrintf(ctx,
|
|
|
|
"Got %d args. argv[1]: %s, argv[2]: %s",
|
|
|
|
argc,
|
2016-09-21 05:30:38 -04:00
|
|
|
RedisModule_StringPtrLen(argv[1], NULL),
|
|
|
|
RedisModule_StringPtrLen(argv[2], NULL)
|
|
|
|
);
|
|
|
|
|
|
|
|
RedisModule_ReplyWithString(ctx,s);
|
|
|
|
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-07 09:41:43 -05:00
|
|
|
int failTest(RedisModuleCtx *ctx, const char *msg) {
|
|
|
|
RedisModule_ReplyWithError(ctx, msg);
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
}
|
2017-12-07 10:19:04 -05:00
|
|
|
|
2018-01-07 09:41:43 -05:00
|
|
|
int TestUnlink(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModuleKey *k = RedisModule_OpenKey(ctx, RedisModule_CreateStringPrintf(ctx, "unlinked"), REDISMODULE_WRITE | REDISMODULE_READ);
|
|
|
|
if (!k) return failTest(ctx, "Could not create key");
|
2019-03-19 07:11:37 -04:00
|
|
|
|
2018-01-07 09:41:43 -05:00
|
|
|
if (REDISMODULE_ERR == RedisModule_StringSet(k, RedisModule_CreateStringPrintf(ctx, "Foobar"))) {
|
|
|
|
return failTest(ctx, "Could not set string value");
|
|
|
|
}
|
|
|
|
|
|
|
|
RedisModuleCallReply *rep = RedisModule_Call(ctx, "EXISTS", "c", "unlinked");
|
|
|
|
if (!rep || RedisModule_CallReplyInteger(rep) != 1) {
|
|
|
|
return failTest(ctx, "Key does not exist before unlink");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (REDISMODULE_ERR == RedisModule_UnlinkKey(k)) {
|
|
|
|
return failTest(ctx, "Could not unlink key");
|
|
|
|
}
|
|
|
|
|
|
|
|
rep = RedisModule_Call(ctx, "EXISTS", "c", "unlinked");
|
|
|
|
if (!rep || RedisModule_CallReplyInteger(rep) != 0) {
|
|
|
|
return failTest(ctx, "Could not verify key to be unlinked");
|
|
|
|
}
|
|
|
|
return RedisModule_ReplyWithSimpleString(ctx, "OK");
|
2021-06-22 05:26:48 -04:00
|
|
|
}
|
|
|
|
|
2021-09-09 04:03:05 -04:00
|
|
|
int TestNestedCallReplyArrayElement(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModuleString *expect_key = RedisModule_CreateString(ctx, "mykey", strlen("mykey"));
|
|
|
|
RedisModule_SelectDb(ctx, 1);
|
|
|
|
RedisModule_Call(ctx, "LPUSH", "sc", expect_key, "myvalue");
|
|
|
|
|
2021-09-09 16:03:02 -04:00
|
|
|
RedisModuleCallReply *scan_reply = RedisModule_Call(ctx, "SCAN", "l", (long long)0);
|
2021-09-09 04:03:05 -04:00
|
|
|
RedisModule_Assert(scan_reply != NULL && RedisModule_CallReplyType(scan_reply) == REDISMODULE_REPLY_ARRAY);
|
|
|
|
RedisModule_Assert(RedisModule_CallReplyLength(scan_reply) == 2);
|
|
|
|
|
|
|
|
long long scan_cursor;
|
|
|
|
RedisModuleCallReply *cursor_reply = RedisModule_CallReplyArrayElement(scan_reply, 0);
|
|
|
|
RedisModule_Assert(RedisModule_CallReplyType(cursor_reply) == REDISMODULE_REPLY_STRING);
|
|
|
|
RedisModule_Assert(RedisModule_StringToLongLong(RedisModule_CreateStringFromCallReply(cursor_reply), &scan_cursor) == REDISMODULE_OK);
|
|
|
|
RedisModule_Assert(scan_cursor == 0);
|
|
|
|
|
|
|
|
RedisModuleCallReply *keys_reply = RedisModule_CallReplyArrayElement(scan_reply, 1);
|
|
|
|
RedisModule_Assert(RedisModule_CallReplyType(keys_reply) == REDISMODULE_REPLY_ARRAY);
|
|
|
|
RedisModule_Assert( RedisModule_CallReplyLength(keys_reply) == 1);
|
|
|
|
|
|
|
|
RedisModuleCallReply *key_reply = RedisModule_CallReplyArrayElement(keys_reply, 0);
|
|
|
|
RedisModule_Assert(RedisModule_CallReplyType(key_reply) == REDISMODULE_REPLY_STRING);
|
|
|
|
RedisModuleString *key = RedisModule_CreateStringFromCallReply(key_reply);
|
|
|
|
RedisModule_Assert(RedisModule_StringCompare(key, expect_key) == 0);
|
|
|
|
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx, "OK");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
2021-06-22 05:26:48 -04:00
|
|
|
/* TEST.STRING.TRUNCATE -- Test truncating an existing string object. */
|
|
|
|
int TestStringTruncate(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
2021-06-22 10:22:40 -04:00
|
|
|
RedisModule_AutoMemory(ctx);
|
2021-06-22 05:26:48 -04:00
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
RedisModule_Call(ctx, "SET", "cc", "foo", "abcde");
|
|
|
|
RedisModuleKey *k = RedisModule_OpenKey(ctx, RedisModule_CreateStringPrintf(ctx, "foo"), REDISMODULE_READ | REDISMODULE_WRITE);
|
|
|
|
if (!k) return failTest(ctx, "Could not create key");
|
2019-03-19 07:11:37 -04:00
|
|
|
|
2021-06-22 05:26:48 -04:00
|
|
|
size_t len = 0;
|
|
|
|
char* s;
|
|
|
|
|
|
|
|
/* expand from 5 to 8 and check null pad */
|
|
|
|
if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 8)) {
|
|
|
|
return failTest(ctx, "Could not truncate string value (8)");
|
|
|
|
}
|
|
|
|
s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
|
|
|
|
if (!s) {
|
|
|
|
return failTest(ctx, "Failed to read truncated string (8)");
|
|
|
|
} else if (len != 8) {
|
|
|
|
return failTest(ctx, "Failed to expand string value (8)");
|
|
|
|
} else if (0 != strncmp(s, "abcde\0\0\0", 8)) {
|
|
|
|
return failTest(ctx, "Failed to null pad string value (8)");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* shrink from 8 to 4 */
|
|
|
|
if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 4)) {
|
|
|
|
return failTest(ctx, "Could not truncate string value (4)");
|
|
|
|
}
|
|
|
|
s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
|
|
|
|
if (!s) {
|
|
|
|
return failTest(ctx, "Failed to read truncated string (4)");
|
|
|
|
} else if (len != 4) {
|
|
|
|
return failTest(ctx, "Failed to shrink string value (4)");
|
|
|
|
} else if (0 != strncmp(s, "abcd", 4)) {
|
|
|
|
return failTest(ctx, "Failed to truncate string value (4)");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* shrink to 0 */
|
|
|
|
if (REDISMODULE_ERR == RedisModule_StringTruncate(k, 0)) {
|
|
|
|
return failTest(ctx, "Could not truncate string value (0)");
|
|
|
|
}
|
|
|
|
s = RedisModule_StringDMA(k, &len, REDISMODULE_READ);
|
|
|
|
if (!s) {
|
|
|
|
return failTest(ctx, "Failed to read truncated string (0)");
|
|
|
|
} else if (len != 0) {
|
|
|
|
return failTest(ctx, "Failed to shrink string value to (0)");
|
|
|
|
}
|
|
|
|
|
|
|
|
return RedisModule_ReplyWithSimpleString(ctx, "OK");
|
2018-01-07 09:41:43 -05:00
|
|
|
}
|
2016-09-21 05:30:38 -04:00
|
|
|
|
2017-12-07 10:19:04 -05:00
|
|
|
int NotifyCallback(RedisModuleCtx *ctx, int type, const char *event,
|
|
|
|
RedisModuleString *key) {
|
2021-06-22 10:22:40 -04:00
|
|
|
RedisModule_AutoMemory(ctx);
|
2017-12-07 10:19:04 -05:00
|
|
|
/* Increment a counter on the notifications: for each key notified we
|
|
|
|
* increment a counter */
|
|
|
|
RedisModule_Log(ctx, "notice", "Got event type %d, event %s, key %s", type,
|
|
|
|
event, RedisModule_StringPtrLen(key, NULL));
|
2017-11-27 09:29:55 -05:00
|
|
|
|
|
|
|
RedisModule_Call(ctx, "HINCRBY", "csc", "notifications", key, "1");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TEST.NOTIFICATIONS -- Test Keyspace Notifications. */
|
|
|
|
int TestNotifications(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
2021-06-22 10:22:40 -04:00
|
|
|
RedisModule_AutoMemory(ctx);
|
2017-12-07 10:19:04 -05:00
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
|
|
|
#define FAIL(msg, ...) \
|
|
|
|
{ \
|
|
|
|
RedisModule_Log(ctx, "warning", "Failed NOTIFY Test. Reason: " #msg, ##__VA_ARGS__); \
|
|
|
|
goto err; \
|
|
|
|
}
|
|
|
|
RedisModule_Call(ctx, "FLUSHDB", "");
|
|
|
|
|
|
|
|
RedisModule_Call(ctx, "SET", "cc", "foo", "bar");
|
|
|
|
RedisModule_Call(ctx, "SET", "cc", "foo", "baz");
|
|
|
|
RedisModule_Call(ctx, "SADD", "cc", "bar", "x");
|
|
|
|
RedisModule_Call(ctx, "SADD", "cc", "bar", "y");
|
|
|
|
|
|
|
|
RedisModule_Call(ctx, "HSET", "ccc", "baz", "x", "y");
|
|
|
|
/* LPUSH should be ignored and not increment any counters */
|
|
|
|
RedisModule_Call(ctx, "LPUSH", "cc", "l", "y");
|
|
|
|
RedisModule_Call(ctx, "LPUSH", "cc", "l", "y");
|
|
|
|
|
2019-03-21 14:33:11 -04:00
|
|
|
/* Miss some keys intentionally so we will get a "keymiss" notification. */
|
2019-03-19 07:11:37 -04:00
|
|
|
RedisModule_Call(ctx, "GET", "c", "nosuchkey");
|
|
|
|
RedisModule_Call(ctx, "SMEMBERS", "c", "nosuchkey");
|
|
|
|
|
2017-12-07 10:19:04 -05:00
|
|
|
size_t sz;
|
|
|
|
const char *rep;
|
|
|
|
RedisModuleCallReply *r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "foo");
|
|
|
|
if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
|
|
|
|
FAIL("Wrong or no reply for foo");
|
|
|
|
} else {
|
|
|
|
rep = RedisModule_CallReplyStringPtr(r, &sz);
|
|
|
|
if (sz != 1 || *rep != '2') {
|
|
|
|
FAIL("Got reply '%s'. expected '2'", RedisModule_CallReplyStringPtr(r, NULL));
|
|
|
|
}
|
2017-11-27 09:29:55 -05:00
|
|
|
}
|
2017-12-07 10:19:04 -05:00
|
|
|
|
|
|
|
r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "bar");
|
|
|
|
if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
|
|
|
|
FAIL("Wrong or no reply for bar");
|
|
|
|
} else {
|
|
|
|
rep = RedisModule_CallReplyStringPtr(r, &sz);
|
|
|
|
if (sz != 1 || *rep != '2') {
|
|
|
|
FAIL("Got reply '%s'. expected '2'", rep);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "baz");
|
|
|
|
if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
|
|
|
|
FAIL("Wrong or no reply for baz");
|
|
|
|
} else {
|
|
|
|
rep = RedisModule_CallReplyStringPtr(r, &sz);
|
|
|
|
if (sz != 1 || *rep != '1') {
|
2020-11-13 08:16:40 -05:00
|
|
|
FAIL("Got reply '%.*s'. expected '1'", (int)sz, rep);
|
2017-12-07 10:19:04 -05:00
|
|
|
}
|
2017-11-27 09:29:55 -05:00
|
|
|
}
|
2017-12-07 10:19:04 -05:00
|
|
|
/* For l we expect nothing since we didn't subscribe to list events */
|
|
|
|
r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "l");
|
|
|
|
if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_NULL) {
|
|
|
|
FAIL("Wrong reply for l");
|
2017-11-27 09:29:55 -05:00
|
|
|
}
|
|
|
|
|
2019-03-19 07:11:37 -04:00
|
|
|
r = RedisModule_Call(ctx, "HGET", "cc", "notifications", "nosuchkey");
|
|
|
|
if (r == NULL || RedisModule_CallReplyType(r) != REDISMODULE_REPLY_STRING) {
|
|
|
|
FAIL("Wrong or no reply for nosuchkey");
|
|
|
|
} else {
|
|
|
|
rep = RedisModule_CallReplyStringPtr(r, &sz);
|
|
|
|
if (sz != 1 || *rep != '2') {
|
2020-11-13 08:16:40 -05:00
|
|
|
FAIL("Got reply '%.*s'. expected '2'", (int)sz, rep);
|
2019-03-19 07:11:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 16:13:45 -05:00
|
|
|
RedisModule_Call(ctx, "FLUSHDB", "");
|
|
|
|
|
2017-12-07 10:19:04 -05:00
|
|
|
return RedisModule_ReplyWithSimpleString(ctx, "OK");
|
2017-11-27 09:29:55 -05:00
|
|
|
err:
|
2017-12-07 10:19:04 -05:00
|
|
|
RedisModule_Call(ctx, "FLUSHDB", "");
|
2017-11-27 16:13:45 -05:00
|
|
|
|
2017-12-07 10:19:04 -05:00
|
|
|
return RedisModule_ReplyWithSimpleString(ctx, "ERR");
|
2017-11-27 09:29:55 -05:00
|
|
|
}
|
|
|
|
|
2017-09-27 04:56:40 -04:00
|
|
|
/* TEST.CTXFLAGS -- Test GetContextFlags. */
|
2017-09-27 04:52:39 -04:00
|
|
|
int TestCtxFlags(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
REDISMODULE_NOT_USED(argv);
|
2017-12-07 10:19:04 -05:00
|
|
|
|
2017-09-27 04:52:39 -04:00
|
|
|
RedisModule_AutoMemory(ctx);
|
2017-12-07 10:19:04 -05:00
|
|
|
|
2017-09-27 04:52:39 -04:00
|
|
|
int ok = 1;
|
|
|
|
const char *errString = NULL;
|
2017-12-07 10:19:04 -05:00
|
|
|
#undef FAIL
|
|
|
|
#define FAIL(msg) \
|
|
|
|
{ \
|
|
|
|
ok = 0; \
|
|
|
|
errString = msg; \
|
|
|
|
goto end; \
|
2017-09-27 04:52:39 -04:00
|
|
|
}
|
2017-12-07 10:19:04 -05:00
|
|
|
|
2017-09-27 04:56:40 -04:00
|
|
|
int flags = RedisModule_GetContextFlags(ctx);
|
2017-09-27 04:52:39 -04:00
|
|
|
if (flags == 0) {
|
2017-12-07 10:19:04 -05:00
|
|
|
FAIL("Got no flags");
|
2017-09-27 04:52:39 -04:00
|
|
|
}
|
2017-12-07 10:19:04 -05:00
|
|
|
|
2017-09-27 04:52:39 -04:00
|
|
|
if (flags & REDISMODULE_CTX_FLAGS_LUA) FAIL("Lua flag was set");
|
|
|
|
if (flags & REDISMODULE_CTX_FLAGS_MULTI) FAIL("Multi flag was set");
|
2017-12-07 10:19:04 -05:00
|
|
|
|
2017-09-27 04:52:39 -04:00
|
|
|
if (flags & REDISMODULE_CTX_FLAGS_AOF) FAIL("AOF Flag was set")
|
|
|
|
/* Enable AOF to test AOF flags */
|
|
|
|
RedisModule_Call(ctx, "config", "ccc", "set", "appendonly", "yes");
|
2017-09-27 04:56:40 -04:00
|
|
|
flags = RedisModule_GetContextFlags(ctx);
|
2017-12-07 10:19:04 -05:00
|
|
|
if (!(flags & REDISMODULE_CTX_FLAGS_AOF)) FAIL("AOF Flag not set after config set");
|
|
|
|
|
2021-06-24 05:44:13 -04:00
|
|
|
/* Disable RDB saving and test the flag. */
|
|
|
|
RedisModule_Call(ctx, "config", "ccc", "set", "save", "");
|
|
|
|
flags = RedisModule_GetContextFlags(ctx);
|
2017-09-27 04:52:39 -04:00
|
|
|
if (flags & REDISMODULE_CTX_FLAGS_RDB) FAIL("RDB Flag was set");
|
|
|
|
/* Enable RDB to test RDB flags */
|
|
|
|
RedisModule_Call(ctx, "config", "ccc", "set", "save", "900 1");
|
2017-09-27 04:56:40 -04:00
|
|
|
flags = RedisModule_GetContextFlags(ctx);
|
2017-12-07 10:19:04 -05:00
|
|
|
if (!(flags & REDISMODULE_CTX_FLAGS_RDB)) FAIL("RDB Flag was not set after config set");
|
|
|
|
|
2017-09-27 04:52:39 -04:00
|
|
|
if (!(flags & REDISMODULE_CTX_FLAGS_MASTER)) FAIL("Master flag was not set");
|
|
|
|
if (flags & REDISMODULE_CTX_FLAGS_SLAVE) FAIL("Slave flag was set");
|
|
|
|
if (flags & REDISMODULE_CTX_FLAGS_READONLY) FAIL("Read-only flag was set");
|
|
|
|
if (flags & REDISMODULE_CTX_FLAGS_CLUSTER) FAIL("Cluster flag was set");
|
2017-12-07 10:19:04 -05:00
|
|
|
|
2021-06-24 05:44:13 -04:00
|
|
|
/* Disable maxmemory and test the flag. (it is implicitly set in 32bit builds. */
|
|
|
|
RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory", "0");
|
|
|
|
flags = RedisModule_GetContextFlags(ctx);
|
2017-09-27 04:52:39 -04:00
|
|
|
if (flags & REDISMODULE_CTX_FLAGS_MAXMEMORY) FAIL("Maxmemory flag was set");
|
2017-12-07 10:19:04 -05:00
|
|
|
|
2021-06-24 05:44:13 -04:00
|
|
|
/* Enable maxmemory and test the flag. */
|
2017-09-27 04:52:39 -04:00
|
|
|
RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory", "100000000");
|
2017-09-27 04:56:40 -04:00
|
|
|
flags = RedisModule_GetContextFlags(ctx);
|
2017-09-27 04:52:39 -04:00
|
|
|
if (!(flags & REDISMODULE_CTX_FLAGS_MAXMEMORY))
|
2017-12-07 10:19:04 -05:00
|
|
|
FAIL("Maxmemory flag was not set after config set");
|
|
|
|
|
2017-09-27 04:52:39 -04:00
|
|
|
if (flags & REDISMODULE_CTX_FLAGS_EVICT) FAIL("Eviction flag was set");
|
2017-12-07 10:19:04 -05:00
|
|
|
RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory-policy", "allkeys-lru");
|
2017-09-27 04:56:40 -04:00
|
|
|
flags = RedisModule_GetContextFlags(ctx);
|
2017-12-07 10:19:04 -05:00
|
|
|
if (!(flags & REDISMODULE_CTX_FLAGS_EVICT)) FAIL("Eviction flag was not set after config set");
|
|
|
|
|
|
|
|
end:
|
2017-09-27 04:52:39 -04:00
|
|
|
/* Revert config changes */
|
|
|
|
RedisModule_Call(ctx, "config", "ccc", "set", "appendonly", "no");
|
|
|
|
RedisModule_Call(ctx, "config", "ccc", "set", "save", "");
|
|
|
|
RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory", "0");
|
|
|
|
RedisModule_Call(ctx, "config", "ccc", "set", "maxmemory-policy", "noeviction");
|
2017-12-07 10:19:04 -05:00
|
|
|
|
2017-09-27 04:52:39 -04:00
|
|
|
if (!ok) {
|
2017-12-07 10:19:04 -05:00
|
|
|
RedisModule_Log(ctx, "warning", "Failed CTXFLAGS Test. Reason: %s", errString);
|
|
|
|
return RedisModule_ReplyWithSimpleString(ctx, "ERR");
|
2017-09-27 04:52:39 -04:00
|
|
|
}
|
|
|
|
|
2017-12-07 10:19:04 -05:00
|
|
|
return RedisModule_ReplyWithSimpleString(ctx, "OK");
|
|
|
|
}
|
2017-09-27 04:52:39 -04:00
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
/* ----------------------------- Test framework ----------------------------- */
|
|
|
|
|
|
|
|
/* Return 1 if the reply matches the specified string, otherwise log errors
|
|
|
|
* in the server log and return 0. */
|
Add new RM_Call flags for script mode, no writes, and error replies. (#10372)
The PR extends RM_Call with 3 new capabilities using new flags that
are given to RM_Call as part of the `fmt` argument.
It aims to assist modules that are getting a list of commands to be
executed from the user (not hard coded as part of the module logic),
think of a module that implements a new scripting language...
* `S` - Run the command in a script mode, this means that it will raise an
error if a command which are not allowed inside a script (flaged with the
`deny-script` flag) is invoked (like SHUTDOWN). In addition, on script mode,
write commands are not allowed if there is not enough good replicas (as
configured with `min-replicas-to-write`) and/or a disk error happened.
* `W` - no writes mode, Redis will reject any command that is marked with `write`
flag. Again can be useful to modules that implement a new scripting language
and wants to prevent any write commands.
* `E` - Return errors as RedisModuleCallReply. Today the errors that happened
before the command was invoked (like unknown commands or acl error) return
a NULL reply and set errno. This might be missing important information about
the failure and it is also impossible to just pass the error to the user using
RM_ReplyWithCallReply. This new flag allows you to get a RedisModuleCallReply
object with the relevant error message and treat it as if it was an error that was
raised by the command invocation.
Tests were added to verify the new code paths.
In addition small refactoring was done to share some code between modules,
scripts, and `processCommand` function:
1. `getAclErrorMessage` was added to `acl.c` to unified to log message extraction
from the acl result
2. `checkGoodReplicasStatus` was added to `replication.c` to check the status of
good replicas. It is used on `scriptVerifyWriteCommandAllow`, `RM_Call`, and
`processCommand`.
3. `writeCommandsGetDiskErrorMessage` was added to `server.c` to get the error
message on persistence failure. Again it is used on `scriptVerifyWriteCommandAllow`,
`RM_Call`, and `processCommand`.
2022-03-22 08:13:28 -04:00
|
|
|
int TestAssertErrorReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply, char *str, size_t len) {
|
|
|
|
RedisModuleString *mystr, *expected;
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ERROR) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mystr = RedisModule_CreateStringFromCallReply(reply);
|
|
|
|
expected = RedisModule_CreateString(ctx,str,len);
|
|
|
|
if (RedisModule_StringCompare(mystr,expected) != 0) {
|
|
|
|
const char *mystr_ptr = RedisModule_StringPtrLen(mystr,NULL);
|
|
|
|
const char *expected_ptr = RedisModule_StringPtrLen(expected,NULL);
|
|
|
|
RedisModule_Log(ctx,"warning",
|
|
|
|
"Unexpected Error reply reply '%s' (instead of '%s')",
|
|
|
|
mystr_ptr, expected_ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
int TestAssertStringReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply, char *str, size_t len) {
|
|
|
|
RedisModuleString *mystr, *expected;
|
|
|
|
|
2021-06-22 05:26:48 -04:00
|
|
|
if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR) {
|
|
|
|
RedisModule_Log(ctx,"warning","Test error reply: %s",
|
|
|
|
RedisModule_CallReplyStringPtr(reply, NULL));
|
|
|
|
return 0;
|
|
|
|
} else if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_STRING) {
|
2016-08-03 04:23:03 -04:00
|
|
|
RedisModule_Log(ctx,"warning","Unexpected reply type %d",
|
|
|
|
RedisModule_CallReplyType(reply));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
mystr = RedisModule_CreateStringFromCallReply(reply);
|
|
|
|
expected = RedisModule_CreateString(ctx,str,len);
|
|
|
|
if (RedisModule_StringCompare(mystr,expected) != 0) {
|
|
|
|
const char *mystr_ptr = RedisModule_StringPtrLen(mystr,NULL);
|
|
|
|
const char *expected_ptr = RedisModule_StringPtrLen(expected,NULL);
|
|
|
|
RedisModule_Log(ctx,"warning",
|
|
|
|
"Unexpected string reply '%s' (instead of '%s')",
|
|
|
|
mystr_ptr, expected_ptr);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-03 12:10:11 -04:00
|
|
|
/* Return 1 if the reply matches the specified integer, otherwise log errors
|
|
|
|
* in the server log and return 0. */
|
|
|
|
int TestAssertIntegerReply(RedisModuleCtx *ctx, RedisModuleCallReply *reply, long long expected) {
|
2021-06-22 05:26:48 -04:00
|
|
|
if (RedisModule_CallReplyType(reply) == REDISMODULE_REPLY_ERROR) {
|
|
|
|
RedisModule_Log(ctx,"warning","Test error reply: %s",
|
|
|
|
RedisModule_CallReplyStringPtr(reply, NULL));
|
|
|
|
return 0;
|
|
|
|
} else if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_INTEGER) {
|
2016-08-03 12:10:11 -04:00
|
|
|
RedisModule_Log(ctx,"warning","Unexpected reply type %d",
|
|
|
|
RedisModule_CallReplyType(reply));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
long long val = RedisModule_CallReplyInteger(reply);
|
|
|
|
if (val != expected) {
|
|
|
|
RedisModule_Log(ctx,"warning",
|
|
|
|
"Unexpected integer reply '%lld' (instead of '%lld')",
|
|
|
|
val, expected);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
#define T(name,...) \
|
|
|
|
do { \
|
|
|
|
RedisModule_Log(ctx,"warning","Testing %s", name); \
|
|
|
|
reply = RedisModule_Call(ctx,name,__VA_ARGS__); \
|
2020-12-22 01:57:45 -05:00
|
|
|
} while (0)
|
2016-08-03 04:23:03 -04:00
|
|
|
|
2021-06-22 05:26:48 -04:00
|
|
|
/* TEST.BASICS -- Run all the tests.
|
|
|
|
* Note: it is useful to run these tests from the module rather than TCL
|
|
|
|
* since it's easier to check the reply types like that (make a distinction
|
|
|
|
* between 0 and "0", etc. */
|
|
|
|
int TestBasics(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
2016-10-07 07:10:29 -04:00
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
RedisModule_AutoMemory(ctx);
|
|
|
|
RedisModuleCallReply *reply;
|
|
|
|
|
2016-08-03 12:10:11 -04:00
|
|
|
/* Make sure the DB is empty before to proceed. */
|
|
|
|
T("dbsize","");
|
|
|
|
if (!TestAssertIntegerReply(ctx,reply,0)) goto fail;
|
|
|
|
|
|
|
|
T("ping","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"PONG",4)) goto fail;
|
|
|
|
|
|
|
|
T("test.call","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
|
|
|
|
Unified Lua and modules reply parsing and added RESP3 support to RM_Call (#9202)
## Current state
1. Lua has its own parser that handles parsing `reds.call` replies and translates them
to Lua objects that can be used by the user Lua code. The parser partially handles
resp3 (missing big number, verbatim, attribute, ...)
2. Modules have their own parser that handles parsing `RM_Call` replies and translates
them to RedisModuleCallReply objects. The parser does not support resp3.
In addition, in the future, we want to add Redis Function (#8693) that will probably
support more languages. At some point maintaining so many parsers will stop
scaling (bug fixes and protocol changes will need to be applied on all of them).
We will probably end up with different parsers that support different parts of the
resp protocol (like we already have today with Lua and modules)
## PR Changes
This PR attempt to unified the reply parsing of Lua and modules (and in the future
Redis Function) by introducing a new parser unit (`resp_parser.c`). The new parser
handles parsing the reply and calls different callbacks to allow the users (another
unit that uses the parser, i.e, Lua, modules, or Redis Function) to analyze the reply.
### Lua API Additions
The code that handles reply parsing on `scripting.c` was removed. Instead, it uses
the resp_parser to parse and create a Lua object out of the reply. As mentioned
above the Lua parser did not handle parsing big numbers, verbatim, and attribute.
The new parser can handle those and so Lua also gets it for free.
Those are translated to Lua objects in the following way:
1. Big Number - Lua table `{'big_number':'<str representation for big number>'}`
2. Verbatim - Lua table `{'verbatim_string':{'format':'<verbatim format>', 'string':'<verbatim string value>'}}`
3. Attribute - currently ignored and not expose to the Lua parser, another issue will be open to decide how to expose it.
Tests were added to check resp3 reply parsing on Lua
### Modules API Additions
The reply parsing code on `module.c` was also removed and the new resp_parser is used instead.
In addition, the RedisModuleCallReply was also extracted to a separate unit located on `call_reply.c`
(in the future, this unit will also be used by Redis Function). A nice side effect of unified parsing is
that modules now also support resp3. Resp3 can be enabled by giving `3` as a parameter to the
fmt argument of `RM_Call`. It is also possible to give `0`, which will indicate an auto mode. i.e, Redis
will automatically chose the reply protocol base on the current client set on the RedisModuleCtx
(this mode will mostly be used when the module want to pass the reply to the client as is).
In addition, the following RedisModuleAPI were added to allow analyzing resp3 replies:
* New RedisModuleCallReply types:
* `REDISMODULE_REPLY_MAP`
* `REDISMODULE_REPLY_SET`
* `REDISMODULE_REPLY_BOOL`
* `REDISMODULE_REPLY_DOUBLE`
* `REDISMODULE_REPLY_BIG_NUMBER`
* `REDISMODULE_REPLY_VERBATIM_STRING`
* `REDISMODULE_REPLY_ATTRIBUTE`
* New RedisModuleAPI:
* `RedisModule_CallReplyDouble` - getting double value from resp3 double reply
* `RedisModule_CallReplyBool` - getting boolean value from resp3 boolean reply
* `RedisModule_CallReplyBigNumber` - getting big number value from resp3 big number reply
* `RedisModule_CallReplyVerbatim` - getting format and value from resp3 verbatim reply
* `RedisModule_CallReplySetElement` - getting element from resp3 set reply
* `RedisModule_CallReplyMapElement` - getting key and value from resp3 map reply
* `RedisModule_CallReplyAttribute` - getting a reply attribute
* `RedisModule_CallReplyAttributeElement` - getting key and value from resp3 attribute reply
* New context flags:
* `REDISMODULE_CTX_FLAGS_RESP3` - indicate that the client is using resp3
Tests were added to check the new RedisModuleAPI
### Modules API Changes
* RM_ReplyWithCallReply might return REDISMODULE_ERR if the given CallReply is in resp3
but the client expects resp2. This is not a breaking change because in order to get a resp3
CallReply one needs to specifically specify `3` as a parameter to the fmt argument of
`RM_Call` (as mentioned above).
Tests were added to check this change
### More small Additions
* Added `debug set-disable-deny-scripts` that allows to turn on and off the commands no-script
flag protection. This is used by the Lua resp3 tests so it will be possible to run `debug protocol`
and check the resp3 parsing code.
Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Yossi Gottlieb <yossigo@gmail.com>
2021-08-04 09:28:07 -04:00
|
|
|
T("test.callresp3map","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
|
|
|
|
|
|
|
T("test.callresp3set","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
|
|
|
|
|
|
|
T("test.callresp3double","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
|
|
|
|
|
|
|
T("test.callresp3bool","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
|
|
|
|
|
|
|
T("test.callresp3null","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
|
|
|
|
|
|
|
T("test.callreplywithnestedreply","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"test",4)) goto fail;
|
|
|
|
|
|
|
|
T("test.callreplywithbignumberreply","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"1234567999999999999999999999999999999",37)) goto fail;
|
|
|
|
|
|
|
|
T("test.callreplywithverbatimstringreply","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"txt:This is a verbatim\nstring",29)) goto fail;
|
|
|
|
|
2017-09-27 04:52:39 -04:00
|
|
|
T("test.ctxflags","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
T("test.string.append","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"foobar",6)) goto fail;
|
|
|
|
|
2021-06-22 05:26:48 -04:00
|
|
|
T("test.string.truncate","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
|
|
|
|
2018-01-07 09:41:43 -05:00
|
|
|
T("test.unlink","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
|
|
|
|
2021-09-09 04:03:05 -04:00
|
|
|
T("test.nestedcallreplyarray","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
T("test.string.append.am","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"foobar",6)) goto fail;
|
2023-02-28 12:38:58 -05:00
|
|
|
|
|
|
|
T("test.string.trim","");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
2016-08-03 04:23:03 -04:00
|
|
|
|
2016-09-21 05:30:38 -04:00
|
|
|
T("test.string.printf", "cc", "foo", "bar");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"Got 3 args. argv[1]: foo, argv[2]: bar",38)) goto fail;
|
|
|
|
|
2017-11-27 09:29:55 -05:00
|
|
|
T("test.notify", "");
|
|
|
|
if (!TestAssertStringReply(ctx,reply,"OK",2)) goto fail;
|
|
|
|
|
Unified Lua and modules reply parsing and added RESP3 support to RM_Call (#9202)
## Current state
1. Lua has its own parser that handles parsing `reds.call` replies and translates them
to Lua objects that can be used by the user Lua code. The parser partially handles
resp3 (missing big number, verbatim, attribute, ...)
2. Modules have their own parser that handles parsing `RM_Call` replies and translates
them to RedisModuleCallReply objects. The parser does not support resp3.
In addition, in the future, we want to add Redis Function (#8693) that will probably
support more languages. At some point maintaining so many parsers will stop
scaling (bug fixes and protocol changes will need to be applied on all of them).
We will probably end up with different parsers that support different parts of the
resp protocol (like we already have today with Lua and modules)
## PR Changes
This PR attempt to unified the reply parsing of Lua and modules (and in the future
Redis Function) by introducing a new parser unit (`resp_parser.c`). The new parser
handles parsing the reply and calls different callbacks to allow the users (another
unit that uses the parser, i.e, Lua, modules, or Redis Function) to analyze the reply.
### Lua API Additions
The code that handles reply parsing on `scripting.c` was removed. Instead, it uses
the resp_parser to parse and create a Lua object out of the reply. As mentioned
above the Lua parser did not handle parsing big numbers, verbatim, and attribute.
The new parser can handle those and so Lua also gets it for free.
Those are translated to Lua objects in the following way:
1. Big Number - Lua table `{'big_number':'<str representation for big number>'}`
2. Verbatim - Lua table `{'verbatim_string':{'format':'<verbatim format>', 'string':'<verbatim string value>'}}`
3. Attribute - currently ignored and not expose to the Lua parser, another issue will be open to decide how to expose it.
Tests were added to check resp3 reply parsing on Lua
### Modules API Additions
The reply parsing code on `module.c` was also removed and the new resp_parser is used instead.
In addition, the RedisModuleCallReply was also extracted to a separate unit located on `call_reply.c`
(in the future, this unit will also be used by Redis Function). A nice side effect of unified parsing is
that modules now also support resp3. Resp3 can be enabled by giving `3` as a parameter to the
fmt argument of `RM_Call`. It is also possible to give `0`, which will indicate an auto mode. i.e, Redis
will automatically chose the reply protocol base on the current client set on the RedisModuleCtx
(this mode will mostly be used when the module want to pass the reply to the client as is).
In addition, the following RedisModuleAPI were added to allow analyzing resp3 replies:
* New RedisModuleCallReply types:
* `REDISMODULE_REPLY_MAP`
* `REDISMODULE_REPLY_SET`
* `REDISMODULE_REPLY_BOOL`
* `REDISMODULE_REPLY_DOUBLE`
* `REDISMODULE_REPLY_BIG_NUMBER`
* `REDISMODULE_REPLY_VERBATIM_STRING`
* `REDISMODULE_REPLY_ATTRIBUTE`
* New RedisModuleAPI:
* `RedisModule_CallReplyDouble` - getting double value from resp3 double reply
* `RedisModule_CallReplyBool` - getting boolean value from resp3 boolean reply
* `RedisModule_CallReplyBigNumber` - getting big number value from resp3 big number reply
* `RedisModule_CallReplyVerbatim` - getting format and value from resp3 verbatim reply
* `RedisModule_CallReplySetElement` - getting element from resp3 set reply
* `RedisModule_CallReplyMapElement` - getting key and value from resp3 map reply
* `RedisModule_CallReplyAttribute` - getting a reply attribute
* `RedisModule_CallReplyAttributeElement` - getting key and value from resp3 attribute reply
* New context flags:
* `REDISMODULE_CTX_FLAGS_RESP3` - indicate that the client is using resp3
Tests were added to check the new RedisModuleAPI
### Modules API Changes
* RM_ReplyWithCallReply might return REDISMODULE_ERR if the given CallReply is in resp3
but the client expects resp2. This is not a breaking change because in order to get a resp3
CallReply one needs to specifically specify `3` as a parameter to the fmt argument of
`RM_Call` (as mentioned above).
Tests were added to check this change
### More small Additions
* Added `debug set-disable-deny-scripts` that allows to turn on and off the commands no-script
flag protection. This is used by the Lua resp3 tests so it will be possible to run `debug protocol`
and check the resp3 parsing code.
Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Yossi Gottlieb <yossigo@gmail.com>
2021-08-04 09:28:07 -04:00
|
|
|
T("test.callreplywitharrayreply", "");
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_ARRAY) goto fail;
|
|
|
|
if (RedisModule_CallReplyLength(reply) != 2) goto fail;
|
|
|
|
if (!TestAssertStringReply(ctx,RedisModule_CallReplyArrayElement(reply, 0),"test",4)) goto fail;
|
|
|
|
if (!TestAssertStringReply(ctx,RedisModule_CallReplyArrayElement(reply, 1),"1234",4)) goto fail;
|
|
|
|
|
Add new RM_Call flags for script mode, no writes, and error replies. (#10372)
The PR extends RM_Call with 3 new capabilities using new flags that
are given to RM_Call as part of the `fmt` argument.
It aims to assist modules that are getting a list of commands to be
executed from the user (not hard coded as part of the module logic),
think of a module that implements a new scripting language...
* `S` - Run the command in a script mode, this means that it will raise an
error if a command which are not allowed inside a script (flaged with the
`deny-script` flag) is invoked (like SHUTDOWN). In addition, on script mode,
write commands are not allowed if there is not enough good replicas (as
configured with `min-replicas-to-write`) and/or a disk error happened.
* `W` - no writes mode, Redis will reject any command that is marked with `write`
flag. Again can be useful to modules that implement a new scripting language
and wants to prevent any write commands.
* `E` - Return errors as RedisModuleCallReply. Today the errors that happened
before the command was invoked (like unknown commands or acl error) return
a NULL reply and set errno. This might be missing important information about
the failure and it is also impossible to just pass the error to the user using
RM_ReplyWithCallReply. This new flag allows you to get a RedisModuleCallReply
object with the relevant error message and treat it as if it was an error that was
raised by the command invocation.
Tests were added to verify the new code paths.
In addition small refactoring was done to share some code between modules,
scripts, and `processCommand` function:
1. `getAclErrorMessage` was added to `acl.c` to unified to log message extraction
from the acl result
2. `checkGoodReplicasStatus` was added to `replication.c` to check the status of
good replicas. It is used on `scriptVerifyWriteCommandAllow`, `RM_Call`, and
`processCommand`.
3. `writeCommandsGetDiskErrorMessage` was added to `server.c` to get the error
message on persistence failure. Again it is used on `scriptVerifyWriteCommandAllow`,
`RM_Call`, and `processCommand`.
2022-03-22 08:13:28 -04:00
|
|
|
T("foo", "E");
|
2022-04-25 06:08:13 -04:00
|
|
|
if (!TestAssertErrorReply(ctx,reply,"ERR unknown command 'foo', with args beginning with: ",53)) goto fail;
|
Add new RM_Call flags for script mode, no writes, and error replies. (#10372)
The PR extends RM_Call with 3 new capabilities using new flags that
are given to RM_Call as part of the `fmt` argument.
It aims to assist modules that are getting a list of commands to be
executed from the user (not hard coded as part of the module logic),
think of a module that implements a new scripting language...
* `S` - Run the command in a script mode, this means that it will raise an
error if a command which are not allowed inside a script (flaged with the
`deny-script` flag) is invoked (like SHUTDOWN). In addition, on script mode,
write commands are not allowed if there is not enough good replicas (as
configured with `min-replicas-to-write`) and/or a disk error happened.
* `W` - no writes mode, Redis will reject any command that is marked with `write`
flag. Again can be useful to modules that implement a new scripting language
and wants to prevent any write commands.
* `E` - Return errors as RedisModuleCallReply. Today the errors that happened
before the command was invoked (like unknown commands or acl error) return
a NULL reply and set errno. This might be missing important information about
the failure and it is also impossible to just pass the error to the user using
RM_ReplyWithCallReply. This new flag allows you to get a RedisModuleCallReply
object with the relevant error message and treat it as if it was an error that was
raised by the command invocation.
Tests were added to verify the new code paths.
In addition small refactoring was done to share some code between modules,
scripts, and `processCommand` function:
1. `getAclErrorMessage` was added to `acl.c` to unified to log message extraction
from the acl result
2. `checkGoodReplicasStatus` was added to `replication.c` to check the status of
good replicas. It is used on `scriptVerifyWriteCommandAllow`, `RM_Call`, and
`processCommand`.
3. `writeCommandsGetDiskErrorMessage` was added to `server.c` to get the error
message on persistence failure. Again it is used on `scriptVerifyWriteCommandAllow`,
`RM_Call`, and `processCommand`.
2022-03-22 08:13:28 -04:00
|
|
|
|
|
|
|
T("set", "Ec", "x");
|
2022-04-25 06:08:13 -04:00
|
|
|
if (!TestAssertErrorReply(ctx,reply,"ERR wrong number of arguments for 'set' command",47)) goto fail;
|
Add new RM_Call flags for script mode, no writes, and error replies. (#10372)
The PR extends RM_Call with 3 new capabilities using new flags that
are given to RM_Call as part of the `fmt` argument.
It aims to assist modules that are getting a list of commands to be
executed from the user (not hard coded as part of the module logic),
think of a module that implements a new scripting language...
* `S` - Run the command in a script mode, this means that it will raise an
error if a command which are not allowed inside a script (flaged with the
`deny-script` flag) is invoked (like SHUTDOWN). In addition, on script mode,
write commands are not allowed if there is not enough good replicas (as
configured with `min-replicas-to-write`) and/or a disk error happened.
* `W` - no writes mode, Redis will reject any command that is marked with `write`
flag. Again can be useful to modules that implement a new scripting language
and wants to prevent any write commands.
* `E` - Return errors as RedisModuleCallReply. Today the errors that happened
before the command was invoked (like unknown commands or acl error) return
a NULL reply and set errno. This might be missing important information about
the failure and it is also impossible to just pass the error to the user using
RM_ReplyWithCallReply. This new flag allows you to get a RedisModuleCallReply
object with the relevant error message and treat it as if it was an error that was
raised by the command invocation.
Tests were added to verify the new code paths.
In addition small refactoring was done to share some code between modules,
scripts, and `processCommand` function:
1. `getAclErrorMessage` was added to `acl.c` to unified to log message extraction
from the acl result
2. `checkGoodReplicasStatus` was added to `replication.c` to check the status of
good replicas. It is used on `scriptVerifyWriteCommandAllow`, `RM_Call`, and
`processCommand`.
3. `writeCommandsGetDiskErrorMessage` was added to `server.c` to get the error
message on persistence failure. Again it is used on `scriptVerifyWriteCommandAllow`,
`RM_Call`, and `processCommand`.
2022-03-22 08:13:28 -04:00
|
|
|
|
|
|
|
T("shutdown", "SE");
|
|
|
|
if (!TestAssertErrorReply(ctx,reply,"ERR command 'shutdown' is not allowed on script mode",52)) goto fail;
|
|
|
|
|
|
|
|
T("set", "WEcc", "x", "1");
|
|
|
|
if (!TestAssertErrorReply(ctx,reply,"ERR Write command 'set' was called while write is not allowed.",62)) goto fail;
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
RedisModule_ReplyWithSimpleString(ctx,"ALL TESTS PASSED");
|
|
|
|
return REDISMODULE_OK;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
RedisModule_ReplyWithSimpleString(ctx,
|
2021-06-22 05:26:48 -04:00
|
|
|
"SOME TEST DID NOT PASS! Check server logs");
|
2016-08-03 04:23:03 -04:00
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
|
2016-10-07 07:10:29 -04:00
|
|
|
REDISMODULE_NOT_USED(argv);
|
|
|
|
REDISMODULE_NOT_USED(argc);
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
if (RedisModule_Init(ctx,"test",1,REDISMODULE_APIVER_1)
|
|
|
|
== REDISMODULE_ERR) return REDISMODULE_ERR;
|
|
|
|
|
2022-10-12 06:09:51 -04:00
|
|
|
/* Perform RM_Call inside the RedisModule_OnLoad
|
|
|
|
* to verify that it works as expected without crashing.
|
|
|
|
* The tests will verify it on different configurations
|
|
|
|
* options (cluster/no cluster). A simple ping command
|
|
|
|
* is enough for this test. */
|
|
|
|
RedisModuleCallReply *reply = RedisModule_Call(ctx, "ping", "");
|
|
|
|
if (RedisModule_CallReplyType(reply) != REDISMODULE_REPLY_STRING) {
|
|
|
|
RedisModule_FreeCallReply(reply);
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
}
|
|
|
|
size_t len;
|
|
|
|
const char *reply_str = RedisModule_CallReplyStringPtr(reply, &len);
|
|
|
|
if (len != 4) {
|
|
|
|
RedisModule_FreeCallReply(reply);
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
}
|
|
|
|
if (memcmp(reply_str, "PONG", 4) != 0) {
|
|
|
|
RedisModule_FreeCallReply(reply);
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
}
|
|
|
|
RedisModule_FreeCallReply(reply);
|
|
|
|
|
2016-08-03 12:10:11 -04:00
|
|
|
if (RedisModule_CreateCommand(ctx,"test.call",
|
|
|
|
TestCall,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
Unified Lua and modules reply parsing and added RESP3 support to RM_Call (#9202)
## Current state
1. Lua has its own parser that handles parsing `reds.call` replies and translates them
to Lua objects that can be used by the user Lua code. The parser partially handles
resp3 (missing big number, verbatim, attribute, ...)
2. Modules have their own parser that handles parsing `RM_Call` replies and translates
them to RedisModuleCallReply objects. The parser does not support resp3.
In addition, in the future, we want to add Redis Function (#8693) that will probably
support more languages. At some point maintaining so many parsers will stop
scaling (bug fixes and protocol changes will need to be applied on all of them).
We will probably end up with different parsers that support different parts of the
resp protocol (like we already have today with Lua and modules)
## PR Changes
This PR attempt to unified the reply parsing of Lua and modules (and in the future
Redis Function) by introducing a new parser unit (`resp_parser.c`). The new parser
handles parsing the reply and calls different callbacks to allow the users (another
unit that uses the parser, i.e, Lua, modules, or Redis Function) to analyze the reply.
### Lua API Additions
The code that handles reply parsing on `scripting.c` was removed. Instead, it uses
the resp_parser to parse and create a Lua object out of the reply. As mentioned
above the Lua parser did not handle parsing big numbers, verbatim, and attribute.
The new parser can handle those and so Lua also gets it for free.
Those are translated to Lua objects in the following way:
1. Big Number - Lua table `{'big_number':'<str representation for big number>'}`
2. Verbatim - Lua table `{'verbatim_string':{'format':'<verbatim format>', 'string':'<verbatim string value>'}}`
3. Attribute - currently ignored and not expose to the Lua parser, another issue will be open to decide how to expose it.
Tests were added to check resp3 reply parsing on Lua
### Modules API Additions
The reply parsing code on `module.c` was also removed and the new resp_parser is used instead.
In addition, the RedisModuleCallReply was also extracted to a separate unit located on `call_reply.c`
(in the future, this unit will also be used by Redis Function). A nice side effect of unified parsing is
that modules now also support resp3. Resp3 can be enabled by giving `3` as a parameter to the
fmt argument of `RM_Call`. It is also possible to give `0`, which will indicate an auto mode. i.e, Redis
will automatically chose the reply protocol base on the current client set on the RedisModuleCtx
(this mode will mostly be used when the module want to pass the reply to the client as is).
In addition, the following RedisModuleAPI were added to allow analyzing resp3 replies:
* New RedisModuleCallReply types:
* `REDISMODULE_REPLY_MAP`
* `REDISMODULE_REPLY_SET`
* `REDISMODULE_REPLY_BOOL`
* `REDISMODULE_REPLY_DOUBLE`
* `REDISMODULE_REPLY_BIG_NUMBER`
* `REDISMODULE_REPLY_VERBATIM_STRING`
* `REDISMODULE_REPLY_ATTRIBUTE`
* New RedisModuleAPI:
* `RedisModule_CallReplyDouble` - getting double value from resp3 double reply
* `RedisModule_CallReplyBool` - getting boolean value from resp3 boolean reply
* `RedisModule_CallReplyBigNumber` - getting big number value from resp3 big number reply
* `RedisModule_CallReplyVerbatim` - getting format and value from resp3 verbatim reply
* `RedisModule_CallReplySetElement` - getting element from resp3 set reply
* `RedisModule_CallReplyMapElement` - getting key and value from resp3 map reply
* `RedisModule_CallReplyAttribute` - getting a reply attribute
* `RedisModule_CallReplyAttributeElement` - getting key and value from resp3 attribute reply
* New context flags:
* `REDISMODULE_CTX_FLAGS_RESP3` - indicate that the client is using resp3
Tests were added to check the new RedisModuleAPI
### Modules API Changes
* RM_ReplyWithCallReply might return REDISMODULE_ERR if the given CallReply is in resp3
but the client expects resp2. This is not a breaking change because in order to get a resp3
CallReply one needs to specifically specify `3` as a parameter to the fmt argument of
`RM_Call` (as mentioned above).
Tests were added to check this change
### More small Additions
* Added `debug set-disable-deny-scripts` that allows to turn on and off the commands no-script
flag protection. This is used by the Lua resp3 tests so it will be possible to run `debug protocol`
and check the resp3 parsing code.
Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Yossi Gottlieb <yossigo@gmail.com>
2021-08-04 09:28:07 -04:00
|
|
|
if (RedisModule_CreateCommand(ctx,"test.callresp3map",
|
|
|
|
TestCallResp3Map,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.callresp3attribute",
|
|
|
|
TestCallResp3Attribute,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.callresp3set",
|
|
|
|
TestCallResp3Set,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.callresp3double",
|
|
|
|
TestCallResp3Double,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.callresp3bool",
|
|
|
|
TestCallResp3Bool,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.callresp3null",
|
|
|
|
TestCallResp3Null,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.callreplywitharrayreply",
|
|
|
|
TestCallReplyWithArrayReply,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.callreplywithnestedreply",
|
|
|
|
TestCallReplyWithNestedReply,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.callreplywithbignumberreply",
|
|
|
|
TestCallResp3BigNumber,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.callreplywithverbatimstringreply",
|
|
|
|
TestCallResp3Verbatim,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
if (RedisModule_CreateCommand(ctx,"test.string.append",
|
|
|
|
TestStringAppend,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2023-02-28 12:38:58 -05:00
|
|
|
if (RedisModule_CreateCommand(ctx,"test.string.trim",
|
|
|
|
TestTrimString,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
if (RedisModule_CreateCommand(ctx,"test.string.append.am",
|
|
|
|
TestStringAppendAM,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2021-06-22 05:26:48 -04:00
|
|
|
if (RedisModule_CreateCommand(ctx,"test.string.truncate",
|
|
|
|
TestStringTruncate,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2016-09-21 05:30:38 -04:00
|
|
|
if (RedisModule_CreateCommand(ctx,"test.string.printf",
|
|
|
|
TestStringPrintf,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2017-09-27 04:52:39 -04:00
|
|
|
if (RedisModule_CreateCommand(ctx,"test.ctxflags",
|
|
|
|
TestCtxFlags,"readonly",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
2019-03-19 07:11:37 -04:00
|
|
|
|
2018-01-07 09:41:43 -05:00
|
|
|
if (RedisModule_CreateCommand(ctx,"test.unlink",
|
|
|
|
TestUnlink,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
2017-09-27 04:52:39 -04:00
|
|
|
|
2021-09-09 04:03:05 -04:00
|
|
|
if (RedisModule_CreateCommand(ctx,"test.nestedcallreplyarray",
|
|
|
|
TestNestedCallReplyArrayElement,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2021-06-22 05:26:48 -04:00
|
|
|
if (RedisModule_CreateCommand(ctx,"test.basics",
|
2021-11-28 04:26:28 -05:00
|
|
|
TestBasics,"write",1,1,1) == REDISMODULE_ERR)
|
2016-08-03 04:23:03 -04:00
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
Unified Lua and modules reply parsing and added RESP3 support to RM_Call (#9202)
## Current state
1. Lua has its own parser that handles parsing `reds.call` replies and translates them
to Lua objects that can be used by the user Lua code. The parser partially handles
resp3 (missing big number, verbatim, attribute, ...)
2. Modules have their own parser that handles parsing `RM_Call` replies and translates
them to RedisModuleCallReply objects. The parser does not support resp3.
In addition, in the future, we want to add Redis Function (#8693) that will probably
support more languages. At some point maintaining so many parsers will stop
scaling (bug fixes and protocol changes will need to be applied on all of them).
We will probably end up with different parsers that support different parts of the
resp protocol (like we already have today with Lua and modules)
## PR Changes
This PR attempt to unified the reply parsing of Lua and modules (and in the future
Redis Function) by introducing a new parser unit (`resp_parser.c`). The new parser
handles parsing the reply and calls different callbacks to allow the users (another
unit that uses the parser, i.e, Lua, modules, or Redis Function) to analyze the reply.
### Lua API Additions
The code that handles reply parsing on `scripting.c` was removed. Instead, it uses
the resp_parser to parse and create a Lua object out of the reply. As mentioned
above the Lua parser did not handle parsing big numbers, verbatim, and attribute.
The new parser can handle those and so Lua also gets it for free.
Those are translated to Lua objects in the following way:
1. Big Number - Lua table `{'big_number':'<str representation for big number>'}`
2. Verbatim - Lua table `{'verbatim_string':{'format':'<verbatim format>', 'string':'<verbatim string value>'}}`
3. Attribute - currently ignored and not expose to the Lua parser, another issue will be open to decide how to expose it.
Tests were added to check resp3 reply parsing on Lua
### Modules API Additions
The reply parsing code on `module.c` was also removed and the new resp_parser is used instead.
In addition, the RedisModuleCallReply was also extracted to a separate unit located on `call_reply.c`
(in the future, this unit will also be used by Redis Function). A nice side effect of unified parsing is
that modules now also support resp3. Resp3 can be enabled by giving `3` as a parameter to the
fmt argument of `RM_Call`. It is also possible to give `0`, which will indicate an auto mode. i.e, Redis
will automatically chose the reply protocol base on the current client set on the RedisModuleCtx
(this mode will mostly be used when the module want to pass the reply to the client as is).
In addition, the following RedisModuleAPI were added to allow analyzing resp3 replies:
* New RedisModuleCallReply types:
* `REDISMODULE_REPLY_MAP`
* `REDISMODULE_REPLY_SET`
* `REDISMODULE_REPLY_BOOL`
* `REDISMODULE_REPLY_DOUBLE`
* `REDISMODULE_REPLY_BIG_NUMBER`
* `REDISMODULE_REPLY_VERBATIM_STRING`
* `REDISMODULE_REPLY_ATTRIBUTE`
* New RedisModuleAPI:
* `RedisModule_CallReplyDouble` - getting double value from resp3 double reply
* `RedisModule_CallReplyBool` - getting boolean value from resp3 boolean reply
* `RedisModule_CallReplyBigNumber` - getting big number value from resp3 big number reply
* `RedisModule_CallReplyVerbatim` - getting format and value from resp3 verbatim reply
* `RedisModule_CallReplySetElement` - getting element from resp3 set reply
* `RedisModule_CallReplyMapElement` - getting key and value from resp3 map reply
* `RedisModule_CallReplyAttribute` - getting a reply attribute
* `RedisModule_CallReplyAttributeElement` - getting key and value from resp3 attribute reply
* New context flags:
* `REDISMODULE_CTX_FLAGS_RESP3` - indicate that the client is using resp3
Tests were added to check the new RedisModuleAPI
### Modules API Changes
* RM_ReplyWithCallReply might return REDISMODULE_ERR if the given CallReply is in resp3
but the client expects resp2. This is not a breaking change because in order to get a resp3
CallReply one needs to specifically specify `3` as a parameter to the fmt argument of
`RM_Call` (as mentioned above).
Tests were added to check this change
### More small Additions
* Added `debug set-disable-deny-scripts` that allows to turn on and off the commands no-script
flag protection. This is used by the Lua resp3 tests so it will be possible to run `debug protocol`
and check the resp3 parsing code.
Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Yossi Gottlieb <yossigo@gmail.com>
2021-08-04 09:28:07 -04:00
|
|
|
/* the following commands are used by an external test and should not be added to TestBasics */
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.rmcallautomode",
|
2021-11-28 04:26:28 -05:00
|
|
|
TestCallRespAutoMode,"write",1,1,1) == REDISMODULE_ERR)
|
Unified Lua and modules reply parsing and added RESP3 support to RM_Call (#9202)
## Current state
1. Lua has its own parser that handles parsing `reds.call` replies and translates them
to Lua objects that can be used by the user Lua code. The parser partially handles
resp3 (missing big number, verbatim, attribute, ...)
2. Modules have their own parser that handles parsing `RM_Call` replies and translates
them to RedisModuleCallReply objects. The parser does not support resp3.
In addition, in the future, we want to add Redis Function (#8693) that will probably
support more languages. At some point maintaining so many parsers will stop
scaling (bug fixes and protocol changes will need to be applied on all of them).
We will probably end up with different parsers that support different parts of the
resp protocol (like we already have today with Lua and modules)
## PR Changes
This PR attempt to unified the reply parsing of Lua and modules (and in the future
Redis Function) by introducing a new parser unit (`resp_parser.c`). The new parser
handles parsing the reply and calls different callbacks to allow the users (another
unit that uses the parser, i.e, Lua, modules, or Redis Function) to analyze the reply.
### Lua API Additions
The code that handles reply parsing on `scripting.c` was removed. Instead, it uses
the resp_parser to parse and create a Lua object out of the reply. As mentioned
above the Lua parser did not handle parsing big numbers, verbatim, and attribute.
The new parser can handle those and so Lua also gets it for free.
Those are translated to Lua objects in the following way:
1. Big Number - Lua table `{'big_number':'<str representation for big number>'}`
2. Verbatim - Lua table `{'verbatim_string':{'format':'<verbatim format>', 'string':'<verbatim string value>'}}`
3. Attribute - currently ignored and not expose to the Lua parser, another issue will be open to decide how to expose it.
Tests were added to check resp3 reply parsing on Lua
### Modules API Additions
The reply parsing code on `module.c` was also removed and the new resp_parser is used instead.
In addition, the RedisModuleCallReply was also extracted to a separate unit located on `call_reply.c`
(in the future, this unit will also be used by Redis Function). A nice side effect of unified parsing is
that modules now also support resp3. Resp3 can be enabled by giving `3` as a parameter to the
fmt argument of `RM_Call`. It is also possible to give `0`, which will indicate an auto mode. i.e, Redis
will automatically chose the reply protocol base on the current client set on the RedisModuleCtx
(this mode will mostly be used when the module want to pass the reply to the client as is).
In addition, the following RedisModuleAPI were added to allow analyzing resp3 replies:
* New RedisModuleCallReply types:
* `REDISMODULE_REPLY_MAP`
* `REDISMODULE_REPLY_SET`
* `REDISMODULE_REPLY_BOOL`
* `REDISMODULE_REPLY_DOUBLE`
* `REDISMODULE_REPLY_BIG_NUMBER`
* `REDISMODULE_REPLY_VERBATIM_STRING`
* `REDISMODULE_REPLY_ATTRIBUTE`
* New RedisModuleAPI:
* `RedisModule_CallReplyDouble` - getting double value from resp3 double reply
* `RedisModule_CallReplyBool` - getting boolean value from resp3 boolean reply
* `RedisModule_CallReplyBigNumber` - getting big number value from resp3 big number reply
* `RedisModule_CallReplyVerbatim` - getting format and value from resp3 verbatim reply
* `RedisModule_CallReplySetElement` - getting element from resp3 set reply
* `RedisModule_CallReplyMapElement` - getting key and value from resp3 map reply
* `RedisModule_CallReplyAttribute` - getting a reply attribute
* `RedisModule_CallReplyAttributeElement` - getting key and value from resp3 attribute reply
* New context flags:
* `REDISMODULE_CTX_FLAGS_RESP3` - indicate that the client is using resp3
Tests were added to check the new RedisModuleAPI
### Modules API Changes
* RM_ReplyWithCallReply might return REDISMODULE_ERR if the given CallReply is in resp3
but the client expects resp2. This is not a breaking change because in order to get a resp3
CallReply one needs to specifically specify `3` as a parameter to the fmt argument of
`RM_Call` (as mentioned above).
Tests were added to check this change
### More small Additions
* Added `debug set-disable-deny-scripts` that allows to turn on and off the commands no-script
flag protection. This is used by the Lua resp3 tests so it will be possible to run `debug protocol`
and check the resp3 parsing code.
Co-authored-by: Oran Agra <oran@redislabs.com>
Co-authored-by: Yossi Gottlieb <yossigo@gmail.com>
2021-08-04 09:28:07 -04:00
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.getresp",
|
|
|
|
TestGetResp,"readonly",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2017-11-27 09:29:55 -05:00
|
|
|
RedisModule_SubscribeToKeyspaceEvents(ctx,
|
|
|
|
REDISMODULE_NOTIFY_HASH |
|
|
|
|
REDISMODULE_NOTIFY_SET |
|
2019-03-19 07:11:37 -04:00
|
|
|
REDISMODULE_NOTIFY_STRING |
|
2019-03-21 05:47:14 -04:00
|
|
|
REDISMODULE_NOTIFY_KEY_MISS,
|
2017-11-27 09:29:55 -05:00
|
|
|
NotifyCallback);
|
|
|
|
if (RedisModule_CreateCommand(ctx,"test.notify",
|
|
|
|
TestNotifications,"write deny-oom",1,1,1) == REDISMODULE_ERR)
|
|
|
|
return REDISMODULE_ERR;
|
|
|
|
|
2016-08-03 04:23:03 -04:00
|
|
|
return REDISMODULE_OK;
|
|
|
|
}
|