Skip to content

Commit 36b1e66

Browse files
committed
fix(redis): do not cache ASK redirection in slot table
1 parent dbca72f commit 36b1e66

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

src/brpc/redis_cluster.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,11 @@ bool RedisClusterChannel::ExecuteSingleCommand(const std::vector<std::string>& a
431431
next_endpoint = redirect.endpoint;
432432
GetOrCreateChannel(next_endpoint);
433433
}
434-
if (redirect.slot >= 0 && static_cast<size_t>(redirect.slot) < _slot_to_endpoint.size() &&
434+
// ASK is a temporary redirection during slot migration and should not
435+
// overwrite the stable slot map. Only persist MOVED target.
436+
if (!redirect.asking &&
437+
redirect.slot >= 0 &&
438+
static_cast<size_t>(redirect.slot) < _slot_to_endpoint.size() &&
435439
!redirect.endpoint.empty()) {
436440
BAIDU_SCOPED_LOCK(_mutex);
437441
_slot_to_endpoint[redirect.slot] = redirect.endpoint;

test/brpc_redis_cluster_unittest.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,32 @@ TEST_F(RedisClusterChannelTest, ask_redirection) {
581581
ASSERT_EQ("ask-value", resp.reply(0).data());
582582
}
583583

584+
TEST_F(RedisClusterChannelTest, ask_redirection_does_not_override_slot_cache) {
585+
brpc::RedisClusterChannel channel;
586+
InitChannel(&channel);
587+
588+
_meta->enable_ask = true;
589+
_meta->ask_from = 0;
590+
_meta->ask_to = 1;
591+
_meta->ask_key = FindKeyForNode(0);
592+
{
593+
BAIDU_SCOPED_LOCK(_node[1].mutex);
594+
_node[1].kv[_meta->ask_key] = "ask-value";
595+
}
596+
597+
for (int i = 0; i < 5; ++i) {
598+
brpc::RedisRequest req;
599+
brpc::RedisResponse resp;
600+
brpc::Controller cntl;
601+
ASSERT_TRUE(req.AddCommand("get %s", _meta->ask_key.c_str()));
602+
channel.CallMethod(NULL, &cntl, &req, &resp, NULL);
603+
ASSERT_FALSE(cntl.Failed()) << cntl.ErrorText();
604+
ASSERT_EQ(1, resp.reply_size());
605+
ASSERT_TRUE(resp.reply(0).is_string());
606+
ASSERT_EQ("ask-value", resp.reply(0).data());
607+
}
608+
}
609+
584610
TEST_F(RedisClusterChannelTest, cluster_nodes_fallback) {
585611
_meta->fail_slots = true;
586612
brpc::RedisClusterChannel channel;

0 commit comments

Comments
 (0)