foundationdb/fdbserver/workloads/FastTriggeredWatches.actor.cpp

185 lines
6.2 KiB
C++
Raw Normal View History

2017-05-26 04:48:44 +08:00
/*
* FastTriggeredWatches.actor.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2018 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 "fdbrpc/ContinuousSample.h"
#include "fdbclient/NativeAPI.actor.h"
#include "fdbserver/TesterInterface.actor.h"
2017-05-26 04:48:44 +08:00
#include "fdbclient/ReadYourWrites.h"
#include "fdbserver/Knobs.h"
#include "fdbserver/workloads/workloads.actor.h"
#include "flow/actorcompiler.h" // This must be the last #include.
2017-05-26 04:48:44 +08:00
struct FastTriggeredWatchesWorkload : TestWorkload {
// Tests the time it takes for a watch to be fired after the value has changed in the storage server
2017-05-26 04:48:44 +08:00
int nodes, keyBytes;
double testDuration;
vector<Future<Void>> clients;
PerfIntCounter operations, retries;
Value defaultValue;
FastTriggeredWatchesWorkload(WorkloadContext const& wcx)
: TestWorkload(wcx), operations("Operations"), retries("Retries") {
testDuration = getOption(options, LiteralStringRef("testDuration"), 600.0);
nodes = getOption(options, LiteralStringRef("nodes"), 100);
defaultValue = StringRef(format("%010d", deterministicRandom()->randomInt(0, 1000)));
keyBytes = std::max(getOption(options, LiteralStringRef("keyBytes"), 16), 16);
2017-05-26 04:48:44 +08:00
}
2020-10-05 13:29:07 +08:00
std::string description() const override { return "Watches"; }
2017-05-26 04:48:44 +08:00
2020-10-05 13:29:07 +08:00
Future<Void> setup(Database const& cx) override {
if (clientId == 0)
return _setup(cx, this);
2017-05-26 04:48:44 +08:00
return Void();
}
ACTOR Future<Void> _setup(Database cx, FastTriggeredWatchesWorkload* self) {
2017-05-26 04:48:44 +08:00
state Transaction tr(cx);
loop {
try {
for (int i = 0; i < self->nodes; i += 2)
tr.set(self->keyForIndex(i), self->defaultValue);
2017-05-26 04:48:44 +08:00
wait(tr.commit());
2017-05-26 04:48:44 +08:00
break;
} catch (Error& e) {
wait(tr.onError(e));
2017-05-26 04:48:44 +08:00
}
}
return Void();
}
2020-10-05 13:29:07 +08:00
Future<Void> start(Database const& cx) override {
if (clientId == 0)
return _start(cx, this);
2017-05-26 04:48:44 +08:00
return Void();
}
ACTOR Future<Version> setter(Database cx, Key key, Optional<Value> value) {
state ReadYourWritesTransaction tr(cx);
// set the value of key and return the commit version
wait(delay(deterministicRandom()->random01()));
2017-05-26 04:48:44 +08:00
loop {
try {
if (value.present())
tr.set(key, value.get());
2017-05-26 04:48:44 +08:00
else
tr.clear(key);
//TraceEvent("FTWSetBegin").detail("Key", printable(key)).detail("Value", printable(value));
wait(tr.commit());
//TraceEvent("FTWSetEnd").detail("Key", printable(key)).detail("Value", printable(value)).detail("Ver", tr.getCommittedVersion());
2017-05-26 04:48:44 +08:00
return tr.getCommittedVersion();
} catch (Error& e) {
//TraceEvent("FTWSetError").error(e).detail("Key", printable(key)).detail("Value", printable(value));
wait(tr.onError(e));
2017-05-26 04:48:44 +08:00
}
}
}
ACTOR static Future<Void> _start(Database cx, FastTriggeredWatchesWorkload* self) {
2017-05-26 04:48:44 +08:00
state double testStart = now();
state Version lastReadVersion = 0;
try {
loop {
state double getDuration = 0;
state double watchEnd = 0;
state bool first = true;
state Key setKey = self->keyForIndex(deterministicRandom()->randomInt(0, self->nodes));
2017-05-26 04:48:44 +08:00
state Optional<Value> setValue;
if (deterministicRandom()->random01() > 0.5)
setValue = StringRef(format("%010d", deterministicRandom()->randomInt(0, 1000)));
// Set the value at setKey to something random
state Future<Version> setFuture = self->setter(cx, setKey, setValue);
wait(delay(deterministicRandom()->random01()));
2017-05-26 04:48:44 +08:00
loop {
state ReadYourWritesTransaction tr(cx);
2017-05-26 04:48:44 +08:00
try {
Optional<Value> val = wait(tr.get(setKey));
if (!first) {
2017-05-26 04:48:44 +08:00
getDuration = now() - watchEnd;
}
lastReadVersion = tr.getReadVersion().get();
//TraceEvent("FTWGet").detail("Key", printable(setKey)).detail("Value", printable(val)).detail("Ver", tr.getReadVersion().get());
// if the value is already setValue then there is no point setting a watch so break out of the loop
if (val == setValue)
2017-05-26 04:48:44 +08:00
break;
ASSERT(first);
// set a watch and wait for it to be triggered (i.e for self->setter to set the value)
state Future<Void> watchFuture = tr.watch(setKey);
wait(tr.commit());
//TraceEvent("FTWStartWatch").detail("Key", printable(setKey));
wait(watchFuture);
2017-05-26 04:48:44 +08:00
watchEnd = now();
first = false;
} catch (Error& e) {
//TraceEvent("FTWWatchError").error(e).detail("Key", printable(setKey));
wait(tr.onError(e));
2017-05-26 04:48:44 +08:00
}
}
Version ver = wait(setFuture);
//TraceEvent("FTWWatchDone").detail("Key", printable(setKey));
// Assert that the time from setting the key to triggering the watch is no greater than 25s
// TODO: This assertion can cause flaky behaviour since sometimes a watch can take longer to fire
ASSERT(lastReadVersion - ver >= SERVER_KNOBS->MAX_VERSIONS_IN_FLIGHT ||
lastReadVersion - ver < SERVER_KNOBS->VERSIONS_PER_SECOND * (25 + getDuration));
2017-05-26 04:48:44 +08:00
if (now() - testStart > self->testDuration)
2017-05-26 04:48:44 +08:00
break;
}
return Void();
} catch (Error& e) {
TraceEvent(SevError, "FastWatchError").error(e, true);
2017-05-26 04:48:44 +08:00
throw;
}
}
2020-10-05 13:29:07 +08:00
Future<bool> check(Database const& cx) override {
2017-05-26 04:48:44 +08:00
bool ok = true;
for (int i = 0; i < clients.size(); i++)
if (clients[i].isError())
2017-05-26 04:48:44 +08:00
ok = false;
clients.clear();
return ok;
}
2020-10-05 13:29:07 +08:00
void getMetrics(vector<PerfMetric>& m) override {
2017-05-26 04:48:44 +08:00
double duration = testDuration;
m.push_back(PerfMetric("Operations/sec", operations.getValue() / duration, false));
m.push_back(operations.getMetric());
m.push_back(retries.getMetric());
2017-05-26 04:48:44 +08:00
}
2020-10-05 13:29:07 +08:00
Key keyForIndex(uint64_t index) const {
Key result = makeString(keyBytes);
uint8_t* data = mutateString(result);
2017-05-26 04:48:44 +08:00
memset(data, '.', keyBytes);
double d = double(index) / nodes;
emplaceIndex(data, 0, *(int64_t*)&d);
2017-05-26 04:48:44 +08:00
return result;
}
};
WorkloadFactory<FastTriggeredWatchesWorkload> FastTriggeredWatchesWorkloadFactory("FastTriggeredWatches");