foundationdb/fdbserver/workloads/RandomMoveKeys.actor.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

249 lines
9.3 KiB
C++
Raw Normal View History

2017-05-26 04:48:44 +08:00
/*
* RandomMoveKeys.actor.cpp
*
* This source file is part of the FoundationDB open source project
*
2022-03-22 04:36:23 +08:00
* Copyright 2013-2022 Apple Inc. and the FoundationDB project authors
*
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
*
2017-05-26 04:48:44 +08:00
* http://www.apache.org/licenses/LICENSE-2.0
*
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.
*/
#include "fdbclient/FDBOptions.g.h"
2017-05-26 04:48:44 +08:00
#include "fdbrpc/simulator.h"
#include "fdbclient/StorageServerInterface.h"
#include "fdbclient/ManagementAPI.actor.h"
#include "fdbserver/MoveKeys.actor.h"
#include "fdbclient/NativeAPI.actor.h"
#include "fdbserver/workloads/workloads.actor.h"
2017-05-26 04:48:44 +08:00
#include "fdbserver/ServerDBInfo.h"
#include "fdbserver/QuietDatabase.h"
#include "flow/DeterministicRandom.h"
#include "flow/actorcompiler.h" // This must be the last #include.
2017-05-26 04:48:44 +08:00
2022-09-13 07:20:57 +08:00
struct MoveKeysWorkload : FailureInjectionWorkload {
static constexpr auto NAME = "RandomMoveKeys";
2017-05-26 04:48:44 +08:00
bool enabled;
2022-09-13 07:20:57 +08:00
double testDuration = 10.0, meanDelay = 0.05;
double maxKeyspace = 0.1;
2017-05-26 04:48:44 +08:00
DatabaseConfiguration configuration;
2022-09-13 07:20:57 +08:00
MoveKeysWorkload(WorkloadContext const& wcx, NoOptions) : FailureInjectionWorkload(wcx) {
2017-05-26 04:48:44 +08:00
enabled = !clientId && g_network->isSimulated(); // only do this on the "first" client
2022-09-13 07:20:57 +08:00
}
MoveKeysWorkload(WorkloadContext const& wcx) : FailureInjectionWorkload(wcx) {
enabled = !clientId && g_network->isSimulated(); // only do this on the "first" client
meanDelay = getOption(options, "meanDelay"_sr, meanDelay);
testDuration = getOption(options, "testDuration"_sr, testDuration);
maxKeyspace = getOption(options, "maxKeyspace"_sr, maxKeyspace);
2017-05-26 04:48:44 +08:00
}
2020-10-05 13:29:07 +08:00
Future<Void> setup(Database const& cx) override { return Void(); }
Future<Void> start(Database const& cx) override { return _start(cx, this); }
2017-05-26 04:48:44 +08:00
bool shouldInject(DeterministicRandom& random,
const WorkloadRequest& work,
const unsigned alreadyAdded) const override {
return alreadyAdded < 1 && work.useDatabase && 0.1 / (1 + alreadyAdded) > random.random01();
}
2017-05-26 04:48:44 +08:00
ACTOR Future<Void> _start(Database cx, MoveKeysWorkload* self) {
if (self->enabled) {
// Get the database configuration so as to use proper team size
state Transaction tr(cx);
loop {
tr.setOption(FDBTransactionOptions::READ_SYSTEM_KEYS);
tr.setOption(FDBTransactionOptions::READ_LOCK_AWARE);
2017-05-26 04:48:44 +08:00
try {
2021-05-04 04:14:16 +08:00
RangeResult res = wait(tr.getRange(configKeys, 1000));
2017-05-26 04:48:44 +08:00
ASSERT(res.size() < 1000);
for (int i = 0; i < res.size(); i++)
self->configuration.set(res[i].key, res[i].value);
break;
} catch (Error& e) {
wait(tr.onError(e));
2017-05-26 04:48:44 +08:00
}
}
state int oldMode = wait(setDDMode(cx, 0));
TraceEvent("RMKStartModeSetting").log();
wait(timeout(
reportErrors(self->worker(cx, self), "MoveKeysWorkloadWorkerError"), self->testDuration, Void()));
2017-05-26 04:48:44 +08:00
// Always set the DD mode back, even if we die with an error
TraceEvent("RMKDoneMoving").log();
wait(success(setDDMode(cx, oldMode)));
TraceEvent("RMKDoneModeSetting").log();
2017-05-26 04:48:44 +08:00
}
return Void();
}
2020-10-05 13:29:07 +08:00
double getCheckTimeout() const override { return testDuration / 2 + 1; }
Future<bool> check(Database const& cx) override {
return tag(delay(testDuration / 2), true);
} // Give the database time to recover from our damage
void getMetrics(std::vector<PerfMetric>& m) override {}
2017-05-26 04:48:44 +08:00
KeyRange getRandomKeys() const {
double len = deterministicRandom()->random01() * this->maxKeyspace;
double pos = deterministicRandom()->random01() * (1.0 - len);
2017-05-26 04:48:44 +08:00
return KeyRangeRef(doubleToTestKey(pos), doubleToTestKey(pos + len));
}
std::vector<StorageServerInterface> getRandomTeam(std::vector<StorageServerInterface> storageServers,
int teamSize) {
2017-05-26 04:48:44 +08:00
if (storageServers.size() < teamSize) {
TraceEvent(SevWarnAlways, "LessThanThreeStorageServers").log();
2017-05-26 04:48:44 +08:00
throw operation_failed();
}
deterministicRandom()->randomShuffle(storageServers);
2017-05-26 04:48:44 +08:00
std::set<StorageServerInterface> t;
std::set<Optional<Standalone<StringRef>>> machines;
while (t.size() < teamSize && storageServers.size()) {
auto s = storageServers.back();
storageServers.pop_back();
if (!machines.count(s.locality.zoneId())) {
machines.insert(s.locality.zoneId());
t.insert(s);
}
}
if (t.size() < teamSize) {
TraceEvent(SevWarnAlways, "LessThanThreeUniqueMachines").log();
2017-05-26 04:48:44 +08:00
throw operation_failed();
}
return std::vector<StorageServerInterface>(t.begin(), t.end());
2017-05-26 04:48:44 +08:00
}
ACTOR Future<Void> doMoveKeys(Database cx,
MoveKeysWorkload* self,
KeyRange keys,
std::vector<StorageServerInterface> destinationTeam,
MoveKeysLock lock) {
2017-05-26 04:48:44 +08:00
state TraceInterval relocateShardInterval("RelocateShard");
state FlowLock fl1(1);
state FlowLock fl2(1);
std::string desc;
for (int s = 0; s < destinationTeam.size(); s++)
desc +=
format("%s (%llx),", destinationTeam[s].address().toString().c_str(), destinationTeam[s].id().first());
std::vector<UID> destinationTeamIDs;
destinationTeamIDs.reserve(destinationTeam.size());
for (int s = 0; s < destinationTeam.size(); s++)
destinationTeamIDs.push_back(destinationTeam[s].id());
2017-05-26 04:48:44 +08:00
TraceEvent(relocateShardInterval.begin())
.detail("KeyBegin", printable(keys.begin))
.detail("KeyEnd", printable(keys.end))
.detail("Priority", 0)
.detail("Source", "RandomMoveKeys")
.detail("DestinationTeam", desc);
2017-05-26 04:48:44 +08:00
try {
state Promise<Void> signal;
2020-09-28 06:26:50 +08:00
state DDEnabledState ddEnabledState;
wait(moveKeys(cx,
MoveKeysParams{ deterministicRandom()->randomUniqueID(),
keys,
destinationTeamIDs,
destinationTeamIDs,
lock,
signal,
&fl1,
&fl2,
false,
relocateShardInterval.pairID,
&ddEnabledState,
CancelConflictingDataMoves::True }));
2017-05-26 04:48:44 +08:00
TraceEvent(relocateShardInterval.end()).detail("Result", "Success");
return Void();
} catch (Error& e) {
TraceEvent(relocateShardInterval.end(), self->dbInfo->get().master.id()).errorUnsuppressed(e);
2017-05-26 04:48:44 +08:00
throw;
}
}
static void eliminateDuplicates(std::vector<StorageServerInterface>& servers) {
2017-05-26 04:48:44 +08:00
// The real data distribution algorithm doesn't want to deal with multiple servers
// with the same address having keys. So if there are two servers with the same address,
// don't use either one (so we don't have to find out which of them, if any, already has keys).
2021-03-06 03:28:15 +08:00
// Also get rid of tss since we don't want to move a shard to a tss.
2017-05-26 04:48:44 +08:00
std::map<NetworkAddress, int> count;
for (int s = 0; s < servers.size(); s++)
count[servers[s].address()]++;
int o = 0;
for (int s = 0; s < servers.size(); s++)
2021-05-13 02:53:20 +08:00
if (count[servers[s].address()] == 1 && !servers[s].isTss())
2017-05-26 04:48:44 +08:00
servers[o++] = servers[s];
servers.resize(o);
}
ACTOR Future<Void> forceMasterFailure(Database cx, MoveKeysWorkload* self) {
ASSERT(g_network->isSimulated());
loop {
2022-09-15 08:10:49 +08:00
if (g_simulator->killZone(self->dbInfo->get().master.locality.zoneId(), ISimulator::Reboot, true))
2017-05-26 04:48:44 +08:00
return Void();
wait(delay(1.0));
2017-05-26 04:48:44 +08:00
}
}
ACTOR Future<Void> worker(Database cx, MoveKeysWorkload* self) {
state KeyRangeMap<std::vector<StorageServerInterface>> inFlight;
2017-05-26 04:48:44 +08:00
state KeyRangeActorMap inFlightActors;
state double lastTime = now();
ASSERT(self->configuration.storageTeamSize > 0);
if (self->configuration.usableRegions > 1) { // FIXME: add support for generating random teams across DCs
return Void();
}
loop {
2017-05-26 04:48:44 +08:00
try {
state MoveKeysLock lock = wait(takeMoveKeysLock(cx, UID()));
state std::vector<StorageServerInterface> storageServers = wait(getStorageServers(cx));
2017-05-26 04:48:44 +08:00
eliminateDuplicates(storageServers);
loop {
wait(poisson(&lastTime, self->meanDelay));
2017-05-26 04:48:44 +08:00
KeyRange keys = self->getRandomKeys();
std::vector<StorageServerInterface> team =
2017-05-26 04:48:44 +08:00
self->getRandomTeam(storageServers, self->configuration.storageTeamSize);
// update both inFlightActors and inFlight key range maps, cancelling deleted RelocateShards
std::vector<KeyRange> ranges;
2017-05-26 04:48:44 +08:00
inFlightActors.getRangesAffectedByInsertion(keys, ranges);
inFlightActors.cancel(KeyRangeRef(ranges.front().begin, ranges.back().end));
inFlight.insert(keys, team);
for (int r = 0; r < ranges.size(); r++) {
auto& rTeam = inFlight.rangeContaining(ranges[r].begin)->value();
inFlightActors.insert(ranges[r], self->doMoveKeys(cx, self, ranges[r], rTeam, lock));
2017-05-26 04:48:44 +08:00
}
}
} catch (Error& e) {
if (e.code() != error_code_movekeys_conflict && e.code() != error_code_operation_failed)
throw;
wait(delay(FLOW_KNOBS->PREVENT_FAST_SPIN_DELAY));
2017-05-26 04:48:44 +08:00
// Keep trying to get the moveKeysLock
}
}
}
};
WorkloadFactory<MoveKeysWorkload> MoveKeysWorkloadFactory;
2022-09-13 07:20:57 +08:00
FailureInjectorFactory<MoveKeysWorkload> MoveKeysFailureInjectionFactory;