2020-03-25 00:48:03 +08:00
|
|
|
/*
|
|
|
|
* ReportConflictingKeys.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
|
2020-03-25 00:48:03 +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
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* 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/NativeAPI.actor.h"
|
|
|
|
#include "fdbclient/ReadYourWrites.h"
|
|
|
|
#include "fdbclient/SystemData.h"
|
|
|
|
#include "fdbserver/TesterInterface.actor.h"
|
|
|
|
#include "fdbserver/workloads/workloads.actor.h"
|
|
|
|
#include "fdbserver/workloads/BulkSetup.actor.h"
|
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
|
|
|
|
|
|
|
// For this test to report properly buggify must be disabled (flow.h) , and failConnection must be disabled in
|
|
|
|
// (sim2.actor.cpp)
|
|
|
|
struct ReportConflictingKeysWorkload : TestWorkload {
|
|
|
|
|
|
|
|
double testDuration, transactionsPerSecond, addReadConflictRangeProb, addWriteConflictRangeProb;
|
|
|
|
Key keyPrefix;
|
|
|
|
|
|
|
|
int nodeCount, actorCount, keyBytes, valueBytes, readConflictRangeCount, writeConflictRangeCount;
|
|
|
|
|
|
|
|
PerfIntCounter invalidReports, commits, conflicts, xacts;
|
|
|
|
|
|
|
|
ReportConflictingKeysWorkload(WorkloadContext const& wcx)
|
2021-07-23 13:48:27 +08:00
|
|
|
: TestWorkload(wcx), invalidReports("InvalidReports"), commits("commits"), conflicts("Conflicts"),
|
2020-03-25 00:48:03 +08:00
|
|
|
xacts("Transactions") {
|
|
|
|
testDuration = getOption(options, LiteralStringRef("testDuration"), 10.0);
|
|
|
|
// transactionsPerSecond = getOption(options, LiteralStringRef("transactionsPerSecond"), 5000.0) / clientCount;
|
|
|
|
actorCount = getOption(options, LiteralStringRef("actorsPerClient"), 1);
|
|
|
|
keyPrefix = unprintable(
|
|
|
|
getOption(options, LiteralStringRef("keyPrefix"), LiteralStringRef("ReportConflictingKeysWorkload"))
|
|
|
|
.toString());
|
2020-05-12 06:59:39 +08:00
|
|
|
keyBytes = getOption(options, LiteralStringRef("keyBytes"), 64);
|
2020-03-25 00:48:03 +08:00
|
|
|
|
|
|
|
readConflictRangeCount = getOption(options, LiteralStringRef("readConflictRangeCountPerTx"), 1);
|
|
|
|
writeConflictRangeCount = getOption(options, LiteralStringRef("writeConflictRangeCountPerTx"), 1);
|
|
|
|
ASSERT(readConflictRangeCount >= 1 && writeConflictRangeCount >= 1);
|
|
|
|
// modeled by geometric distribution: (1 - prob) / prob = mean - 1, where we add at least one conflictRange to
|
|
|
|
// each tx
|
|
|
|
addReadConflictRangeProb = (readConflictRangeCount - 1.0) / readConflictRangeCount;
|
|
|
|
addWriteConflictRangeProb = (writeConflictRangeCount - 1.0) / writeConflictRangeCount;
|
2020-05-12 06:59:39 +08:00
|
|
|
ASSERT(keyPrefix.size() + 8 <= keyBytes); // make sure the string format is valid
|
2020-03-25 00:48:03 +08:00
|
|
|
nodeCount = getOption(options, LiteralStringRef("nodeCount"), 100);
|
|
|
|
}
|
|
|
|
|
2020-10-05 13:29:07 +08:00
|
|
|
std::string description() const override { return "ReportConflictingKeysWorkload"; }
|
2020-03-25 00:48:03 +08:00
|
|
|
|
|
|
|
Future<Void> setup(Database const& cx) override { return Void(); }
|
|
|
|
|
|
|
|
Future<Void> start(const Database& cx) override { return _start(cx->clone(), this); }
|
|
|
|
|
|
|
|
ACTOR Future<Void> _start(Database cx, ReportConflictingKeysWorkload* self) {
|
2020-05-12 06:59:39 +08:00
|
|
|
wait(timeout(self->conflictingClient(cx, self), self->testDuration, Void()));
|
2020-03-25 00:48:03 +08:00
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> check(Database const& cx) override { return invalidReports.getValue() == 0; }
|
|
|
|
|
2021-09-17 08:42:34 +08:00
|
|
|
void getMetrics(std::vector<PerfMetric>& m) override {
|
2021-08-30 06:38:21 +08:00
|
|
|
m.emplace_back("Measured Duration", testDuration, Averaged::True);
|
2020-03-25 00:48:03 +08:00
|
|
|
m.push_back(xacts.getMetric());
|
2021-08-30 06:38:21 +08:00
|
|
|
m.emplace_back("Transactions/sec", xacts.getValue() / testDuration, Averaged::True);
|
2020-03-25 00:48:03 +08:00
|
|
|
m.push_back(commits.getMetric());
|
2021-08-30 06:38:21 +08:00
|
|
|
m.emplace_back("Commits/sec", commits.getValue() / testDuration, Averaged::True);
|
2020-03-25 00:48:03 +08:00
|
|
|
m.push_back(conflicts.getMetric());
|
2021-08-30 06:38:21 +08:00
|
|
|
m.emplace_back("Conflicts/sec", conflicts.getValue() / testDuration, Averaged::True);
|
2020-03-25 00:48:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// disable the default timeout setting
|
2020-10-08 02:46:23 +08:00
|
|
|
double getCheckTimeout() const override { return std::numeric_limits<double>::max(); }
|
2020-03-25 00:48:03 +08:00
|
|
|
|
|
|
|
// Copied from tester.actor.cpp, added parameter to determine the key's length
|
|
|
|
Key keyForIndex(int n) {
|
|
|
|
double p = (double)n / nodeCount;
|
2020-05-12 06:59:39 +08:00
|
|
|
// 8 bytes for Cid_* suffix of each client
|
|
|
|
int paddingLen = keyBytes - 8 - keyPrefix.size();
|
|
|
|
// left padding by zero, each client has different prefix
|
|
|
|
Key prefixWithClientId = StringRef(format("Cid_%04d", clientId)).withPrefix(keyPrefix);
|
|
|
|
return StringRef(format("%0*llx", paddingLen, *(uint64_t*)&p)).withPrefix(prefixWithClientId);
|
2020-03-25 00:48:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void addRandomReadConflictRange(ReadYourWritesTransaction* tr, std::vector<KeyRange>* readConflictRanges) {
|
|
|
|
int startIdx, endIdx;
|
|
|
|
Key startKey, endKey;
|
2020-03-25 15:55:40 +08:00
|
|
|
do { // add at least one non-empty range
|
2020-03-25 00:48:03 +08:00
|
|
|
startIdx = deterministicRandom()->randomInt(0, nodeCount);
|
2020-03-25 15:55:40 +08:00
|
|
|
endIdx = deterministicRandom()->randomInt(startIdx + 1, nodeCount + 1);
|
2020-03-25 00:48:03 +08:00
|
|
|
startKey = keyForIndex(startIdx);
|
|
|
|
endKey = keyForIndex(endIdx);
|
2020-05-22 05:48:35 +08:00
|
|
|
ASSERT(startKey < endKey);
|
2020-03-25 00:48:03 +08:00
|
|
|
tr->addReadConflictRange(KeyRangeRef(startKey, endKey));
|
2021-03-11 02:06:03 +08:00
|
|
|
if (readConflictRanges)
|
|
|
|
readConflictRanges->push_back(KeyRangeRef(startKey, endKey));
|
2020-03-25 00:48:03 +08:00
|
|
|
} while (deterministicRandom()->random01() < addReadConflictRangeProb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void addRandomWriteConflictRange(ReadYourWritesTransaction* tr, std::vector<KeyRange>* writeConflictRanges) {
|
|
|
|
int startIdx, endIdx;
|
|
|
|
Key startKey, endKey;
|
2020-03-25 15:55:40 +08:00
|
|
|
do { // add at least one non-empty range
|
2020-03-25 00:48:03 +08:00
|
|
|
startIdx = deterministicRandom()->randomInt(0, nodeCount);
|
2020-03-25 15:55:40 +08:00
|
|
|
endIdx = deterministicRandom()->randomInt(startIdx + 1, nodeCount + 1);
|
2020-03-25 00:48:03 +08:00
|
|
|
startKey = keyForIndex(startIdx);
|
|
|
|
endKey = keyForIndex(endIdx);
|
2020-05-22 05:48:35 +08:00
|
|
|
ASSERT(startKey < endKey);
|
2020-03-25 00:48:03 +08:00
|
|
|
tr->addWriteConflictRange(KeyRangeRef(startKey, endKey));
|
2021-03-11 02:06:03 +08:00
|
|
|
if (writeConflictRanges)
|
|
|
|
writeConflictRanges->push_back(KeyRangeRef(startKey, endKey));
|
2020-03-25 00:48:03 +08:00
|
|
|
} while (deterministicRandom()->random01() < addWriteConflictRangeProb);
|
|
|
|
}
|
|
|
|
|
2020-07-25 07:31:00 +08:00
|
|
|
void emptyConflictingKeysTest(const Reference<ReadYourWritesTransaction>& ryw) {
|
2020-05-13 15:39:21 +08:00
|
|
|
// This test is called when you want to make sure there is no conflictingKeys,
|
2020-05-22 05:48:35 +08:00
|
|
|
// which means you will get an empty result form getRange(\xff\xff/transaction/conflicting_keys/,
|
|
|
|
// \xff\xff/transaction/conflicting_keys0)
|
2020-05-13 15:39:21 +08:00
|
|
|
auto resultFuture = ryw->getRange(conflictingKeysRange, CLIENT_KNOBS->TOO_MANY);
|
|
|
|
auto result = resultFuture.get();
|
|
|
|
ASSERT(!result.more && result.size() == 0);
|
|
|
|
}
|
|
|
|
|
2020-03-25 00:48:03 +08:00
|
|
|
ACTOR Future<Void> conflictingClient(Database cx, ReportConflictingKeysWorkload* self) {
|
|
|
|
|
2020-07-25 07:31:00 +08:00
|
|
|
state Reference<ReadYourWritesTransaction> tr1(new ReadYourWritesTransaction(cx));
|
|
|
|
state Reference<ReadYourWritesTransaction> tr2(new ReadYourWritesTransaction(cx));
|
2020-03-25 00:48:03 +08:00
|
|
|
state std::vector<KeyRange> readConflictRanges;
|
|
|
|
state std::vector<KeyRange> writeConflictRanges;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
try {
|
2020-05-13 15:39:21 +08:00
|
|
|
// set the flag for empty key range testing
|
2020-07-25 07:31:00 +08:00
|
|
|
tr1->setOption(FDBTransactionOptions::REPORT_CONFLICTING_KEYS);
|
2020-05-13 15:39:21 +08:00
|
|
|
// tr1 should never have conflicting keys, the result should always be empty
|
2020-07-25 07:31:00 +08:00
|
|
|
self->emptyConflictingKeysTest(tr1);
|
2020-05-13 15:39:21 +08:00
|
|
|
|
2020-07-25 07:31:00 +08:00
|
|
|
tr2->setOption(FDBTransactionOptions::REPORT_CONFLICTING_KEYS);
|
2020-03-25 00:48:03 +08:00
|
|
|
// If READ_YOUR_WRITES_DISABLE set, it behaves like native transaction object
|
|
|
|
// where overlapped conflict ranges are not merged.
|
2021-03-11 02:06:03 +08:00
|
|
|
if (deterministicRandom()->coinflip())
|
|
|
|
tr1->setOption(FDBTransactionOptions::READ_YOUR_WRITES_DISABLE);
|
|
|
|
if (deterministicRandom()->coinflip())
|
|
|
|
tr2->setOption(FDBTransactionOptions::READ_YOUR_WRITES_DISABLE);
|
2020-03-25 00:48:03 +08:00
|
|
|
// We have the two tx with same grv, then commit the first
|
|
|
|
// If the second one is not able to commit due to conflicts, verify the returned conflicting keys
|
|
|
|
// Otherwise, there is no conflicts between tr1's writeConflictRange and tr2's readConflictRange
|
2020-07-25 07:31:00 +08:00
|
|
|
Version readVersion = wait(tr1->getReadVersion());
|
|
|
|
tr2->setVersion(readVersion);
|
|
|
|
self->addRandomReadConflictRange(tr1.getPtr(), nullptr);
|
|
|
|
self->addRandomWriteConflictRange(tr1.getPtr(), &writeConflictRanges);
|
2020-03-25 00:48:03 +08:00
|
|
|
++self->commits;
|
2020-07-25 07:31:00 +08:00
|
|
|
wait(tr1->commit());
|
2020-03-25 00:48:03 +08:00
|
|
|
++self->xacts;
|
2020-05-13 15:39:21 +08:00
|
|
|
// tr1 should never have conflicting keys, test again after the commit
|
2020-07-25 07:31:00 +08:00
|
|
|
self->emptyConflictingKeysTest(tr1);
|
2020-03-25 00:48:03 +08:00
|
|
|
|
|
|
|
state bool foundConflict = false;
|
|
|
|
try {
|
2020-07-25 07:31:00 +08:00
|
|
|
self->addRandomReadConflictRange(tr2.getPtr(), &readConflictRanges);
|
|
|
|
self->addRandomWriteConflictRange(tr2.getPtr(), nullptr);
|
2020-03-25 00:48:03 +08:00
|
|
|
++self->commits;
|
2020-07-25 07:31:00 +08:00
|
|
|
wait(tr2->commit());
|
2020-03-25 00:48:03 +08:00
|
|
|
++self->xacts;
|
|
|
|
} catch (Error& e) {
|
2021-03-11 02:06:03 +08:00
|
|
|
if (e.code() != error_code_not_committed)
|
|
|
|
throw e;
|
2020-03-25 00:48:03 +08:00
|
|
|
foundConflict = true;
|
|
|
|
++self->conflicts;
|
|
|
|
}
|
2020-05-22 05:48:35 +08:00
|
|
|
// These two conflict sets should not be empty
|
|
|
|
ASSERT(readConflictRanges.size());
|
|
|
|
ASSERT(writeConflictRanges.size());
|
2020-03-25 00:48:03 +08:00
|
|
|
// check API correctness
|
|
|
|
if (foundConflict) {
|
|
|
|
// \xff\xff/transaction/conflicting_keys is always initialized to false, skip it here
|
|
|
|
state KeyRange ckr =
|
2020-04-09 03:43:25 +08:00
|
|
|
KeyRangeRef(keyAfter(LiteralStringRef("").withPrefix(conflictingKeysRange.begin)),
|
|
|
|
LiteralStringRef("\xff\xff").withPrefix(conflictingKeysRange.begin));
|
2020-03-25 00:48:03 +08:00
|
|
|
// The getRange here using the special key prefix "\xff\xff/transaction/conflicting_keys/" happens
|
|
|
|
// locally Thus, the error handling is not needed here
|
2021-05-04 04:14:16 +08:00
|
|
|
Future<RangeResult> conflictingKeyRangesFuture = tr2->getRange(ckr, CLIENT_KNOBS->TOO_MANY);
|
2020-03-25 00:48:03 +08:00
|
|
|
ASSERT(conflictingKeyRangesFuture.isReady());
|
2020-07-25 07:31:00 +08:00
|
|
|
|
2020-11-07 15:50:55 +08:00
|
|
|
tr2 = makeReference<ReadYourWritesTransaction>(cx);
|
2020-07-25 07:31:00 +08:00
|
|
|
|
2021-05-04 04:14:16 +08:00
|
|
|
const RangeResult conflictingKeyRanges = conflictingKeyRangesFuture.get();
|
2020-03-25 00:48:03 +08:00
|
|
|
ASSERT(conflictingKeyRanges.size() &&
|
|
|
|
(conflictingKeyRanges.size() <= readConflictRanges.size() * 2));
|
|
|
|
ASSERT(conflictingKeyRanges.size() % 2 == 0);
|
|
|
|
ASSERT(!conflictingKeyRanges.more);
|
|
|
|
for (int i = 0; i < conflictingKeyRanges.size(); i += 2) {
|
|
|
|
KeyValueRef startKeyWithPrefix = conflictingKeyRanges[i];
|
2020-04-09 03:43:25 +08:00
|
|
|
ASSERT(startKeyWithPrefix.key.startsWith(conflictingKeysRange.begin));
|
2020-03-25 00:48:03 +08:00
|
|
|
ASSERT(startKeyWithPrefix.value == conflictingKeysTrue);
|
|
|
|
KeyValueRef endKeyWithPrefix = conflictingKeyRanges[i + 1];
|
2020-04-09 03:43:25 +08:00
|
|
|
ASSERT(endKeyWithPrefix.key.startsWith(conflictingKeysRange.begin));
|
2020-03-25 00:48:03 +08:00
|
|
|
ASSERT(endKeyWithPrefix.value == conflictingKeysFalse);
|
|
|
|
// Remove the prefix of returning keys
|
2020-04-09 03:43:25 +08:00
|
|
|
Key startKey = startKeyWithPrefix.key.removePrefix(conflictingKeysRange.begin);
|
|
|
|
Key endKey = endKeyWithPrefix.key.removePrefix(conflictingKeysRange.begin);
|
2020-03-25 00:48:03 +08:00
|
|
|
KeyRangeRef kr = KeyRangeRef(startKey, endKey);
|
|
|
|
if (!std::any_of(readConflictRanges.begin(), readConflictRanges.end(), [&kr](KeyRange rCR) {
|
|
|
|
// Read_conflict_range remains same in the resolver.
|
|
|
|
// Thus, the returned keyrange is either the original read_conflict_range or merged
|
|
|
|
// by several overlapped ones in either cases, it contains at least one original
|
|
|
|
// read_conflict_range
|
|
|
|
return kr.contains(rCR);
|
|
|
|
})) {
|
|
|
|
++self->invalidReports;
|
2020-05-22 05:48:35 +08:00
|
|
|
std::string allReadConflictRanges = "";
|
|
|
|
for (int i = 0; i < readConflictRanges.size(); i++) {
|
|
|
|
allReadConflictRanges += "Begin:" + printable(readConflictRanges[i].begin) +
|
|
|
|
", End:" + printable(readConflictRanges[i].end) + "; ";
|
|
|
|
}
|
2020-03-25 00:48:03 +08:00
|
|
|
TraceEvent(SevError, "TestFailure")
|
|
|
|
.detail("Reason",
|
2020-05-22 05:48:35 +08:00
|
|
|
"Returned conflicting keys are not original or merged readConflictRanges")
|
|
|
|
.detail("ConflictingKeyRange", kr.toString())
|
|
|
|
.detail("ReadConflictRanges", allReadConflictRanges);
|
2021-03-11 02:06:03 +08:00
|
|
|
} else if (!std::any_of(
|
|
|
|
writeConflictRanges.begin(), writeConflictRanges.end(), [&kr](KeyRange wCR) {
|
|
|
|
// Returned key range should be conflicting with at least one
|
|
|
|
// writeConflictRange
|
|
|
|
return kr.intersects(wCR);
|
|
|
|
})) {
|
2020-03-25 00:48:03 +08:00
|
|
|
++self->invalidReports;
|
2020-05-22 05:48:35 +08:00
|
|
|
std::string allWriteConflictRanges = "";
|
|
|
|
for (int i = 0; i < writeConflictRanges.size(); i++) {
|
|
|
|
allWriteConflictRanges += "Begin:" + printable(writeConflictRanges[i].begin) +
|
|
|
|
", End:" + printable(writeConflictRanges[i].end) + "; ";
|
|
|
|
}
|
2020-03-25 00:48:03 +08:00
|
|
|
TraceEvent(SevError, "TestFailure")
|
2020-05-22 05:48:35 +08:00
|
|
|
.detail("Reason", "Returned keyrange is not conflicting with any writeConflictRange")
|
|
|
|
.detail("ConflictingKeyRange", kr.toString())
|
|
|
|
.detail("WriteConflictRanges", allWriteConflictRanges);
|
2020-03-25 00:48:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// make sure no conflicts between tr2's readConflictRange and tr1's writeConflictRange
|
|
|
|
for (const KeyRange& rCR : readConflictRanges) {
|
|
|
|
if (std::any_of(writeConflictRanges.begin(), writeConflictRanges.end(), [&rCR](KeyRange wCR) {
|
|
|
|
bool result = wCR.intersects(rCR);
|
|
|
|
if (result)
|
|
|
|
TraceEvent(SevError, "TestFailure")
|
2020-03-25 15:55:40 +08:00
|
|
|
.detail("Reason", "No conflicts returned but it should")
|
|
|
|
.detail("WriteConflictRangeInTr1", wCR.toString())
|
|
|
|
.detail("ReadConflictRangeInTr2", rCR.toString());
|
2020-03-25 00:48:03 +08:00
|
|
|
return result;
|
|
|
|
})) {
|
|
|
|
++self->invalidReports;
|
2020-05-22 05:48:35 +08:00
|
|
|
std::string allReadConflictRanges = "";
|
|
|
|
for (int i = 0; i < readConflictRanges.size(); i++) {
|
|
|
|
allReadConflictRanges += "Begin:" + printable(readConflictRanges[i].begin) +
|
|
|
|
", End:" + printable(readConflictRanges[i].end) + "; ";
|
|
|
|
}
|
|
|
|
std::string allWriteConflictRanges = "";
|
|
|
|
for (int i = 0; i < writeConflictRanges.size(); i++) {
|
|
|
|
allWriteConflictRanges += "Begin:" + printable(writeConflictRanges[i].begin) +
|
|
|
|
", End:" + printable(writeConflictRanges[i].end) + "; ";
|
|
|
|
}
|
|
|
|
TraceEvent(SevError, "TestFailure")
|
|
|
|
.detail("Reason", "No conflicts returned but it should")
|
|
|
|
.detail("ReadConflictRanges", allReadConflictRanges)
|
|
|
|
.detail("WriteConflictRanges", allWriteConflictRanges);
|
2020-03-25 00:48:03 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Error& e) {
|
|
|
|
state Error e2 = e;
|
2020-07-25 07:31:00 +08:00
|
|
|
wait(tr1->onError(e2));
|
|
|
|
wait(tr2->onError(e2));
|
2020-03-25 00:48:03 +08:00
|
|
|
}
|
|
|
|
readConflictRanges.clear();
|
|
|
|
writeConflictRanges.clear();
|
2020-07-25 07:31:00 +08:00
|
|
|
tr1->reset();
|
|
|
|
tr2->reset();
|
2020-03-25 00:48:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
WorkloadFactory<ReportConflictingKeysWorkload> ReportConflictingKeysWorkload("ReportConflictingKeys");
|