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): Wait before all access to slot migrations #3180

Merged
merged 3 commits into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 0 additions & 1 deletion src/server/cluster/cluster_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ enum class MigrationState : uint8_t {
C_SYNC,
C_ERROR,
C_FINISHED,
C_MAX_INVALID = std::numeric_limits<uint8_t>::max()
};

SlotId KeySlot(std::string_view key);
Expand Down
2 changes: 0 additions & 2 deletions src/server/cluster/cluster_family.cc
Original file line number Diff line number Diff line change
Expand Up @@ -660,8 +660,6 @@ static string_view StateToStr(MigrationState state) {
return "ERROR"sv;
case MigrationState::C_FINISHED:
return "FINISHED"sv;
case MigrationState::C_MAX_INVALID:
break;
}
DCHECK(false) << "Unknown State value " << static_cast<underlying_type_t<MigrationState>>(state);
return "UNDEFINED_STATE"sv;
Expand Down
54 changes: 35 additions & 19 deletions src/server/cluster/outgoing_slot_migration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,35 @@ bool OutgoingMigration::ChangeState(MigrationState new_state) {
}

void OutgoingMigration::Finish(bool is_error) {
const auto new_state = is_error ? MigrationState::C_ERROR : MigrationState::C_FINISHED;
if (!ChangeState(new_state)) {
return;
bool should_cancel_flows = false;

std::lock_guard lk(state_mu_);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's better to wrap 112-129 into block to avoid locking over AwaitFiberOnAll.

switch (state_) {
case MigrationState::C_FINISHED:
return; // Already finished, nothing else to do

case MigrationState::C_NO_STATE:
case MigrationState::C_CONNECTING:
should_cancel_flows = false;
break;

case MigrationState::C_SYNC:
case MigrationState::C_ERROR:
should_cancel_flows = true;
break;
}

shard_set->pool()->AwaitFiberOnAll([this](util::ProactorBase* pb) {
if (const auto* shard = EngineShard::tlocal(); shard) {
auto& flow = slot_migrations_[shard->shard_id()];
CHECK(flow != nullptr);
flow->Cancel();
}
});
state_ = is_error ? MigrationState::C_ERROR : MigrationState::C_FINISHED;

if (should_cancel_flows) {
shard_set->pool()->AwaitFiberOnAll([this](util::ProactorBase* pb) {
if (const auto* shard = EngineShard::tlocal(); shard) {
auto& flow = slot_migrations_[shard->shard_id()];
CHECK(flow != nullptr);
flow->Cancel();
}
});
}
}

MigrationState OutgoingMigration::GetState() const {
Expand Down Expand Up @@ -174,10 +191,6 @@ void OutgoingMigration::SyncFb() {
continue;
}

if (!ChangeState(MigrationState::C_SYNC)) {
break;
}

shard_set->pool()->AwaitFiberOnAll([this](util::ProactorBase* pb) {
if (auto* shard = EngineShard::tlocal(); shard) {
server_family_->journal()->StartInThread();
Expand All @@ -186,13 +199,16 @@ void OutgoingMigration::SyncFb() {
}
});

// Start migrations in a separate hop to make sure that Finish() is called only after all
// migrations are created (see #3139)
if (!ChangeState(MigrationState::C_SYNC)) {
break;
}

shard_set->pool()->AwaitFiberOnAll([this](util::ProactorBase* pb) {
if (auto* shard = EngineShard::tlocal(); shard) {
auto& migration = *slot_migrations_[shard->shard_id()];
migration.Sync(cf_->MyID(), shard->shard_id());
if (migration.GetError()) {
auto& migration = slot_migrations_[shard->shard_id()];
CHECK(migration != nullptr);
migration->Sync(cf_->MyID(), shard->shard_id());
if (migration->GetError()) {
Finish(true);
}
}
Expand Down
Loading