2022-02-24 00:06:39 +08:00
|
|
|
/*
|
|
|
|
* Tenant.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2022 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.
|
|
|
|
*/
|
|
|
|
|
2022-07-19 02:24:49 +08:00
|
|
|
#include "fdbclient/NativeAPI.actor.h"
|
2022-02-24 00:06:39 +08:00
|
|
|
#include "fdbclient/SystemData.h"
|
|
|
|
#include "fdbclient/Tenant.h"
|
2022-09-15 01:58:32 +08:00
|
|
|
#include "fdbrpc/TenantInfo.h"
|
|
|
|
#include "flow/BooleanParam.h"
|
2022-09-17 02:20:34 +08:00
|
|
|
#include "flow/IRandom.h"
|
|
|
|
#include "libb64/decode.h"
|
2022-07-26 08:08:32 +08:00
|
|
|
#include "libb64/encode.h"
|
2022-09-02 15:28:13 +08:00
|
|
|
#include "flow/ApiVersion.h"
|
2022-02-24 00:06:39 +08:00
|
|
|
#include "flow/UnitTest.h"
|
|
|
|
|
2022-09-15 01:58:32 +08:00
|
|
|
FDB_DEFINE_BOOLEAN_PARAM(EnforceValidTenantId);
|
|
|
|
|
2022-12-06 07:47:58 +08:00
|
|
|
namespace TenantAPI {
|
2023-02-02 08:40:58 +08:00
|
|
|
|
|
|
|
KeyRef idToPrefix(Arena& p, int64_t id) {
|
2022-05-27 03:49:22 +08:00
|
|
|
int64_t swapped = bigEndian64(id);
|
2023-02-02 08:40:58 +08:00
|
|
|
return StringRef(p, reinterpret_cast<const uint8_t*>(&swapped), TenantAPI::PREFIX_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
Key idToPrefix(int64_t id) {
|
|
|
|
Arena p(TenantAPI::PREFIX_SIZE);
|
|
|
|
return Key(idToPrefix(p, id), p);
|
2022-05-27 03:49:22 +08:00
|
|
|
}
|
|
|
|
|
2022-12-02 03:13:35 +08:00
|
|
|
int64_t prefixToId(KeyRef prefix, EnforceValidTenantId enforceValidTenantId) {
|
2022-12-06 07:47:58 +08:00
|
|
|
ASSERT(prefix.size() == TenantAPI::PREFIX_SIZE);
|
2022-05-27 03:49:22 +08:00
|
|
|
int64_t id = *reinterpret_cast<const int64_t*>(prefix.begin());
|
|
|
|
id = bigEndian64(id);
|
2022-09-15 01:58:32 +08:00
|
|
|
if (enforceValidTenantId) {
|
|
|
|
ASSERT(id >= 0);
|
|
|
|
} else if (id < 0) {
|
|
|
|
return TenantInfo::INVALID_TENANT;
|
|
|
|
}
|
2022-05-27 03:49:22 +08:00
|
|
|
return id;
|
|
|
|
}
|
2023-02-09 06:35:35 +08:00
|
|
|
|
2023-02-22 01:55:24 +08:00
|
|
|
KeyRangeRef clampRangeToTenant(KeyRangeRef range, TenantInfo const& tenantInfo, Arena& arena) {
|
|
|
|
if (tenantInfo.hasTenant()) {
|
|
|
|
return KeyRangeRef(range.begin.startsWith(tenantInfo.prefix.get()) ? range.begin : tenantInfo.prefix.get(),
|
|
|
|
range.end.startsWith(tenantInfo.prefix.get())
|
|
|
|
? range.end
|
|
|
|
: allKeys.end.withPrefix(tenantInfo.prefix.get(), arena));
|
|
|
|
} else {
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-09 06:35:35 +08:00
|
|
|
bool withinSingleTenant(KeyRangeRef const& range) {
|
2023-02-11 12:58:15 +08:00
|
|
|
if (range.begin >= "\x80"_sr || range.begin.size() < TenantAPI::PREFIX_SIZE) {
|
2023-02-09 06:35:35 +08:00
|
|
|
return false;
|
|
|
|
}
|
2023-02-11 04:29:38 +08:00
|
|
|
auto tRange = prefixRange(range.begin.substr(0, TenantAPI::PREFIX_SIZE));
|
|
|
|
return tRange.contains(range);
|
2023-02-09 06:35:35 +08:00
|
|
|
}
|
|
|
|
|
2023-02-22 02:29:36 +08:00
|
|
|
std::string tenantLockStateToString(TenantLockState tenantState) {
|
2022-07-30 03:54:40 +08:00
|
|
|
switch (tenantState) {
|
|
|
|
case TenantLockState::UNLOCKED:
|
|
|
|
return "unlocked";
|
|
|
|
case TenantLockState::READ_ONLY:
|
|
|
|
return "read only";
|
|
|
|
case TenantLockState::LOCKED:
|
|
|
|
return "locked";
|
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 02:29:36 +08:00
|
|
|
TenantLockState stringToTenantLockState(std::string stateStr) {
|
2022-10-26 01:59:21 +08:00
|
|
|
std::transform(stateStr.begin(), stateStr.end(), stateStr.begin(), [](unsigned char c) { return std::tolower(c); });
|
2022-07-30 03:54:40 +08:00
|
|
|
if (stateStr == "unlocked") {
|
|
|
|
return TenantLockState::UNLOCKED;
|
|
|
|
} else if (stateStr == "read only") {
|
|
|
|
return TenantLockState::READ_ONLY;
|
|
|
|
} else if (stateStr == "locked") {
|
|
|
|
return TenantLockState::LOCKED;
|
|
|
|
}
|
|
|
|
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
2023-02-22 02:29:36 +08:00
|
|
|
} // namespace TenantAPI
|
2022-07-30 03:54:40 +08:00
|
|
|
|
2022-12-06 04:26:01 +08:00
|
|
|
json_spirit::mObject binaryToJson(StringRef bytes) {
|
|
|
|
json_spirit::mObject obj;
|
|
|
|
std::string encodedBytes = base64::encoder::from_string(bytes.toString());
|
|
|
|
// Remove trailing newline
|
|
|
|
encodedBytes.resize(encodedBytes.size() - 1);
|
|
|
|
|
|
|
|
obj["base64"] = encodedBytes;
|
|
|
|
obj["printable"] = printable(bytes);
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2022-05-27 03:49:22 +08:00
|
|
|
TenantMapEntry::TenantMapEntry() {}
|
2023-02-09 02:39:46 +08:00
|
|
|
TenantMapEntry::TenantMapEntry(int64_t id, TenantName tenantName) : tenantName(tenantName) {
|
2022-07-21 03:56:00 +08:00
|
|
|
setId(id);
|
|
|
|
}
|
2023-02-09 02:39:46 +08:00
|
|
|
TenantMapEntry::TenantMapEntry(int64_t id, TenantName tenantName, Optional<TenantGroupName> tenantGroup)
|
|
|
|
: tenantName(tenantName), tenantGroup(tenantGroup) {
|
|
|
|
setId(id);
|
|
|
|
}
|
2022-07-21 03:56:00 +08:00
|
|
|
|
|
|
|
void TenantMapEntry::setId(int64_t id) {
|
|
|
|
ASSERT(id >= 0);
|
|
|
|
this->id = id;
|
2022-12-06 07:47:58 +08:00
|
|
|
prefix = TenantAPI::idToPrefix(id);
|
2022-07-21 03:56:00 +08:00
|
|
|
}
|
2022-05-27 03:49:22 +08:00
|
|
|
|
2022-09-08 23:22:36 +08:00
|
|
|
std::string TenantMapEntry::toJson() const {
|
2022-07-19 02:24:49 +08:00
|
|
|
json_spirit::mObject tenantEntry;
|
|
|
|
tenantEntry["id"] = id;
|
2022-12-06 04:26:01 +08:00
|
|
|
tenantEntry["name"] = binaryToJson(tenantName);
|
|
|
|
tenantEntry["prefix"] = binaryToJson(prefix);
|
2022-07-19 02:24:49 +08:00
|
|
|
|
|
|
|
if (tenantGroup.present()) {
|
2022-12-06 04:26:01 +08:00
|
|
|
tenantEntry["tenant_group"] = binaryToJson(tenantGroup.get());
|
2022-07-19 02:24:49 +08:00
|
|
|
}
|
|
|
|
|
2023-02-09 02:39:46 +08:00
|
|
|
tenantEntry["lock_state"] = TenantAPI::tenantLockStateToString(tenantLockState);
|
2023-02-24 05:25:36 +08:00
|
|
|
if (tenantLockId.present()) {
|
|
|
|
tenantEntry["lock_id"] = tenantLockId.get().toString();
|
|
|
|
}
|
2023-02-09 02:39:46 +08:00
|
|
|
|
2022-07-23 03:51:24 +08:00
|
|
|
return json_spirit::write_string(json_spirit::mValue(tenantEntry));
|
|
|
|
}
|
2022-07-09 06:56:22 +08:00
|
|
|
|
2022-07-23 03:51:24 +08:00
|
|
|
bool TenantMapEntry::matchesConfiguration(TenantMapEntry const& other) const {
|
2023-02-28 08:53:13 +08:00
|
|
|
return tenantGroup == other.tenantGroup && tenantLockState == other.tenantLockState &&
|
|
|
|
tenantLockId == other.tenantLockId;
|
2022-07-23 03:51:24 +08:00
|
|
|
}
|
2022-07-23 02:38:23 +08:00
|
|
|
|
2022-07-23 03:51:24 +08:00
|
|
|
void TenantMapEntry::configure(Standalone<StringRef> parameter, Optional<Value> value) {
|
|
|
|
if (parameter == "tenant_group"_sr) {
|
|
|
|
tenantGroup = value;
|
|
|
|
} else {
|
|
|
|
TraceEvent(SevWarnAlways, "UnknownTenantConfigurationParameter").detail("Parameter", parameter);
|
|
|
|
throw invalid_tenant_configuration();
|
|
|
|
}
|
2022-07-19 02:24:49 +08:00
|
|
|
}
|
|
|
|
|
2023-02-10 00:42:23 +08:00
|
|
|
bool TenantMapEntry::operator==(TenantMapEntry const& other) const {
|
2023-02-17 02:14:29 +08:00
|
|
|
return id == other.id && tenantName == other.tenantName && tenantLockState == other.tenantLockState &&
|
2023-02-25 02:32:37 +08:00
|
|
|
tenantLockId == other.tenantLockId && tenantGroup == other.tenantGroup &&
|
|
|
|
configurationSequenceNum == other.configurationSequenceNum;
|
2023-02-10 00:42:23 +08:00
|
|
|
}
|
|
|
|
|
2023-02-18 12:57:49 +08:00
|
|
|
bool TenantMapEntry::operator!=(TenantMapEntry const& other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2022-09-23 04:24:21 +08:00
|
|
|
json_spirit::mObject TenantGroupEntry::toJson() const {
|
|
|
|
json_spirit::mObject tenantGroupEntry;
|
2023-02-24 07:11:49 +08:00
|
|
|
// No fields currently
|
2022-09-23 04:24:21 +08:00
|
|
|
return tenantGroupEntry;
|
|
|
|
}
|
|
|
|
|
2023-02-18 12:57:49 +08:00
|
|
|
bool TenantGroupEntry::operator==(TenantGroupEntry const& other) const {
|
2023-02-24 07:11:49 +08:00
|
|
|
return true;
|
2023-02-18 12:57:49 +08:00
|
|
|
}
|
|
|
|
bool TenantGroupEntry::operator!=(TenantGroupEntry const& other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TenantTombstoneCleanupData::operator==(TenantTombstoneCleanupData const& other) const {
|
|
|
|
return tombstonesErasedThrough == other.tombstonesErasedThrough &&
|
|
|
|
nextTombstoneEraseVersion == other.nextTombstoneEraseVersion &&
|
|
|
|
nextTombstoneEraseId == other.nextTombstoneEraseId;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TenantTombstoneCleanupData::operator!=(TenantTombstoneCleanupData const& other) const {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2023-02-24 07:11:49 +08:00
|
|
|
TenantMetadataSpecification<StandardTenantTypes>& TenantMetadata::instance() {
|
|
|
|
static TenantMetadataSpecification _instance = TenantMetadataSpecification<StandardTenantTypes>("\xff/"_sr);
|
2022-08-08 23:29:49 +08:00
|
|
|
return _instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
Key TenantMetadata::tenantMapPrivatePrefix() {
|
|
|
|
static Key _prefix = "\xff"_sr.withSuffix(tenantMap().subspace.begin);
|
|
|
|
return _prefix;
|
|
|
|
}
|
|
|
|
|
2023-02-11 07:56:41 +08:00
|
|
|
KeyBackedProperty<int64_t>& TenantMetadata::tenantIdPrefix() {
|
|
|
|
static KeyBackedProperty<int64_t> instance(TenantMetadata::instance().subspace.withSuffix("idPrefix"_sr));
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2022-09-17 02:20:34 +08:00
|
|
|
TEST_CASE("/fdbclient/libb64/base64decoder") {
|
|
|
|
Standalone<StringRef> buf = makeString(100);
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
int length = deterministicRandom()->randomInt(0, 100);
|
|
|
|
deterministicRandom()->randomBytes(mutateString(buf), length);
|
|
|
|
|
|
|
|
StringRef str = buf.substr(0, length);
|
|
|
|
std::string encodedStr = base64::encoder::from_string(str.toString());
|
|
|
|
// Remove trailing newline
|
|
|
|
encodedStr.resize(encodedStr.size() - 1);
|
|
|
|
|
|
|
|
std::string decodedStr = base64::decoder::from_string(encodedStr);
|
|
|
|
ASSERT(decodedStr == str.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2022-02-24 00:06:39 +08:00
|
|
|
TEST_CASE("/fdbclient/TenantMapEntry/Serialization") {
|
2023-02-09 02:39:46 +08:00
|
|
|
TenantMapEntry entry1(1, "name"_sr);
|
2022-02-24 00:06:39 +08:00
|
|
|
ASSERT(entry1.prefix == "\x00\x00\x00\x00\x00\x00\x00\x01"_sr);
|
2022-06-30 01:58:58 +08:00
|
|
|
TenantMapEntry entry2 = TenantMapEntry::decode(entry1.encode());
|
2022-02-24 00:06:39 +08:00
|
|
|
ASSERT(entry1.id == entry2.id && entry1.prefix == entry2.prefix);
|
|
|
|
|
2023-02-09 02:39:46 +08:00
|
|
|
TenantMapEntry entry3(std::numeric_limits<int64_t>::max(), "name"_sr);
|
2022-07-20 05:32:05 +08:00
|
|
|
ASSERT(entry3.prefix == "\x7f\xff\xff\xff\xff\xff\xff\xff"_sr);
|
2022-06-30 01:58:58 +08:00
|
|
|
TenantMapEntry entry4 = TenantMapEntry::decode(entry3.encode());
|
2022-02-24 00:06:39 +08:00
|
|
|
ASSERT(entry3.id == entry4.id && entry3.prefix == entry4.prefix);
|
|
|
|
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
int bits = deterministicRandom()->randomInt(1, 64);
|
2022-05-25 05:58:47 +08:00
|
|
|
int64_t min = bits == 1 ? 0 : (UINT64_C(1) << (bits - 1));
|
|
|
|
int64_t maxPlusOne = std::min<uint64_t>(UINT64_C(1) << bits, std::numeric_limits<int64_t>::max());
|
2022-02-24 00:06:39 +08:00
|
|
|
int64_t id = deterministicRandom()->randomInt64(min, maxPlusOne);
|
|
|
|
|
2023-02-09 02:39:46 +08:00
|
|
|
TenantMapEntry entry(id, "name"_sr);
|
2022-02-24 00:06:39 +08:00
|
|
|
int64_t bigEndianId = bigEndian64(id);
|
2022-07-20 05:32:05 +08:00
|
|
|
ASSERT(entry.id == id && entry.prefix == StringRef(reinterpret_cast<uint8_t*>(&bigEndianId), 8));
|
2022-02-24 00:06:39 +08:00
|
|
|
|
2022-06-30 01:58:58 +08:00
|
|
|
TenantMapEntry decodedEntry = TenantMapEntry::decode(entry.encode());
|
2022-05-25 05:58:47 +08:00
|
|
|
ASSERT(decodedEntry.id == entry.id && decodedEntry.prefix == entry.prefix);
|
2022-02-24 00:06:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|