Skip to content

Commit

Permalink
fix(cluster): should stop the migration if it's changed to the slave …
Browse files Browse the repository at this point in the history
…role (#2716)

Co-authored-by: hulk <[email protected]>
  • Loading branch information
RiversJin and git-hulk authored Jan 9, 2025
1 parent 77b8b1f commit a21df19
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/cluster/cluster.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,23 +237,40 @@ 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();
if (!s.IsOK()) {
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();
Expand Down

0 comments on commit a21df19

Please sign in to comment.