Skip to content

Commit 40851f9

Browse files
committed
Simplify handling error response check
1 parent f080a52 commit 40851f9

6 files changed

+19
-22
lines changed

src/server/cluster/cluster_family.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ ClusterFamily::TakeOutOutgoingMigrations(shared_ptr<ClusterConfig> new_config,
835835
removed_slots.Merge(slots);
836836
LOG(INFO) << "Outgoing migration cancelled: slots " << slots.ToString() << " to "
837837
<< migration.GetHostIp() << ":" << migration.GetPort();
838-
migration.Finish();
838+
migration.Finish(MigrationState::C_FINISHED);
839839
res.migrations.push_back(std::move(*it));
840840
outgoing_migration_jobs_.erase(it);
841841
}

src/server/cluster/incoming_slot_migration.cc

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "base/flags.h"
1111
#include "base/logging.h"
1212
#include "cluster_utility.h"
13-
#include "server/cluster/cluster_defs.h"
1413
#include "server/error.h"
1514
#include "server/journal/executor.h"
1615
#include "server/journal/tx_executor.h"

src/server/cluster/outgoing_slot_migration.cc

+9-19
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "base/logging.h"
1313
#include "cluster_family.h"
1414
#include "cluster_utility.h"
15-
#include "server/cluster/cluster_defs.h"
1615
#include "server/db_slice.h"
1716
#include "server/engine_shard_set.h"
1817
#include "server/error.h"
@@ -37,7 +36,8 @@ class OutgoingMigration::SliceSlotMigration : private ProtocolClient {
3736
SliceSlotMigration(DbSlice* slice, ServerContext server_context, SlotSet slots,
3837
journal::Journal* journal, OutgoingMigration* om)
3938
: ProtocolClient(server_context), streamer_(slice, std::move(slots), journal, &exec_st_) {
40-
exec_st_.SwitchErrorHandler([om](auto ge) { om->Finish(std::move(ge)); });
39+
exec_st_.SwitchErrorHandler(
40+
[om](auto ge) { om->Finish(MigrationState::C_ERROR, std::move(ge)); });
4141
}
4242

4343
~SliceSlotMigration() {
@@ -139,14 +139,8 @@ void OutgoingMigration::OnAllShards(
139139
});
140140
}
141141

142-
void OutgoingMigration::Finish(GenericError error) {
143-
auto next_state = MigrationState::C_FINISHED;
142+
void OutgoingMigration::Finish(MigrationState next_state, GenericError error) {
144143
if (error) {
145-
if (error.Format() == kIncomingMigrationOOM) {
146-
next_state = MigrationState::C_FATAL;
147-
} else {
148-
next_state = MigrationState::C_ERROR;
149-
}
150144
LOG(WARNING) << "Finish outgoing migration for " << cf_->MyID() << ": "
151145
<< migration_info_.node_info.id << " with error: " << error.Format();
152146
exec_st_.ReportError(std::move(error));
@@ -230,8 +224,7 @@ void OutgoingMigration::SyncFb() {
230224
// Break outgoing migration if INIT from incoming node responded with OOM. Usually this will
231225
// happen on second iteration after first failed with OOM. Sending second INIT is required to
232226
// cleanup slots on incoming slot migration node.
233-
if (CheckRespFirstTypes({RespExpr::ERROR}) &&
234-
facade::ToSV(LastResponseArgs().front().GetBuf()) == kIncomingMigrationOOM) {
227+
if (CheckRespSimpleError(kIncomingMigrationOOM)) {
235228
ChangeState(MigrationState::C_FATAL);
236229
break;
237230
}
@@ -374,13 +367,10 @@ bool OutgoingMigration::FinalizeMigration(long attempt) {
374367
return false;
375368
}
376369

377-
if (CheckRespFirstTypes({RespExpr::ERROR})) {
378-
auto error = facade::ToSV(LastResponseArgs().front().GetBuf());
379-
// Check if returned incoming slot OOM and finish migration
380-
if (error == kIncomingMigrationOOM) {
381-
Finish(std::string(error));
382-
return false;
383-
}
370+
// Check OOM from incoming slot migration on ACK request
371+
if (CheckRespSimpleError(kIncomingMigrationOOM)) {
372+
Finish(MigrationState::C_FATAL, std::string(kIncomingMigrationOOM));
373+
return false;
384374
}
385375

386376
if (!CheckRespFirstTypes({RespExpr::INT64})) {
@@ -399,7 +389,7 @@ bool OutgoingMigration::FinalizeMigration(long attempt) {
399389
}
400390

401391
if (!exec_st_.GetError()) {
402-
Finish();
392+
Finish(MigrationState::C_FINISHED);
403393
keys_number_ = cluster::GetKeyCount(migration_info_.slot_ranges);
404394
cf_->ApplyMigrationSlotRangeToConfig(migration_info_.node_info.id, migration_info_.slot_ranges,
405395
false);

src/server/cluster/outgoing_slot_migration.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class OutgoingMigration : private ProtocolClient {
3030
// if is_error = false mark migration as FINISHED and cancel migration if it's not finished yet
3131
// can be called from any thread, but only after Start()
3232
// if is_error = true and migration is in progress it will be restarted otherwise nothing happens
33-
void Finish(GenericError error = {}) ABSL_LOCKS_EXCLUDED(state_mu_);
33+
void Finish(MigrationState next_state, GenericError error = {}) ABSL_LOCKS_EXCLUDED(state_mu_);
3434

3535
MigrationState GetState() const ABSL_LOCKS_EXCLUDED(state_mu_);
3636

src/server/protocol_client.cc

+5
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,11 @@ bool ProtocolClient::CheckRespIsSimpleReply(string_view reply) const {
339339
ToSV(resp_args_.front().GetBuf()) == reply;
340340
}
341341

342+
bool ProtocolClient::CheckRespSimpleError(string_view error) const {
343+
return resp_args_.size() == 1 && resp_args_.front().type == RespExpr::ERROR &&
344+
ToSV(resp_args_.front().GetBuf()) == error;
345+
}
346+
342347
bool ProtocolClient::CheckRespFirstTypes(initializer_list<RespExpr::Type> types) const {
343348
unsigned i = 0;
344349
for (RespExpr::Type type : types) {

src/server/protocol_client.h

+3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ class ProtocolClient {
8585
// Check if reps_args contains a simple reply.
8686
bool CheckRespIsSimpleReply(std::string_view reply) const;
8787

88+
// Check if resp_args contains a simple error
89+
bool CheckRespSimpleError(std::string_view error) const;
90+
8891
// Check resp_args contains the following types at front.
8992
bool CheckRespFirstTypes(std::initializer_list<facade::RespExpr::Type> types) const;
9093

0 commit comments

Comments
 (0)