Unverified Commit 86781600 authored by guybe7's avatar guybe7 Committed by GitHub
Browse files

Auto-generate the command table from JSON files (#9656)

Delete the hardcoded command table and replace it with an auto-generated table, based
on a JSON file that describes the commands (each command must have a JSON file).

These JSON files are the SSOT of everything there is to know about Redis commands,
and it is reflected fully in COMMAND INFO.

These JSON files are used to generate commands.c (using a python script), which is then
committed to the repo and compiled.

The purpose is:
* Clients and proxies will be able to get much more info from redis, instead of relying on hard coded logic.
* drop the dependency between Redis-user and the commands.json in redis-doc.
* delete help.h and have redis-cli learn everything it needs to know just by issuing COMMAND (will be
  done in a separate PR)
* redis.io should stop using commands.json and learn everything from Redis (ultimately one of the release
  artifacts should be a large JSON, containing all the information about all of the commands, which will be
  generated from COMMAND's reply)
* the byproduct of this is:
  * module commands will be able to provide that info and possibly be more of a first-class citizens
  * in theory, one may be able to generate a redis client library for a strictly typed language, by using this info.

### Interface changes

#### COMMAND INFO's reply change (and arg-less COMMAND)

Before this commit the reply at index 7 contained the key-specs list
and reply at index 8 contained the sub-commands list (Both unreleased).
Now, reply at index 7 is a map of:
- summary - short command description
- since - debut version
- group - command group
- complexity - complexity string
- doc-flags - flags used for documentation (e.g. "deprecated")
- deprecated-since - if deprecated, from which version?
- replaced-by - if deprecated, which command replaced it?
- history - a list of (version, what-changed) tuples
- hints - a list of strings, meant to provide hints for clients/proxies. see https://github.com/redis/redis/issues/9876
- arguments - an array of arguments. each element is a map, with the possibility of nesting (sub-arguments)
- key-specs - an array of keys specs (already in unstable, just changed location)
- subcommands - a list of sub-commands (already in unstable, just changed location)
- reply-schema - will be added in the future (see https://github.com/redis/redis/issues/9845)

more details on these can be found in https://github.com/redis/redis-doc/pull/1697

only the first three fields are mandatory 

#### API changes (unreleased API obviously)

now they take RedisModuleCommand opaque pointer instead of looking up the command by name

- RM_CreateSubcommand
- RM_AddCommandKeySpec
- RM_SetCommandKeySpecBeginSearchIndex
- RM_SetCommandKeySpecBeginSearchKeyword
- RM_SetCommandKeySpecFindKeysRange
- RM_SetCommandKeySpecFindKeysKeynum

Currently, we did not add module API to provide additional information about their commands because
we couldn't agree on how the API should look like, see https://github.com/redis/redis/issues/9944

.

### Somehow related changes
1. Literals should be in uppercase while placeholder in lowercase. Now all the GEO* command
   will be documented with M|KM|FT|MI and can take both lowercase and uppercase

### Unrelated changes
1. Bugfix: no_madaory_keys was absent in COMMAND's reply
2. expose CMD_MODULE as "module" via COMMAND
3. have a dedicated uint64 for ACL categories (instead of having them in the same uint64 as command flags)
Co-authored-by: default avatarItamar Haber <itamar@garantiadata.com>
parent fbfdf513
......@@ -45,3 +45,4 @@ $TCLSH tests/test_helper.tcl \
--single unit/moduleapi/subcommands \
--single unit/moduleapi/reply \
"${@}"
......@@ -309,7 +309,7 @@ endif
REDIS_SERVER_NAME=redis-server$(PROG_SUFFIX)
REDIS_SENTINEL_NAME=redis-sentinel$(PROG_SUFFIX)
REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o eval.o bio.o rio.o rand.o memtest.o crcspeed.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o lolwut6.o acl.o tracking.o connection.o tls.o sha256.o timeout.o setcpuaffinity.o monotonic.o mt19937-64.o resp_parser.o call_reply.o script_lua.o script.o functions.o function_lua.o
REDIS_SERVER_OBJ=adlist.o quicklist.o ae.o anet.o dict.o server.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o cluster.o crc16.o endianconv.o slowlog.o eval.o bio.o rio.o rand.o memtest.o crcspeed.o crc64.o bitops.o sentinel.o notify.o setproctitle.o blocked.o hyperloglog.o latency.o sparkline.o redis-check-rdb.o redis-check-aof.o geo.o lazyfree.o module.o evict.o expire.o geohash.o geohash_helper.o childinfo.o defrag.o siphash.o rax.o t_stream.o listpack.o localtime.o lolwut.o lolwut5.o lolwut6.o acl.o tracking.o connection.o tls.o sha256.o timeout.o setcpuaffinity.o monotonic.o mt19937-64.o resp_parser.o call_reply.o script_lua.o script.o functions.o function_lua.o commands.o
REDIS_CLI_NAME=redis-cli$(PROG_SUFFIX)
REDIS_CLI_OBJ=anet.o adlist.o dict.o redis-cli.o zmalloc.o release.o ae.o redisassert.o crcspeed.o crc64.o siphash.o crc16.o monotonic.o cli_common.o mt19937-64.o
REDIS_BENCHMARK_NAME=redis-benchmark$(PROG_SUFFIX)
......@@ -451,9 +451,6 @@ valgrind:
helgrind:
$(MAKE) OPTIMIZATION="-O0" MALLOC="libc" CFLAGS="-D__ATOMIC_VAR_FORCE_SYNC_MACROS" REDIS_CFLAGS="-I/usr/local/include" REDIS_LDFLAGS="-L/usr/local/lib"
src/help.h:
@../utils/generate-command-help.rb > help.h
install: all
@mkdir -p $(INSTALL_BIN)
$(call MAKE_INSTALL,$(REDIS_SERVER_NAME),$(INSTALL_BIN))
......
......@@ -61,27 +61,27 @@ struct ACLCategoryItem {
const char *name;
uint64_t flag;
} ACLCommandCategories[] = { /* See redis.conf for details on each category. */
{"keyspace", CMD_CATEGORY_KEYSPACE},
{"read", CMD_CATEGORY_READ},
{"write", CMD_CATEGORY_WRITE},
{"set", CMD_CATEGORY_SET},
{"sortedset", CMD_CATEGORY_SORTEDSET},
{"list", CMD_CATEGORY_LIST},
{"hash", CMD_CATEGORY_HASH},
{"string", CMD_CATEGORY_STRING},
{"bitmap", CMD_CATEGORY_BITMAP},
{"hyperloglog", CMD_CATEGORY_HYPERLOGLOG},
{"geo", CMD_CATEGORY_GEO},
{"stream", CMD_CATEGORY_STREAM},
{"pubsub", CMD_CATEGORY_PUBSUB},
{"admin", CMD_CATEGORY_ADMIN},
{"fast", CMD_CATEGORY_FAST},
{"slow", CMD_CATEGORY_SLOW},
{"blocking", CMD_CATEGORY_BLOCKING},
{"dangerous", CMD_CATEGORY_DANGEROUS},
{"connection", CMD_CATEGORY_CONNECTION},
{"transaction", CMD_CATEGORY_TRANSACTION},
{"scripting", CMD_CATEGORY_SCRIPTING},
{"keyspace", ACL_CATEGORY_KEYSPACE},
{"read", ACL_CATEGORY_READ},
{"write", ACL_CATEGORY_WRITE},
{"set", ACL_CATEGORY_SET},
{"sortedset", ACL_CATEGORY_SORTEDSET},
{"list", ACL_CATEGORY_LIST},
{"hash", ACL_CATEGORY_HASH},
{"string", ACL_CATEGORY_STRING},
{"bitmap", ACL_CATEGORY_BITMAP},
{"hyperloglog", ACL_CATEGORY_HYPERLOGLOG},
{"geo", ACL_CATEGORY_GEO},
{"stream", ACL_CATEGORY_STREAM},
{"pubsub", ACL_CATEGORY_PUBSUB},
{"admin", ACL_CATEGORY_ADMIN},
{"fast", ACL_CATEGORY_FAST},
{"slow", ACL_CATEGORY_SLOW},
{"blocking", ACL_CATEGORY_BLOCKING},
{"dangerous", ACL_CATEGORY_DANGEROUS},
{"connection", ACL_CATEGORY_CONNECTION},
{"transaction", ACL_CATEGORY_TRANSACTION},
{"scripting", ACL_CATEGORY_SCRIPTING},
{NULL,0} /* Terminator. */
};
......@@ -436,7 +436,7 @@ void ACLSetUserCommandBitsForCategoryLogic(dict *commands, user *u, uint64_t cfl
while ((de = dictNext(di)) != NULL) {
struct redisCommand *cmd = dictGetVal(de);
if (cmd->flags & CMD_MODULE) continue; /* Ignore modules commands. */
if (cmd->flags & cflag) {
if (cmd->acl_categories & cflag) {
ACLChangeCommandPerm(u,cmd,value);
}
if (cmd->subcommands_dict) {
......@@ -464,7 +464,7 @@ void ACLCountCategoryBitsForCommands(dict *commands, user *u, unsigned long *on,
dictEntry *de;
while ((de = dictNext(di)) != NULL) {
struct redisCommand *cmd = dictGetVal(de);
if (cmd->flags & cflag) {
if (cmd->acl_categories & cflag) {
if (ACLGetUserCommandBit(u,cmd->id))
(*on)++;
else
......@@ -1875,6 +1875,8 @@ void ACLFreeLogEntry(void *leptr) {
*
* The last 2 arguments are a manual override to be used, instead of any of the automatic
* ones which depend on the client and reason arguments (use NULL for default).
*
* If `object` is not NULL, this functions takes over it.
*/
void addACLLogEntry(client *c, int reason, int context, int argpos, sds username, sds object) {
/* Create a new entry. */
......@@ -1885,7 +1887,7 @@ void addACLLogEntry(client *c, int reason, int context, int argpos, sds username
le->ctime = mstime();
if (object) {
le->object = sdsnew(object);
le->object = object;
} else {
switch(reason) {
case ACL_DENIED_CMD: le->object = sdsnew(c->cmd->name); break;
......@@ -2171,7 +2173,7 @@ void aclCommand(client *c) {
while ((de = dictNext(di)) != NULL) {
struct redisCommand *cmd = dictGetVal(de);
if (cmd->flags & CMD_MODULE) continue;
if (cmd->flags & cflag) {
if (cmd->acl_categories & cflag) {
addReplyBulkCString(c,cmd->name);
arraylen++;
}
......@@ -2303,7 +2305,7 @@ void addReplyCommandCategories(client *c, struct redisCommand *cmd) {
int flagcount = 0;
void *flaglen = addReplyDeferredLen(c);
for (int j = 0; ACLCommandCategories[j].flag != 0; j++) {
if (cmd->flags & ACLCommandCategories[j].flag) {
if (cmd->acl_categories & ACLCommandCategories[j].flag) {
addReplyStatusFormat(c, "@%s", ACLCommandCategories[j].name);
flagcount++;
}
......
This diff is collapsed.
{
"CAT": {
"summary": "List the ACL categories or the commands inside a category",
"complexity": "O(1) since the categories and commands are a fixed set.",
"group": "server",
"since": "6.0.0",
"arity": -2,
"container": "ACL",
"function": "aclCommand",
"command_flags": [
"NOSCRIPT",
"LOADING",
"STALE",
"SENTINEL"
],
"arguments": [
{
"name": "categoryname",
"type": "string",
"optional": true
}
]
}
}
{
"DELUSER": {
"summary": "Remove the specified ACL users and the associated rules",
"complexity": "O(1) amortized time considering the typical user.",
"group": "server",
"since": "6.0.0",
"arity": -3,
"container": "ACL",
"function": "aclCommand",
"command_flags": [
"ADMIN",
"NOSCRIPT",
"LOADING",
"STALE",
"SENTINEL"
],
"arguments": [
{
"name": "username",
"type": "string",
"multiple": true
}
]
}
}
{
"GENPASS": {
"summary": "Generate a pseudorandom secure password to use for ACL users",
"complexity": "O(1)",
"group": "server",
"since": "6.0.0",
"arity": -2,
"container": "ACL",
"function": "aclCommand",
"command_flags": [
"NOSCRIPT",
"LOADING",
"STALE",
"SENTINEL"
],
"arguments": [
{
"name": "bits",
"type": "integer",
"optional": true
}
]
}
}
{
"GETUSER": {
"summary": "Get the rules for a specific ACL user",
"complexity": "O(N). Where N is the number of password, command and pattern rules that the user has.",
"group": "server",
"since": "6.0.0",
"arity": 3,
"container": "ACL",
"function": "aclCommand",
"history": [
[
"6.2",
"Added Pub/Sub channel patterns."
]
],
"command_flags": [
"ADMIN",
"NOSCRIPT",
"LOADING",
"STALE",
"SENTINEL"
],
"arguments": [
{
"name": "username",
"type": "string"
}
]
}
}
{
"HELP": {
"summary": "Show helpful text about the different subcommands",
"complexity": "O(1)",
"group": "server",
"since": "6.0.0",
"arity": 2,
"container": "ACL",
"function": "aclCommand",
"command_flags": [
"LOADING",
"STALE",
"SENTINEL"
]
}
}
{
"LIST": {
"summary": "List the current ACL rules in ACL config file format",
"complexity": "O(N). Where N is the number of configured users.",
"group": "server",
"since": "6.0.0",
"arity": 2,
"container": "ACL",
"function": "aclCommand",
"command_flags": [
"ADMIN",
"NOSCRIPT",
"LOADING",
"STALE",
"SENTINEL"
]
}
}
{
"LOAD": {
"summary": "Reload the ACLs from the configured ACL file",
"complexity": "O(N). Where N is the number of configured users.",
"group": "server",
"since": "6.0.0",
"arity": 2,
"container": "ACL",
"function": "aclCommand",
"command_flags": [
"ADMIN",
"NOSCRIPT",
"LOADING",
"STALE",
"SENTINEL"
]
}
}
{
"LOG": {
"summary": "List latest events denied because of ACLs in place",
"complexity": "O(N) with N being the number of entries shown.",
"group": "server",
"since": "6.0.0",
"arity": -2,
"container": "ACL",
"function": "aclCommand",
"command_flags": [
"ADMIN",
"NOSCRIPT",
"LOADING",
"STALE",
"SENTINEL"
],
"arguments": [
{
"name": "operation",
"type": "oneof",
"optional": true,
"arguments": [
{
"name": "count",
"type": "integer"
},
{
"name": "reset",
"type": "pure-token",
"token": "RESET"
}
]
}
]
}
}
{
"SAVE": {
"summary": "Save the current ACL rules in the configured ACL file",
"complexity": "O(N). Where N is the number of configured users.",
"group": "server",
"since": "6.0.0",
"arity": 2,
"container": "ACL",
"function": "aclCommand",
"command_flags": [
"ADMIN",
"NOSCRIPT",
"LOADING",
"STALE",
"SENTINEL"
]
}
}
{
"SETUSER": {
"summary": "Modify or create the rules for a specific ACL user",
"complexity": "O(N). Where N is the number of rules provided.",
"group": "server",
"since": "6.0.0",
"arity": -3,
"container": "ACL",
"function": "aclCommand",
"history": [
[
"6.2",
"Added Pub/Sub channel patterns."
]
],
"command_flags": [
"ADMIN",
"NOSCRIPT",
"LOADING",
"STALE",
"SENTINEL"
],
"arguments": [
{
"name": "username",
"type": "string"
},
{
"name": "rule",
"type": "string",
"optional": true,
"multiple": true
}
]
}
}
{
"USERS": {
"summary": "List the username of all the configured ACL rules",
"complexity": "O(N). Where N is the number of configured users.",
"group": "server",
"since": "6.0.0",
"arity": 2,
"container": "ACL",
"function": "aclCommand",
"command_flags": [
"ADMIN",
"NOSCRIPT",
"LOADING",
"STALE",
"SENTINEL"
]
}
}
{
"WHOAMI": {
"summary": "Return the name of the user associated to the current connection",
"complexity": "O(1)",
"group": "server",
"since": "6.0.0",
"arity": 2,
"container": "ACL",
"function": "aclCommand",
"command_flags": [
"NOSCRIPT",
"LOADING",
"STALE",
"SENTINEL"
]
}
}
{
"ACL": {
"summary": "A container for Access List Control commands ",
"complexity": "Depends on subcommand.",
"group": "server",
"since": "6.0.0",
"arity": -2,
"command_flags": [
"SENTINEL"
]
}
}
{
"APPEND": {
"summary": "Append a value to a key",
"complexity": "O(1). The amortized time complexity is O(1) assuming the appended value is small and the already present value is of any size, since the dynamic string library used by Redis will double the free space available on every reallocation.",
"group": "string",
"since": "2.0.0",
"arity": 3,
"function": "appendCommand",
"command_flags": [
"WRITE",
"DENYOOM",
"FAST"
],
"acl_categories": [
"STRING"
],
"key_specs": [
{
"flags": [
"WRITE"
],
"begin_search": {
"index": {
"pos": 1
}
},
"find_keys": {
"range": {
"lastkey": 0,
"step": 1,
"limit": 0
}
}
}
],
"arguments": [
{
"name": "key",
"type": "key",
"key_spec_index": 0
},
{
"name": "value",
"type": "string"
}
]
}
}
{
"ASKING": {
"summary": "Sent by cluster clients after an -ASK redirect",
"complexity": "O(1)",
"group": "cluster",
"since": "3.0.0",
"arity": 1,
"function": "askingCommand",
"command_flags": [
"FAST"
],
"acl_categories": [
"CONNECTION"
]
}
}
{
"AUTH": {
"summary": "Authenticate to the server",
"complexity": "O(N) where N is the number of passwords defined for the user",
"group": "connection",
"since": "1.0.0",
"arity": -2,
"function": "authCommand",
"history": [
[
"6.0.0",
"Added ACL style (username and password)."
]
],
"command_flags": [
"NOSCRIPT",
"LOADING",
"STALE",
"FAST",
"NO_AUTH",
"SENTINEL"
],
"acl_categories": [
"CONNECTION"
],
"arguments": [
{
"name": "username",
"type": "string",
"optional": true
},
{
"name": "password",
"type": "string"
}
]
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment