2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* ChangeConfig.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-02-18 07:41:16 +08:00
|
|
|
#include "fdbclient/NativeAPI.actor.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#include "fdbclient/ClusterInterface.h"
|
2019-02-18 11:25:16 +08:00
|
|
|
#include "fdbserver/TesterInterface.actor.h"
|
2019-02-18 09:38:13 +08:00
|
|
|
#include "fdbclient/ManagementAPI.actor.h"
|
2019-02-18 11:18:30 +08:00
|
|
|
#include "fdbserver/workloads/workloads.actor.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
#include "fdbrpc/simulator.h"
|
2021-02-17 05:01:37 +08:00
|
|
|
#include "fdbclient/Schemas.h"
|
2021-03-11 02:06:03 +08:00
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
struct ChangeConfigWorkload : TestWorkload {
|
|
|
|
double minDelayBeforeChange, maxDelayBeforeChange;
|
|
|
|
std::string configMode; //<\"single\"|\"double\"|\"triple\">
|
2021-03-11 02:06:03 +08:00
|
|
|
std::string networkAddresses; // comma separated list e.g. "127.0.0.1:4000,127.0.0.1:4001"
|
|
|
|
|
|
|
|
ChangeConfigWorkload(WorkloadContext const& wcx) : TestWorkload(wcx) {
|
|
|
|
minDelayBeforeChange = getOption(options, LiteralStringRef("minDelayBeforeChange"), 0);
|
|
|
|
maxDelayBeforeChange = getOption(options, LiteralStringRef("maxDelayBeforeChange"), 0);
|
|
|
|
ASSERT(maxDelayBeforeChange >= minDelayBeforeChange);
|
|
|
|
configMode = getOption(options, LiteralStringRef("configMode"), StringRef()).toString();
|
|
|
|
networkAddresses = getOption(options, LiteralStringRef("coordinators"), StringRef()).toString();
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
std::string description() const override { return "ChangeConfig"; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
Future<Void> start(Database const& cx) override {
|
2021-03-11 02:06:03 +08:00
|
|
|
if (this->clientId != 0)
|
|
|
|
return Void();
|
|
|
|
return ChangeConfigClient(cx->clone(), this);
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
Future<bool> check(Database const& cx) override { return true; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-09-17 08:42:34 +08:00
|
|
|
void getMetrics(std::vector<PerfMetric>& m) override {}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-31 10:48:54 +08:00
|
|
|
// When simulated two clusters for DR tests, this actor sets the starting configuration
|
|
|
|
// for the extra cluster.
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> extraDatabaseConfigure(ChangeConfigWorkload* self) {
|
2017-05-26 04:48:44 +08:00
|
|
|
if (g_network->isSimulated() && g_simulator.extraDB) {
|
2020-11-07 15:50:55 +08:00
|
|
|
auto extraFile = makeReference<ClusterConnectionFile>(*g_simulator.extraDB);
|
2018-09-22 06:58:14 +08:00
|
|
|
state Database extraDB = Database::createDatabase(extraFile, -1);
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
wait(delay(5 * deterministicRandom()->random01()));
|
2018-04-09 12:24:05 +08:00
|
|
|
if (self->configMode.size()) {
|
2021-03-31 12:11:26 +08:00
|
|
|
if (g_simulator.startingDisabledConfiguration != "") {
|
2021-03-31 10:48:54 +08:00
|
|
|
// It is not safe to allow automatic failover to a region which is not fully replicated,
|
|
|
|
// so wait for both regions to be fully replicated before enabling failover
|
2021-09-21 05:41:20 +08:00
|
|
|
wait(success(ManagementAPI::changeConfig(
|
|
|
|
extraDB.getReference(), g_simulator.startingDisabledConfiguration, true)));
|
2021-07-27 10:55:10 +08:00
|
|
|
TraceEvent("WaitForReplicasExtra").log();
|
2021-03-31 10:48:54 +08:00
|
|
|
wait(waitForFullReplication(extraDB));
|
2021-07-27 10:55:10 +08:00
|
|
|
TraceEvent("WaitForReplicasExtraEnd").log();
|
2021-03-31 10:48:54 +08:00
|
|
|
}
|
2021-09-21 05:41:20 +08:00
|
|
|
wait(success(ManagementAPI::changeConfig(extraDB.getReference(), self->configMode, true)));
|
2021-03-11 02:06:03 +08:00
|
|
|
}
|
|
|
|
if (self->networkAddresses.size()) {
|
2017-05-26 04:48:44 +08:00
|
|
|
if (self->networkAddresses == "auto")
|
2021-02-17 05:01:37 +08:00
|
|
|
wait(CoordinatorsChangeActor(extraDB, self, true));
|
2017-05-26 04:48:44 +08:00
|
|
|
else
|
2021-02-17 05:01:37 +08:00
|
|
|
wait(CoordinatorsChangeActor(extraDB, self));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
wait(delay(5 * deterministicRandom()->random01()));
|
2017-06-01 07:23:37 +08:00
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2021-03-31 10:48:54 +08:00
|
|
|
// Either changes the database configuration, or changes the coordinators based on the parameters
|
|
|
|
// of the workload.
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR Future<Void> ChangeConfigClient(Database cx, ChangeConfigWorkload* self) {
|
|
|
|
wait(delay(self->minDelayBeforeChange +
|
|
|
|
deterministicRandom()->random01() * (self->maxDelayBeforeChange - self->minDelayBeforeChange)));
|
2017-06-01 07:23:37 +08:00
|
|
|
|
2019-05-11 05:01:52 +08:00
|
|
|
state bool extraConfigureBefore = deterministicRandom()->random01() < 0.5;
|
2017-06-01 07:23:37 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (extraConfigureBefore) {
|
|
|
|
wait(self->extraDatabaseConfigure(self));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (self->configMode.size()) {
|
2021-03-31 12:11:26 +08:00
|
|
|
if (g_network->isSimulated() && g_simulator.startingDisabledConfiguration != "") {
|
|
|
|
// It is not safe to allow automatic failover to a region which is not fully replicated,
|
|
|
|
// so wait for both regions to be fully replicated before enabling failover
|
2021-09-21 05:41:20 +08:00
|
|
|
wait(success(ManagementAPI::changeConfig(cx.getReference(), g_simulator.startingDisabledConfiguration, true)));
|
2021-07-27 10:55:10 +08:00
|
|
|
TraceEvent("WaitForReplicas").log();
|
2021-03-31 10:48:54 +08:00
|
|
|
wait(waitForFullReplication(cx));
|
2021-07-27 10:55:10 +08:00
|
|
|
TraceEvent("WaitForReplicasEnd").log();
|
2021-03-31 10:48:54 +08:00
|
|
|
}
|
2021-09-21 05:41:20 +08:00
|
|
|
wait(success(ManagementAPI::changeConfig(cx.getReference(), self->configMode, true)));
|
2018-04-09 12:24:05 +08:00
|
|
|
}
|
2021-03-11 02:06:03 +08:00
|
|
|
if (self->networkAddresses.size()) {
|
2017-05-26 04:48:44 +08:00
|
|
|
if (self->networkAddresses == "auto")
|
2021-02-17 05:01:37 +08:00
|
|
|
wait(CoordinatorsChangeActor(cx, self, true));
|
2017-05-26 04:48:44 +08:00
|
|
|
else
|
2021-02-17 05:01:37 +08:00
|
|
|
wait(CoordinatorsChangeActor(cx, self));
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
2017-06-01 07:23:37 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
if (!extraConfigureBefore) {
|
|
|
|
wait(self->extraDatabaseConfigure(self));
|
2017-06-01 07:23:37 +08:00
|
|
|
}
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
return Void();
|
|
|
|
}
|
2021-02-17 05:01:37 +08:00
|
|
|
|
2021-03-11 02:06:03 +08:00
|
|
|
ACTOR static Future<Void> CoordinatorsChangeActor(Database cx,
|
|
|
|
ChangeConfigWorkload* self,
|
2021-02-17 05:01:37 +08:00
|
|
|
bool autoChange = false) {
|
|
|
|
state ReadYourWritesTransaction tr(cx);
|
|
|
|
state int notEnoughMachineResults = 0; // Retry for the second time if we first get this result
|
|
|
|
state std::string desiredCoordinatorsKey; // comma separated
|
2021-02-17 15:55:58 +08:00
|
|
|
if (autoChange) { // if auto, we first get the desired addresses by read \xff\xff/management/auto_coordinators
|
2021-02-17 05:01:37 +08:00
|
|
|
loop {
|
|
|
|
try {
|
2021-02-17 15:55:58 +08:00
|
|
|
Optional<Value> newCoordinatorsKey = wait(tr.get(
|
|
|
|
LiteralStringRef("auto_coordinators")
|
|
|
|
.withPrefix(SpecialKeySpace::getModuleRange(SpecialKeySpace::MODULE::MANAGEMENT).begin)));
|
|
|
|
ASSERT(newCoordinatorsKey.present());
|
|
|
|
desiredCoordinatorsKey = newCoordinatorsKey.get().toString();
|
2021-02-17 05:01:37 +08:00
|
|
|
tr.reset();
|
|
|
|
break;
|
|
|
|
} catch (Error& e) {
|
2021-02-17 15:55:58 +08:00
|
|
|
if (e.code() == error_code_special_keys_api_failure) {
|
|
|
|
Optional<Value> errorMsg =
|
|
|
|
wait(tr.get(SpecialKeySpace::getModuleRange(SpecialKeySpace::MODULE::ERRORMSG).begin));
|
|
|
|
ASSERT(errorMsg.present());
|
|
|
|
std::string errorStr;
|
|
|
|
auto valueObj = readJSONStrictly(errorMsg.get().toString()).get_obj();
|
|
|
|
auto schema = readJSONStrictly(JSONSchemas::managementApiErrorSchema.toString()).get_obj();
|
|
|
|
// special_key_space_management_api_error_msg schema validation
|
|
|
|
TraceEvent(SevDebug, "GetAutoCoordinatorsChange")
|
|
|
|
.detail("ErrorMessage", valueObj["message"].get_str());
|
|
|
|
ASSERT(schemaMatch(schema, valueObj, errorStr, SevError, true));
|
|
|
|
ASSERT(valueObj["command"].get_str() == "auto_coordinators");
|
|
|
|
if (valueObj["retriable"].get_bool() && notEnoughMachineResults < 1) {
|
|
|
|
notEnoughMachineResults++;
|
|
|
|
wait(delay(1.0));
|
|
|
|
tr.reset();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
wait(delay(FLOW_KNOBS->PREVENT_FAST_SPIN_DELAY));
|
2021-02-17 05:01:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
desiredCoordinatorsKey = self->networkAddresses;
|
|
|
|
}
|
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
tr.setOption(FDBTransactionOptions::SPECIAL_KEY_SPACE_ENABLE_WRITES);
|
|
|
|
tr.set(LiteralStringRef("processes")
|
|
|
|
.withPrefix(SpecialKeySpace::getManagementApiCommandPrefix("coordinators")),
|
|
|
|
Value(desiredCoordinatorsKey));
|
|
|
|
TraceEvent(SevDebug, "CoordinatorsChangeBeforeCommit")
|
|
|
|
.detail("Auto", autoChange)
|
|
|
|
.detail("NewCoordinatorsKey", describe(desiredCoordinatorsKey));
|
|
|
|
wait(tr.commit());
|
|
|
|
ASSERT(false);
|
|
|
|
} catch (Error& e) {
|
|
|
|
state Error err(e);
|
|
|
|
if (e.code() == error_code_special_keys_api_failure) {
|
|
|
|
Optional<Value> errorMsg =
|
|
|
|
wait(tr.get(SpecialKeySpace::getModuleRange(SpecialKeySpace::MODULE::ERRORMSG).begin));
|
|
|
|
ASSERT(errorMsg.present());
|
|
|
|
std::string errorStr;
|
|
|
|
auto valueObj = readJSONStrictly(errorMsg.get().toString()).get_obj();
|
|
|
|
auto schema = readJSONStrictly(JSONSchemas::managementApiErrorSchema.toString()).get_obj();
|
|
|
|
// special_key_space_management_api_error_msg schema validation
|
|
|
|
TraceEvent(SevDebug, "CoordinatorsChangeError")
|
|
|
|
.detail("Auto", autoChange)
|
|
|
|
.detail("ErrorMessage", valueObj["message"].get_str());
|
|
|
|
ASSERT(schemaMatch(schema, valueObj, errorStr, SevError, true));
|
|
|
|
ASSERT(valueObj["command"].get_str() == "coordinators");
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
wait(tr.onError(err));
|
|
|
|
}
|
|
|
|
wait(delay(FLOW_KNOBS->PREVENT_FAST_SPIN_DELAY));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
WorkloadFactory<ChangeConfigWorkload> ChangeConfigWorkloadFactory("ChangeConfig");
|