Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cluster section into the info command #1379

Merged
merged 4 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"))
})
enjoy-binbin marked this conversation as resolved.
Show resolved Hide resolved

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