2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* KeyRangeMap.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
2022-03-22 04:36:23 +08:00
|
|
|
* Copyright 2013-2022 Apple Inc. and the FoundationDB project authors
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-02-22 02:25:11 +08:00
|
|
|
*
|
2017-05-26 04:48:44 +08:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbclient/KeyRangeMap.h"
|
2019-02-18 07:41:16 +08:00
|
|
|
#include "fdbclient/NativeAPI.actor.h"
|
2018-10-20 01:30:13 +08:00
|
|
|
#include "fdbclient/CommitTransaction.h"
|
|
|
|
#include "fdbclient/FDBTypes.h"
|
|
|
|
#include "fdbclient/ReadYourWrites.h"
|
2019-02-18 06:55:47 +08:00
|
|
|
#include "flow/actorcompiler.h" // has to be last include
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2021-09-17 08:42:34 +08:00
|
|
|
void KeyRangeActorMap::getRangesAffectedByInsertion(const KeyRangeRef& keys, std::vector<KeyRange>& affectedRanges) {
|
2017-05-26 04:48:44 +08:00
|
|
|
auto s = map.rangeContaining(keys.begin);
|
|
|
|
if (s.begin() != keys.begin && s.value().isValid() && !s.value().isReady())
|
|
|
|
affectedRanges.push_back(KeyRangeRef(s.begin(), keys.begin));
|
|
|
|
affectedRanges.push_back(keys);
|
|
|
|
auto e = map.rangeContaining(keys.end);
|
|
|
|
if (e.begin() != keys.end && e.value().isValid() && !e.value().isReady())
|
|
|
|
affectedRanges.push_back(KeyRangeRef(keys.end, e.end()));
|
|
|
|
}
|
|
|
|
|
2021-05-04 04:14:16 +08:00
|
|
|
RangeResult krmDecodeRanges(KeyRef mapPrefix, KeyRange keys, RangeResult kv) {
|
2017-05-26 04:48:44 +08:00
|
|
|
ASSERT(!kv.more || kv.size() > 1);
|
|
|
|
KeyRange withPrefix =
|
|
|
|
KeyRangeRef(mapPrefix.toString() + keys.begin.toString(), mapPrefix.toString() + keys.end.toString());
|
|
|
|
|
|
|
|
ValueRef beginValue, endValue;
|
|
|
|
if (kv.size() && kv[0].key.startsWith(mapPrefix))
|
|
|
|
beginValue = kv[0].value;
|
|
|
|
if (kv.size() && kv.end()[-1].key.startsWith(mapPrefix))
|
|
|
|
endValue = kv.end()[-1].value;
|
|
|
|
|
2021-05-04 04:14:16 +08:00
|
|
|
RangeResult result;
|
2017-05-26 04:48:44 +08:00
|
|
|
result.arena().dependsOn(kv.arena());
|
|
|
|
result.arena().dependsOn(keys.arena());
|
|
|
|
|
|
|
|
result.push_back(result.arena(), KeyValueRef(keys.begin, beginValue));
|
|
|
|
for (int i = 0; i < kv.size(); i++) {
|
|
|
|
if (kv[i].key > withPrefix.begin && kv[i].key < withPrefix.end) {
|
|
|
|
KeyRef k = kv[i].key.removePrefix(mapPrefix);
|
|
|
|
result.push_back(result.arena(), KeyValueRef(k, kv[i].value));
|
|
|
|
} else if (kv[i].key >= withPrefix.end)
|
|
|
|
kv.more = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!kv.more)
|
|
|
|
result.push_back(result.arena(), KeyValueRef(keys.end, endValue));
|
|
|
|
result.more = kv.more;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns keys.begin, all transitional points in keys, and keys.end, and their values
|
2021-05-04 04:14:16 +08:00
|
|
|
ACTOR Future<RangeResult> krmGetRanges(Transaction* tr, Key mapPrefix, KeyRange keys, int limit, int limitBytes) {
|
2017-05-26 04:48:44 +08:00
|
|
|
KeyRange withPrefix =
|
|
|
|
KeyRangeRef(mapPrefix.toString() + keys.begin.toString(), mapPrefix.toString() + keys.end.toString());
|
|
|
|
|
|
|
|
state GetRangeLimits limits(limit, limitBytes);
|
|
|
|
limits.minRows = 2;
|
2021-05-04 04:14:16 +08:00
|
|
|
RangeResult kv = wait(tr->getRange(lastLessOrEqual(withPrefix.begin), firstGreaterThan(withPrefix.end), limits));
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
return krmDecodeRanges(mapPrefix, keys, kv);
|
|
|
|
}
|
|
|
|
|
2021-05-04 04:14:16 +08:00
|
|
|
ACTOR Future<RangeResult> krmGetRanges(Reference<ReadYourWritesTransaction> tr,
|
|
|
|
Key mapPrefix,
|
|
|
|
KeyRange keys,
|
|
|
|
int limit,
|
|
|
|
int limitBytes) {
|
2017-05-26 04:48:44 +08:00
|
|
|
KeyRange withPrefix =
|
|
|
|
KeyRangeRef(mapPrefix.toString() + keys.begin.toString(), mapPrefix.toString() + keys.end.toString());
|
|
|
|
|
|
|
|
state GetRangeLimits limits(limit, limitBytes);
|
|
|
|
limits.minRows = 2;
|
2021-05-04 04:14:16 +08:00
|
|
|
RangeResult kv = wait(tr->getRange(lastLessOrEqual(withPrefix.begin), firstGreaterThan(withPrefix.end), limits));
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
return krmDecodeRanges(mapPrefix, keys, kv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void krmSetPreviouslyEmptyRange(Transaction* tr,
|
|
|
|
const KeyRef& mapPrefix,
|
|
|
|
const KeyRangeRef& keys,
|
|
|
|
const ValueRef& newValue,
|
|
|
|
const ValueRef& oldEndValue) {
|
|
|
|
KeyRange withPrefix =
|
|
|
|
KeyRangeRef(mapPrefix.toString() + keys.begin.toString(), mapPrefix.toString() + keys.end.toString());
|
|
|
|
tr->set(withPrefix.begin, newValue);
|
|
|
|
tr->set(withPrefix.end, oldEndValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void krmSetPreviouslyEmptyRange(CommitTransactionRef& tr,
|
|
|
|
Arena& trArena,
|
|
|
|
const KeyRef& mapPrefix,
|
|
|
|
const KeyRangeRef& keys,
|
|
|
|
const ValueRef& newValue,
|
|
|
|
const ValueRef& oldEndValue) {
|
|
|
|
KeyRange withPrefix =
|
|
|
|
KeyRangeRef(mapPrefix.toString() + keys.begin.toString(), mapPrefix.toString() + keys.end.toString());
|
|
|
|
tr.set(trArena, withPrefix.begin, newValue);
|
|
|
|
tr.set(trArena, withPrefix.end, oldEndValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<Void> krmSetRange(Transaction* tr, Key mapPrefix, KeyRange range, Value value) {
|
|
|
|
state KeyRange withPrefix =
|
|
|
|
KeyRangeRef(mapPrefix.toString() + range.begin.toString(), mapPrefix.toString() + range.end.toString());
|
2021-07-03 12:41:50 +08:00
|
|
|
RangeResult old =
|
2021-07-17 15:11:40 +08:00
|
|
|
wait(tr->getRange(lastLessOrEqual(withPrefix.end), firstGreaterThan(withPrefix.end), 1, Snapshot::True));
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
Value oldValue;
|
|
|
|
bool hasResult = old.size() > 0 && old[0].key.startsWith(mapPrefix);
|
|
|
|
if (hasResult)
|
|
|
|
oldValue = old[0].value;
|
|
|
|
|
|
|
|
KeyRange conflictRange = KeyRangeRef(hasResult ? old[0].key : mapPrefix.toString(), keyAfter(withPrefix.end));
|
|
|
|
if (!conflictRange.empty())
|
|
|
|
tr->addReadConflictRange(conflictRange);
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
tr->clear(withPrefix);
|
|
|
|
tr->set(withPrefix.begin, value);
|
|
|
|
tr->set(withPrefix.end, oldValue);
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
ACTOR Future<Void> krmSetRange(Reference<ReadYourWritesTransaction> tr, Key mapPrefix, KeyRange range, Value value) {
|
|
|
|
state KeyRange withPrefix =
|
|
|
|
KeyRangeRef(mapPrefix.toString() + range.begin.toString(), mapPrefix.toString() + range.end.toString());
|
2021-07-03 12:41:50 +08:00
|
|
|
RangeResult old =
|
2021-07-17 15:11:40 +08:00
|
|
|
wait(tr->getRange(lastLessOrEqual(withPrefix.end), firstGreaterThan(withPrefix.end), 1, Snapshot::True));
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
Value oldValue;
|
|
|
|
bool hasResult = old.size() > 0 && old[0].key.startsWith(mapPrefix);
|
|
|
|
if (hasResult)
|
|
|
|
oldValue = old[0].value;
|
|
|
|
|
|
|
|
KeyRange conflictRange = KeyRangeRef(hasResult ? old[0].key : mapPrefix.toString(), keyAfter(withPrefix.end));
|
|
|
|
if (!conflictRange.empty())
|
|
|
|
tr->addReadConflictRange(conflictRange);
|
2021-03-11 02:06:03 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
tr->clear(withPrefix);
|
|
|
|
tr->set(withPrefix.begin, value);
|
|
|
|
tr->set(withPrefix.end, oldValue);
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets a range of keys in a key range map, coalescing with adjacent regions if the values match
|
|
|
|
// Ranges outside of maxRange will not be coalesced
|
|
|
|
// CAUTION: use care when attempting to coalesce multiple ranges in the same prefix in a single transaction
|
2020-03-25 00:48:03 +08:00
|
|
|
ACTOR template <class Transaction>
|
2020-03-27 06:52:30 +08:00
|
|
|
static Future<Void> krmSetRangeCoalescing_(Transaction* tr,
|
|
|
|
Key mapPrefix,
|
|
|
|
KeyRange range,
|
|
|
|
KeyRange maxRange,
|
|
|
|
Value value) {
|
2017-05-26 04:48:44 +08:00
|
|
|
ASSERT(maxRange.contains(range));
|
|
|
|
|
|
|
|
state KeyRange withPrefix =
|
|
|
|
KeyRangeRef(mapPrefix.toString() + range.begin.toString(), mapPrefix.toString() + range.end.toString());
|
|
|
|
state KeyRange maxWithPrefix =
|
|
|
|
KeyRangeRef(mapPrefix.toString() + maxRange.begin.toString(), mapPrefix.toString() + maxRange.end.toString());
|
|
|
|
|
2021-09-17 08:42:34 +08:00
|
|
|
state std::vector<Future<RangeResult>> keys;
|
2021-07-03 12:41:50 +08:00
|
|
|
keys.push_back(
|
2021-07-17 15:11:40 +08:00
|
|
|
tr->getRange(lastLessThan(withPrefix.begin), firstGreaterOrEqual(withPrefix.begin), 1, Snapshot::True));
|
2021-07-03 12:41:50 +08:00
|
|
|
keys.push_back(
|
2021-07-17 15:11:40 +08:00
|
|
|
tr->getRange(lastLessOrEqual(withPrefix.end), firstGreaterThan(withPrefix.end) + 1, 2, Snapshot::True));
|
2018-08-11 04:57:10 +08:00
|
|
|
wait(waitForAll(keys));
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
// Determine how far to extend this range at the beginning
|
2020-03-13 00:53:00 +08:00
|
|
|
auto beginRange = keys[0].get();
|
|
|
|
bool hasBegin = beginRange.size() > 0 && beginRange[0].key.startsWith(mapPrefix);
|
|
|
|
Value beginValue = hasBegin ? beginRange[0].value : LiteralStringRef("");
|
|
|
|
|
|
|
|
state Key beginKey = withPrefix.begin;
|
|
|
|
if (beginValue == value) {
|
|
|
|
bool outsideRange = !hasBegin || beginRange[0].key < maxWithPrefix.begin;
|
|
|
|
beginKey = outsideRange ? maxWithPrefix.begin : beginRange[0].key;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine how far to extend this range at the end
|
|
|
|
auto endRange = keys[1].get();
|
|
|
|
bool hasEnd = endRange.size() >= 1 && endRange[0].key.startsWith(mapPrefix) && endRange[0].key <= withPrefix.end;
|
|
|
|
bool hasNext = (endRange.size() == 2 && endRange[1].key.startsWith(mapPrefix)) ||
|
|
|
|
(endRange.size() == 1 && withPrefix.end < endRange[0].key && endRange[0].key.startsWith(mapPrefix));
|
|
|
|
Value existingValue = hasEnd ? endRange[0].value : LiteralStringRef("");
|
|
|
|
bool valueMatches = value == existingValue;
|
|
|
|
|
|
|
|
KeyRange conflictRange = KeyRangeRef(hasBegin ? beginRange[0].key : mapPrefix, withPrefix.begin);
|
|
|
|
if (!conflictRange.empty())
|
|
|
|
tr->addReadConflictRange(conflictRange);
|
|
|
|
|
|
|
|
conflictRange = KeyRangeRef(hasEnd ? endRange[0].key : mapPrefix,
|
|
|
|
hasNext ? keyAfter(endRange.end()[-1].key) : strinc(mapPrefix));
|
|
|
|
if (!conflictRange.empty())
|
|
|
|
tr->addReadConflictRange(conflictRange);
|
|
|
|
|
|
|
|
state Key endKey;
|
|
|
|
state Value endValue;
|
|
|
|
|
|
|
|
// Case 1: Coalesce completely with the following range
|
|
|
|
if (hasNext && endRange.end()[-1].key <= maxWithPrefix.end && valueMatches) {
|
|
|
|
endKey = endRange.end()[-1].key;
|
|
|
|
endValue = endRange.end()[-1].value;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 2: Coalesce with the following range only up to the end of maxRange
|
|
|
|
else if (valueMatches) {
|
|
|
|
endKey = maxWithPrefix.end;
|
|
|
|
endValue = existingValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Case 3: Don't coalesce
|
|
|
|
else {
|
|
|
|
endKey = withPrefix.end;
|
|
|
|
endValue = existingValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
tr->clear(KeyRangeRef(beginKey, endKey));
|
|
|
|
|
|
|
|
ASSERT(value != endValue || endKey == maxWithPrefix.end);
|
|
|
|
tr->set(beginKey, value);
|
|
|
|
tr->set(endKey, endValue);
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
2020-03-25 00:48:03 +08:00
|
|
|
Future<Void> krmSetRangeCoalescing(Transaction* const& tr,
|
|
|
|
Key const& mapPrefix,
|
|
|
|
KeyRange const& range,
|
|
|
|
KeyRange const& maxRange,
|
|
|
|
Value const& value) {
|
2020-03-27 06:52:30 +08:00
|
|
|
return krmSetRangeCoalescing_(tr, mapPrefix, range, maxRange, value);
|
2020-03-25 00:48:03 +08:00
|
|
|
}
|
|
|
|
Future<Void> krmSetRangeCoalescing(Reference<ReadYourWritesTransaction> const& tr,
|
|
|
|
Key const& mapPrefix,
|
|
|
|
KeyRange const& range,
|
|
|
|
KeyRange const& maxRange,
|
|
|
|
Value const& value) {
|
2020-03-27 06:52:30 +08:00
|
|
|
return holdWhile(tr, krmSetRangeCoalescing_(tr.getPtr(), mapPrefix, range, maxRange, value));
|
2020-03-25 00:48:03 +08:00
|
|
|
}
|