Skip to content

Commit

Permalink
redirect by per client's capability
Browse files Browse the repository at this point in the history
  • Loading branch information
soloestoy committed May 16, 2024
1 parent 15163c9 commit 0c67122
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 17 deletions.
23 changes: 23 additions & 0 deletions src/commands.def
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,28 @@ struct COMMAND_ARG CLIENT_CACHING_Args[] = {
{MAKE_ARG("mode",ARG_TYPE_ONEOF,-1,NULL,NULL,NULL,CMD_ARG_NONE,2,NULL),.subargs=CLIENT_CACHING_mode_Subargs},
};

/********** CLIENT CAPA ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
/* CLIENT CAPA history */
#define CLIENT_CAPA_History NULL
#endif

#ifndef SKIP_CMD_TIPS_TABLE
/* CLIENT CAPA tips */
#define CLIENT_CAPA_Tips NULL
#endif

#ifndef SKIP_CMD_KEY_SPECS_TABLE
/* CLIENT CAPA key specs */
#define CLIENT_CAPA_Keyspecs NULL
#endif

/* CLIENT CAPA argument table */
struct COMMAND_ARG CLIENT_CAPA_Args[] = {
{MAKE_ARG("capability",ARG_TYPE_STRING,-1,NULL,NULL,NULL,CMD_ARG_NONE,0,NULL)},
};

/********** CLIENT GETNAME ********************/

#ifndef SKIP_CMD_HISTORY_TABLE
Expand Down Expand Up @@ -1543,6 +1565,7 @@ struct COMMAND_ARG CLIENT_UNBLOCK_Args[] = {
/* CLIENT command table */
struct COMMAND_STRUCT CLIENT_Subcommands[] = {
{MAKE_CMD("caching","Instructs the server whether to track the keys in the next request.","O(1)","6.0.0",CMD_DOC_NONE,NULL,NULL,"connection",COMMAND_GROUP_CONNECTION,CLIENT_CACHING_History,0,CLIENT_CACHING_Tips,0,clientCommand,3,CMD_NOSCRIPT|CMD_LOADING|CMD_STALE|CMD_SENTINEL,ACL_CATEGORY_CONNECTION,CLIENT_CACHING_Keyspecs,0,NULL,1),.args=CLIENT_CACHING_Args},
{MAKE_CMD("capa","A client claims its capability.","O(1)","8.0.0",CMD_DOC_NONE,NULL,NULL,"connection",COMMAND_GROUP_CONNECTION,CLIENT_CAPA_History,0,CLIENT_CAPA_Tips,0,clientCommand,3,CMD_NOSCRIPT|CMD_LOADING|CMD_STALE,ACL_CATEGORY_CONNECTION,CLIENT_CAPA_Keyspecs,0,NULL,1),.args=CLIENT_CAPA_Args},
{MAKE_CMD("getname","Returns the name of the connection.","O(1)","2.6.9",CMD_DOC_NONE,NULL,NULL,"connection",COMMAND_GROUP_CONNECTION,CLIENT_GETNAME_History,0,CLIENT_GETNAME_Tips,0,clientCommand,2,CMD_NOSCRIPT|CMD_LOADING|CMD_STALE|CMD_SENTINEL,ACL_CATEGORY_CONNECTION,CLIENT_GETNAME_Keyspecs,0,NULL,0)},
{MAKE_CMD("getredir","Returns the client ID to which the connection's tracking notifications are redirected.","O(1)","6.0.0",CMD_DOC_NONE,NULL,NULL,"connection",COMMAND_GROUP_CONNECTION,CLIENT_GETREDIR_History,0,CLIENT_GETREDIR_Tips,0,clientCommand,2,CMD_NOSCRIPT|CMD_LOADING|CMD_STALE|CMD_SENTINEL,ACL_CATEGORY_CONNECTION,CLIENT_GETREDIR_Keyspecs,0,NULL,0)},
{MAKE_CMD("help","Returns helpful text about the different subcommands.","O(1)","5.0.0",CMD_DOC_NONE,NULL,NULL,"connection",COMMAND_GROUP_CONNECTION,CLIENT_HELP_History,0,CLIENT_HELP_Tips,0,clientCommand,2,CMD_LOADING|CMD_STALE|CMD_SENTINEL,ACL_CATEGORY_CONNECTION,CLIENT_HELP_Keyspecs,0,NULL,0)},
Expand Down
1 change: 0 additions & 1 deletion src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -3100,7 +3100,6 @@ standardConfig static_configs[] = {
createBoolConfig("replica-serve-stale-data", "slave-serve-stale-data", MODIFIABLE_CONFIG, server.repl_serve_stale_data, 1, NULL, NULL),
createBoolConfig("replica-read-only", "slave-read-only", DEBUG_CONFIG | MODIFIABLE_CONFIG, server.repl_slave_ro, 1, NULL, NULL),
createBoolConfig("replica-ignore-maxmemory", "slave-ignore-maxmemory", MODIFIABLE_CONFIG, server.repl_slave_ignore_maxmemory, 1, NULL, NULL),
createBoolConfig("replica-enable-redirect", NULL, MODIFIABLE_CONFIG, server.repl_replica_enable_redirect, 0, NULL, NULL),
createBoolConfig("jemalloc-bg-thread", NULL, MODIFIABLE_CONFIG, server.jemalloc_bg_thread, 1, NULL, updateJemallocBgThread),
createBoolConfig("activedefrag", NULL, DEBUG_CONFIG | MODIFIABLE_CONFIG, server.active_defrag_enabled, 0, isValidActiveDefrag, NULL),
createBoolConfig("syslog-enabled", NULL, IMMUTABLE_CONFIG, server.syslog_enabled, 0, NULL, NULL),
Expand Down
8 changes: 8 additions & 0 deletions src/networking.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ client *createClient(connection *conn) {
c->bulklen = -1;
c->sentlen = 0;
c->flags = 0;
c->capa = 0;
c->slot = -1;
c->ctime = c->lastinteraction = server.unixtime;
c->duration = 0;
Expand Down Expand Up @@ -3583,6 +3584,13 @@ NULL
} else {
addReplyErrorObject(c,shared.syntaxerr);
}
} else if (!strcasecmp(c->argv[1]->ptr,"capa") && c->argc == 3) {
if (!strcasecmp(c->argv[2]->ptr,"redirect")) {
c->capa |= CLIENT_CAPA_REDIRECT;
addReply(c,shared.ok);
} else {
addReplyErrorObject(c,shared.syntaxerr);
}
} else {
addReplySubcommandSyntaxError(c);
}
Expand Down
2 changes: 1 addition & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -4040,7 +4040,7 @@ int processCommand(client *c) {
}

if (!server.cluster_enabled &&
server.repl_replica_enable_redirect &&
c->capa & CLIENT_CAPA_REDIRECT &&
server.masterhost &&
!mustObeyClient(c) &&
(is_write_command ||
Expand Down
5 changes: 4 additions & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,9 @@ extern int configOOMScoreAdjValuesDefaults[CONFIG_OOM_COUNT];
#define CLIENT_REPROCESSING_COMMAND (1ULL<<50) /* The client is re-processing the command. */
#define CLIENT_PREREPL_DONE (1ULL<<51) /* Indicate that pre-replication has been done on the client */

/* Client capabilities */
#define CLIENT_CAPA_REDIRECT (1<<0) /* Indicate that the client can handle redirection */

/* Client block type (btype field in client structure)
* if CLIENT_BLOCKED flag is set. */
typedef enum blocking_type {
Expand Down Expand Up @@ -1164,6 +1167,7 @@ typedef struct {
typedef struct client {
uint64_t id; /* Client incremental unique ID. */
uint64_t flags; /* Client flags: CLIENT_* macros. */
uint64_t capa; /* Client capabilities: CLIENT_CAPA* macros. */
connection *conn;
int resp; /* RESP protocol version. Can be 2 or 3. */
serverDb *db; /* Pointer to currently SELECTed DB. */
Expand Down Expand Up @@ -1917,7 +1921,6 @@ struct valkeyServer {
int repl_serve_stale_data; /* Serve stale data when link is down? */
int repl_slave_ro; /* Slave is read only? */
int repl_slave_ignore_maxmemory; /* If true slaves do not evict. */
int repl_replica_enable_redirect; /* If true replica would redirect read&write commands to master. */
time_t repl_down_since; /* Unix time at which link with master went down */
int repl_disable_tcp_nodelay; /* Disable TCP_NODELAY after SYNC? */
int slave_priority; /* Reported in INFO and used by Sentinel. */
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/replica-redirect.tcl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ start_server {tags {needs:repl external:skip}} {
assert_error {READONLY*} {r set foo bar}
}

test {replica redirect read and write command when enable replica-enable-redirect} {
r config set replica-enable-redirect yes
test {replica redirect read and write command after CLIENT CAPA REDIRECT} {
r client capa redirect
assert_error "REDIRECT $master_host:$master_port" {r set foo bar}
assert_error "REDIRECT $master_host:$master_port" {r get foo}
}
Expand Down
12 changes: 0 additions & 12 deletions valkey.conf
Original file line number Diff line number Diff line change
Expand Up @@ -826,18 +826,6 @@ replica-priority 100
# replica-announce-ip 5.5.5.5
# replica-announce-port 1234

# You can configure a replica instance to redirect data access commands
# to its master or not, excluding commands such as:
# INFO, AUTH, ROLE, SUBSCRIBE, UNSUBSCRIBE, PUBLISH.
#
# When enabled, a replica instance will reply "-MOVED -1 master-ip:port"
# for data access commands. Normally these are write or read commands, if
# you want to run read commands while replica-enable-redirect is enabled,
# use the READONLY command first.
#
# This config only takes effect in standalone mode.
replica-enable-redirect no

############################### KEYS TRACKING #################################

# The client side caching of values is assisted via server-side support.
Expand Down

0 comments on commit 0c67122

Please sign in to comment.