foundationdb/fdbclient/GlobalConfig.actor.h

114 lines
3.1 KiB
C
Raw Normal View History

/*
* 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>
#include <map>
2021-02-25 10:29:53 +08:00
#include <type_traits>
#include <unordered_map>
#include "fdbclient/CommitProxyInterface.h"
#include "fdbclient/ReadYourWrites.h"
#include "flow/actorcompiler.h" // has to be last include
// 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.
// 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;
};
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>
const T get(KeyRef name) {
try {
2021-02-25 10:29:53 +08:00
auto any = get(name).value;
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>
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-25 10:29:53 +08:00
return defaultVal;
} catch (Error& e) {
throw;
}
}
// 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.
Future<Void> onInitialized();
private:
GlobalConfig();
void insert(KeyRef key, ValueRef value);
void erase(KeyRef key);
void erase(KeyRangeRef range);
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);
Database cx;
Future<Void> _updater;
Promise<Void> initialized;
2021-02-25 10:29:53 +08:00
std::unordered_map<StringRef, ConfigValue> data;
Version lastUpdate;
};
#endif