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.
|
|
|
|
*/
|
|
|
|
|
2021-02-05 16:55:34 +08:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
|
2020-03-04 10:35:24 +08:00
|
|
|
#include "fdbclient/SpecialKeySpace.actor.h"
|
|
|
|
#include "flow/UnitTest.h"
|
2020-07-07 02:02:48 +08:00
|
|
|
#include "fdbclient/ManagementAPI.actor.h"
|
|
|
|
#include "fdbclient/StatusClient.h"
|
2020-03-04 10:35:24 +08:00
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
|
|
|
|
2020-12-04 06:06:11 +08:00
|
|
|
namespace {
|
|
|
|
const std::string kTracingTransactionIdKey = "transaction_id";
|
|
|
|
const std::string kTracingTokenKey = "token";
|
2021-01-30 03:45:52 +08:00
|
|
|
|
|
|
|
static bool isAlphaNumeric(const std::string& key) {
|
|
|
|
// [A-Za-z0-9_]+
|
|
|
|
if (!key.size()) return false;
|
|
|
|
for (const char& c : key) {
|
|
|
|
if (!((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_')) return false;
|
|
|
|
}
|
|
|
|
return true;
|
2020-12-04 06:06:11 +08:00
|
|
|
}
|
2021-01-30 03:45:52 +08:00
|
|
|
} // namespace
|
2020-12-04 06:06:11 +08:00
|
|
|
|
2020-06-30 06:37:46 +08:00
|
|
|
std::unordered_map<SpecialKeySpace::MODULE, KeyRange> SpecialKeySpace::moduleToBoundary = {
|
2020-05-13 17:28:04 +08:00
|
|
|
{ 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,
|
2020-06-15 13:29:44 +08:00
|
|
|
KeyRangeRef(LiteralStringRef("\xff\xff/metrics/"), LiteralStringRef("\xff\xff/metrics0")) },
|
2020-07-18 06:16:28 +08:00
|
|
|
{ SpecialKeySpace::MODULE::MANAGEMENT,
|
2020-07-31 17:18:49 +08:00
|
|
|
KeyRangeRef(LiteralStringRef("\xff\xff/management/"), LiteralStringRef("\xff\xff/management0")) },
|
2020-08-14 16:01:08 +08:00
|
|
|
{ SpecialKeySpace::MODULE::ERRORMSG, singleKeyRange(LiteralStringRef("\xff\xff/error_message")) },
|
|
|
|
{ SpecialKeySpace::MODULE::CONFIGURATION,
|
2020-12-01 06:57:17 +08:00
|
|
|
KeyRangeRef(LiteralStringRef("\xff\xff/configuration/"), LiteralStringRef("\xff\xff/configuration0")) },
|
|
|
|
{ SpecialKeySpace::MODULE::TRACING,
|
|
|
|
KeyRangeRef(LiteralStringRef("\xff\xff/tracing/"), LiteralStringRef("\xff\xff/tracing0")) }
|
2020-05-13 17:28:04 +08:00
|
|
|
};
|
|
|
|
|
2020-07-18 02:56:27 +08:00
|
|
|
std::unordered_map<std::string, KeyRange> SpecialKeySpace::managementApiCommandToRange = {
|
2020-07-18 06:16:28 +08:00
|
|
|
{ "exclude", KeyRangeRef(LiteralStringRef("excluded/"), LiteralStringRef("excluded0"))
|
|
|
|
.withPrefix(moduleToBoundary[MODULE::MANAGEMENT].begin) },
|
|
|
|
{ "failed", KeyRangeRef(LiteralStringRef("failed/"), LiteralStringRef("failed0"))
|
2020-10-09 05:23:02 +08:00
|
|
|
.withPrefix(moduleToBoundary[MODULE::MANAGEMENT].begin) },
|
2020-10-23 02:35:39 +08:00
|
|
|
{ "lock", singleKeyRange(LiteralStringRef("db_locked")).withPrefix(moduleToBoundary[MODULE::MANAGEMENT].begin) },
|
2020-10-23 02:08:54 +08:00
|
|
|
{ "consistencycheck", singleKeyRange(LiteralStringRef("consistency_check_suspended"))
|
2021-01-30 03:45:52 +08:00
|
|
|
.withPrefix(moduleToBoundary[MODULE::MANAGEMENT].begin) },
|
|
|
|
{ "coordinators", KeyRangeRef(LiteralStringRef("coordinators/"), LiteralStringRef("coordinators0"))
|
|
|
|
.withPrefix(moduleToBoundary[MODULE::CONFIGURATION].begin) }
|
2020-07-15 01:31:19 +08:00
|
|
|
};
|
|
|
|
|
2021-01-30 03:45:52 +08:00
|
|
|
std::set<std::string> SpecialKeySpace::options = { "excluded/force", "failed/force", "coordinators/auto" };
|
2020-07-18 03:32:42 +08:00
|
|
|
|
2020-12-05 03:21:39 +08:00
|
|
|
std::set<std::string> SpecialKeySpace::tracingOptions = { kTracingTransactionIdKey, kTracingTokenKey };
|
|
|
|
|
2020-09-09 02:08:48 +08:00
|
|
|
Standalone<RangeResultRef> rywGetRange(ReadYourWritesTransaction* ryw, const KeyRangeRef& kr,
|
|
|
|
const Standalone<RangeResultRef>& res);
|
|
|
|
|
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
|
2020-06-16 06:31:55 +08:00
|
|
|
// The cache object is used to cache the first read result from the rpc call during the key resolution,
|
|
|
|
// then when we need to do key resolution or result filtering,
|
|
|
|
// we, instead of rpc call, read from this cache object have consistent results
|
2020-06-16 03:47:27 +08:00
|
|
|
ACTOR Future<Void> moveKeySelectorOverRangeActor(const SpecialKeyRangeReadImpl* skrImpl, ReadYourWritesTransaction* ryw,
|
2020-06-12 03:22:19 +08:00
|
|
|
KeySelector* ks, Optional<Standalone<RangeResultRef>>* cache) {
|
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-06-12 03:22:19 +08:00
|
|
|
state Standalone<RangeResultRef> result;
|
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-06-12 03:22:19 +08:00
|
|
|
if (skrImpl->isAsync()) {
|
|
|
|
const SpecialKeyRangeAsyncImpl* ptr = dynamic_cast<const SpecialKeyRangeAsyncImpl*>(skrImpl);
|
|
|
|
Standalone<RangeResultRef> result_ = wait(ptr->getRange(ryw, KeyRangeRef(startKey, endKey), cache));
|
|
|
|
result = result_;
|
|
|
|
} else {
|
|
|
|
Standalone<RangeResultRef> result_ = wait(skrImpl->getRange(ryw, KeyRangeRef(startKey, endKey)));
|
|
|
|
result = result_;
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-06-28 03:22:32 +08:00
|
|
|
ks->setKey(KeyRef(
|
|
|
|
ks->arena(),
|
|
|
|
keyAfter(result[result.size() - 1].key))); // TODO : the keyAfter will just return if key == \xff\xff
|
2020-03-04 10:35:24 +08:00
|
|
|
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 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
|
2020-06-18 02:28:52 +08:00
|
|
|
// Boundary is the range of the legal key space, which, by default is the range of the module
|
2020-06-18 03:47:54 +08:00
|
|
|
// And (\xff\xff, \xff\xff\xff) if SPECIAL_KEY_SPACE_RELAXED is turned on
|
2020-05-21 04:55:07 +08:00
|
|
|
ACTOR Future<Void> normalizeKeySelectorActor(SpecialKeySpace* sks, ReadYourWritesTransaction* ryw, KeySelector* ks,
|
2020-06-18 01:33:52 +08:00
|
|
|
KeyRangeRef boundary, int* actualOffset,
|
2020-06-12 03:22:19 +08:00
|
|
|
Standalone<RangeResultRef>* result,
|
|
|
|
Optional<Standalone<RangeResultRef>>* cache) {
|
2020-12-05 06:58:40 +08:00
|
|
|
// If offset < 1, where we need to move left, iter points to the range containing at least one smaller key
|
2020-12-07 06:04:16 +08:00
|
|
|
// (It's a wasting of time to walk through the range whose begin key is same as ks->key)
|
2020-12-05 06:58:40 +08:00
|
|
|
// (rangeContainingKeyBefore itself handles the case where ks->key == Key())
|
|
|
|
// Otherwise, we only need to move right if offset > 1, iter points to the range containing the key
|
|
|
|
// Since boundary.end is always a key in the RangeMap, it is always safe to move right
|
2020-07-15 01:36:47 +08:00
|
|
|
state RangeMap<Key, SpecialKeyRangeReadImpl*, KeyRangeRef>::iterator iter =
|
2020-06-16 03:47:27 +08:00
|
|
|
ks->offset < 1 ? sks->getReadImpls().rangeContainingKeyBefore(ks->getKey())
|
|
|
|
: sks->getReadImpls().rangeContaining(ks->getKey());
|
2020-12-05 06:58:40 +08:00
|
|
|
while ((ks->offset < 1 && iter->begin() >= boundary.begin) || (ks->offset > 1 && iter->begin() < boundary.end)) {
|
2020-05-12 15:42:43 +08:00
|
|
|
if (iter->value() != nullptr) {
|
2020-06-12 03:22:19 +08:00
|
|
|
wait(moveKeySelectorOverRangeActor(iter->value(), ryw, ks, cache));
|
2020-05-12 15:42:43 +08:00
|
|
|
}
|
2020-12-05 06:58:40 +08:00
|
|
|
// Check if we can still move the iterator left
|
|
|
|
if (ks->offset < 1) {
|
2020-12-08 06:26:11 +08:00
|
|
|
if (iter == sks->getReadImpls().ranges().begin()) {
|
2020-12-05 06:58:40 +08:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
--iter;
|
|
|
|
}
|
|
|
|
} else if (ks->offset > 1) {
|
|
|
|
// Always safe to move right
|
|
|
|
++iter;
|
|
|
|
}
|
2020-05-12 15:42:43 +08:00
|
|
|
}
|
|
|
|
*actualOffset = ks->offset;
|
|
|
|
|
|
|
|
if (!ks->isFirstGreaterOrEqual()) {
|
2020-10-16 04:55:31 +08:00
|
|
|
TraceEvent(SevDebug, "ReadToBoundary")
|
2020-05-12 15:42:43 +08:00
|
|
|
.detail("TerminateKey", ks->getKey())
|
|
|
|
.detail("TerminateOffset", ks->offset);
|
2021-01-30 03:45:52 +08:00
|
|
|
// If still not normalized after moving to the boundary,
|
2020-12-05 06:58:40 +08:00
|
|
|
// let key selector clamp up to the boundary
|
|
|
|
if (ks->offset < 1) {
|
2020-05-12 15:42:43 +08:00
|
|
|
result->readToBegin = true;
|
2020-12-05 06:58:40 +08:00
|
|
|
ks->setKey(boundary.begin);
|
2021-01-30 03:45:52 +08:00
|
|
|
} else {
|
2020-05-12 15:42:43 +08:00
|
|
|
result->readThroughEnd = true;
|
2020-12-05 06:58:40 +08:00
|
|
|
ks->setKey(boundary.end);
|
|
|
|
}
|
2020-05-12 15:42:43 +08:00
|
|
|
ks->offset = 1;
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2020-07-07 05:02:22 +08:00
|
|
|
SpecialKeySpace::SpecialKeySpace(KeyRef spaceStartKey, KeyRef spaceEndKey, bool testOnly)
|
|
|
|
: range(KeyRangeRef(spaceStartKey, spaceEndKey)), readImpls(nullptr, spaceEndKey), writeImpls(nullptr, spaceEndKey),
|
|
|
|
modules(testOnly ? SpecialKeySpace::MODULE::TESTONLY : SpecialKeySpace::MODULE::UNKNOWN, spaceEndKey) {
|
|
|
|
// Default begin of KeyRangeMap is Key(), insert the range to update start key
|
|
|
|
readImpls.insert(range, nullptr);
|
|
|
|
writeImpls.insert(range, nullptr);
|
|
|
|
if (!testOnly) modulesBoundaryInit(); // testOnly is used in the correctness workload
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialKeySpace::modulesBoundaryInit() {
|
|
|
|
for (const auto& pair : moduleToBoundary) {
|
|
|
|
ASSERT(range.contains(pair.second));
|
|
|
|
// Make sure the module is not overlapping with any registered read modules
|
|
|
|
// Note: same like ranges, one module's end cannot be another module's start, relax the condition if needed
|
|
|
|
ASSERT(modules.rangeContaining(pair.second.begin) == modules.rangeContaining(pair.second.end) &&
|
|
|
|
modules[pair.second.begin] == SpecialKeySpace::MODULE::UNKNOWN);
|
|
|
|
modules.insert(pair.second, pair.first);
|
|
|
|
// Note: Due to underlying implementation, the insertion here is important to make cross_module_read being
|
|
|
|
// handled correctly
|
|
|
|
readImpls.insert(pair.second, nullptr);
|
|
|
|
writeImpls.insert(pair.second, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 02:13:55 +08:00
|
|
|
ACTOR Future<Standalone<RangeResultRef>> SpecialKeySpace::checkRYWValid(SpecialKeySpace* sks,
|
|
|
|
ReadYourWritesTransaction* ryw,
|
|
|
|
KeySelector begin, KeySelector end,
|
|
|
|
GetRangeLimits limits, bool reverse) {
|
|
|
|
ASSERT(ryw);
|
2020-05-21 04:55:07 +08:00
|
|
|
choose {
|
2020-06-18 01:33:52 +08:00
|
|
|
when(Standalone<RangeResultRef> result =
|
2020-05-21 04:55:07 +08:00
|
|
|
wait(SpecialKeySpace::getRangeAggregationActor(sks, ryw, begin, end, limits, reverse))) {
|
2020-06-18 01:33:52 +08:00
|
|
|
return result;
|
2020-05-15 14:49:57 +08:00
|
|
|
}
|
2020-06-18 02:13:55 +08:00
|
|
|
when(wait(ryw->resetFuture())) { throw internal_error(); }
|
2020-05-06 04:07:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-18 01:33:52 +08:00
|
|
|
ACTOR Future<Standalone<RangeResultRef>> SpecialKeySpace::getRangeAggregationActor(SpecialKeySpace* sks,
|
|
|
|
ReadYourWritesTransaction* ryw,
|
|
|
|
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;
|
2020-06-12 03:22:19 +08:00
|
|
|
state Standalone<RangeResultRef> pairs;
|
2020-07-15 01:36:47 +08:00
|
|
|
state RangeMap<Key, SpecialKeyRangeReadImpl*, KeyRangeRef>::iterator iter;
|
2020-03-31 14:27:09 +08:00
|
|
|
state int actualBeginOffset;
|
|
|
|
state int actualEndOffset;
|
2020-06-18 01:33:52 +08:00
|
|
|
state KeyRangeRef moduleBoundary;
|
2020-06-12 03:22:19 +08:00
|
|
|
// used to cache result from potential first read
|
|
|
|
state Optional<Standalone<RangeResultRef>> cache;
|
2020-04-03 15:26:11 +08:00
|
|
|
|
2020-06-18 03:47:54 +08:00
|
|
|
if (ryw->specialKeySpaceRelaxed()) {
|
|
|
|
moduleBoundary = sks->range;
|
|
|
|
} else {
|
2020-06-18 01:33:52 +08:00
|
|
|
auto beginIter = sks->getModules().rangeContaining(begin.getKey());
|
2020-06-18 03:47:54 +08:00
|
|
|
if (beginIter->begin() <= end.getKey() && end.getKey() <= beginIter->end()) {
|
2020-06-18 01:33:52 +08:00
|
|
|
if (beginIter->value() == SpecialKeySpace::MODULE::UNKNOWN)
|
|
|
|
throw special_keys_no_module_found();
|
|
|
|
else
|
|
|
|
moduleBoundary = beginIter->range();
|
|
|
|
} else {
|
|
|
|
TraceEvent(SevInfo, "SpecialKeyCrossModuleRead")
|
|
|
|
.detail("Begin", begin.toString())
|
|
|
|
.detail("End", end.toString())
|
|
|
|
.detail("BoundaryBegin", beginIter->begin())
|
|
|
|
.detail("BoundaryEnd", beginIter->end());
|
|
|
|
throw special_keys_cross_module_read();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wait(normalizeKeySelectorActor(sks, ryw, &begin, moduleBoundary, &actualBeginOffset, &result, &cache));
|
|
|
|
wait(normalizeKeySelectorActor(sks, ryw, &end, moduleBoundary, &actualEndOffset, &result, &cache));
|
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-11-11 05:01:07 +08:00
|
|
|
TEST(true); // inverted range
|
2020-06-18 01:33:52 +08:00
|
|
|
return RangeResultRef(false, false);
|
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-06-18 01:33:52 +08:00
|
|
|
if (begin.getKey() == moduleBoundary.end || end.getKey() == moduleBoundary.begin) {
|
2020-11-11 05:01:07 +08:00
|
|
|
TEST(true); // query touches begin or end
|
2020-06-18 01:33:52 +08:00
|
|
|
return result;
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
2020-06-16 03:47:27 +08:00
|
|
|
state RangeMap<Key, SpecialKeyRangeReadImpl*, KeyRangeRef>::Ranges ranges =
|
2020-06-15 14:11:57 +08:00
|
|
|
sks->getReadImpls().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;
|
|
|
|
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;
|
2020-06-12 03:22:19 +08:00
|
|
|
if (iter->value()->isAsync() && cache.present()) {
|
|
|
|
const SpecialKeyRangeAsyncImpl* ptr = dynamic_cast<const SpecialKeyRangeAsyncImpl*>(iter->value());
|
|
|
|
Standalone<RangeResultRef> pairs_ = wait(ptr->getRange(ryw, KeyRangeRef(keyStart, keyEnd), &cache));
|
|
|
|
pairs = pairs_;
|
|
|
|
} else {
|
|
|
|
Standalone<RangeResultRef> pairs_ = wait(iter->value()->getRange(ryw, KeyRangeRef(keyStart, keyEnd)));
|
|
|
|
pairs = pairs_;
|
|
|
|
}
|
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-07-31 09:58:09 +08:00
|
|
|
ASSERT(iter->range().contains(pairs[i].key));
|
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-06-18 01:33:52 +08:00
|
|
|
return result;
|
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) {
|
|
|
|
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;
|
2020-06-12 03:22:19 +08:00
|
|
|
if (iter->value()->isAsync() && cache.present()) {
|
|
|
|
const SpecialKeyRangeAsyncImpl* ptr = dynamic_cast<const SpecialKeyRangeAsyncImpl*>(iter->value());
|
|
|
|
Standalone<RangeResultRef> pairs_ = wait(ptr->getRange(ryw, KeyRangeRef(keyStart, keyEnd), &cache));
|
|
|
|
pairs = pairs_;
|
|
|
|
} else {
|
|
|
|
Standalone<RangeResultRef> pairs_ = wait(iter->value()->getRange(ryw, KeyRangeRef(keyStart, keyEnd)));
|
|
|
|
pairs = pairs_;
|
|
|
|
}
|
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-07-31 09:58:09 +08:00
|
|
|
ASSERT(iter->range().contains(pairs[i].key));
|
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-06-18 01:33:52 +08:00
|
|
|
return result;
|
2020-03-31 14:27:09 +08:00
|
|
|
};
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-18 01:33:52 +08:00
|
|
|
return result;
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
|
|
|
|
2020-05-21 04:55:07 +08:00
|
|
|
Future<Standalone<RangeResultRef>> SpecialKeySpace::getRange(ReadYourWritesTransaction* ryw, KeySelector begin,
|
|
|
|
KeySelector end, GetRangeLimits limits, 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-06-18 02:13:55 +08:00
|
|
|
return checkRYWValid(this, ryw, begin, end, limits, reverse);
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
|
|
|
|
2020-05-21 04:55:07 +08:00
|
|
|
ACTOR Future<Optional<Value>> SpecialKeySpace::getActor(SpecialKeySpace* sks, 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-05-21 04:55:07 +08:00
|
|
|
Future<Optional<Value>> SpecialKeySpace::get(ReadYourWritesTransaction* ryw, const Key& key) {
|
2020-04-09 04:38:12 +08:00
|
|
|
return getActor(this, ryw, key);
|
2020-03-04 10:35:24 +08:00
|
|
|
}
|
|
|
|
|
2020-07-07 05:02:22 +08:00
|
|
|
void SpecialKeySpace::set(ReadYourWritesTransaction* ryw, const KeyRef& key, const ValueRef& value) {
|
2020-07-16 07:15:43 +08:00
|
|
|
if (!ryw->specialKeySpaceChangeConfiguration()) throw special_keys_write_disabled();
|
2020-07-07 05:02:22 +08:00
|
|
|
auto impl = writeImpls[key];
|
2020-07-18 03:36:50 +08:00
|
|
|
if (impl == nullptr) {
|
|
|
|
TraceEvent(SevDebug, "SpecialKeySpaceNoWriteModuleFound")
|
2020-07-18 06:16:28 +08:00
|
|
|
.detail("Key", key.toString())
|
|
|
|
.detail("Value", value.toString());
|
2020-07-18 03:36:50 +08:00
|
|
|
throw special_keys_no_write_module_found();
|
|
|
|
}
|
2020-07-07 05:02:22 +08:00
|
|
|
return impl->set(ryw, key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialKeySpace::clear(ReadYourWritesTransaction* ryw, const KeyRangeRef& range) {
|
2020-07-16 07:15:43 +08:00
|
|
|
if (!ryw->specialKeySpaceChangeConfiguration()) throw special_keys_write_disabled();
|
2020-07-07 05:02:22 +08:00
|
|
|
if (range.empty()) return;
|
|
|
|
auto begin = writeImpls[range.begin];
|
2020-07-15 02:56:40 +08:00
|
|
|
auto end = writeImpls.rangeContainingKeyBefore(range.end)->value();
|
|
|
|
if (begin != end) {
|
|
|
|
TraceEvent(SevDebug, "SpecialKeySpaceCrossModuleClear").detail("Range", range.toString());
|
2020-07-07 05:02:22 +08:00
|
|
|
throw special_keys_cross_module_clear(); // ban cross module clear
|
2020-07-16 07:15:43 +08:00
|
|
|
} else if (begin == nullptr) {
|
2020-07-15 02:56:40 +08:00
|
|
|
TraceEvent(SevDebug, "SpecialKeySpaceNoWriteModuleFound").detail("Range", range.toString());
|
2020-07-07 05:02:22 +08:00
|
|
|
throw special_keys_no_write_module_found();
|
2020-07-15 02:56:40 +08:00
|
|
|
}
|
2020-07-07 05:02:22 +08:00
|
|
|
return begin->clear(ryw, range);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpecialKeySpace::clear(ReadYourWritesTransaction* ryw, const KeyRef& key) {
|
2020-07-16 07:15:43 +08:00
|
|
|
if (!ryw->specialKeySpaceChangeConfiguration()) throw special_keys_write_disabled();
|
2020-07-07 05:02:22 +08:00
|
|
|
auto impl = writeImpls[key];
|
|
|
|
if (impl == nullptr) throw special_keys_no_write_module_found();
|
|
|
|
return impl->clear(ryw, key);
|
|
|
|
}
|
|
|
|
|
2020-10-23 09:25:51 +08:00
|
|
|
bool validateSnakeCaseNaming(const KeyRef& k) {
|
|
|
|
KeyRef key(k);
|
|
|
|
// Remove prefix \xff\xff
|
|
|
|
ASSERT(key.startsWith(specialKeys.begin));
|
|
|
|
key = key.removePrefix(specialKeys.begin);
|
|
|
|
// Suffix can be \xff\xff or \x00 in single key range
|
|
|
|
if (key.endsWith(specialKeys.begin))
|
|
|
|
key = key.removeSuffix(specialKeys.end);
|
|
|
|
else if (key.endsWith(LiteralStringRef("\x00")))
|
|
|
|
key = key.removeSuffix(LiteralStringRef("\x00"));
|
|
|
|
for (const char& c : key.toString()) {
|
|
|
|
// only small letters, numbers, '/', '_' is allowed
|
|
|
|
ASSERT((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '/' || c == '_');
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-28 03:29:03 +08:00
|
|
|
void SpecialKeySpace::registerKeyRange(SpecialKeySpace::MODULE module, SpecialKeySpace::IMPLTYPE type,
|
|
|
|
const KeyRangeRef& kr, SpecialKeyRangeReadImpl* impl) {
|
2020-07-07 05:02:22 +08:00
|
|
|
// module boundary check
|
2020-10-23 09:25:51 +08:00
|
|
|
if (module == SpecialKeySpace::MODULE::TESTONLY) {
|
2020-07-07 07:42:42 +08:00
|
|
|
ASSERT(normalKeys.contains(kr));
|
2020-10-23 09:27:58 +08:00
|
|
|
} else {
|
2020-07-07 05:02:22 +08:00
|
|
|
ASSERT(moduleToBoundary.at(module).contains(kr));
|
2020-10-23 09:25:51 +08:00
|
|
|
ASSERT(validateSnakeCaseNaming(kr.begin) &&
|
|
|
|
validateSnakeCaseNaming(kr.end)); // validate keys follow snake case naming style
|
|
|
|
}
|
2020-07-07 05:02:22 +08:00
|
|
|
// make sure the registered range is not overlapping with existing ones
|
|
|
|
// Note: kr.end should not be the same as another range's begin, although it should work even they are the same
|
|
|
|
for (auto iter = readImpls.rangeContaining(kr.begin); true; ++iter) {
|
|
|
|
ASSERT(iter->value() == nullptr);
|
|
|
|
if (iter == readImpls.rangeContaining(kr.end))
|
|
|
|
break; // Note: relax the condition that the end can be another range's start, if needed
|
|
|
|
}
|
|
|
|
readImpls.insert(kr, impl);
|
|
|
|
// if rw, it means the module can do both read and write
|
2020-07-28 03:29:03 +08:00
|
|
|
if (type == SpecialKeySpace::IMPLTYPE::READWRITE) {
|
2020-07-07 05:02:22 +08:00
|
|
|
// since write impls are always subset of read impls,
|
|
|
|
// no need to check overlapped registration
|
|
|
|
auto rwImpl = dynamic_cast<SpecialKeyRangeRWImpl*>(impl);
|
|
|
|
ASSERT(rwImpl);
|
|
|
|
writeImpls.insert(kr, rwImpl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-18 06:16:28 +08:00
|
|
|
Key SpecialKeySpace::decode(const KeyRef& key) {
|
|
|
|
auto impl = writeImpls[key];
|
|
|
|
ASSERT(impl != nullptr);
|
|
|
|
return impl->decode(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyRange SpecialKeySpace::decode(const KeyRangeRef& kr) {
|
|
|
|
// Only allow to decode key range in the same underlying impl range
|
|
|
|
auto begin = writeImpls.rangeContaining(kr.begin);
|
|
|
|
ASSERT(begin->value() != nullptr);
|
|
|
|
auto end = writeImpls.rangeContainingKeyBefore(kr.end);
|
|
|
|
ASSERT(begin == end);
|
|
|
|
return KeyRangeRef(begin->value()->decode(kr.begin), begin->value()->decode(kr.end));
|
|
|
|
}
|
|
|
|
|
2021-01-30 03:45:52 +08:00
|
|
|
// Sometimes, we need to force impl::commit to execute without write to the underlying key range
|
|
|
|
void precommitUpdate(SpecialKeySpace* sks, ReadYourWritesTransaction* ryw,
|
|
|
|
std::set<SpecialKeyRangeRWImpl*>& writeModulePtrs) {
|
|
|
|
// if "coordinators/auto" is set, make sure we execute CoordinatorsImpl::commit
|
|
|
|
Key option_key = SpecialKeySpace::getManagementApiCommandOptionSpecialKey("coordinators", "auto");
|
|
|
|
auto entry = ryw->getSpecialKeySpaceWriteMap()[option_key];
|
|
|
|
if (entry.first) {
|
|
|
|
writeModulePtrs.insert(
|
|
|
|
sks->getRWImpls().rangeContaining(SpecialKeySpace::getManagementApiCommandPrefix("coordinators"))->value());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 02:21:03 +08:00
|
|
|
ACTOR Future<Void> commitActor(SpecialKeySpace* sks, ReadYourWritesTransaction* ryw) {
|
2020-06-28 03:22:32 +08:00
|
|
|
state RangeMap<Key, std::pair<bool, Optional<Value>>, KeyRangeRef>::Ranges ranges =
|
|
|
|
ryw->getSpecialKeySpaceWriteMap().containedRanges(specialKeys);
|
2020-07-15 01:36:47 +08:00
|
|
|
state RangeMap<Key, std::pair<bool, Optional<Value>>, KeyRangeRef>::iterator iter = ranges.begin();
|
2020-06-24 02:21:03 +08:00
|
|
|
state std::set<SpecialKeyRangeRWImpl*> writeModulePtrs;
|
2021-01-30 03:45:52 +08:00
|
|
|
precommitUpdate(sks, ryw, writeModulePtrs);
|
2020-06-24 02:21:03 +08:00
|
|
|
while (iter != ranges.end()) {
|
|
|
|
std::pair<bool, Optional<Value>> entry = iter->value();
|
|
|
|
if (entry.first) {
|
2020-07-17 09:15:35 +08:00
|
|
|
auto modulePtr = sks->getRWImpls().rangeContaining(iter->begin())->value();
|
|
|
|
writeModulePtrs.insert(modulePtr);
|
2020-06-24 02:21:03 +08:00
|
|
|
}
|
2020-07-07 02:02:48 +08:00
|
|
|
++iter;
|
2020-06-24 02:21:03 +08:00
|
|
|
}
|
|
|
|
state std::set<SpecialKeyRangeRWImpl*>::const_iterator it;
|
2020-06-28 03:22:32 +08:00
|
|
|
for (it = writeModulePtrs.begin(); it != writeModulePtrs.end(); ++it) {
|
|
|
|
Optional<std::string> msg = wait((*it)->commit(ryw));
|
|
|
|
if (msg.present()) {
|
|
|
|
ryw->setSpecialKeySpaceErrorMsg(msg.get());
|
2020-07-17 09:15:35 +08:00
|
|
|
TraceEvent(SevDebug, "SpecialKeySpaceManagemetnAPIError")
|
|
|
|
.detail("Reason", msg.get())
|
|
|
|
.detail("Range", (*it)->getKeyRange().toString());
|
2020-07-31 09:58:09 +08:00
|
|
|
throw special_keys_api_failure();
|
2020-06-28 03:22:32 +08:00
|
|
|
}
|
|
|
|
}
|
2020-06-24 02:21:03 +08:00
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Void> SpecialKeySpace::commit(ReadYourWritesTransaction* ryw) {
|
|
|
|
return commitActor(this, ryw);
|
|
|
|
}
|
|
|
|
|
2020-09-09 02:08:48 +08:00
|
|
|
SKSCTestImpl::SKSCTestImpl(KeyRangeRef kr) : SpecialKeyRangeRWImpl(kr) {}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> SKSCTestImpl::getRange(ReadYourWritesTransaction* ryw, KeyRangeRef kr) const {
|
|
|
|
ASSERT(range.contains(kr));
|
|
|
|
auto resultFuture = ryw->getRange(kr, CLIENT_KNOBS->TOO_MANY);
|
|
|
|
// all keys are written to RYW, since GRV is set, the read should happen locally
|
|
|
|
ASSERT(resultFuture.isReady());
|
|
|
|
auto result = resultFuture.getValue();
|
|
|
|
ASSERT(!result.more && result.size() < CLIENT_KNOBS->TOO_MANY);
|
|
|
|
auto kvs = resultFuture.getValue();
|
|
|
|
return rywGetRange(ryw, kr, kvs);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Optional<std::string>> SKSCTestImpl::commit(ReadYourWritesTransaction* ryw) {
|
|
|
|
ASSERT(false);
|
|
|
|
return Optional<std::string>();
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:47:27 +08:00
|
|
|
ReadConflictRangeImpl::ReadConflictRangeImpl(KeyRangeRef kr) : SpecialKeyRangeReadImpl(kr) {}
|
2020-04-29 00:00:06 +08:00
|
|
|
|
2020-05-21 04:55:07 +08:00
|
|
|
ACTOR static Future<Standalone<RangeResultRef>> getReadConflictRangeImpl(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
|
|
|
}
|
|
|
|
|
2020-05-21 04:55:07 +08:00
|
|
|
Future<Standalone<RangeResultRef>> ReadConflictRangeImpl::getRange(ReadYourWritesTransaction* ryw,
|
2020-04-29 00:00:06 +08:00
|
|
|
KeyRangeRef kr) const {
|
|
|
|
return getReadConflictRangeImpl(ryw, kr);
|
|
|
|
}
|
|
|
|
|
2020-06-16 03:47:27 +08:00
|
|
|
WriteConflictRangeImpl::WriteConflictRangeImpl(KeyRangeRef kr) : SpecialKeyRangeReadImpl(kr) {}
|
2020-04-29 01:34:10 +08:00
|
|
|
|
2020-05-21 04:55:07 +08:00
|
|
|
Future<Standalone<RangeResultRef>> WriteConflictRangeImpl::getRange(ReadYourWritesTransaction* ryw,
|
2020-04-29 01:34:10 +08:00
|
|
|
KeyRangeRef kr) const {
|
2020-04-30 05:43:37 +08:00
|
|
|
return ryw->getWriteConflictRangeIntersecting(kr);
|
2020-04-29 01:34:10 +08:00
|
|
|
}
|
|
|
|
|
2020-06-16 03:47:27 +08:00
|
|
|
ConflictingKeysImpl::ConflictingKeysImpl(KeyRangeRef kr) : SpecialKeyRangeReadImpl(kr) {}
|
2020-04-07 04:23:41 +08:00
|
|
|
|
2020-05-21 04:55:07 +08:00
|
|
|
Future<Standalone<RangeResultRef>> ConflictingKeysImpl::getRange(ReadYourWritesTransaction* ryw, 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-07-25 07:31:16 +08:00
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(it->begin(), it->value()));
|
2020-04-07 04:23:41 +08:00
|
|
|
}
|
|
|
|
if (endIter->begin() != kr.end)
|
2020-07-25 07:31:16 +08:00
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(endIter->begin(), endIter->value()));
|
2020-04-07 04:23:41 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2020-06-12 03:22:19 +08:00
|
|
|
ACTOR Future<Standalone<RangeResultRef>> ddMetricsGetRangeActor(ReadYourWritesTransaction* ryw, KeyRangeRef kr) {
|
2020-10-20 09:16:09 +08:00
|
|
|
loop {
|
|
|
|
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) {
|
|
|
|
// each begin key is the previous end key, thus we only encode the begin key in the result
|
|
|
|
KeyRef beginKey = ddMetricsRef.beginKey.withPrefix(ddStatsRange.begin, result.arena());
|
|
|
|
// Use json string encoded in utf-8 to encode the values, easy for adding more fields in the future
|
|
|
|
json_spirit::mObject statsObj;
|
|
|
|
statsObj["shard_bytes"] = ddMetricsRef.shardBytes;
|
|
|
|
std::string statsString =
|
|
|
|
json_spirit::write_string(json_spirit::mValue(statsObj), json_spirit::Output_options::raw_utf8);
|
|
|
|
ValueRef bytes(result.arena(), statsString);
|
|
|
|
result.push_back(result.arena(), KeyValueRef(beginKey, bytes));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} catch (Error& e) {
|
2020-10-22 07:58:23 +08:00
|
|
|
state Error err(e);
|
2020-11-20 05:17:38 +08:00
|
|
|
if (e.code() == error_code_dd_not_found) {
|
2020-10-20 09:16:09 +08:00
|
|
|
TraceEvent(SevWarnAlways, "DataDistributorNotPresent")
|
|
|
|
.detail("Operation", "DDMetricsReqestThroughSpecialKeys");
|
2020-10-22 07:58:23 +08:00
|
|
|
wait(delayJittered(FLOW_KNOBS->PREVENT_FAST_SPIN_DELAY));
|
2020-10-20 09:16:09 +08:00
|
|
|
continue;
|
|
|
|
}
|
2020-10-22 07:58:23 +08:00
|
|
|
throw err;
|
2020-05-19 01:38:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 03:22:19 +08:00
|
|
|
DDStatsRangeImpl::DDStatsRangeImpl(KeyRangeRef kr) : SpecialKeyRangeAsyncImpl(kr) {}
|
2020-05-19 01:38:23 +08:00
|
|
|
|
2020-05-22 02:26:34 +08:00
|
|
|
Future<Standalone<RangeResultRef>> DDStatsRangeImpl::getRange(ReadYourWritesTransaction* ryw, KeyRangeRef kr) const {
|
2020-06-12 03:22:19 +08:00
|
|
|
return ddMetricsGetRangeActor(ryw, kr);
|
2020-05-19 01:38:23 +08:00
|
|
|
}
|
2020-06-15 13:29:44 +08:00
|
|
|
|
2020-07-18 03:32:42 +08:00
|
|
|
Key SpecialKeySpace::getManagementApiCommandOptionSpecialKey(const std::string& command, const std::string& option) {
|
2020-07-18 06:16:28 +08:00
|
|
|
Key prefix = LiteralStringRef("options/").withPrefix(moduleToBoundary[MODULE::MANAGEMENT].begin);
|
2020-07-18 03:32:42 +08:00
|
|
|
auto pair = command + "/" + option;
|
|
|
|
ASSERT(options.find(pair) != options.end());
|
|
|
|
return prefix.withSuffix(pair);
|
|
|
|
}
|
2020-07-17 09:15:35 +08:00
|
|
|
|
|
|
|
ManagementCommandsOptionsImpl::ManagementCommandsOptionsImpl(KeyRangeRef kr) : SpecialKeyRangeRWImpl(kr) {}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> ManagementCommandsOptionsImpl::getRange(ReadYourWritesTransaction* ryw,
|
|
|
|
KeyRangeRef kr) const {
|
|
|
|
Standalone<RangeResultRef> result;
|
|
|
|
// Since we only have limit number of options, a brute force loop here is enough
|
2020-07-29 02:39:29 +08:00
|
|
|
for (const auto& option : SpecialKeySpace::getManagementApiOptionsSet()) {
|
2020-07-17 09:15:35 +08:00
|
|
|
auto key = getKeyRange().begin.withSuffix(option);
|
|
|
|
// ignore all invalid keys
|
|
|
|
auto r = ryw->getSpecialKeySpaceWriteMap()[key];
|
2020-08-06 04:18:39 +08:00
|
|
|
if (kr.contains(key) && r.first && r.second.present()) {
|
2020-07-17 09:15:35 +08:00
|
|
|
result.push_back(result.arena(), KeyValueRef(key, ValueRef()));
|
2020-08-06 04:18:39 +08:00
|
|
|
result.arena().dependsOn(key.arena());
|
|
|
|
}
|
2020-07-17 09:15:35 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ManagementCommandsOptionsImpl::set(ReadYourWritesTransaction* ryw, const KeyRef& key, const ValueRef& value) {
|
|
|
|
std::string option = key.removePrefix(getKeyRange().begin).toString();
|
|
|
|
// ignore all invalid keys
|
2020-07-29 02:39:29 +08:00
|
|
|
if (SpecialKeySpace::getManagementApiOptionsSet().find(option) !=
|
|
|
|
SpecialKeySpace::getManagementApiOptionsSet().end()) {
|
2020-07-18 03:32:42 +08:00
|
|
|
TraceEvent(SevDebug, "ManagementApiOption").detail("Option", option).detail("Key", key);
|
2020-07-17 09:15:35 +08:00
|
|
|
ryw->getSpecialKeySpaceWriteMap().insert(key, std::make_pair(true, Optional<Value>(value)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ManagementCommandsOptionsImpl::clear(ReadYourWritesTransaction* ryw, const KeyRangeRef& range) {
|
2020-07-29 04:32:31 +08:00
|
|
|
ryw->getSpecialKeySpaceWriteMap().rawErase(range);
|
2020-07-17 09:15:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ManagementCommandsOptionsImpl::clear(ReadYourWritesTransaction* ryw, const KeyRef& key) {
|
|
|
|
std::string option = key.removePrefix(getKeyRange().begin).toString();
|
|
|
|
// ignore all invalid keys
|
2020-07-29 02:39:29 +08:00
|
|
|
if (SpecialKeySpace::getManagementApiOptionsSet().find(option) !=
|
|
|
|
SpecialKeySpace::getManagementApiOptionsSet().end()) {
|
2020-07-17 09:15:35 +08:00
|
|
|
ryw->getSpecialKeySpaceWriteMap().rawErase(singleKeyRange(key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Optional<std::string>> ManagementCommandsOptionsImpl::commit(ReadYourWritesTransaction* ryw) {
|
|
|
|
// Nothing to do, keys should be used by other impls' commit callback
|
|
|
|
return Optional<std::string>();
|
|
|
|
}
|
|
|
|
|
2020-09-05 05:54:32 +08:00
|
|
|
Standalone<RangeResultRef> rywGetRange(ReadYourWritesTransaction* ryw, const KeyRangeRef& kr,
|
|
|
|
const Standalone<RangeResultRef>& res) {
|
|
|
|
// "res" is the read result regardless of your writes, if ryw disabled, return immediately
|
|
|
|
if (ryw->readYourWritesDisabled()) return res;
|
|
|
|
// If ryw enabled, we update it with writes from the transaction
|
2020-06-24 02:21:03 +08:00
|
|
|
Standalone<RangeResultRef> result;
|
2020-09-05 05:54:32 +08:00
|
|
|
RangeMap<Key, std::pair<bool, Optional<Value>>, KeyRangeRef>::Ranges ranges =
|
|
|
|
ryw->getSpecialKeySpaceWriteMap().containedRanges(kr);
|
|
|
|
RangeMap<Key, std::pair<bool, Optional<Value>>, KeyRangeRef>::iterator iter = ranges.begin();
|
|
|
|
auto iter2 = res.begin();
|
|
|
|
result.arena().dependsOn(res.arena());
|
|
|
|
while (iter != ranges.end() || iter2 != res.end()) {
|
|
|
|
if (iter == ranges.end()) {
|
|
|
|
result.push_back(result.arena(), KeyValueRef(iter2->key, iter2->value));
|
|
|
|
++iter2;
|
|
|
|
} else if (iter2 == res.end()) {
|
|
|
|
// insert if it is a set entry
|
|
|
|
std::pair<bool, Optional<Value>> entry = iter->value();
|
|
|
|
if (entry.first && entry.second.present()) {
|
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(iter->begin(), entry.second.get()));
|
2020-06-24 02:21:03 +08:00
|
|
|
}
|
2020-09-05 05:54:32 +08:00
|
|
|
++iter;
|
|
|
|
} else if (iter->range().contains(iter2->key)) {
|
2020-06-28 03:22:32 +08:00
|
|
|
std::pair<bool, Optional<Value>> entry = iter->value();
|
2020-09-05 05:54:32 +08:00
|
|
|
// if this is a valid range either for set or clear, move iter2 outside the range
|
2020-06-24 02:21:03 +08:00
|
|
|
if (entry.first) {
|
2020-09-05 05:54:32 +08:00
|
|
|
// insert if this is a set entry
|
|
|
|
if (entry.second.present())
|
2020-07-31 17:18:49 +08:00
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(iter->begin(), entry.second.get()));
|
2020-09-05 05:54:32 +08:00
|
|
|
// move iter2 outside the range
|
|
|
|
while (iter2 != res.end() && iter->range().contains(iter2->key)) ++iter2;
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
} else if (iter->begin() > iter2->key) {
|
|
|
|
result.push_back(result.arena(), KeyValueRef(iter2->key, iter2->value));
|
|
|
|
++iter2;
|
|
|
|
} else if (iter->end() <= iter2->key) {
|
|
|
|
// insert if it is a set entry
|
|
|
|
std::pair<bool, Optional<Value>> entry = iter->value();
|
|
|
|
if (entry.first && entry.second.present()) {
|
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(iter->begin(), entry.second.get()));
|
2020-06-24 02:21:03 +08:00
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 13:29:44 +08:00
|
|
|
return result;
|
|
|
|
}
|
2020-06-15 13:39:20 +08:00
|
|
|
|
2020-09-05 05:54:32 +08:00
|
|
|
// read from those readwrite modules in which special keys have one-to-one mapping with real persisted keys
|
|
|
|
ACTOR Future<Standalone<RangeResultRef>> rwModuleWithMappingGetRangeActor(ReadYourWritesTransaction* ryw,
|
|
|
|
const SpecialKeyRangeRWImpl* impl,
|
|
|
|
KeyRangeRef kr) {
|
|
|
|
Standalone<RangeResultRef> resultWithoutPrefix =
|
|
|
|
wait(ryw->getTransaction().getRange(ryw->getDatabase()->specialKeySpace->decode(kr), CLIENT_KNOBS->TOO_MANY));
|
|
|
|
ASSERT(!resultWithoutPrefix.more && resultWithoutPrefix.size() < CLIENT_KNOBS->TOO_MANY);
|
|
|
|
Standalone<RangeResultRef> result;
|
|
|
|
for (const KeyValueRef& kv : resultWithoutPrefix)
|
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(impl->encode(kv.key), kv.value));
|
|
|
|
return rywGetRange(ryw, kr, result);
|
|
|
|
}
|
|
|
|
|
2020-06-24 02:21:03 +08:00
|
|
|
ExcludeServersRangeImpl::ExcludeServersRangeImpl(KeyRangeRef kr) : SpecialKeyRangeRWImpl(kr) {}
|
2020-06-15 13:39:20 +08:00
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> ExcludeServersRangeImpl::getRange(ReadYourWritesTransaction* ryw,
|
|
|
|
KeyRangeRef kr) const {
|
2020-09-05 05:54:32 +08:00
|
|
|
return rwModuleWithMappingGetRangeActor(ryw, this, kr);
|
2020-06-24 02:21:03 +08:00
|
|
|
}
|
|
|
|
|
2020-09-05 06:21:47 +08:00
|
|
|
void ExcludeServersRangeImpl::set(ReadYourWritesTransaction* ryw, const KeyRef& key, const ValueRef& value) {
|
|
|
|
// ignore value
|
|
|
|
ryw->getSpecialKeySpaceWriteMap().insert(key, std::make_pair(true, Optional<Value>(ValueRef())));
|
|
|
|
}
|
|
|
|
|
2020-07-31 17:18:49 +08:00
|
|
|
Key ExcludeServersRangeImpl::decode(const KeyRef& key) const {
|
|
|
|
return key.removePrefix(SpecialKeySpace::getModuleRange(SpecialKeySpace::MODULE::MANAGEMENT).begin)
|
|
|
|
.withPrefix(LiteralStringRef("\xff/conf/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Key ExcludeServersRangeImpl::encode(const KeyRef& key) const {
|
|
|
|
return key.removePrefix(LiteralStringRef("\xff/conf/"))
|
|
|
|
.withPrefix(SpecialKeySpace::getModuleRange(SpecialKeySpace::MODULE::MANAGEMENT).begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool parseNetWorkAddrFromKeys(ReadYourWritesTransaction* ryw, bool failed, std::vector<AddressExclusion>& addresses,
|
|
|
|
std::set<AddressExclusion>& exclusions, Optional<std::string>& msg) {
|
|
|
|
KeyRangeRef range = failed ? SpecialKeySpace::getManamentApiCommandRange("failed")
|
|
|
|
: SpecialKeySpace::getManamentApiCommandRange("exclude");
|
2020-07-07 02:02:48 +08:00
|
|
|
auto ranges = ryw->getSpecialKeySpaceWriteMap().containedRanges(range);
|
|
|
|
auto iter = ranges.begin();
|
|
|
|
while (iter != ranges.end()) {
|
|
|
|
auto entry = iter->value();
|
|
|
|
// only check for exclude(set) operation, include(clear) are not checked
|
2020-08-21 05:41:17 +08:00
|
|
|
TraceEvent(SevDebug, "ParseNetworkAddress")
|
2020-07-07 02:02:48 +08:00
|
|
|
.detail("Valid", entry.first)
|
|
|
|
.detail("Set", entry.second.present())
|
|
|
|
.detail("Key", iter->begin().toString());
|
|
|
|
if (entry.first && entry.second.present()) {
|
|
|
|
Key address = iter->begin().removePrefix(range.begin);
|
|
|
|
auto a = AddressExclusion::parse(address);
|
|
|
|
if (!a.isValid()) {
|
|
|
|
std::string error = "ERROR: \'" + address.toString() + "\' is not a valid network endpoint address\n";
|
|
|
|
if (address.toString().find(":tls") != std::string::npos)
|
|
|
|
error += " Do not include the `:tls' suffix when naming a process\n";
|
2020-08-01 04:31:36 +08:00
|
|
|
msg = ManagementAPIError::toJsonString(
|
|
|
|
false, entry.second.present() ? (failed ? "exclude failed" : "exclude") : "include", error);
|
2020-07-07 02:02:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
addresses.push_back(a);
|
|
|
|
exclusions.insert(a);
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<bool> checkExclusion(Database db, std::vector<AddressExclusion>* addresses,
|
2020-07-16 07:15:43 +08:00
|
|
|
std::set<AddressExclusion>* exclusions, bool markFailed, Optional<std::string>* msg) {
|
2020-07-07 02:02:48 +08:00
|
|
|
|
|
|
|
if (markFailed) {
|
|
|
|
state bool safe;
|
|
|
|
try {
|
|
|
|
bool _safe = wait(checkSafeExclusions(db, *addresses));
|
|
|
|
safe = _safe;
|
|
|
|
} catch (Error& e) {
|
2020-07-31 13:15:22 +08:00
|
|
|
if (e.code() == error_code_actor_cancelled) throw;
|
2020-07-07 02:02:48 +08:00
|
|
|
TraceEvent("CheckSafeExclusionsError").error(e);
|
|
|
|
safe = false;
|
|
|
|
}
|
|
|
|
if (!safe) {
|
2020-07-16 07:15:43 +08:00
|
|
|
std::string temp = "ERROR: It is unsafe to exclude the specified servers at this time.\n"
|
2020-07-17 09:15:35 +08:00
|
|
|
"Please check that this exclusion does not bring down an entire storage team.\n"
|
|
|
|
"Please also ensure that the exclusion will keep a majority of coordinators alive.\n"
|
2020-07-31 13:38:35 +08:00
|
|
|
"You may add more storage processes or coordinators to make the operation safe.\n"
|
2020-08-01 04:31:36 +08:00
|
|
|
"Call set(\"0xff0xff/management/failed/<ADDRESS...>\", ...) to exclude without "
|
2020-07-31 13:38:35 +08:00
|
|
|
"performing safety checks.\n";
|
2020-07-16 07:15:43 +08:00
|
|
|
*msg = ManagementAPIError::toJsonString(false, markFailed ? "exclude failed" : "exclude", temp);
|
2020-07-07 02:02:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StatusObject status = wait(StatusClient::statusFetcher(db));
|
|
|
|
state std::string errorString =
|
|
|
|
"ERROR: Could not calculate the impact of this exclude on the total free space in the cluster.\n"
|
2020-07-31 13:38:35 +08:00
|
|
|
"Please try the exclude again in 30 seconds.\n"
|
2020-08-01 04:31:36 +08:00
|
|
|
"Call set(\"0xff0xff/management/options/exclude/force\", ...) first to exclude without checking free "
|
2020-07-31 13:38:35 +08:00
|
|
|
"space.\n";
|
2020-07-07 02:02:48 +08:00
|
|
|
|
|
|
|
StatusObjectReader statusObj(status);
|
|
|
|
|
|
|
|
StatusObjectReader statusObjCluster;
|
|
|
|
if (!statusObj.get("cluster", statusObjCluster)) {
|
2020-07-16 07:15:43 +08:00
|
|
|
*msg = ManagementAPIError::toJsonString(false, markFailed ? "exclude failed" : "exclude", errorString);
|
2020-07-07 02:02:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
StatusObjectReader processesMap;
|
|
|
|
if (!statusObjCluster.get("processes", processesMap)) {
|
2020-07-16 07:15:43 +08:00
|
|
|
*msg = ManagementAPIError::toJsonString(false, markFailed ? "exclude failed" : "exclude", errorString);
|
2020-07-07 02:02:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
state int ssTotalCount = 0;
|
|
|
|
state int ssExcludedCount = 0;
|
|
|
|
state double worstFreeSpaceRatio = 1.0;
|
|
|
|
try {
|
|
|
|
for (auto proc : processesMap.obj()) {
|
|
|
|
bool storageServer = false;
|
|
|
|
StatusArray rolesArray = proc.second.get_obj()["roles"].get_array();
|
|
|
|
for (StatusObjectReader role : rolesArray) {
|
|
|
|
if (role["role"].get_str() == "storage") {
|
|
|
|
storageServer = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Skip non-storage servers in free space calculation
|
|
|
|
if (!storageServer) continue;
|
|
|
|
|
|
|
|
StatusObjectReader process(proc.second);
|
|
|
|
std::string addrStr;
|
|
|
|
if (!process.get("address", addrStr)) {
|
2020-07-16 07:15:43 +08:00
|
|
|
*msg = ManagementAPIError::toJsonString(false, markFailed ? "exclude failed" : "exclude", errorString);
|
2020-07-07 02:02:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
NetworkAddress addr = NetworkAddress::parse(addrStr);
|
|
|
|
bool excluded =
|
|
|
|
(process.has("excluded") && process.last().get_bool()) || addressExcluded(*exclusions, addr);
|
|
|
|
ssTotalCount++;
|
|
|
|
if (excluded) ssExcludedCount++;
|
|
|
|
|
|
|
|
if (!excluded) {
|
|
|
|
StatusObjectReader disk;
|
|
|
|
if (!process.get("disk", disk)) {
|
2020-07-17 09:15:35 +08:00
|
|
|
*msg =
|
|
|
|
ManagementAPIError::toJsonString(false, markFailed ? "exclude failed" : "exclude", errorString);
|
2020-07-07 02:02:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t total_bytes;
|
|
|
|
if (!disk.get("total_bytes", total_bytes)) {
|
2020-07-17 09:15:35 +08:00
|
|
|
*msg =
|
|
|
|
ManagementAPIError::toJsonString(false, markFailed ? "exclude failed" : "exclude", errorString);
|
2020-07-07 02:02:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t free_bytes;
|
|
|
|
if (!disk.get("free_bytes", free_bytes)) {
|
2020-07-17 09:15:35 +08:00
|
|
|
*msg =
|
|
|
|
ManagementAPIError::toJsonString(false, markFailed ? "exclude failed" : "exclude", errorString);
|
2020-07-07 02:02:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
worstFreeSpaceRatio = std::min(worstFreeSpaceRatio, double(free_bytes) / total_bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (...) // std::exception
|
|
|
|
{
|
2020-07-16 07:15:43 +08:00
|
|
|
*msg = ManagementAPIError::toJsonString(false, markFailed ? "exclude failed" : "exclude", errorString);
|
2020-07-07 02:02:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ssExcludedCount == ssTotalCount ||
|
|
|
|
(1 - worstFreeSpaceRatio) * ssTotalCount / (ssTotalCount - ssExcludedCount) > 0.9) {
|
2020-07-31 13:38:35 +08:00
|
|
|
std::string temp = "ERROR: This exclude may cause the total free space in the cluster to drop below 10%.\n"
|
2020-08-01 04:31:36 +08:00
|
|
|
"Call set(\"0xff0xff/management/options/exclude/force\", ...) first to exclude without "
|
2020-07-31 13:38:35 +08:00
|
|
|
"checking free space.\n";
|
2020-07-16 07:15:43 +08:00
|
|
|
*msg = ManagementAPIError::toJsonString(false, markFailed ? "exclude failed" : "exclude", temp);
|
2020-07-07 02:02:48 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void includeServers(ReadYourWritesTransaction* ryw) {
|
2020-07-16 07:15:43 +08:00
|
|
|
ryw->setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE);
|
|
|
|
ryw->setOption(FDBTransactionOptions::LOCK_AWARE);
|
|
|
|
ryw->setOption(FDBTransactionOptions::USE_PROVISIONAL_PROXIES);
|
|
|
|
// includeServers might be used in an emergency transaction, so make sure it is retry-self-conflicting and
|
|
|
|
// CAUSAL_WRITE_RISKY
|
|
|
|
ryw->setOption(FDBTransactionOptions::CAUSAL_WRITE_RISKY);
|
2020-07-07 02:02:48 +08:00
|
|
|
std::string versionKey = deterministicRandom()->randomUniqueID().toString();
|
2020-07-15 01:31:19 +08:00
|
|
|
// for exluded servers
|
2020-07-31 17:18:49 +08:00
|
|
|
auto ranges =
|
|
|
|
ryw->getSpecialKeySpaceWriteMap().containedRanges(SpecialKeySpace::getManamentApiCommandRange("exclude"));
|
2020-07-07 02:02:48 +08:00
|
|
|
auto iter = ranges.begin();
|
|
|
|
Transaction& tr = ryw->getTransaction();
|
|
|
|
while (iter != ranges.end()) {
|
|
|
|
auto entry = iter->value();
|
|
|
|
if (entry.first && !entry.second.present()) {
|
|
|
|
tr.addReadConflictRange(singleKeyRange(excludedServersVersionKey));
|
|
|
|
tr.set(excludedServersVersionKey, versionKey);
|
2020-07-18 06:16:28 +08:00
|
|
|
tr.clear(ryw->getDatabase()->specialKeySpace->decode(iter->range()));
|
2020-07-07 02:02:48 +08:00
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
2020-07-15 01:31:19 +08:00
|
|
|
// for failed servers
|
2020-07-31 17:18:49 +08:00
|
|
|
ranges = ryw->getSpecialKeySpaceWriteMap().containedRanges(SpecialKeySpace::getManamentApiCommandRange("failed"));
|
2020-07-15 01:31:19 +08:00
|
|
|
iter = ranges.begin();
|
|
|
|
while (iter != ranges.end()) {
|
|
|
|
auto entry = iter->value();
|
|
|
|
if (entry.first && !entry.second.present()) {
|
|
|
|
tr.addReadConflictRange(singleKeyRange(failedServersVersionKey));
|
|
|
|
tr.set(failedServersVersionKey, versionKey);
|
2020-07-18 06:16:28 +08:00
|
|
|
tr.clear(ryw->getDatabase()->specialKeySpace->decode(iter->range()));
|
2020-07-15 01:31:19 +08:00
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
2020-07-07 02:02:48 +08:00
|
|
|
}
|
|
|
|
|
2020-07-17 09:15:35 +08:00
|
|
|
ACTOR Future<Optional<std::string>> excludeCommitActor(ReadYourWritesTransaction* ryw, bool failed) {
|
2020-07-07 02:02:48 +08:00
|
|
|
// parse network addresses
|
|
|
|
state Optional<std::string> result;
|
|
|
|
state std::vector<AddressExclusion> addresses;
|
|
|
|
state std::set<AddressExclusion> exclusions;
|
2020-07-31 17:18:49 +08:00
|
|
|
if (!parseNetWorkAddrFromKeys(ryw, failed, addresses, exclusions, result)) return result;
|
2020-07-17 09:15:35 +08:00
|
|
|
// If force option is not set, we need to do safety check
|
2020-07-18 03:32:42 +08:00
|
|
|
auto force = ryw->getSpecialKeySpaceWriteMap()[SpecialKeySpace::getManagementApiCommandOptionSpecialKey(
|
2020-08-06 04:18:39 +08:00
|
|
|
failed ? "failed" : "excluded", "force")];
|
2020-07-17 09:15:35 +08:00
|
|
|
// only do safety check when we have servers to be excluded and the force option key is not set
|
|
|
|
if (addresses.size() && !(force.first && force.second.present())) {
|
|
|
|
bool safe = wait(checkExclusion(ryw->getDatabase(), &addresses, &exclusions, failed, &result));
|
|
|
|
if (!safe) return result;
|
|
|
|
}
|
|
|
|
excludeServers(ryw->getTransaction(), addresses, failed);
|
2020-07-07 02:02:48 +08:00
|
|
|
includeServers(ryw);
|
|
|
|
|
2020-06-28 03:22:32 +08:00
|
|
|
return result;
|
2020-06-15 13:39:20 +08:00
|
|
|
}
|
2020-07-07 02:02:48 +08:00
|
|
|
|
|
|
|
Future<Optional<std::string>> ExcludeServersRangeImpl::commit(ReadYourWritesTransaction* ryw) {
|
2020-07-17 09:15:35 +08:00
|
|
|
return excludeCommitActor(ryw, false);
|
2020-07-07 02:02:48 +08:00
|
|
|
}
|
2020-07-07 06:40:21 +08:00
|
|
|
|
|
|
|
FailedServersRangeImpl::FailedServersRangeImpl(KeyRangeRef kr) : SpecialKeyRangeRWImpl(kr) {}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> FailedServersRangeImpl::getRange(ReadYourWritesTransaction* ryw,
|
2020-07-16 07:15:43 +08:00
|
|
|
KeyRangeRef kr) const {
|
2020-09-05 05:54:32 +08:00
|
|
|
return rwModuleWithMappingGetRangeActor(ryw, this, kr);
|
2020-07-07 06:40:21 +08:00
|
|
|
}
|
|
|
|
|
2020-09-05 06:21:47 +08:00
|
|
|
void FailedServersRangeImpl::set(ReadYourWritesTransaction* ryw, const KeyRef& key, const ValueRef& value) {
|
|
|
|
// ignore value
|
|
|
|
ryw->getSpecialKeySpaceWriteMap().insert(key, std::make_pair(true, Optional<Value>(ValueRef())));
|
|
|
|
}
|
|
|
|
|
2020-07-31 17:18:49 +08:00
|
|
|
Key FailedServersRangeImpl::decode(const KeyRef& key) const {
|
|
|
|
return key.removePrefix(SpecialKeySpace::getModuleRange(SpecialKeySpace::MODULE::MANAGEMENT).begin)
|
|
|
|
.withPrefix(LiteralStringRef("\xff/conf/"));
|
|
|
|
}
|
|
|
|
|
|
|
|
Key FailedServersRangeImpl::encode(const KeyRef& key) const {
|
|
|
|
return key.removePrefix(LiteralStringRef("\xff/conf/"))
|
|
|
|
.withPrefix(SpecialKeySpace::getModuleRange(SpecialKeySpace::MODULE::MANAGEMENT).begin);
|
|
|
|
}
|
|
|
|
|
2020-07-07 06:40:21 +08:00
|
|
|
Future<Optional<std::string>> FailedServersRangeImpl::commit(ReadYourWritesTransaction* ryw) {
|
2020-07-17 09:15:35 +08:00
|
|
|
return excludeCommitActor(ryw, true);
|
2020-07-14 04:35:25 +08:00
|
|
|
}
|
|
|
|
|
2020-07-16 07:15:43 +08:00
|
|
|
ACTOR Future<Standalone<RangeResultRef>> ExclusionInProgressActor(ReadYourWritesTransaction* ryw, KeyRef prefix,
|
|
|
|
KeyRangeRef kr) {
|
2020-07-14 04:35:25 +08:00
|
|
|
state Standalone<RangeResultRef> result;
|
|
|
|
state Transaction& tr = ryw->getTransaction();
|
2020-07-16 07:15:43 +08:00
|
|
|
tr.setOption(FDBTransactionOptions::READ_SYSTEM_KEYS);
|
|
|
|
tr.setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE); // necessary?
|
|
|
|
tr.setOption(FDBTransactionOptions::LOCK_AWARE);
|
2020-07-14 04:35:25 +08:00
|
|
|
|
|
|
|
state std::vector<AddressExclusion> excl = wait((getExcludedServers(&tr)));
|
2020-07-16 07:15:43 +08:00
|
|
|
state std::set<AddressExclusion> exclusions(excl.begin(), excl.end());
|
2020-07-14 04:35:25 +08:00
|
|
|
state std::set<NetworkAddress> inProgressExclusion;
|
2020-07-16 07:15:43 +08:00
|
|
|
// Just getting a consistent read version proves that a set of tlogs satisfying the exclusions has completed
|
|
|
|
// recovery Check that there aren't any storage servers with addresses violating the exclusions
|
|
|
|
state Standalone<RangeResultRef> serverList = wait(tr.getRange(serverListKeys, CLIENT_KNOBS->TOO_MANY));
|
|
|
|
ASSERT(!serverList.more && serverList.size() < CLIENT_KNOBS->TOO_MANY);
|
|
|
|
|
|
|
|
for (auto& s : serverList) {
|
|
|
|
auto addresses = decodeServerListValue(s.value).getKeyValues.getEndpoint().addresses;
|
|
|
|
if (addressExcluded(exclusions, addresses.address)) {
|
2020-07-14 04:35:25 +08:00
|
|
|
inProgressExclusion.insert(addresses.address);
|
|
|
|
}
|
2020-07-16 07:15:43 +08:00
|
|
|
if (addresses.secondaryAddress.present() && addressExcluded(exclusions, addresses.secondaryAddress.get())) {
|
2020-07-14 04:35:25 +08:00
|
|
|
inProgressExclusion.insert(addresses.secondaryAddress.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 07:15:43 +08:00
|
|
|
Optional<Standalone<StringRef>> value = wait(tr.get(logsKey));
|
2020-07-14 04:35:25 +08:00
|
|
|
ASSERT(value.present());
|
|
|
|
auto logs = decodeLogsValue(value.get());
|
2020-07-16 07:15:43 +08:00
|
|
|
for (auto const& log : logs.first) {
|
2020-07-14 04:35:25 +08:00
|
|
|
if (log.second == NetworkAddress() || addressExcluded(exclusions, log.second)) {
|
|
|
|
inProgressExclusion.insert(log.second);
|
|
|
|
}
|
|
|
|
}
|
2020-07-16 07:15:43 +08:00
|
|
|
for (auto const& log : logs.second) {
|
2020-07-14 04:35:25 +08:00
|
|
|
if (log.second == NetworkAddress() || addressExcluded(exclusions, log.second)) {
|
|
|
|
inProgressExclusion.insert(log.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-21 16:20:14 +08:00
|
|
|
// sort and remove :tls
|
|
|
|
std::set<std::string> inProgressAddresses;
|
2020-07-16 07:15:43 +08:00
|
|
|
for (auto const& address : inProgressExclusion) {
|
2020-08-21 16:20:14 +08:00
|
|
|
inProgressAddresses.insert(formatIpPort(address.ip, address.port));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const& address : inProgressAddresses) {
|
|
|
|
Key addrKey = prefix.withSuffix(address);
|
2020-07-31 09:58:09 +08:00
|
|
|
if (kr.contains(addrKey)) {
|
|
|
|
result.push_back(result.arena(), KeyValueRef(addrKey, ValueRef()));
|
|
|
|
result.arena().dependsOn(addrKey.arena());
|
|
|
|
}
|
2020-07-14 04:35:25 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExclusionInProgressRangeImpl::ExclusionInProgressRangeImpl(KeyRangeRef kr) : SpecialKeyRangeAsyncImpl(kr) {}
|
|
|
|
|
2020-07-16 07:15:43 +08:00
|
|
|
Future<Standalone<RangeResultRef>> ExclusionInProgressRangeImpl::getRange(ReadYourWritesTransaction* ryw,
|
|
|
|
KeyRangeRef kr) const {
|
2020-07-14 04:35:25 +08:00
|
|
|
return ExclusionInProgressActor(ryw, getKeyRange().begin, kr);
|
2020-07-16 08:30:45 +08:00
|
|
|
}
|
2020-08-14 16:01:08 +08:00
|
|
|
|
2020-08-21 04:50:35 +08:00
|
|
|
ACTOR Future<Standalone<RangeResultRef>> getProcessClassActor(ReadYourWritesTransaction* ryw, KeyRef prefix,
|
|
|
|
KeyRangeRef kr) {
|
|
|
|
vector<ProcessData> _workers = wait(getWorkers(&ryw->getTransaction()));
|
|
|
|
auto workers = _workers; // strip const
|
2020-08-26 09:18:32 +08:00
|
|
|
// Note : the sort by string is anti intuition, ex. 1.1.1.1:11 < 1.1.1.1:5
|
2020-08-21 15:56:12 +08:00
|
|
|
std::sort(workers.begin(), workers.end(), [](const ProcessData& lhs, const ProcessData& rhs) {
|
|
|
|
return formatIpPort(lhs.address.ip, lhs.address.port) < formatIpPort(rhs.address.ip, rhs.address.port);
|
|
|
|
});
|
2020-08-14 16:01:08 +08:00
|
|
|
Standalone<RangeResultRef> result;
|
2020-08-21 04:50:35 +08:00
|
|
|
for (auto& w : workers) {
|
|
|
|
// exclude :tls in keys even the network addresss is TLS
|
2020-09-03 04:38:24 +08:00
|
|
|
KeyRef k(prefix.withSuffix(formatIpPort(w.address.ip, w.address.port), result.arena()));
|
2020-08-21 04:50:35 +08:00
|
|
|
if (kr.contains(k)) {
|
2020-09-03 04:38:24 +08:00
|
|
|
ValueRef v(result.arena(), w.processClass.toString());
|
2020-08-21 04:50:35 +08:00
|
|
|
result.push_back(result.arena(), KeyValueRef(k, v));
|
2020-08-14 16:01:08 +08:00
|
|
|
}
|
|
|
|
}
|
2020-09-05 05:54:32 +08:00
|
|
|
return rywGetRange(ryw, kr, result);
|
2020-08-14 16:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<Optional<std::string>> processClassCommitActor(ReadYourWritesTransaction* ryw, KeyRangeRef range) {
|
2020-08-20 08:54:38 +08:00
|
|
|
// enable related options
|
|
|
|
ryw->setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE);
|
|
|
|
ryw->setOption(FDBTransactionOptions::LOCK_AWARE);
|
|
|
|
ryw->setOption(FDBTransactionOptions::USE_PROVISIONAL_PROXIES);
|
|
|
|
vector<ProcessData> workers = wait(
|
|
|
|
getWorkers(&ryw->getTransaction())); // make sure we use the Transaction object to avoid used_during_commit()
|
|
|
|
|
2020-08-14 16:01:08 +08:00
|
|
|
auto ranges = ryw->getSpecialKeySpaceWriteMap().containedRanges(range);
|
|
|
|
auto iter = ranges.begin();
|
|
|
|
while (iter != ranges.end()) {
|
|
|
|
auto entry = iter->value();
|
2020-08-21 15:56:12 +08:00
|
|
|
// only loop through (set) operation, (clear) not exist
|
2020-08-14 16:01:08 +08:00
|
|
|
if (entry.first && entry.second.present()) {
|
2020-08-21 15:56:12 +08:00
|
|
|
// parse network address
|
2020-08-14 16:01:08 +08:00
|
|
|
Key address = iter->begin().removePrefix(range.begin);
|
2020-08-20 08:54:38 +08:00
|
|
|
AddressExclusion addr = AddressExclusion::parse(address);
|
2020-08-21 15:56:12 +08:00
|
|
|
// parse class type
|
2020-08-14 16:01:08 +08:00
|
|
|
ValueRef processClassType = entry.second.get();
|
|
|
|
ProcessClass processClass(processClassType.toString(), ProcessClass::DBSource);
|
2020-08-21 15:56:12 +08:00
|
|
|
// make sure we use the underlying Transaction object to avoid used_during_commit()
|
2020-08-20 08:54:38 +08:00
|
|
|
bool foundChange = false;
|
|
|
|
for (int i = 0; i < workers.size(); i++) {
|
|
|
|
if (addr.excludes(workers[i].address)) {
|
|
|
|
if (processClass.classType() != ProcessClass::InvalidClass)
|
|
|
|
ryw->getTransaction().set(processClassKeyFor(workers[i].locality.processId().get()),
|
|
|
|
processClassValue(processClass));
|
|
|
|
else
|
|
|
|
ryw->getTransaction().clear(processClassKeyFor(workers[i].locality.processId().get()));
|
|
|
|
foundChange = true;
|
|
|
|
}
|
2020-08-14 16:01:08 +08:00
|
|
|
}
|
2020-08-20 08:54:38 +08:00
|
|
|
if (foundChange)
|
|
|
|
ryw->getTransaction().set(processClassChangeKey, deterministicRandom()->randomUniqueID().toString());
|
2020-08-14 16:01:08 +08:00
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
2020-08-21 15:56:12 +08:00
|
|
|
return Optional<std::string>();
|
2020-08-14 16:01:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ProcessClassRangeImpl::ProcessClassRangeImpl(KeyRangeRef kr) : SpecialKeyRangeRWImpl(kr) {}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> ProcessClassRangeImpl::getRange(ReadYourWritesTransaction* ryw,
|
|
|
|
KeyRangeRef kr) const {
|
|
|
|
return getProcessClassActor(ryw, getKeyRange().begin, kr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Optional<std::string>> ProcessClassRangeImpl::commit(ReadYourWritesTransaction* ryw) {
|
2020-08-21 15:56:12 +08:00
|
|
|
// Validate network address and process class type
|
|
|
|
Optional<std::string> errorMsg;
|
|
|
|
auto ranges = ryw->getSpecialKeySpaceWriteMap().containedRanges(getKeyRange());
|
|
|
|
auto iter = ranges.begin();
|
|
|
|
while (iter != ranges.end()) {
|
|
|
|
auto entry = iter->value();
|
|
|
|
// only check for setclass(set) operation, (clear) are forbidden thus not exist
|
|
|
|
if (entry.first && entry.second.present()) {
|
|
|
|
// validate network address
|
|
|
|
Key address = iter->begin().removePrefix(range.begin);
|
|
|
|
AddressExclusion addr = AddressExclusion::parse(address);
|
|
|
|
if (!addr.isValid()) {
|
|
|
|
std::string error = "ERROR: \'" + address.toString() + "\' is not a valid network endpoint address\n";
|
|
|
|
if (address.toString().find(":tls") != std::string::npos)
|
|
|
|
error += " Do not include the `:tls' suffix when naming a process\n";
|
|
|
|
errorMsg = ManagementAPIError::toJsonString(false, "setclass", error);
|
|
|
|
return errorMsg;
|
|
|
|
}
|
|
|
|
// validate class type
|
|
|
|
ValueRef processClassType = entry.second.get();
|
|
|
|
ProcessClass processClass(processClassType.toString(), ProcessClass::DBSource);
|
|
|
|
if (processClass.classType() == ProcessClass::InvalidClass &&
|
|
|
|
processClassType != LiteralStringRef("default")) {
|
|
|
|
std::string error = "ERROR: \'" + processClassType.toString() + "\' is not a valid process class\n";
|
|
|
|
errorMsg = ManagementAPIError::toJsonString(false, "setclass", error);
|
|
|
|
return errorMsg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
++iter;
|
|
|
|
}
|
2020-08-14 16:01:08 +08:00
|
|
|
return processClassCommitActor(ryw, getKeyRange());
|
2020-08-20 08:54:38 +08:00
|
|
|
}
|
|
|
|
|
2020-10-09 05:23:02 +08:00
|
|
|
void throwSpecialKeyApiFailure(ReadYourWritesTransaction* ryw, std::string command, std::string message) {
|
|
|
|
auto msg = ManagementAPIError::toJsonString(false, command, message);
|
2020-08-21 05:38:07 +08:00
|
|
|
ryw->setSpecialKeySpaceErrorMsg(msg);
|
2020-08-20 08:54:38 +08:00
|
|
|
throw special_keys_api_failure();
|
|
|
|
}
|
|
|
|
|
2020-08-26 05:59:43 +08:00
|
|
|
void ProcessClassRangeImpl::clear(ReadYourWritesTransaction* ryw, const KeyRangeRef& range) {
|
2020-10-09 05:23:02 +08:00
|
|
|
return throwSpecialKeyApiFailure(ryw, "setclass", "Clear operation is meaningless thus forbidden for setclass");
|
2020-08-21 05:38:07 +08:00
|
|
|
}
|
|
|
|
|
2020-08-26 05:59:43 +08:00
|
|
|
void ProcessClassRangeImpl::clear(ReadYourWritesTransaction* ryw, const KeyRef& key) {
|
2020-10-09 05:23:02 +08:00
|
|
|
return throwSpecialKeyApiFailure(ryw, "setclass",
|
|
|
|
"Clear range operation is meaningless thus forbidden for setclass");
|
2020-08-21 05:51:41 +08:00
|
|
|
}
|
2020-08-26 09:18:32 +08:00
|
|
|
|
|
|
|
ACTOR Future<Standalone<RangeResultRef>> getProcessClassSourceActor(ReadYourWritesTransaction* ryw, KeyRef prefix,
|
|
|
|
KeyRangeRef kr) {
|
|
|
|
vector<ProcessData> _workers = wait(getWorkers(&ryw->getTransaction()));
|
|
|
|
auto workers = _workers; // strip const
|
|
|
|
// Note : the sort by string is anti intuition, ex. 1.1.1.1:11 < 1.1.1.1:5
|
|
|
|
std::sort(workers.begin(), workers.end(), [](const ProcessData& lhs, const ProcessData& rhs) {
|
|
|
|
return formatIpPort(lhs.address.ip, lhs.address.port) < formatIpPort(rhs.address.ip, rhs.address.port);
|
|
|
|
});
|
|
|
|
Standalone<RangeResultRef> result;
|
|
|
|
for (auto& w : workers) {
|
|
|
|
// exclude :tls in keys even the network addresss is TLS
|
|
|
|
Key k(prefix.withSuffix(formatIpPort(w.address.ip, w.address.port)));
|
|
|
|
if (kr.contains(k)) {
|
|
|
|
Value v(w.processClass.sourceString());
|
|
|
|
result.push_back(result.arena(), KeyValueRef(k, v));
|
|
|
|
result.arena().dependsOn(k.arena());
|
|
|
|
result.arena().dependsOn(v.arena());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessClassSourceRangeImpl::ProcessClassSourceRangeImpl(KeyRangeRef kr) : SpecialKeyRangeReadImpl(kr) {}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> ProcessClassSourceRangeImpl::getRange(ReadYourWritesTransaction* ryw,
|
|
|
|
KeyRangeRef kr) const {
|
|
|
|
return getProcessClassSourceActor(ryw, getKeyRange().begin, kr);
|
2020-10-09 05:23:02 +08:00
|
|
|
}
|
|
|
|
|
2020-10-23 02:08:54 +08:00
|
|
|
ACTOR Future<Standalone<RangeResultRef>> getLockedKeyActor(ReadYourWritesTransaction* ryw, KeyRangeRef kr) {
|
2020-10-09 05:23:02 +08:00
|
|
|
ryw->getTransaction().setOption(FDBTransactionOptions::LOCK_AWARE);
|
|
|
|
Optional<Value> val = wait(ryw->getTransaction().get(databaseLockedKey));
|
|
|
|
Standalone<RangeResultRef> result;
|
|
|
|
if (val.present()) {
|
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(kr.begin, val.get()));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
LockDatabaseImpl::LockDatabaseImpl(KeyRangeRef kr) : SpecialKeyRangeRWImpl(kr) {}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> LockDatabaseImpl::getRange(ReadYourWritesTransaction* ryw, KeyRangeRef kr) const {
|
2020-10-23 06:36:10 +08:00
|
|
|
// single key range, the queried range should always be the same as the underlying range
|
2020-10-09 05:23:02 +08:00
|
|
|
ASSERT(kr == getKeyRange());
|
|
|
|
auto lockEntry = ryw->getSpecialKeySpaceWriteMap()[SpecialKeySpace::getManagementApiCommandPrefix("lock")];
|
|
|
|
if (!ryw->readYourWritesDisabled() && lockEntry.first) {
|
|
|
|
// ryw enabled and we have written to the special key
|
|
|
|
Standalone<RangeResultRef> result;
|
|
|
|
if (lockEntry.second.present()) {
|
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(kr.begin, lockEntry.second.get()));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return getLockedKeyActor(ryw, kr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<Optional<std::string>> lockDatabaseCommitActor(ReadYourWritesTransaction* ryw) {
|
|
|
|
state Optional<std::string> msg;
|
|
|
|
ryw->getTransaction().setOption(FDBTransactionOptions::LOCK_AWARE);
|
|
|
|
Optional<Value> val = wait(ryw->getTransaction().get(databaseLockedKey));
|
|
|
|
UID uid = deterministicRandom()->randomUniqueID();
|
|
|
|
|
|
|
|
if (val.present() && BinaryReader::fromStringRef<UID>(val.get().substr(10), Unversioned()) != uid) {
|
|
|
|
// check database not locked
|
|
|
|
// if locked already, throw error
|
|
|
|
msg = ManagementAPIError::toJsonString(false, "lock", "Database has already been locked");
|
|
|
|
} else if (!val.present()) {
|
|
|
|
// lock database
|
|
|
|
ryw->getTransaction().atomicOp(databaseLockedKey,
|
|
|
|
BinaryWriter::toValue(uid, Unversioned())
|
|
|
|
.withPrefix(LiteralStringRef("0123456789"))
|
|
|
|
.withSuffix(LiteralStringRef("\x00\x00\x00\x00")),
|
|
|
|
MutationRef::SetVersionstampedValue);
|
|
|
|
ryw->getTransaction().addWriteConflictRange(normalKeys);
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<Optional<std::string>> unlockDatabaseCommitActor(ReadYourWritesTransaction* ryw) {
|
|
|
|
ryw->getTransaction().setOption(FDBTransactionOptions::LOCK_AWARE);
|
|
|
|
Optional<Value> val = wait(ryw->getTransaction().get(databaseLockedKey));
|
|
|
|
if (val.present()) {
|
|
|
|
ryw->getTransaction().clear(singleKeyRange(databaseLockedKey));
|
|
|
|
}
|
|
|
|
return Optional<std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Optional<std::string>> LockDatabaseImpl::commit(ReadYourWritesTransaction* ryw) {
|
|
|
|
auto lockId = ryw->getSpecialKeySpaceWriteMap()[SpecialKeySpace::getManagementApiCommandPrefix("lock")].second;
|
|
|
|
if (lockId.present()) {
|
|
|
|
return lockDatabaseCommitActor(ryw);
|
|
|
|
} else {
|
|
|
|
return unlockDatabaseCommitActor(ryw);
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 02:08:54 +08:00
|
|
|
|
|
|
|
ACTOR Future<Standalone<RangeResultRef>> getConsistencyCheckKeyActor(ReadYourWritesTransaction* ryw, KeyRangeRef kr) {
|
|
|
|
ryw->getTransaction().setOption(FDBTransactionOptions::LOCK_AWARE);
|
|
|
|
ryw->getTransaction().setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE);
|
|
|
|
Optional<Value> val = wait(ryw->getTransaction().get(fdbShouldConsistencyCheckBeSuspended));
|
|
|
|
bool ccSuspendSetting = val.present() ? BinaryReader::fromStringRef<bool>(val.get(), Unversioned()) : false;
|
|
|
|
Standalone<RangeResultRef> result;
|
|
|
|
if (ccSuspendSetting) {
|
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(kr.begin, ValueRef()));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConsistencyCheckImpl::ConsistencyCheckImpl(KeyRangeRef kr) : SpecialKeyRangeRWImpl(kr) {}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> ConsistencyCheckImpl::getRange(ReadYourWritesTransaction* ryw,
|
|
|
|
KeyRangeRef kr) const {
|
2020-10-23 06:36:10 +08:00
|
|
|
// single key range, the queried range should always be the same as the underlying range
|
2020-10-23 02:08:54 +08:00
|
|
|
ASSERT(kr == getKeyRange());
|
|
|
|
auto entry = ryw->getSpecialKeySpaceWriteMap()[SpecialKeySpace::getManagementApiCommandPrefix("consistencycheck")];
|
|
|
|
if (!ryw->readYourWritesDisabled() && entry.first) {
|
|
|
|
// ryw enabled and we have written to the special key
|
|
|
|
Standalone<RangeResultRef> result;
|
|
|
|
if (entry.second.present()) {
|
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(kr.begin, entry.second.get()));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
} else {
|
|
|
|
return getConsistencyCheckKeyActor(ryw, kr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Optional<std::string>> ConsistencyCheckImpl::commit(ReadYourWritesTransaction* ryw) {
|
|
|
|
auto entry =
|
|
|
|
ryw->getSpecialKeySpaceWriteMap()[SpecialKeySpace::getManagementApiCommandPrefix("consistencycheck")].second;
|
|
|
|
ryw->getTransaction().setOption(FDBTransactionOptions::PRIORITY_SYSTEM_IMMEDIATE);
|
|
|
|
ryw->getTransaction().setOption(FDBTransactionOptions::LOCK_AWARE);
|
|
|
|
ryw->getTransaction().set(fdbShouldConsistencyCheckBeSuspended,
|
|
|
|
BinaryWriter::toValue(entry.present(), Unversioned()));
|
|
|
|
return Optional<std::string>();
|
|
|
|
}
|
2020-12-01 06:57:17 +08:00
|
|
|
|
2020-12-04 06:06:11 +08:00
|
|
|
TracingOptionsImpl::TracingOptionsImpl(KeyRangeRef kr) : SpecialKeyRangeRWImpl(kr) {
|
|
|
|
TraceEvent("TracingOptionsImpl::TracingOptionsImpl").detail("Range", kr);
|
|
|
|
}
|
2020-12-01 06:57:17 +08:00
|
|
|
|
2021-01-30 03:45:52 +08:00
|
|
|
Future<Standalone<RangeResultRef>> TracingOptionsImpl::getRange(ReadYourWritesTransaction* ryw, KeyRangeRef kr) const {
|
2020-12-01 06:57:17 +08:00
|
|
|
Standalone<RangeResultRef> result;
|
2020-12-05 03:21:39 +08:00
|
|
|
for (const auto& option : SpecialKeySpace::getTracingOptions()) {
|
|
|
|
auto key = getKeyRange().begin.withSuffix(option);
|
|
|
|
if (!kr.contains(key)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key.endsWith(kTracingTransactionIdKey)) {
|
2021-01-30 03:45:52 +08:00
|
|
|
result.push_back_deep(result.arena(),
|
|
|
|
KeyValueRef(key, std::to_string(ryw->getTransactionInfo().spanID.first())));
|
2020-12-05 03:21:39 +08:00
|
|
|
} else if (key.endsWith(kTracingTokenKey)) {
|
2021-01-30 03:45:52 +08:00
|
|
|
result.push_back_deep(result.arena(),
|
|
|
|
KeyValueRef(key, std::to_string(ryw->getTransactionInfo().spanID.second())));
|
2020-12-05 03:21:39 +08:00
|
|
|
}
|
2020-12-01 06:57:17 +08:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TracingOptionsImpl::set(ReadYourWritesTransaction* ryw, const KeyRef& key, const ValueRef& value) {
|
2020-12-04 06:06:11 +08:00
|
|
|
if (ryw->getApproximateSize() > 0) {
|
|
|
|
ryw->setSpecialKeySpaceErrorMsg("tracing options must be set first");
|
2020-12-08 06:43:44 +08:00
|
|
|
ryw->getSpecialKeySpaceWriteMap().insert(key, std::make_pair(true, Optional<Value>()));
|
|
|
|
return;
|
2020-12-04 06:06:11 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (key.endsWith(kTracingTransactionIdKey)) {
|
|
|
|
ryw->setTransactionID(std::stoul(value.toString()));
|
|
|
|
} else if (key.endsWith(kTracingTokenKey)) {
|
|
|
|
if (value.toString() == "true") {
|
|
|
|
ryw->setToken(deterministicRandom()->randomUInt64());
|
|
|
|
} else if (value.toString() == "false") {
|
|
|
|
ryw->setToken(0);
|
|
|
|
} else {
|
|
|
|
ryw->setSpecialKeySpaceErrorMsg("token must be set to true/false");
|
|
|
|
throw special_keys_api_failure();
|
|
|
|
}
|
2020-12-01 06:57:17 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Optional<std::string>> TracingOptionsImpl::commit(ReadYourWritesTransaction* ryw) {
|
2020-12-08 06:43:44 +08:00
|
|
|
if (ryw->getSpecialKeySpaceWriteMap().size() > 0) {
|
|
|
|
throw special_keys_api_failure();
|
|
|
|
}
|
2020-12-01 06:57:17 +08:00
|
|
|
return Optional<std::string>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TracingOptionsImpl::clear(ReadYourWritesTransaction* ryw, const KeyRangeRef& range) {
|
2020-12-04 06:06:11 +08:00
|
|
|
ryw->setSpecialKeySpaceErrorMsg("clear range disabled");
|
|
|
|
throw special_keys_api_failure();
|
2020-12-01 06:57:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TracingOptionsImpl::clear(ReadYourWritesTransaction* ryw, const KeyRef& key) {
|
2020-12-04 06:06:11 +08:00
|
|
|
ryw->setSpecialKeySpaceErrorMsg("clear disabled");
|
|
|
|
throw special_keys_api_failure();
|
2020-12-01 06:57:17 +08:00
|
|
|
}
|
2021-01-30 03:45:52 +08:00
|
|
|
|
|
|
|
CoordinatorsImpl::CoordinatorsImpl(KeyRangeRef kr) : SpecialKeyRangeRWImpl(kr) {}
|
|
|
|
|
|
|
|
Future<Standalone<RangeResultRef>> CoordinatorsImpl::getRange(ReadYourWritesTransaction* ryw, KeyRangeRef kr) const {
|
|
|
|
Standalone<RangeResultRef> result;
|
|
|
|
KeyRef prefix(getKeyRange().begin);
|
|
|
|
// the constructor of ClusterConnectionFile already checks whether the file is valid
|
|
|
|
auto cs = ClusterConnectionFile(ryw->getDatabase()->getConnectionFile()->getFilename()).getConnectionString();
|
|
|
|
auto coordinator_processes = cs.coordinators();
|
|
|
|
Key cluster_decription_key = prefix.withSuffix(LiteralStringRef("cluster_description"));
|
|
|
|
if (kr.contains(cluster_decription_key)) {
|
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(cluster_decription_key, cs.clusterKeyName()));
|
|
|
|
}
|
|
|
|
// Note : the sort by string is anti intuition, ex. 1.1.1.1:11 < 1.1.1.1:5
|
|
|
|
// include :tls in keys if the network addresss is TLS
|
|
|
|
std::sort(coordinator_processes.begin(), coordinator_processes.end(),
|
2021-02-05 16:55:34 +08:00
|
|
|
[](const NetworkAddress& lhs, const NetworkAddress& rhs) { return lhs.toString() < rhs.toString(); });
|
|
|
|
std::string processes_str;
|
2021-01-30 03:45:52 +08:00
|
|
|
for (auto& w : coordinator_processes) {
|
2021-02-05 16:55:34 +08:00
|
|
|
if (processes_str.size()) processes_str += ",";
|
|
|
|
processes_str += w.toString();
|
|
|
|
}
|
|
|
|
Key processes_key = prefix.withSuffix(LiteralStringRef("processes"));
|
|
|
|
if (kr.contains(processes_key)) {
|
|
|
|
result.push_back_deep(result.arena(), KeyValueRef(processes_key, Value(processes_str)));
|
2021-01-30 03:45:52 +08:00
|
|
|
}
|
|
|
|
return rywGetRange(ryw, kr, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR static Future<Optional<std::string>> coordinatorsCommitActor(ReadYourWritesTransaction* ryw, KeyRangeRef kr) {
|
|
|
|
state Reference<IQuorumChange> change;
|
|
|
|
state std::vector<NetworkAddress> addressesVec;
|
2021-02-05 16:55:34 +08:00
|
|
|
state std::vector<std::string> process_address_strs;
|
2021-01-30 03:45:52 +08:00
|
|
|
state Optional<std::string> msg;
|
2021-02-05 16:55:34 +08:00
|
|
|
state int index;
|
|
|
|
state bool parse_error = false;
|
2021-01-30 03:45:52 +08:00
|
|
|
|
2021-02-05 16:55:34 +08:00
|
|
|
// check update for cluster_description
|
|
|
|
Key processes_key = LiteralStringRef("processes").withPrefix(kr.begin);
|
|
|
|
auto processes_entry = ryw->getSpecialKeySpaceWriteMap()[processes_key];
|
|
|
|
if (processes_entry.first) {
|
|
|
|
ASSERT(processes_entry.second.present()); // no clear should be seen here
|
|
|
|
auto processesStr = processes_entry.second.get().toString();
|
|
|
|
boost::split(process_address_strs, processesStr, [](char c) { return c == ','; });
|
|
|
|
if (!process_address_strs.size()) {
|
|
|
|
return ManagementAPIError::toJsonString(
|
|
|
|
false, "coordinators",
|
|
|
|
"New coordinators\' processes are empty, please specify new processes\' network addresses with format "
|
|
|
|
"\"IP:PORT,IP:PORT,...,IP:PORT\"");
|
|
|
|
}
|
|
|
|
for (index = 0; index < process_address_strs.size(); index++) {
|
|
|
|
try {
|
|
|
|
auto a = NetworkAddress::parse(process_address_strs[index]);
|
|
|
|
if (!a.isValid())
|
|
|
|
parse_error = true;
|
|
|
|
else
|
|
|
|
addressesVec.push_back(a);
|
|
|
|
} catch (Error& e) {
|
|
|
|
TraceEvent(SevDebug, "SpeicalKeysNetworkParseError").error(e);
|
|
|
|
parse_error = true;
|
|
|
|
}
|
2021-01-30 03:45:52 +08:00
|
|
|
|
2021-02-05 16:55:34 +08:00
|
|
|
if (parse_error) {
|
2021-02-09 10:52:36 +08:00
|
|
|
std::string error =
|
|
|
|
"ERROR: \'" + process_address_strs[index] + "\' is not a valid network endpoint address\n";
|
2021-02-05 16:55:34 +08:00
|
|
|
if (process_address_strs[index].find(":tls") != std::string::npos)
|
2021-01-30 03:45:52 +08:00
|
|
|
error += " Do not include the `:tls' suffix when naming a process\n";
|
2021-02-05 16:55:34 +08:00
|
|
|
return ManagementAPIError::toJsonString(false, "coordinators", error);
|
2021-01-30 03:45:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check auto option
|
|
|
|
auto auto_option = ryw->getSpecialKeySpaceWriteMap()[SpecialKeySpace::getManagementApiCommandOptionSpecialKey(
|
|
|
|
"coordinators", "auto")];
|
|
|
|
if (auto_option.first) {
|
|
|
|
change = autoQuorumChange();
|
|
|
|
} else {
|
|
|
|
if (addressesVec.size())
|
|
|
|
change = specifiedQuorumChange(addressesVec);
|
|
|
|
else
|
|
|
|
change = noQuorumChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
// check update for cluster_description
|
|
|
|
Key cluster_decription_key = LiteralStringRef("cluster_description").withPrefix(kr.begin);
|
|
|
|
auto entry = ryw->getSpecialKeySpaceWriteMap()[cluster_decription_key];
|
|
|
|
if (entry.first) {
|
|
|
|
// check valid description [a-zA-Z0-9_]+
|
|
|
|
if (entry.second.present() && isAlphaNumeric(entry.second.get().toString())) {
|
|
|
|
// do the name change
|
|
|
|
change = nameQuorumChange(entry.second.get().toString(), change);
|
|
|
|
} else {
|
|
|
|
// throw the error
|
|
|
|
return Optional<std::string>(ManagementAPIError::toJsonString(
|
|
|
|
false, "coordinators", "Cluster description must match [A-Za-z0-9_]+"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(change.isValid());
|
|
|
|
|
|
|
|
TraceEvent(SevDebug, "SKSChangeCoordinatorsStart")
|
|
|
|
.detail("NewAddresses", describe(addressesVec))
|
|
|
|
.detail("Auto", auto_option.first)
|
|
|
|
.detail("Description", entry.first ? entry.second.get().toString() : "");
|
|
|
|
|
2021-02-17 05:01:37 +08:00
|
|
|
Optional<CoordinatorsResult> r = wait(changeQuorumChecker(&ryw->getTransaction(), change, &addressesVec));
|
2021-01-30 03:45:52 +08:00
|
|
|
|
|
|
|
TraceEvent(SevDebug, "SKSChangeCoordinatorsFinish")
|
2021-02-17 05:06:25 +08:00
|
|
|
.detail("Result", r.present() ? static_cast<int>(r.get()) : -1); // -1 means success
|
2021-01-30 03:45:52 +08:00
|
|
|
if (r.present()) {
|
|
|
|
auto res = r.get();
|
|
|
|
std::string error_msg;
|
2021-02-17 05:01:37 +08:00
|
|
|
bool retriable = false;
|
2021-01-30 03:45:52 +08:00
|
|
|
if (res == CoordinatorsResult::INVALID_NETWORK_ADDRESSES) {
|
|
|
|
error_msg = "The specified network addresses are invalid";
|
|
|
|
} else if (res == CoordinatorsResult::SAME_NETWORK_ADDRESSES) {
|
|
|
|
error_msg = "No change (existing configuration satisfies request)";
|
|
|
|
} else if (res == CoordinatorsResult::NOT_COORDINATORS) {
|
|
|
|
error_msg = "Coordination servers are not running on the specified network addresses";
|
|
|
|
} else if (res == CoordinatorsResult::DATABASE_UNREACHABLE) {
|
|
|
|
error_msg = "Database unreachable";
|
|
|
|
} else if (res == CoordinatorsResult::BAD_DATABASE_STATE) {
|
|
|
|
error_msg = "The database is in an unexpected state from which changing coordinators might be unsafe";
|
|
|
|
} else if (res == CoordinatorsResult::COORDINATOR_UNREACHABLE) {
|
|
|
|
error_msg = "One of the specified coordinators is unreachable";
|
2021-02-17 05:01:37 +08:00
|
|
|
retriable = true;
|
2021-01-30 03:45:52 +08:00
|
|
|
} else if (res == CoordinatorsResult::NOT_ENOUGH_MACHINES) {
|
|
|
|
error_msg = "Too few fdbserver machines to provide coordination at the current redundancy level";
|
|
|
|
} else if (res == CoordinatorsResult::SUCCESS) {
|
|
|
|
TraceEvent(SevError, "SpecialKeysForCoordinators").detail("UnexpectedSuccessfulResult", "");
|
|
|
|
} else {
|
|
|
|
ASSERT(false);
|
|
|
|
}
|
2021-02-17 05:01:37 +08:00
|
|
|
msg = ManagementAPIError::toJsonString(retriable, "coordinators", error_msg);
|
2021-01-30 03:45:52 +08:00
|
|
|
}
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Optional<std::string>> CoordinatorsImpl::commit(ReadYourWritesTransaction* ryw) {
|
|
|
|
return coordinatorsCommitActor(ryw, getKeyRange());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoordinatorsImpl::clear(ReadYourWritesTransaction* ryw, const KeyRangeRef& range) {
|
|
|
|
return throwSpecialKeyApiFailure(ryw, "coordinators", "Clear range is meaningless thus forbidden for coordinators");
|
|
|
|
}
|
|
|
|
|
|
|
|
void CoordinatorsImpl::clear(ReadYourWritesTransaction* ryw, const KeyRef& key) {
|
|
|
|
return throwSpecialKeyApiFailure(ryw, "coordinators",
|
|
|
|
"Clear operation is meaningless thus forbidden for coordinators");
|
2021-01-30 10:20:09 +08:00
|
|
|
}
|