This is an automated email from the ASF dual-hosted git repository.

twice pushed a commit to branch unstable
in repository https://gitbox.apache.org/repos/asf/kvrocks.git


The following commit(s) were added to refs/heads/unstable by this push:
     new a21df196 fix(cluster): should stop the migration if it's changed to 
the slave role (#2716)
a21df196 is described below

commit a21df1968fd36340e8649c3e3495abb07db8228a
Author: Rivers <[email protected]>
AuthorDate: Thu Jan 9 10:18:18 2025 +0800

    fix(cluster): should stop the migration if it's changed to the slave role 
(#2716)
    
    Co-authored-by: hulk <[email protected]>
---
 src/cluster/cluster.cc | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/cluster/cluster.cc b/src/cluster/cluster.cc
index b04fb589..822a4073 100644
--- a/src/cluster/cluster.cc
+++ b/src/cluster/cluster.cc
@@ -237,6 +237,9 @@ Status Cluster::SetMasterSlaveRepl() {
     return Status::OK();
   }
 
+  bool is_slave = srv_->IsSlave();
+  bool is_cluster_enabled = srv_->GetConfig()->cluster_enabled;
+
   if (myself_->role == kClusterMaster) {
     // Master mode
     auto s = srv_->RemoveMaster();
@@ -244,16 +247,30 @@ Status Cluster::SetMasterSlaveRepl() {
       return s.Prefixed("failed to remove master");
     }
     LOG(INFO) << "MASTER MODE enabled by cluster topology setting";
-  } else if (nodes_.find(myself_->master_id) != nodes_.end()) {
+    if (srv_->slot_migrator && is_cluster_enabled && is_slave) {
+      // Slave -> Master
+      srv_->slot_migrator->SetStopMigrationFlag(false);
+      LOG(INFO) << "Change server role to master, restart migration task";
+    }
+    return Status::OK();
+  }
+
+  auto it = nodes_.find(myself_->master_id);
+  if (it != nodes_.end()) {
     // Replica mode and master node is existing
-    std::shared_ptr<ClusterNode> master = nodes_[myself_->master_id];
+    std::shared_ptr<ClusterNode> master = it->second;
     auto s = srv_->AddMaster(master->host, master->port, false);
     if (!s.IsOK()) {
       LOG(WARNING) << "SLAVE OF " << master->host << ":" << master->port
                    << " wasn't enabled by cluster topology setting, encounter 
error: " << s.Msg();
       return s.Prefixed("failed to add master");
     }
-    LOG(INFO) << "SLAVE OF " << master->host << ":" << master->port << " 
enabled by cluster topology setting";
+    if (srv_->slot_migrator && is_cluster_enabled && !is_slave) {
+      // Master -> Slave
+      srv_->slot_migrator->SetStopMigrationFlag(true);
+      LOG(INFO) << "Change server role to slave, stop migration task";
+    }
+    LOG(INFO) << fmt::format("SLAVE OF {}:{} enabled by cluster topology 
setting", master->host, master->port);
   }
 
   return Status::OK();

Reply via email to