mapleFU commented on code in PR #1219:
URL: 
https://github.com/apache/incubator-kvrocks/pull/1219#discussion_r1064588690


##########
src/cluster/cluster.cc:
##########
@@ -487,8 +490,46 @@ Status Cluster::GetClusterNodes(std::string *nodes_str) {
 }
 
 std::string Cluster::GenNodesDescription() {
-  // Generate slots info firstly
+  UpdateSlotsInfo();
+
+  std::string nodes_desc;
+  for (const auto &item : nodes_) {
+    const std::shared_ptr<ClusterNode> n = item.second;
+
+    std::string node_str;
+    // ID, host, port
+    node_str.append(n->id_ + " ");
+    node_str.append(fmt::format("{}:{}@{} ", n->host_, n->port_, n->port_ + 
kClusterPortIncr));
+
+    // Flags
+    if (n->id_ == myid_) node_str.append("myself,");
+    if (n->role_ == kClusterMaster) {
+      node_str.append("master - ");
+    } else {
+      node_str.append("slave " + n->master_id_ + " ");
+    }
+
+    // Ping sent, pong received, config epoch, link status

Review Comment:
   Why do we need to `GetTimeStampMS` every time? Is there some heavy api 
spending a lot of time?



##########
src/cluster/cluster.cc:
##########
@@ -487,8 +490,46 @@ Status Cluster::GetClusterNodes(std::string *nodes_str) {
 }
 
 std::string Cluster::GenNodesDescription() {
-  // Generate slots info firstly
+  UpdateSlotsInfo();
+
+  std::string nodes_desc;
+  for (const auto &item : nodes_) {
+    const std::shared_ptr<ClusterNode> n = item.second;

Review Comment:
   why not using `const std::shared_ptr<ClusterNode>& n = item.second;`



##########
src/server/server.cc:
##########
@@ -137,12 +137,17 @@ Status Server::Start() {
   }
 
   if (config_->cluster_enabled) {
+    auto s = cluster_->LoadClusterNodes(config_->NodesFilePath());

Review Comment:
   Seems ok. It was my fault, `ofstream::close` will flush the buffer and close 
the fd. So it's safe to rename it.
   
   What will happen if a crashed server thinks itself is a master node? Will it 
harm the cluster?



##########
src/commands/redis_cmd.cc:
##########
@@ -5304,6 +5308,9 @@ class CommandClusterX : public Commander {
     } else {
       *output = Redis::Error("Invalid cluster command options");
     }
+    if (needPersistNodesInfo) {

Review Comment:
   Seems that command will failed if `DumpClusterNodes` failed. However, the 
cluster info is already updated.



##########
src/cluster/cluster.cc:
##########
@@ -487,8 +490,46 @@ Status Cluster::GetClusterNodes(std::string *nodes_str) {
 }
 
 std::string Cluster::GenNodesDescription() {
-  // Generate slots info firstly
+  UpdateSlotsInfo();
+
+  std::string nodes_desc;
+  for (const auto &item : nodes_) {
+    const std::shared_ptr<ClusterNode> n = item.second;
+
+    std::string node_str;
+    // ID, host, port
+    node_str.append(n->id_ + " ");
+    node_str.append(fmt::format("{}:{}@{} ", n->host_, n->port_, n->port_ + 
kClusterPortIncr));
+
+    // Flags
+    if (n->id_ == myid_) node_str.append("myself,");
+    if (n->role_ == kClusterMaster) {
+      node_str.append("master - ");
+    } else {
+      node_str.append("slave " + n->master_id_ + " ");
+    }
+
+    // Ping sent, pong received, config epoch, link status
+    auto now = Util::GetTimeStampMS();
+    node_str.append(fmt::format("{} {} {} connected", now - 1, now, version_));
+
+    if (n->role_ == kClusterMaster && n->slots_info_.size() > 0) {
+      node_str.append(" " + n->slots_info_);
+    }
+
+    nodes_desc.append(node_str + "\n");
+  }
+  return nodes_desc;
+}
+
+void Cluster::UpdateSlotsInfo() {
   int start = -1;
+  // reset the previous slots info
+  for (const auto &item : nodes_) {
+    const std::shared_ptr<ClusterNode> n = item.second;

Review Comment:
   why not using `const std::shared_ptr<ClusterNode>& n = item.second;`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to