From 78960ad57b8a5e6af743d789ed8fd767e37d42b8 Mon Sep 17 00:00:00 2001 From: Huang Zhw Date: Tue, 14 Jun 2022 12:32:43 +0800 Subject: Throw -TRYAGAIN instead of -ASK on migrating nodes for multi-key commands when the node only has some of the keys (#9526) * In cluster getNodeByQuery when target slot is in migrating state and the slot lack some keys but have at least one key, should return TRYAGAIN. Before this commit, when a node is in migrating state and recevies multiple keys command, if some keys don't exist, the command emits an `ASK` redirection. After this commit, if some keys exist and some keys don't exist, the command emits a TRYAGAIN error. If all keys don't exist, the command emits an `ASK` redirection. --- src/cluster.c | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) (limited to 'src/cluster.c') diff --git a/src/cluster.c b/src/cluster.c index d328ede2f..0975db53a 100644 --- a/src/cluster.c +++ b/src/cluster.c @@ -6583,7 +6583,8 @@ clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, in int multiple_keys = 0; multiState *ms, _ms; multiCmd mc; - int i, slot = 0, migrating_slot = 0, importing_slot = 0, missing_keys = 0; + int i, slot = 0, migrating_slot = 0, importing_slot = 0, missing_keys = 0, + existing_keys = 0; /* Allow any key to be set if a module disabled cluster redirections. */ if (server.cluster_module_flags & CLUSTER_MODULE_FLAG_NO_REDIRECTION) @@ -6695,10 +6696,10 @@ clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, in * node until the migration completes with CLUSTER SETSLOT * NODE . */ int flags = LOOKUP_NOTOUCH | LOOKUP_NOSTATS | LOOKUP_NONOTIFY; - if ((migrating_slot || importing_slot) && !is_pubsubshard && - lookupKeyReadWithFlags(&server.db[0], thiskey, flags) == NULL) + if ((migrating_slot || importing_slot) && !is_pubsubshard) { - missing_keys++; + if (lookupKeyReadWithFlags(&server.db[0], thiskey, flags) == NULL) missing_keys++; + else existing_keys++; } } getKeysFreeResult(&result); @@ -6742,10 +6743,16 @@ clusterNode *getNodeByQuery(client *c, struct redisCommand *cmd, robj **argv, in return myself; /* If we don't have all the keys and we are migrating the slot, send - * an ASK redirection. */ + * an ASK redirection or TRYAGAIN. */ if (migrating_slot && missing_keys) { - if (error_code) *error_code = CLUSTER_REDIR_ASK; - return server.cluster->migrating_slots_to[slot]; + /* If we have keys but we don't have all keys, we return TRYAGAIN */ + if (existing_keys) { + if (error_code) *error_code = CLUSTER_REDIR_UNSTABLE; + return NULL; + } else { + if (error_code) *error_code = CLUSTER_REDIR_ASK; + return server.cluster->migrating_slots_to[slot]; + } } /* If we are receiving the slot, and the client correctly flagged the -- cgit v1.2.1