Skip to content

Commit

Permalink
Add cluster section into the info command (#1379)
Browse files Browse the repository at this point in the history
Currently, the Redis Info cluster command only has cluster_enabled,
a value of 0 or 1 indicates whether the cluster mode is enabled.

---------

Co-authored-by: Yaroslav <[email protected]>
  • Loading branch information
enjoy-binbin and torwig authored Apr 12, 2023
1 parent db5d91d commit d1e52d3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,15 @@ void Server::GetCommandsStatsInfo(std::string *info) {
*info = string_stream.str();
}

void Server::GetClusterInfo(std::string *info) {
std::ostringstream string_stream;

string_stream << "# Cluster\r\n";
string_stream << "cluster_enabled:" << config_->cluster_enabled << "\r\n";

*info = string_stream.str();
}

// WARNING: we must not access DB(i.e. RocksDB) when server is loading since
// DB is closed and the pointer is invalid. Server may crash if we access DB during loading.
// If you add new fields which access DB into INFO command output, make sure
Expand Down Expand Up @@ -1110,6 +1119,13 @@ void Server::GetInfo(const std::string &ns, const std::string &section, std::str
string_stream << commands_stats_info;
}

if (all || section == "cluster") {
std::string cluster_info;
GetClusterInfo(&cluster_info);
if (section_cnt++) string_stream << "\r\n";
string_stream << cluster_info;
}

// In keyspace section, we access DB, so we can't do that when loading
if (!is_loading_ && (all || section == "keyspace")) {
KeyNumStats stats;
Expand Down
1 change: 1 addition & 0 deletions src/server/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class Server {
void GetReplicationInfo(std::string *info);
void GetRoleInfo(std::string *info);
void GetCommandsStatsInfo(std::string *info);
void GetClusterInfo(std::string *info);
void GetInfo(const std::string &ns, const std::string &section, std::string *info);
std::string GetRocksDBStatsJson() const;
ReplState GetReplicationState();
Expand Down
13 changes: 13 additions & 0 deletions tests/gocase/unit/info/info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import (
)

func TestInfo(t *testing.T) {
srv0 := util.StartServer(t, map[string]string{"cluster-enabled": "yes"})
defer func() { srv0.Close() }()
rdb0 := srv0.NewClient()
defer func() { require.NoError(t, rdb0.Close()) }()

srv := util.StartServer(t, map[string]string{})
defer srv.Close()

Expand Down Expand Up @@ -88,4 +93,12 @@ func TestInfo(t *testing.T) {
require.GreaterOrEqual(t, lastBgsaveTimeSec, 0)
require.Less(t, lastBgsaveTimeSec, 3)
})

t.Run("get cluster information by INFO - cluster not enabled", func(t *testing.T) {
require.Equal(t, "0", util.FindInfoEntry(rdb, "cluster_enabled", "cluster"))
})

t.Run("get cluster information by INFO - cluster enabled", func(t *testing.T) {
require.Equal(t, "1", util.FindInfoEntry(rdb0, "cluster_enabled", "cluster"))
})
}

0 comments on commit d1e52d3

Please sign in to comment.