2021-02-13 10:55:01 +08:00
|
|
|
/*
|
|
|
|
* GlobalConfig.actor.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2021 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#if defined(NO_INTELLISENSE) && !defined(FDBCLIENT_GLOBALCONFIG_ACTOR_G_H)
|
|
|
|
#define FDBCLIENT_GLOBALCONFIG_ACTOR_G_H
|
|
|
|
#include "fdbclient/GlobalConfig.actor.g.h"
|
|
|
|
#elif !defined(FDBCLIENT_GLOBALCONFIG_ACTOR_H)
|
|
|
|
#define FDBCLIENT_GLOBALCONFIG_ACTOR_H
|
|
|
|
|
|
|
|
#include <any>
|
2021-02-20 06:00:07 +08:00
|
|
|
#include <map>
|
2021-02-25 10:29:53 +08:00
|
|
|
#include <type_traits>
|
2021-02-13 10:55:01 +08:00
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
#include "fdbclient/CommitProxyInterface.h"
|
|
|
|
#include "fdbclient/ReadYourWrites.h"
|
|
|
|
|
|
|
|
#include "flow/actorcompiler.h" // has to be last include
|
|
|
|
|
2021-02-20 08:46:08 +08:00
|
|
|
// The global configuration is a series of typed key-value pairs synced to all
|
|
|
|
// nodes (server and client) in an FDB cluster in an eventually consistent
|
|
|
|
// manner.
|
|
|
|
|
2021-02-24 08:17:05 +08:00
|
|
|
// Keys
|
|
|
|
extern const KeyRef fdbClientInfoTxnSampleRate;
|
|
|
|
extern const KeyRef fdbClientInfoTxnSizeLimit;
|
|
|
|
|
|
|
|
extern const KeyRef transactionTagSampleRate;
|
|
|
|
extern const KeyRef transactionTagSampleCost;
|
|
|
|
|
2021-02-25 10:29:53 +08:00
|
|
|
struct ConfigValue {
|
|
|
|
Arena arena;
|
|
|
|
std::any value;
|
|
|
|
};
|
|
|
|
|
2021-02-13 10:55:01 +08:00
|
|
|
class GlobalConfig {
|
|
|
|
public:
|
|
|
|
GlobalConfig(const GlobalConfig&) = delete;
|
|
|
|
GlobalConfig& operator=(const GlobalConfig&) = delete;
|
|
|
|
|
|
|
|
static void create(DatabaseContext* cx, Reference<AsyncVar<ClientDBInfo>> dbInfo);
|
|
|
|
static GlobalConfig& globalConfig();
|
2021-02-20 06:22:58 +08:00
|
|
|
|
2021-02-25 10:29:53 +08:00
|
|
|
const ConfigValue get(KeyRef name);
|
|
|
|
const std::map<KeyRef, ConfigValue> get(KeyRangeRef range);
|
2021-02-20 06:22:58 +08:00
|
|
|
|
2021-02-25 10:29:53 +08:00
|
|
|
template <typename T, typename std::enable_if<std::is_arithmetic<T>{}, bool>::type = true>
|
2021-02-24 08:17:05 +08:00
|
|
|
const T get(KeyRef name) {
|
|
|
|
try {
|
2021-02-25 10:29:53 +08:00
|
|
|
auto any = get(name).value;
|
2021-02-24 08:17:05 +08:00
|
|
|
return std::any_cast<T>(any);
|
|
|
|
} catch (Error& e) {
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-25 10:29:53 +08:00
|
|
|
template <typename T, typename std::enable_if<std::is_arithmetic<T>{}, bool>::type = true>
|
2021-02-24 08:17:05 +08:00
|
|
|
const T get(KeyRef name, T defaultVal) {
|
2021-02-25 10:29:53 +08:00
|
|
|
try {
|
|
|
|
auto any = get(name).value;
|
|
|
|
if (any.has_value()) {
|
|
|
|
return std::any_cast<T>(any);
|
|
|
|
}
|
2021-02-24 08:17:05 +08:00
|
|
|
|
2021-02-25 10:29:53 +08:00
|
|
|
return defaultVal;
|
|
|
|
} catch (Error& e) {
|
|
|
|
throw;
|
|
|
|
}
|
2021-02-24 08:17:05 +08:00
|
|
|
}
|
|
|
|
|
2021-02-20 08:46:08 +08:00
|
|
|
// To write into the global configuration, submit a transaction to
|
|
|
|
// \xff\xff/global_config/<your-key> with <your-value> encoded using the
|
|
|
|
// FDB tuple typecodes.
|
|
|
|
|
2021-02-13 10:55:01 +08:00
|
|
|
Future<Void> onInitialized();
|
|
|
|
|
|
|
|
private:
|
2021-02-25 03:23:29 +08:00
|
|
|
GlobalConfig();
|
|
|
|
|
2021-02-13 10:55:01 +08:00
|
|
|
void insert(KeyRef key, ValueRef value);
|
2021-02-20 06:00:07 +08:00
|
|
|
void erase(KeyRef key);
|
|
|
|
void erase(KeyRangeRef range);
|
2021-02-13 10:55:01 +08:00
|
|
|
|
2021-02-20 06:22:58 +08:00
|
|
|
ACTOR static Future<Void> refresh(GlobalConfig* self);
|
|
|
|
ACTOR static Future<Void> updater(GlobalConfig* self, Reference<AsyncVar<ClientDBInfo>> dbInfo);
|
2021-02-13 10:55:01 +08:00
|
|
|
|
|
|
|
Database cx;
|
|
|
|
Future<Void> _updater;
|
|
|
|
Promise<Void> initialized;
|
2021-02-25 10:29:53 +08:00
|
|
|
std::unordered_map<StringRef, ConfigValue> data;
|
2021-02-13 10:55:01 +08:00
|
|
|
Version lastUpdate;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|