2020-04-09 05:50:55 +08:00
|
|
|
/*
|
|
|
|
* SpecialKeySpace.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2020 Apple Inc. and the FoundationDB project authors
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2020-03-04 10:35:24 +08:00
|
|
|
#include "fdbclient/SpecialKeySpace.actor.h"
|
|
|
|
#include "flow/UnitTest.h"
|
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
|
|
|
|
2020-05-13 17:28:04 +08:00
|
|
|
std::unordered_map<SpecialKeySpace::MODULE, KeyRange> SpecialKeySpace::moduleToBoundary = {
|
|
|
|
{ SpecialKeySpace::MODULE::TRANSACTION,
|
|
|
|
KeyRangeRef(LiteralStringRef("\xff\xff/transaction/"), LiteralStringRef("\xff\xff/transaction0")) },
|
|
|
|
{ SpecialKeySpace::MODULE::WORKERINTERFACE,
|
|
|
|
KeyRangeRef(LiteralStringRef("\xff\xff/worker_interfaces/"), LiteralStringRef("\xff\xff/worker_interfaces0")) },
|
|
|
|
{ SpecialKeySpace::MODULE::STATUSJSON, singleKeyRange(LiteralStringRef("\xff\xff/status/json")) },
|
|
|
|
{ SpecialKeySpace::MODULE::CONNECTIONSTRING, singleKeyRange(LiteralStringRef("\xff\xff/connection_string")) },
|
2020-05-19 01:38:23 +08:00
|
|
|
{ SpecialKeySpace::MODULE::CLUSTERFILEPATH, singleKeyRange(LiteralStringRef("\xff\xff/cluster_file_path")) },
|
2020-05-19 05:23:17 +08:00
|
|
|
{ SpecialKeySpace::MODULE::METRICS,
|
|
|
|
KeyRangeRef(LiteralStringRef("\xff\xff/metrics/"), LiteralStringRef("\xff\xff/metrics0")) }
|
2020-05-13 17:28:04 +08:00
|
|
|
};
|
|
|
|
|
2020-05-12 15:42:43 +08:00
|
|
|
// This function will move the given KeySelector as far as possible to the standard form:
|
2020-03-04 10:35:24 +08:00
|
|
|
// orEqual == false && offset == 1 (Standard form)
|
2020-05-12 15:42:43 +08:00
|
|
|
// If the corresponding key is not in the underlying key range, it will move over the range
|
|
|
|
ACTOR Future<Void> moveKeySelectorOverRangeActor(const SpecialKeyRangeBaseImpl* skrImpl,
|
|
|
|
Reference<ReadYourWritesTransaction> ryw, KeySelector* ks) {
|
2020-03-04 10:35:24 +08:00
|
|
|
ASSERT(!ks->orEqual); // should be removed before calling
|
|
|
|
ASSERT(ks->offset != 1); // never being called if KeySelector is already normalized
|
|
|
|
|
2020-05-12 15:42:43 +08:00
|
|
|
state Key startKey(skrImpl->getKeyRange().begin);
|
|
|
|
state Key endKey(skrImpl->getKeyRange().end);
|
2020-03-04 10:35:24 +08:00
|
|
|
|
|
|
|
if (ks->offset < 1) {
|
|
|
|
// less than the given key
|
2020-05-15 17:04:15 +08:00
|
|
|
if (skrImpl->getKeyRange().contains(ks->getKey())) endKey = ks->getKey();
|
2020-03-04 10:35:24 +08:00
|
|
|
} else {
|
|
|
|
// greater than the given key
|
2020-05-12 15:42:43 +08:00
|
|
|
if (skrImpl->getKeyRange().contains(ks->getKey())) startKey = ks->getKey();
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
2020-05-15 17:04:15 +08:00
|
|
|
ASSERT(startKey < endKey); // Note : startKey never equals endKey here
|
2020-03-04 10:35:24 +08:00
|
|
|
|
2020-04-08 03:49:46 +08:00
|
|
|
TraceEvent(SevDebug, "NormalizeKeySelector")
|
2020-03-04 10:35:24 +08:00
|
|
|
.detail("OriginalKey", ks->getKey())
|
|
|
|
.detail("OriginalOffset", ks->offset)
|
2020-05-12 15:42:43 +08:00
|
|
|
.detail("SpecialKeyRangeStart", skrImpl->getKeyRange().begin)
|
|
|
|
.detail("SpecialKeyRangeEnd", skrImpl->getKeyRange().end);
|
2020-03-04 10:35:24 +08:00
|
|
|
|
2020-05-12 10:37:04 +08:00
|
|
|
Standalone<RangeResultRef> result = wait(skrImpl->getRange(ryw, KeyRangeRef(startKey, endKey)));
|
2020-03-24 14:55:56 +08:00
|
|
|
if (result.size() == 0) {
|
2020-05-15 08:30:48 +08:00
|
|
|
TraceEvent(SevDebug, "ZeroElementsIntheRange").detail("Start", startKey).detail("End", endKey);
|
2020-03-24 14:55:56 +08:00
|
|
|
return Void();
|
|
|
|
}
|
2020-04-01 00:33:25 +08:00
|
|
|
// Note : KeySelector::setKey has byte limit according to the knobs, customize it if needed
|
2020-03-04 10:35:24 +08:00
|
|
|
if (ks->offset < 1) {
|
|
|
|
if (result.size() >= 1 - ks->offset) {
|
|
|
|
ks->setKey(KeyRef(ks->arena(), result[result.size() - (1 - ks->offset)].key));
|
|
|
|
ks->offset = 1;
|
|
|
|
} else {
|
|
|
|
ks->setKey(KeyRef(ks->arena(), result[0].key));
|
|
|
|
ks->offset += result.size();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (result.size() >= ks->offset) {
|
|
|
|
ks->setKey(KeyRef(ks->arena(), result[ks->offset - 1].key));
|
|
|
|
ks->offset = 1;
|
|
|
|
} else {
|
|
|
|
ks->setKey(KeyRef(ks->arena(), keyAfter(result[result.size() - 1].key)));
|
|
|
|
ks->offset -= result.size();
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 03:49:46 +08:00
|
|
|
TraceEvent(SevDebug, "NormalizeKeySelector")
|
2020-03-04 10:35:24 +08:00
|
|
|
.detail("NormalizedKey", ks->getKey())
|
|
|
|
.detail("NormalizedOffset", ks->offset)
|
2020-05-12 15:42:43 +08:00
|
|
|
.detail("SpecialKeyRangeStart", skrImpl->getKeyRange().begin)
|
|
|
|
.detail("SpecialKeyRangeEnd", skrImpl->getKeyRange().end);
|
2020-03-04 10:35:24 +08:00
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:29:17 +08:00
|
|
|
void onModuleRead(const Reference<ReadYourWritesTransaction>& ryw, SpecialKeySpace::MODULE module,
|
|
|
|
Optional<SpecialKeySpace::MODULE>& lastModuleRead) {
|
2020-05-06 02:41:18 +08:00
|
|
|
if (ryw && !ryw->specialKeySpaceRelaxed() && lastModuleRead.present() && lastModuleRead.get() != module) {
|
|
|
|
throw special_keys_cross_module_read();
|
|
|
|
}
|
|
|
|
lastModuleRead = module;
|
|
|
|
}
|
|
|
|
|
2020-05-12 15:42:43 +08:00
|
|
|
// This function will normalize the given KeySelector to a standard KeySelector:
|
|
|
|
// orEqual == false && offset == 1 (Standard form)
|
|
|
|
// If the corresponding key is outside the whole space, it will move to the begin or the end
|
|
|
|
// It does have overhead here since we query all keys twice in the worst case.
|
|
|
|
// However, moving the KeySelector while handling other parameters like limits makes the code much more complex and hard
|
|
|
|
// to maintain; Thus, separate each part to make the code easy to understand and more compact
|
|
|
|
ACTOR Future<Void> normalizeKeySelectorActor(SpecialKeySpace* sks, Reference<ReadYourWritesTransaction> ryw,
|
2020-05-12 18:29:17 +08:00
|
|
|
KeySelector* ks, Optional<SpecialKeySpace::MODULE>* lastModuleRead,
|
2020-05-12 15:42:43 +08:00
|
|
|
int* actualOffset, Standalone<RangeResultRef>* result) {
|
|
|
|
state RangeMap<Key, SpecialKeyRangeBaseImpl*, KeyRangeRef>::Iterator iter =
|
2020-05-15 17:04:15 +08:00
|
|
|
ks->offset < 1 ? sks->getImpls().rangeContainingKeyBefore(ks->getKey())
|
|
|
|
: sks->getImpls().rangeContaining(ks->getKey());
|
2020-05-12 15:42:43 +08:00
|
|
|
while ((ks->offset < 1 && iter != sks->getImpls().ranges().begin()) ||
|
|
|
|
(ks->offset > 1 && iter != sks->getImpls().ranges().end())) {
|
2020-05-15 08:30:48 +08:00
|
|
|
onModuleRead(ryw, sks->getModules().rangeContaining(iter->begin())->value(), *lastModuleRead);
|
2020-05-12 15:42:43 +08:00
|
|
|
if (iter->value() != nullptr) {
|
|
|
|
wait(moveKeySelectorOverRangeActor(iter->value(), ryw, ks));
|
|
|
|
}
|
|
|
|
ks->offset < 1 ? --iter : ++iter;
|
|
|
|
}
|
|
|
|
*actualOffset = ks->offset;
|
|
|
|
if (iter == sks->getImpls().ranges().begin())
|
|
|
|
ks->setKey(sks->getKeyRange().begin);
|
|
|
|
else if (iter == sks->getImpls().ranges().end())
|
|
|
|
ks->setKey(sks->getKeyRange().end);
|
|
|
|
|
|
|
|
if (!ks->isFirstGreaterOrEqual()) {
|
|
|
|
// The Key Selector points to key outside the whole special key space
|
|
|
|
TraceEvent(SevInfo, "KeySelectorPointsOutside")
|
|
|
|
.detail("TerminateKey", ks->getKey())
|
|
|
|
.detail("TerminateOffset", ks->offset);
|
|
|
|
if (ks->offset < 1 && iter == sks->getImpls().ranges().begin())
|
|
|
|
result->readToBegin = true;
|
|
|
|
else
|
|
|
|
result->readThroughEnd = true;
|
|
|
|
ks->offset = 1;
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<Standalone<RangeResultRef>> SpecialKeySpace::checkModuleFound(SpecialKeySpace* sks,
|
2020-05-06 04:07:09 +08:00
|
|
|
Reference<ReadYourWritesTransaction> ryw,
|
|
|
|
KeySelector begin, KeySelector end,
|
|
|
|
GetRangeLimits limits, bool reverse) {
|
2020-05-12 18:29:17 +08:00
|
|
|
std::pair<Standalone<RangeResultRef>, Optional<SpecialKeySpace::MODULE>> result =
|
2020-05-12 15:42:43 +08:00
|
|
|
wait(SpecialKeySpace::getRangeAggregationActor(sks, ryw, begin, end, limits, reverse));
|
2020-05-09 10:46:40 +08:00
|
|
|
if (ryw && !ryw->specialKeySpaceRelaxed()) {
|
2020-05-13 05:52:37 +08:00
|
|
|
auto module = result.second;
|
2020-05-16 03:15:07 +08:00
|
|
|
if (!module.present() || module.get() == SpecialKeySpace::MODULE::UNKNOWN) {
|
2020-05-09 10:46:40 +08:00
|
|
|
throw special_keys_no_module_found();
|
2020-05-15 14:49:57 +08:00
|
|
|
}
|
2020-05-06 04:07:09 +08:00
|
|
|
}
|
|
|
|
return result.first;
|
|
|
|
}
|
|
|
|
|
2020-05-12 18:29:17 +08:00
|
|
|
ACTOR Future<std::pair<Standalone<RangeResultRef>, Optional<SpecialKeySpace::MODULE>>>
|
2020-05-12 15:42:43 +08:00
|
|
|
SpecialKeySpace::getRangeAggregationActor(SpecialKeySpace* sks, Reference<ReadYourWritesTransaction> ryw,
|
2020-05-06 04:07:09 +08:00
|
|
|
KeySelector begin, KeySelector end, GetRangeLimits limits, bool reverse) {
|
2020-03-04 10:35:24 +08:00
|
|
|
// This function handles ranges which cover more than one keyrange and aggregates all results
|
|
|
|
// KeySelector, GetRangeLimits and reverse are all handled here
|
2020-03-31 14:27:09 +08:00
|
|
|
state Standalone<RangeResultRef> result;
|
|
|
|
state RangeMap<Key, SpecialKeyRangeBaseImpl*, KeyRangeRef>::Iterator iter;
|
|
|
|
state int actualBeginOffset;
|
|
|
|
state int actualEndOffset;
|
2020-05-12 18:29:17 +08:00
|
|
|
state Optional<SpecialKeySpace::MODULE> lastModuleRead;
|
2020-04-03 15:26:11 +08:00
|
|
|
|
2020-05-12 15:42:43 +08:00
|
|
|
wait(normalizeKeySelectorActor(sks, ryw, &begin, &lastModuleRead, &actualBeginOffset, &result));
|
|
|
|
wait(normalizeKeySelectorActor(sks, ryw, &end, &lastModuleRead, &actualEndOffset, &result));
|
2020-03-31 14:27:09 +08:00
|
|
|
// Handle all corner cases like what RYW does
|
2020-03-04 10:35:24 +08:00
|
|
|
// return if range inverted
|
2020-03-31 14:27:09 +08:00
|
|
|
if (actualBeginOffset >= actualEndOffset && begin.getKey() >= end.getKey()) {
|
2020-03-04 10:35:24 +08:00
|
|
|
TEST(true);
|
2020-05-06 04:07:09 +08:00
|
|
|
return std::make_pair(RangeResultRef(false, false), lastModuleRead);
|
2020-03-31 14:27:09 +08:00
|
|
|
}
|
2020-03-31 16:36:07 +08:00
|
|
|
// If touches begin or end, return with readToBegin and readThroughEnd flags
|
2020-05-12 15:42:43 +08:00
|
|
|
if (begin.getKey() == sks->range.end || end.getKey() == sks->range.begin) {
|
2020-03-31 16:36:07 +08:00
|
|
|
TEST(true);
|
2020-05-06 04:07:09 +08:00
|
|
|
return std::make_pair(result, lastModuleRead);
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
|
|
|
state RangeMap<Key, SpecialKeyRangeBaseImpl*, KeyRangeRef>::Ranges ranges =
|
2020-05-12 15:42:43 +08:00
|
|
|
sks->impls.intersectingRanges(KeyRangeRef(begin.getKey(), end.getKey()));
|
2020-03-04 10:35:24 +08:00
|
|
|
// TODO : workaround to write this two together to make the code compact
|
|
|
|
// The issue here is boost::iterator_range<> doest not provide rbegin(), rend()
|
|
|
|
iter = reverse ? ranges.end() : ranges.begin();
|
|
|
|
if (reverse) {
|
|
|
|
while (iter != ranges.begin()) {
|
|
|
|
--iter;
|
2020-05-15 08:30:48 +08:00
|
|
|
onModuleRead(ryw, sks->getModules().rangeContaining(iter->begin())->value(), lastModuleRead);
|
2020-03-04 10:35:24 +08:00
|
|
|
if (iter->value() == nullptr) continue;
|
|
|
|
KeyRangeRef kr = iter->range();
|
|
|
|
KeyRef keyStart = kr.contains(begin.getKey()) ? begin.getKey() : kr.begin;
|
|
|
|
KeyRef keyEnd = kr.contains(end.getKey()) ? end.getKey() : kr.end;
|
|
|
|
Standalone<RangeResultRef> pairs = wait(iter->value()->getRange(ryw, KeyRangeRef(keyStart, keyEnd)));
|
2020-04-07 15:08:47 +08:00
|
|
|
result.arena().dependsOn(pairs.arena());
|
2020-03-04 10:35:24 +08:00
|
|
|
// limits handler
|
|
|
|
for (int i = pairs.size() - 1; i >= 0; --i) {
|
2020-04-09 03:43:25 +08:00
|
|
|
result.push_back(result.arena(), pairs[i]);
|
2020-04-09 04:38:12 +08:00
|
|
|
// Note : behavior here is even the last k-v pair makes total bytes larger than specified, it's still
|
|
|
|
// returned. In other words, the total size of the returned value (less the last entry) will be less
|
|
|
|
// than byteLimit
|
2020-04-09 03:43:25 +08:00
|
|
|
limits.decrement(pairs[i]);
|
2020-03-31 14:27:09 +08:00
|
|
|
if (limits.isReached()) {
|
|
|
|
result.more = true;
|
|
|
|
result.readToBegin = false;
|
2020-05-06 04:07:09 +08:00
|
|
|
return std::make_pair(result, lastModuleRead);
|
2020-03-31 14:27:09 +08:00
|
|
|
};
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (iter = ranges.begin(); iter != ranges.end(); ++iter) {
|
2020-05-15 08:30:48 +08:00
|
|
|
onModuleRead(ryw, sks->getModules().rangeContaining(iter->begin())->value(), lastModuleRead);
|
2020-03-04 10:35:24 +08:00
|
|
|
if (iter->value() == nullptr) continue;
|
|
|
|
KeyRangeRef kr = iter->range();
|
|
|
|
KeyRef keyStart = kr.contains(begin.getKey()) ? begin.getKey() : kr.begin;
|
|
|
|
KeyRef keyEnd = kr.contains(end.getKey()) ? end.getKey() : kr.end;
|
|
|
|
Standalone<RangeResultRef> pairs = wait(iter->value()->getRange(ryw, KeyRangeRef(keyStart, keyEnd)));
|
2020-04-07 15:08:47 +08:00
|
|
|
result.arena().dependsOn(pairs.arena());
|
2020-03-04 10:35:24 +08:00
|
|
|
// limits handler
|
2020-03-31 14:27:09 +08:00
|
|
|
for (int i = 0; i < pairs.size(); ++i) {
|
2020-04-09 03:43:25 +08:00
|
|
|
result.push_back(result.arena(), pairs[i]);
|
2020-04-09 04:38:12 +08:00
|
|
|
// Note : behavior here is even the last k-v pair makes total bytes larger than specified, it's still
|
|
|
|
// returned. In other words, the total size of the returned value (less the last entry) will be less
|
|
|
|
// than byteLimit
|
2020-04-09 03:43:25 +08:00
|
|
|
limits.decrement(pairs[i]);
|
2020-03-31 14:27:09 +08:00
|
|
|
if (limits.isReached()) {
|
|
|
|
result.more = true;
|
|
|
|
result.readThroughEnd = false;
|
2020-05-06 04:07:09 +08:00
|
|
|
return std::make_pair(result, lastModuleRead);
|
2020-03-31 14:27:09 +08:00
|
|
|
};
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-06 04:07:09 +08:00
|
|
|
return std::make_pair(result, lastModuleRead);
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> SpecialKeySpace::getRange(Reference<ReadYourWritesTransaction> ryw,
|
|
|
|
KeySelector begin, KeySelector end, GetRangeLimits limits,
|
2020-04-09 04:38:12 +08:00
|
|
|
bool reverse) {
|
2020-03-04 10:35:24 +08:00
|
|
|
// validate limits here
|
|
|
|
if (!limits.isValid()) return range_limits_invalid();
|
|
|
|
if (limits.isReached()) {
|
|
|
|
TEST(true); // read limit 0
|
|
|
|
return Standalone<RangeResultRef>();
|
|
|
|
}
|
2020-03-31 16:44:02 +08:00
|
|
|
// make sure orEqual == false
|
|
|
|
begin.removeOrEqual(begin.arena());
|
|
|
|
end.removeOrEqual(end.arena());
|
2020-04-07 13:09:17 +08:00
|
|
|
|
2020-05-15 17:04:15 +08:00
|
|
|
if (begin.offset >= end.offset && begin.getKey() >= end.getKey()) {
|
2020-05-15 14:49:57 +08:00
|
|
|
TEST(true); // range inverted
|
|
|
|
return Standalone<RangeResultRef>();
|
|
|
|
}
|
2020-05-15 08:30:48 +08:00
|
|
|
|
2020-05-06 04:07:09 +08:00
|
|
|
return checkModuleFound(this, ryw, begin, end, limits, reverse);
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
|
|
|
|
2020-05-12 15:42:43 +08:00
|
|
|
ACTOR Future<Optional<Value>> SpecialKeySpace::getActor(SpecialKeySpace* sks, Reference<ReadYourWritesTransaction> ryw,
|
2020-04-09 04:38:12 +08:00
|
|
|
KeyRef key) {
|
2020-03-04 10:35:24 +08:00
|
|
|
// use getRange to workaround this
|
2020-04-01 00:33:25 +08:00
|
|
|
Standalone<RangeResultRef> result =
|
2020-05-12 15:42:43 +08:00
|
|
|
wait(sks->getRange(ryw, KeySelector(firstGreaterOrEqual(key)), KeySelector(firstGreaterOrEqual(keyAfter(key))),
|
2020-04-09 04:38:12 +08:00
|
|
|
GetRangeLimits(CLIENT_KNOBS->TOO_MANY), false));
|
2020-03-04 10:35:24 +08:00
|
|
|
ASSERT(result.size() <= 1);
|
|
|
|
if (result.size()) {
|
|
|
|
return Optional<Value>(result[0].value);
|
|
|
|
} else {
|
|
|
|
return Optional<Value>();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 04:38:12 +08:00
|
|
|
Future<Optional<Value>> SpecialKeySpace::get(Reference<ReadYourWritesTransaction> ryw, const Key& key) {
|
|
|
|
return getActor(this, ryw, key);
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
|
|
|
|
2020-04-29 00:00:06 +08:00
|
|
|
ReadConflictRangeImpl::ReadConflictRangeImpl(KeyRangeRef kr) : SpecialKeyRangeBaseImpl(kr) {}
|
|
|
|
|
|
|
|
ACTOR static Future<Standalone<RangeResultRef>> getReadConflictRangeImpl(Reference<ReadYourWritesTransaction> ryw,
|
|
|
|
KeyRange kr) {
|
2020-04-29 03:44:34 +08:00
|
|
|
wait(ryw->pendingReads());
|
2020-04-30 05:43:37 +08:00
|
|
|
return ryw->getReadConflictRangeIntersecting(kr);
|
2020-04-29 00:00:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> ReadConflictRangeImpl::getRange(Reference<ReadYourWritesTransaction> ryw,
|
|
|
|
KeyRangeRef kr) const {
|
|
|
|
return getReadConflictRangeImpl(ryw, kr);
|
|
|
|
}
|
|
|
|
|
2020-04-29 01:34:10 +08:00
|
|
|
WriteConflictRangeImpl::WriteConflictRangeImpl(KeyRangeRef kr) : SpecialKeyRangeBaseImpl(kr) {}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> WriteConflictRangeImpl::getRange(Reference<ReadYourWritesTransaction> ryw,
|
|
|
|
KeyRangeRef kr) const {
|
2020-04-30 05:43:37 +08:00
|
|
|
return ryw->getWriteConflictRangeIntersecting(kr);
|
2020-04-29 01:34:10 +08:00
|
|
|
}
|
|
|
|
|
2020-04-15 00:10:40 +08:00
|
|
|
ConflictingKeysImpl::ConflictingKeysImpl(KeyRangeRef kr) : SpecialKeyRangeBaseImpl(kr) {}
|
2020-04-07 04:23:41 +08:00
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> ConflictingKeysImpl::getRange(Reference<ReadYourWritesTransaction> ryw,
|
2020-04-07 04:38:18 +08:00
|
|
|
KeyRangeRef kr) const {
|
2020-04-07 04:23:41 +08:00
|
|
|
Standalone<RangeResultRef> result;
|
2020-04-07 04:38:18 +08:00
|
|
|
if (ryw->getTransactionInfo().conflictingKeys) {
|
|
|
|
auto krMapPtr = ryw->getTransactionInfo().conflictingKeys.get();
|
|
|
|
auto beginIter = krMapPtr->rangeContaining(kr.begin);
|
|
|
|
if (beginIter->begin() != kr.begin) ++beginIter;
|
|
|
|
auto endIter = krMapPtr->rangeContaining(kr.end);
|
2020-04-07 04:23:41 +08:00
|
|
|
for (auto it = beginIter; it != endIter; ++it) {
|
2020-04-07 15:24:01 +08:00
|
|
|
// it->begin() is stored in the CoalescedKeyRangeMap in TransactionInfo
|
|
|
|
// it->value() is always constants in SystemData.cpp
|
|
|
|
// Thus, push_back() can be used
|
|
|
|
result.push_back(result.arena(), KeyValueRef(it->begin(), it->value()));
|
2020-04-07 04:23:41 +08:00
|
|
|
}
|
|
|
|
if (endIter->begin() != kr.end)
|
2020-04-07 15:24:01 +08:00
|
|
|
result.push_back(result.arena(), KeyValueRef(endIter->begin(), endIter->value()));
|
2020-04-07 04:23:41 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-05-19 05:23:17 +08:00
|
|
|
ACTOR Future<Standalone<RangeResultRef>> ddStatsGetRangeActor(Reference<ReadYourWritesTransaction> ryw,
|
|
|
|
KeyRangeRef kr) {
|
2020-05-19 01:38:23 +08:00
|
|
|
try {
|
|
|
|
auto keys = kr.removePrefix(ddStatsRange.begin);
|
|
|
|
Standalone<VectorRef<DDMetricsRef>> resultWithoutPrefix =
|
|
|
|
wait(waitDataDistributionMetricsList(ryw->getDatabase(), keys, CLIENT_KNOBS->STORAGE_METRICS_SHARD_LIMIT));
|
|
|
|
Standalone<RangeResultRef> result;
|
|
|
|
for (const auto& ddMetricsRef : resultWithoutPrefix) {
|
|
|
|
KeyRef beginKey = ddMetricsRef.beginKey.withPrefix(ddStatsRange.begin, result.arena());
|
|
|
|
KeyRef endKey = ddMetricsRef.endKey.withPrefix(ddStatsRange.begin, result.arena());
|
|
|
|
ValueRef bytes(result.arena(), std::to_string(ddMetricsRef.shardBytes));
|
|
|
|
result.push_back(result.arena(), KeyValueRef(beginKey, bytes));
|
2020-05-19 02:53:26 +08:00
|
|
|
result.push_back(result.arena(), KeyValueRef(endKey, ddStatsZeroBytes));
|
2020-05-19 01:38:23 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} catch (Error& e) {
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DDStatsRangeImpl::DDStatsRangeImpl(KeyRangeRef kr) : SpecialKeyRangeBaseImpl(kr) {}
|
|
|
|
|
2020-05-19 05:23:17 +08:00
|
|
|
Future<Standalone<RangeResultRef>> DDStatsRangeImpl::getRange(Reference<ReadYourWritesTransaction> ryw,
|
|
|
|
KeyRangeRef kr) const {
|
2020-05-19 01:38:23 +08:00
|
|
|
return ddStatsGetRangeActor(ryw, kr);
|
|
|
|
}
|
|
|
|
|
2020-03-04 10:35:24 +08:00
|
|
|
class SpecialKeyRangeTestImpl : public SpecialKeyRangeBaseImpl {
|
|
|
|
public:
|
2020-04-15 00:10:40 +08:00
|
|
|
explicit SpecialKeyRangeTestImpl(KeyRangeRef kr, const std::string& prefix, int size)
|
|
|
|
: SpecialKeyRangeBaseImpl(kr), prefix(prefix), size(size) {
|
2020-03-04 10:35:24 +08:00
|
|
|
ASSERT(size > 0);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
|
|
|
kvs.push_back_deep(kvs.arena(),
|
|
|
|
KeyValueRef(getKeyForIndex(i), deterministicRandom()->randomAlphaNumeric(16)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyValueRef getKeyValueForIndex(int idx) { return kvs[idx]; }
|
|
|
|
|
|
|
|
Key getKeyForIndex(int idx) { return Key(prefix + format("%010d", idx)).withPrefix(range.begin); }
|
|
|
|
int getSize() { return size; }
|
2020-04-08 01:25:26 +08:00
|
|
|
Future<Standalone<RangeResultRef>> getRange(Reference<ReadYourWritesTransaction> ryw,
|
2020-04-09 04:38:12 +08:00
|
|
|
KeyRangeRef kr) const override {
|
2020-03-04 10:35:24 +08:00
|
|
|
int startIndex = 0, endIndex = size;
|
|
|
|
while (startIndex < size && kvs[startIndex].key < kr.begin) ++startIndex;
|
|
|
|
while (endIndex > startIndex && kvs[endIndex - 1].key >= kr.end) --endIndex;
|
|
|
|
if (startIndex == endIndex)
|
|
|
|
return Standalone<RangeResultRef>();
|
|
|
|
else
|
|
|
|
return Standalone<RangeResultRef>(RangeResultRef(kvs.slice(startIndex, endIndex), false));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Standalone<VectorRef<KeyValueRef>> kvs;
|
|
|
|
std::string prefix;
|
|
|
|
int size;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_CASE("/fdbclient/SpecialKeySpace/Unittest") {
|
2020-05-12 15:42:43 +08:00
|
|
|
SpecialKeySpace sks(normalKeys.begin, normalKeys.end);
|
2020-04-15 00:10:40 +08:00
|
|
|
SpecialKeyRangeTestImpl pkr1(KeyRangeRef(LiteralStringRef("/cat/"), LiteralStringRef("/cat/\xff")), "small", 10);
|
|
|
|
SpecialKeyRangeTestImpl pkr2(KeyRangeRef(LiteralStringRef("/dog/"), LiteralStringRef("/dog/\xff")), "medium", 100);
|
|
|
|
SpecialKeyRangeTestImpl pkr3(KeyRangeRef(LiteralStringRef("/pig/"), LiteralStringRef("/pig/\xff")), "large", 1000);
|
2020-05-13 05:52:37 +08:00
|
|
|
sks.registerKeyRange(SpecialKeySpace::MODULE::TESTONLY, pkr1.getKeyRange(), &pkr1);
|
|
|
|
sks.registerKeyRange(SpecialKeySpace::MODULE::TESTONLY, pkr2.getKeyRange(), &pkr2);
|
|
|
|
sks.registerKeyRange(SpecialKeySpace::MODULE::TESTONLY, pkr3.getKeyRange(), &pkr3);
|
2020-03-04 10:35:24 +08:00
|
|
|
auto nullRef = Reference<ReadYourWritesTransaction>();
|
|
|
|
// get
|
|
|
|
{
|
2020-05-12 15:42:43 +08:00
|
|
|
auto resultFuture = sks.get(nullRef, LiteralStringRef("/cat/small0000000009"));
|
2020-03-04 10:35:24 +08:00
|
|
|
ASSERT(resultFuture.isReady());
|
|
|
|
auto result = resultFuture.getValue().get();
|
|
|
|
ASSERT(result == pkr1.getKeyValueForIndex(9).value);
|
2020-05-12 15:42:43 +08:00
|
|
|
auto emptyFuture = sks.get(nullRef, LiteralStringRef("/cat/small0000000010"));
|
2020-03-04 10:35:24 +08:00
|
|
|
ASSERT(emptyFuture.isReady());
|
|
|
|
auto emptyResult = emptyFuture.getValue();
|
|
|
|
ASSERT(!emptyResult.present());
|
|
|
|
}
|
|
|
|
// general getRange
|
|
|
|
{
|
2020-04-07 15:08:47 +08:00
|
|
|
KeySelector start = KeySelectorRef(LiteralStringRef("/elepant"), false, -9);
|
|
|
|
KeySelector end = KeySelectorRef(LiteralStringRef("/frog"), false, +11);
|
2020-05-12 15:42:43 +08:00
|
|
|
auto resultFuture = sks.getRange(nullRef, start, end, GetRangeLimits());
|
2020-03-04 10:35:24 +08:00
|
|
|
ASSERT(resultFuture.isReady());
|
|
|
|
auto result = resultFuture.getValue();
|
|
|
|
ASSERT(result.size() == 20);
|
|
|
|
ASSERT(result[0].key == pkr2.getKeyForIndex(90));
|
|
|
|
ASSERT(result[result.size() - 1].key == pkr3.getKeyForIndex(9));
|
|
|
|
}
|
|
|
|
// KeySelector points outside
|
|
|
|
{
|
|
|
|
KeySelector start = KeySelectorRef(pkr3.getKeyForIndex(999), true, -1110);
|
|
|
|
KeySelector end = KeySelectorRef(pkr1.getKeyForIndex(0), false, +1112);
|
2020-05-12 15:42:43 +08:00
|
|
|
auto resultFuture = sks.getRange(nullRef, start, end, GetRangeLimits());
|
2020-03-04 10:35:24 +08:00
|
|
|
ASSERT(resultFuture.isReady());
|
|
|
|
auto result = resultFuture.getValue();
|
|
|
|
ASSERT(result.size() == 1110);
|
|
|
|
ASSERT(result[0].key == pkr1.getKeyForIndex(0));
|
|
|
|
ASSERT(result[result.size() - 1].key == pkr3.getKeyForIndex(999));
|
|
|
|
}
|
|
|
|
// GetRangeLimits with row limit
|
|
|
|
{
|
|
|
|
KeySelector start = KeySelectorRef(pkr2.getKeyForIndex(0), true, 0);
|
|
|
|
KeySelector end = KeySelectorRef(pkr3.getKeyForIndex(0), false, 0);
|
2020-05-12 15:42:43 +08:00
|
|
|
auto resultFuture = sks.getRange(nullRef, start, end, GetRangeLimits(2));
|
2020-03-04 10:35:24 +08:00
|
|
|
ASSERT(resultFuture.isReady());
|
|
|
|
auto result = resultFuture.getValue();
|
|
|
|
ASSERT(result.size() == 2);
|
|
|
|
ASSERT(result[0].key == pkr2.getKeyForIndex(0));
|
|
|
|
ASSERT(result[1].key == pkr2.getKeyForIndex(1));
|
|
|
|
}
|
|
|
|
// GetRangeLimits with byte limit
|
|
|
|
{
|
|
|
|
KeySelector start = KeySelectorRef(pkr2.getKeyForIndex(0), true, 0);
|
|
|
|
KeySelector end = KeySelectorRef(pkr3.getKeyForIndex(0), false, 0);
|
2020-05-12 15:42:43 +08:00
|
|
|
auto resultFuture = sks.getRange(nullRef, start, end, GetRangeLimits(10, 100));
|
2020-03-04 10:35:24 +08:00
|
|
|
ASSERT(resultFuture.isReady());
|
|
|
|
auto result = resultFuture.getValue();
|
|
|
|
int bytes = 0;
|
|
|
|
for (int i = 0; i < result.size() - 1; ++i) bytes += 8 + pkr2.getKeyValueForIndex(i).expectedSize();
|
|
|
|
ASSERT(bytes < 100);
|
|
|
|
ASSERT(bytes + 8 + pkr2.getKeyValueForIndex(result.size()).expectedSize() >= 100);
|
|
|
|
}
|
|
|
|
// reverse test with overlapping key range
|
|
|
|
{
|
|
|
|
KeySelector start = KeySelectorRef(pkr2.getKeyForIndex(0), true, 0);
|
|
|
|
KeySelector end = KeySelectorRef(pkr3.getKeyForIndex(999), true, +1);
|
2020-05-12 15:42:43 +08:00
|
|
|
auto resultFuture = sks.getRange(nullRef, start, end, GetRangeLimits(1100), true);
|
2020-03-04 10:35:24 +08:00
|
|
|
ASSERT(resultFuture.isReady());
|
|
|
|
auto result = resultFuture.getValue();
|
|
|
|
for (int i = 0; i < pkr3.getSize(); ++i) ASSERT(result[i] == pkr3.getKeyValueForIndex(pkr3.getSize() - 1 - i));
|
|
|
|
for (int i = 0; i < pkr2.getSize(); ++i)
|
|
|
|
ASSERT(result[i + pkr3.getSize()] == pkr2.getKeyValueForIndex(pkr2.getSize() - 1 - i));
|
|
|
|
}
|
|
|
|
return Void();
|
2020-04-16 03:24:44 +08:00
|
|
|
}
|