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

fix(cluster): should stop the migration if it's changed to the slave role #2716

Merged
merged 4 commits into from
Jan 9, 2025
Merged
Changes from 3 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
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 (is_slave && is_cluster_enabled) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (is_slave && is_cluster_enabled) {
if (is_slave && is_cluster_enabled && srv_->slot_migrator) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The slot_migrator might be null pointer while starting the server.

// Slave -> Master
srv_->slot_migrator->SetStopMigrationFlag(false);
LOG(INFO) << "Change server role to master, stop migration task";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong log message, it should restart the migration task instead of the stop.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops... fixed

}
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 (!is_slave && is_cluster_enabled) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!is_slave && is_cluster_enabled) {
if (!is_slave && is_cluster_enabled && srv_->slot_migrator) {

// 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
Loading