From c4956d6f52b4d4ea91e19b9efb8d8f0b4788f614 Mon Sep 17 00:00:00 2001 From: hulk Date: Sun, 24 Mar 2024 10:15:19 +0800 Subject: [PATCH] Allow to retrieve the cluster node id via the CLUSTERX command (#2194) --- src/commands/cmd_cluster.cc | 6 ++++-- tests/gocase/integration/cluster/cluster_test.go | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/commands/cmd_cluster.cc b/src/commands/cmd_cluster.cc index 99a79f14d5b..bd8de277f12 100644 --- a/src/commands/cmd_cluster.cc +++ b/src/commands/cmd_cluster.cc @@ -122,7 +122,7 @@ class CommandClusterX : public Commander { Status Parse(const std::vector &args) override { subcommand_ = util::ToLower(args[1]); - if (args.size() == 2 && (subcommand_ == "version")) return Status::OK(); + if (args.size() == 2 && (subcommand_ == "version" || subcommand_ == "myid")) return Status::OK(); if (subcommand_ == "setnodeid" && args_.size() == 3 && args_[2].size() == kClusterNodeIdLen) return Status::OK(); @@ -208,7 +208,7 @@ class CommandClusterX : public Commander { return Status::OK(); } - return {Status::RedisParseErr, "CLUSTERX command, CLUSTERX VERSION|SETNODEID|SETNODES|SETSLOT|MIGRATE"}; + return {Status::RedisParseErr, "CLUSTERX command, CLUSTERX VERSION|MYID|SETNODEID|SETNODES|SETSLOT|MIGRATE"}; } Status Execute(Server *srv, Connection *conn, std::string *output) override { @@ -248,6 +248,8 @@ class CommandClusterX : public Commander { } else if (subcommand_ == "version") { int64_t v = srv->cluster->GetVersion(); *output = redis::BulkString(std::to_string(v)); + } else if (subcommand_ == "myid") { + *output = redis::BulkString(srv->cluster->GetMyId()); } else if (subcommand_ == "migrate") { if (sync_migrate_) { sync_migrate_ctx_ = std::make_unique(srv, conn, sync_migrate_timeout_); diff --git a/tests/gocase/integration/cluster/cluster_test.go b/tests/gocase/integration/cluster/cluster_test.go index 695eb33cd24..97c8ed880f3 100644 --- a/tests/gocase/integration/cluster/cluster_test.go +++ b/tests/gocase/integration/cluster/cluster_test.go @@ -305,6 +305,15 @@ func TestClusterMultiple(t *testing.T) { require.NoError(t, rdb[i].Do(ctx, "clusterx", "setnodes", clusterNodes, "1").Err()) } + t.Run("check if the node id is correct", func(t *testing.T) { + // only node1, node2 and node3 was the member of the cluster + for i := 1; i < 4; i++ { + myid, err := rdb[i].Do(ctx, "clusterx", "myid").Text() + require.NoError(t, err) + require.Equal(t, nodeID[i], myid) + } + }) + t.Run("cluster info command", func(t *testing.T) { r := rdb[1].ClusterInfo(ctx).Val() require.Contains(t, r, "cluster_state:ok")