2021-02-13 10:55:01 +08:00
|
|
|
/*
|
|
|
|
* GlobalConfig.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
|
2021-02-13 10:55:01 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-03-18 11:41:46 +08:00
|
|
|
#include "fdbclient/DatabaseContext.h"
|
2021-02-13 10:55:01 +08:00
|
|
|
#include "fdbclient/GlobalConfig.actor.h"
|
2021-03-12 02:57:46 +08:00
|
|
|
#include "fdbclient/SpecialKeySpace.actor.h"
|
2021-02-20 08:46:08 +08:00
|
|
|
#include "fdbclient/SystemData.h"
|
|
|
|
#include "fdbclient/Tuple.h"
|
|
|
|
#include "flow/flow.h"
|
|
|
|
#include "flow/genericactors.actor.h"
|
2021-02-13 10:55:01 +08:00
|
|
|
|
2021-04-14 03:50:18 +08:00
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
2021-02-13 10:55:01 +08:00
|
|
|
|
2022-09-20 02:35:58 +08:00
|
|
|
const KeyRef fdbClientInfoTxnSampleRate = "config/fdb_client_info/client_txn_sample_rate"_sr;
|
|
|
|
const KeyRef fdbClientInfoTxnSizeLimit = "config/fdb_client_info/client_txn_size_limit"_sr;
|
2021-02-24 08:17:05 +08:00
|
|
|
|
2022-09-20 02:35:58 +08:00
|
|
|
const KeyRef transactionTagSampleRate = "config/transaction_tag_sample_rate"_sr;
|
|
|
|
const KeyRef transactionTagSampleCost = "config/transaction_tag_sample_cost"_sr;
|
2021-02-24 08:17:05 +08:00
|
|
|
|
2022-09-20 02:35:58 +08:00
|
|
|
const KeyRef samplingFrequency = "visibility/sampling/frequency"_sr;
|
|
|
|
const KeyRef samplingWindow = "visibility/sampling/window"_sr;
|
2021-06-04 06:10:04 +08:00
|
|
|
|
2022-05-17 08:23:09 +08:00
|
|
|
GlobalConfig::GlobalConfig(DatabaseContext* cx) : cx(cx), lastUpdate(0) {}
|
2021-02-13 10:55:01 +08:00
|
|
|
|
2022-04-28 03:45:24 +08:00
|
|
|
void GlobalConfig::applyChanges(Transaction& tr,
|
|
|
|
const VectorRef<KeyValueRef>& insertions,
|
|
|
|
const VectorRef<KeyRangeRef>& clears) {
|
|
|
|
VersionHistory vh{ 0 };
|
|
|
|
for (const auto& kv : insertions) {
|
|
|
|
vh.mutations.emplace_back_deep(vh.mutations.arena(), MutationRef(MutationRef::SetValue, kv.key, kv.value));
|
|
|
|
tr.set(kv.key.withPrefix(globalConfigKeysPrefix), kv.value);
|
|
|
|
}
|
|
|
|
for (const auto& range : clears) {
|
|
|
|
vh.mutations.emplace_back_deep(vh.mutations.arena(),
|
|
|
|
MutationRef(MutationRef::ClearRange, range.begin, range.end));
|
|
|
|
tr.clear(
|
|
|
|
KeyRangeRef(range.begin.withPrefix(globalConfigKeysPrefix), range.end.withPrefix(globalConfigKeysPrefix)));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Record the mutations in this commit into the global configuration history.
|
|
|
|
Key historyKey = addVersionStampAtEnd(globalConfigHistoryPrefix);
|
|
|
|
ObjectWriter historyWriter(IncludeVersion());
|
|
|
|
historyWriter.serialize(vh);
|
|
|
|
tr.atomicOp(historyKey, historyWriter.toStringRef(), MutationRef::SetVersionstampedKey);
|
|
|
|
|
|
|
|
// Write version key to trigger update in cluster controller.
|
|
|
|
tr.atomicOp(globalConfigVersionKey,
|
2022-09-20 02:35:58 +08:00
|
|
|
"0123456789\x00\x00\x00\x00"_sr, // versionstamp
|
2022-04-28 03:45:24 +08:00
|
|
|
MutationRef::SetVersionstampedValue);
|
|
|
|
}
|
|
|
|
|
2021-03-12 02:57:46 +08:00
|
|
|
Key GlobalConfig::prefixedKey(KeyRef key) {
|
|
|
|
return key.withPrefix(SpecialKeySpace::getModuleRange(SpecialKeySpace::MODULE::GLOBALCONFIG).begin);
|
|
|
|
}
|
|
|
|
|
2021-03-20 04:28:03 +08:00
|
|
|
const Reference<ConfigValue> GlobalConfig::get(KeyRef name) {
|
2021-02-13 10:55:01 +08:00
|
|
|
auto it = data.find(name);
|
|
|
|
if (it == data.end()) {
|
2021-03-20 04:28:03 +08:00
|
|
|
return Reference<ConfigValue>();
|
2021-02-13 10:55:01 +08:00
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2021-03-20 04:28:03 +08:00
|
|
|
const std::map<KeyRef, Reference<ConfigValue>> GlobalConfig::get(KeyRangeRef range) {
|
|
|
|
std::map<KeyRef, Reference<ConfigValue>> results;
|
2021-02-20 06:00:07 +08:00
|
|
|
for (const auto& [key, value] : data) {
|
|
|
|
if (range.contains(key)) {
|
|
|
|
results[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2021-02-13 10:55:01 +08:00
|
|
|
Future<Void> GlobalConfig::onInitialized() {
|
|
|
|
return initialized.getFuture();
|
|
|
|
}
|
|
|
|
|
2021-06-03 11:21:44 +08:00
|
|
|
Future<Void> GlobalConfig::onChange() {
|
|
|
|
return configChanged.onTrigger();
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalConfig::trigger(KeyRef key, std::function<void(std::optional<std::any>)> fn) {
|
|
|
|
callbacks.emplace(key, std::move(fn));
|
|
|
|
}
|
|
|
|
|
2021-02-13 10:55:01 +08:00
|
|
|
void GlobalConfig::insert(KeyRef key, ValueRef value) {
|
2022-07-09 01:42:09 +08:00
|
|
|
// TraceEvent(SevInfo, "GlobalConfigInsert").detail("Key", key).detail("Value", value);
|
2021-03-20 04:28:03 +08:00
|
|
|
data.erase(key);
|
|
|
|
|
2021-02-27 01:27:55 +08:00
|
|
|
Arena arena(key.expectedSize() + value.expectedSize());
|
2021-02-20 06:00:07 +08:00
|
|
|
KeyRef stableKey = KeyRef(arena, key);
|
2021-02-20 08:46:08 +08:00
|
|
|
try {
|
2021-03-20 04:28:03 +08:00
|
|
|
std::any any;
|
2021-02-20 08:46:08 +08:00
|
|
|
Tuple t = Tuple::unpack(value);
|
|
|
|
if (t.getType(0) == Tuple::ElementType::UTF8) {
|
2021-03-20 04:28:03 +08:00
|
|
|
any = StringRef(arena, t.getString(0).contents());
|
2021-02-20 08:46:08 +08:00
|
|
|
} else if (t.getType(0) == Tuple::ElementType::INT) {
|
2021-03-20 04:28:03 +08:00
|
|
|
any = t.getInt(0);
|
2021-06-03 11:21:44 +08:00
|
|
|
} else if (t.getType(0) == Tuple::ElementType::BOOL) {
|
|
|
|
any = t.getBool(0);
|
2021-02-20 08:46:08 +08:00
|
|
|
} else if (t.getType(0) == Tuple::ElementType::FLOAT) {
|
2021-03-20 04:28:03 +08:00
|
|
|
any = t.getFloat(0);
|
2021-02-20 08:46:08 +08:00
|
|
|
} else if (t.getType(0) == Tuple::ElementType::DOUBLE) {
|
2021-03-20 04:28:03 +08:00
|
|
|
any = t.getDouble(0);
|
2022-06-03 08:44:10 +08:00
|
|
|
} else if (t.getType(0) == Tuple::ElementType::VERSIONSTAMP) {
|
|
|
|
any = t.getVersionstamp(0);
|
2021-02-20 08:46:08 +08:00
|
|
|
} else {
|
|
|
|
ASSERT(false);
|
|
|
|
}
|
2021-03-20 04:28:03 +08:00
|
|
|
data[stableKey] = makeReference<ConfigValue>(std::move(arena), std::move(any));
|
2021-06-03 11:21:44 +08:00
|
|
|
|
|
|
|
if (callbacks.find(stableKey) != callbacks.end()) {
|
|
|
|
callbacks[stableKey](data[stableKey]->value);
|
|
|
|
}
|
2021-02-20 08:46:08 +08:00
|
|
|
} catch (Error& e) {
|
2021-06-03 11:21:44 +08:00
|
|
|
TraceEvent(SevWarn, "GlobalConfigTupleParseError").detail("What", e.what());
|
2021-02-13 10:55:01 +08:00
|
|
|
}
|
|
|
|
}
|
2021-02-20 06:00:07 +08:00
|
|
|
|
2021-06-03 11:21:44 +08:00
|
|
|
void GlobalConfig::erase(Key key) {
|
|
|
|
erase(KeyRangeRef(key, keyAfter(key)));
|
2021-02-20 06:00:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalConfig::erase(KeyRangeRef range) {
|
2022-07-09 01:42:09 +08:00
|
|
|
// TraceEvent(SevInfo, "GlobalConfigErase").detail("Range", range);
|
2021-02-20 06:00:07 +08:00
|
|
|
auto it = data.begin();
|
|
|
|
while (it != data.end()) {
|
|
|
|
if (range.contains(it->first)) {
|
2021-06-03 11:21:44 +08:00
|
|
|
if (callbacks.find(it->first) != callbacks.end()) {
|
|
|
|
callbacks[it->first](std::nullopt);
|
|
|
|
}
|
2021-02-20 06:00:07 +08:00
|
|
|
it = data.erase(it);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-20 06:22:58 +08:00
|
|
|
|
2021-03-16 09:03:54 +08:00
|
|
|
// Updates local copy of global configuration by reading the entire key-range
|
2022-07-08 07:02:43 +08:00
|
|
|
// from storage (proxied through the GrvProxies).
|
2022-07-09 08:51:45 +08:00
|
|
|
ACTOR Future<Void> GlobalConfig::refresh(GlobalConfig* self, Version lastKnown) {
|
2022-07-09 01:42:09 +08:00
|
|
|
// TraceEvent trace(SevInfo, "GlobalConfigRefresh");
|
2021-06-03 11:21:44 +08:00
|
|
|
self->erase(KeyRangeRef(""_sr, "\xff"_sr));
|
2021-03-24 07:22:39 +08:00
|
|
|
|
2022-07-13 02:25:04 +08:00
|
|
|
state Backoff backoff(CLIENT_KNOBS->GLOBAL_CONFIG_REFRESH_BACKOFF, CLIENT_KNOBS->GLOBAL_CONFIG_REFRESH_MAX_BACKOFF);
|
2022-07-09 08:51:45 +08:00
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
GlobalConfigRefreshReply reply =
|
|
|
|
wait(timeoutError(basicLoadBalance(self->cx->getGrvProxies(UseProvisionalProxies::False),
|
|
|
|
&GrvProxyInterface::refreshGlobalConfig,
|
|
|
|
GlobalConfigRefreshRequest{ lastKnown }),
|
|
|
|
CLIENT_KNOBS->GLOBAL_CONFIG_REFRESH_TIMEOUT));
|
|
|
|
for (const auto& kv : reply.result) {
|
|
|
|
KeyRef systemKey = kv.key.removePrefix(globalConfigKeysPrefix);
|
|
|
|
self->insert(systemKey, kv.value);
|
|
|
|
}
|
|
|
|
return Void();
|
|
|
|
} catch (Error& e) {
|
|
|
|
wait(backoff.onError());
|
|
|
|
}
|
2021-02-20 06:22:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-16 09:03:54 +08:00
|
|
|
// Applies updates to the local copy of the global configuration when this
|
|
|
|
// process receives an updated history.
|
2021-06-03 11:21:44 +08:00
|
|
|
ACTOR Future<Void> GlobalConfig::updater(GlobalConfig* self, const ClientDBInfo* dbInfo) {
|
2021-02-20 06:22:58 +08:00
|
|
|
loop {
|
|
|
|
try {
|
2022-05-24 02:56:37 +08:00
|
|
|
if (self->initialized.canBeSet()) {
|
|
|
|
wait(self->cx->onConnected());
|
2021-02-20 06:22:58 +08:00
|
|
|
|
2022-07-09 08:51:45 +08:00
|
|
|
wait(self->refresh(self, -1));
|
2022-05-24 02:56:37 +08:00
|
|
|
self->initialized.send(Void());
|
2021-03-18 11:41:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 02:56:37 +08:00
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
wait(self->dbInfoChanged.onTrigger());
|
|
|
|
|
|
|
|
auto& history = dbInfo->history;
|
|
|
|
if (history.size() == 0) {
|
|
|
|
continue;
|
2021-02-20 06:22:58 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 02:56:37 +08:00
|
|
|
if (self->lastUpdate < history[0].version) {
|
|
|
|
// This process missed too many global configuration
|
|
|
|
// history updates or the protocol version changed, so it
|
|
|
|
// must re-read the entire configuration range.
|
2022-07-09 08:51:45 +08:00
|
|
|
wait(self->refresh(self, history.back().version));
|
2022-05-24 02:56:37 +08:00
|
|
|
if (dbInfo->history.size() > 0) {
|
|
|
|
self->lastUpdate = dbInfo->history.back().version;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Apply history in order, from lowest version to highest
|
|
|
|
// version. Mutation history should already be stored in
|
|
|
|
// ascending version order.
|
|
|
|
for (const auto& vh : history) {
|
|
|
|
if (vh.version <= self->lastUpdate) {
|
|
|
|
continue; // already applied this mutation
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& mutation : vh.mutations.contents()) {
|
|
|
|
if (mutation.type == MutationRef::SetValue) {
|
|
|
|
self->insert(mutation.param1, mutation.param2);
|
|
|
|
} else if (mutation.type == MutationRef::ClearRange) {
|
|
|
|
self->erase(KeyRangeRef(mutation.param1, mutation.param2));
|
|
|
|
} else {
|
|
|
|
ASSERT(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(vh.version > self->lastUpdate);
|
|
|
|
self->lastUpdate = vh.version;
|
2021-02-20 06:22:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-24 02:56:37 +08:00
|
|
|
self->configChanged.trigger();
|
|
|
|
} catch (Error& e) {
|
|
|
|
throw;
|
2021-02-20 06:22:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Error& e) {
|
2022-05-24 02:56:37 +08:00
|
|
|
// There shouldn't be any uncaught errors that fall to this point,
|
|
|
|
// but in case there are, catch them and restart the updater.
|
|
|
|
TraceEvent("GlobalConfigUpdaterError").error(e);
|
|
|
|
wait(delay(1.0));
|
2021-02-20 06:22:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|