2017-05-26 04:48:44 +08:00
|
|
|
/*
|
|
|
|
* DatabaseConfiguration.h
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
|
|
|
* Copyright 2013-2018 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-04-09 12:24:05 +08:00
|
|
|
#ifndef FDBCLIENT_DATABASECONFIGURATION_H
|
|
|
|
#define FDBCLIENT_DATABASECONFIGURATION_H
|
2017-05-26 04:48:44 +08:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "fdbclient/FDBTypes.h"
|
|
|
|
#include "fdbclient/CommitTransaction.h"
|
|
|
|
#include "fdbrpc/ReplicationPolicy.h"
|
2018-03-06 11:27:46 +08:00
|
|
|
#include "fdbclient/Status.h"
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
// SOMEDAY: Buggify DatabaseConfiguration
|
|
|
|
|
2018-03-06 11:27:46 +08:00
|
|
|
struct SatelliteInfo {
|
|
|
|
Key dcId;
|
|
|
|
int32_t priority;
|
|
|
|
|
|
|
|
SatelliteInfo() : priority(0) {}
|
|
|
|
|
|
|
|
struct sort_by_priority {
|
|
|
|
bool operator ()(SatelliteInfo const&a, SatelliteInfo const& b) const { return a.priority > b.priority; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
|
|
|
ar & dcId & priority;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct RegionInfo {
|
|
|
|
Key dcId;
|
|
|
|
int32_t priority;
|
|
|
|
|
|
|
|
IRepPolicyRef satelliteTLogPolicy;
|
|
|
|
int32_t satelliteDesiredTLogCount;
|
|
|
|
int32_t satelliteTLogReplicationFactor;
|
|
|
|
int32_t satelliteTLogWriteAntiQuorum;
|
|
|
|
int32_t satelliteTLogUsableDcs;
|
|
|
|
|
|
|
|
std::vector<SatelliteInfo> satellites;
|
|
|
|
|
2018-06-15 03:56:48 +08:00
|
|
|
RegionInfo() : priority(0), satelliteDesiredTLogCount(-1), satelliteTLogReplicationFactor(0), satelliteTLogWriteAntiQuorum(0), satelliteTLogUsableDcs(1) {}
|
2018-03-06 11:27:46 +08:00
|
|
|
|
|
|
|
struct sort_by_priority {
|
|
|
|
bool operator ()(RegionInfo const&a, RegionInfo const& b) const { return a.priority > b.priority; }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
2018-04-09 12:24:05 +08:00
|
|
|
ar & dcId & priority & satelliteTLogPolicy & satelliteDesiredTLogCount & satelliteTLogReplicationFactor & satelliteTLogWriteAntiQuorum & satelliteTLogUsableDcs & satellites;
|
2018-03-06 11:27:46 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
struct DatabaseConfiguration {
|
|
|
|
DatabaseConfiguration();
|
|
|
|
|
|
|
|
void applyMutation( MutationRef mutation );
|
|
|
|
bool set( KeyRef key, ValueRef value ); // Returns true if a configuration option that requires recovery to take effect is changed
|
|
|
|
bool clear( KeyRangeRef keys );
|
|
|
|
Optional<ValueRef> get( KeyRef key ) const;
|
|
|
|
|
|
|
|
bool isValid() const;
|
|
|
|
|
|
|
|
bool initialized;
|
|
|
|
|
|
|
|
std::string toString() const;
|
2018-03-06 11:27:46 +08:00
|
|
|
StatusObject toJSON(bool noPolicies = false) const;
|
|
|
|
|
|
|
|
RegionInfo getRegion( Optional<Key> dcId ) const {
|
|
|
|
if(!dcId.present()) {
|
|
|
|
return RegionInfo();
|
|
|
|
}
|
|
|
|
for(auto& r : regions) {
|
|
|
|
if(r.dcId == dcId.get()) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return RegionInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
int expectedLogSets( Optional<Key> dcId ) const {
|
2017-09-08 06:32:08 +08:00
|
|
|
int result = 1;
|
2018-03-06 11:27:46 +08:00
|
|
|
if(dcId.present() && getRegion(dcId.get()).satelliteTLogReplicationFactor > 0) {
|
2017-09-08 06:32:08 +08:00
|
|
|
result++;
|
|
|
|
}
|
2018-03-06 11:27:46 +08:00
|
|
|
|
2018-06-18 10:31:15 +08:00
|
|
|
if(usableRegions > 1) {
|
2017-09-08 06:32:08 +08:00
|
|
|
result++;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
2018-02-16 10:32:39 +08:00
|
|
|
int32_t minDatacentersRequired() const {
|
2018-03-06 11:27:46 +08:00
|
|
|
int minRequired = 0;
|
2018-03-14 03:59:07 +08:00
|
|
|
for(auto& r : regions) {
|
2018-03-06 11:27:46 +08:00
|
|
|
minRequired += 1 + r.satellites.size();
|
|
|
|
}
|
|
|
|
return minRequired;
|
|
|
|
}
|
|
|
|
int32_t minMachinesRequiredPerDatacenter() const {
|
|
|
|
int minRequired = std::max( remoteTLogReplicationFactor, std::max(tLogReplicationFactor, storageTeamSize) );
|
2018-03-14 03:59:07 +08:00
|
|
|
for(auto& r : regions) {
|
2018-03-06 11:27:46 +08:00
|
|
|
minRequired = std::max( minRequired, r.satelliteTLogReplicationFactor/std::max(1, r.satelliteTLogUsableDcs) );
|
|
|
|
}
|
|
|
|
return minRequired;
|
2018-02-16 10:32:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//Killing an entire datacenter counts as killing one machine in modes that support it
|
|
|
|
int32_t maxMachineFailuresTolerated() const {
|
2018-03-06 11:27:46 +08:00
|
|
|
int worstSatellite = regions.size() ? std::numeric_limits<int>::max() : 0;
|
2018-03-14 03:59:07 +08:00
|
|
|
for(auto& r : regions) {
|
2018-03-06 11:27:46 +08:00
|
|
|
worstSatellite = std::min(worstSatellite, r.satelliteTLogReplicationFactor - r.satelliteTLogWriteAntiQuorum);
|
|
|
|
}
|
2018-06-18 10:31:15 +08:00
|
|
|
if(usableRegions > 1 && worstSatellite > 0) {
|
2018-06-19 01:24:57 +08:00
|
|
|
return 1 + std::min(std::max(tLogReplicationFactor - 1 - tLogWriteAntiQuorum, worstSatellite - 1), storageTeamSize - 1);
|
2018-03-06 11:27:46 +08:00
|
|
|
} else if(worstSatellite > 0) {
|
2018-06-19 01:24:57 +08:00
|
|
|
return std::min(tLogReplicationFactor + worstSatellite - 2 - tLogWriteAntiQuorum, storageTeamSize - 1);
|
2018-02-16 10:32:39 +08:00
|
|
|
}
|
2018-06-19 01:24:57 +08:00
|
|
|
return std::min(tLogReplicationFactor - 1 - tLogWriteAntiQuorum, storageTeamSize - 1);
|
2018-02-16 10:32:39 +08:00
|
|
|
}
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
// MasterProxy Servers
|
|
|
|
int32_t masterProxyCount;
|
|
|
|
int32_t autoMasterProxyCount;
|
|
|
|
|
|
|
|
// Resolvers
|
|
|
|
int32_t resolverCount;
|
|
|
|
int32_t autoResolverCount;
|
|
|
|
|
|
|
|
// TLogs
|
2017-09-08 06:32:08 +08:00
|
|
|
IRepPolicyRef tLogPolicy;
|
2017-05-26 04:48:44 +08:00
|
|
|
int32_t desiredTLogCount;
|
|
|
|
int32_t autoDesiredTLogCount;
|
|
|
|
int32_t tLogWriteAntiQuorum;
|
|
|
|
int32_t tLogReplicationFactor;
|
|
|
|
KeyValueStoreType tLogDataStoreType;
|
|
|
|
|
2017-09-08 06:32:08 +08:00
|
|
|
// Storage Servers
|
|
|
|
IRepPolicyRef storagePolicy;
|
2017-05-26 04:48:44 +08:00
|
|
|
int32_t storageTeamSize;
|
|
|
|
KeyValueStoreType storageServerStoreType;
|
|
|
|
|
2017-09-08 06:32:08 +08:00
|
|
|
// Remote TLogs
|
2018-06-22 15:04:00 +08:00
|
|
|
int32_t desiredLogRouterCount;
|
2018-06-19 01:22:34 +08:00
|
|
|
int32_t remoteDesiredTLogCount;
|
2017-09-08 06:32:08 +08:00
|
|
|
int32_t remoteTLogReplicationFactor;
|
|
|
|
IRepPolicyRef remoteTLogPolicy;
|
|
|
|
|
2018-03-06 11:27:46 +08:00
|
|
|
//Data centers
|
2018-06-18 10:31:15 +08:00
|
|
|
int32_t usableRegions;
|
2018-03-06 11:27:46 +08:00
|
|
|
std::vector<RegionInfo> regions;
|
2017-09-08 06:32:08 +08:00
|
|
|
|
2017-05-26 04:48:44 +08:00
|
|
|
// Excluded servers (no state should be here)
|
|
|
|
bool isExcludedServer( NetworkAddress ) const;
|
|
|
|
std::set<AddressExclusion> getExcludedServers() const;
|
|
|
|
|
|
|
|
int32_t getDesiredProxies() const { if(masterProxyCount == -1) return autoMasterProxyCount; return masterProxyCount; }
|
|
|
|
int32_t getDesiredResolvers() const { if(resolverCount == -1) return autoResolverCount; return resolverCount; }
|
|
|
|
int32_t getDesiredLogs() const { if(desiredTLogCount == -1) return autoDesiredTLogCount; return desiredTLogCount; }
|
2018-06-19 01:22:34 +08:00
|
|
|
int32_t getDesiredRemoteLogs() const { if(remoteDesiredTLogCount == -1) return getDesiredLogs(); return remoteDesiredTLogCount; }
|
2018-03-06 11:27:46 +08:00
|
|
|
int32_t getDesiredSatelliteLogs( Optional<Key> dcId ) const {
|
|
|
|
auto desired = getRegion(dcId).satelliteDesiredTLogCount;
|
|
|
|
if(desired == -1) return autoDesiredTLogCount; return desired;
|
|
|
|
}
|
2018-06-18 10:31:15 +08:00
|
|
|
int32_t getRemoteTLogReplicationFactor() const { if(remoteTLogReplicationFactor == 0) return tLogReplicationFactor; return remoteTLogReplicationFactor; }
|
|
|
|
IRepPolicyRef getRemoteTLogPolicy() const { if(remoteTLogReplicationFactor == 0) return tLogPolicy; return remoteTLogPolicy; }
|
2017-05-26 04:48:44 +08:00
|
|
|
|
|
|
|
bool operator == ( DatabaseConfiguration const& rhs ) const {
|
|
|
|
const_cast<DatabaseConfiguration*>(this)->makeConfigurationImmutable();
|
|
|
|
const_cast<DatabaseConfiguration*>(&rhs)->makeConfigurationImmutable();
|
|
|
|
return rawConfiguration == rhs.rawConfiguration;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class Ar>
|
|
|
|
void serialize(Ar& ar) {
|
|
|
|
if (!ar.isDeserializing) makeConfigurationImmutable();
|
|
|
|
ar & rawConfiguration;
|
|
|
|
if (ar.isDeserializing) {
|
|
|
|
for(auto c=rawConfiguration.begin(); c!=rawConfiguration.end(); ++c)
|
|
|
|
setInternal(c->key, c->value);
|
2018-02-16 10:32:39 +08:00
|
|
|
setDefaultReplicationPolicy();
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fromKeyValues( Standalone<VectorRef<KeyValueRef>> rawConfig ) {
|
|
|
|
resetInternal();
|
|
|
|
this->rawConfiguration = rawConfig;
|
|
|
|
for(auto c=rawConfiguration.begin(); c!=rawConfiguration.end(); ++c)
|
|
|
|
setInternal(c->key, c->value);
|
2018-02-16 10:32:39 +08:00
|
|
|
setDefaultReplicationPolicy();
|
2017-05-26 04:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
Optional< std::map<std::string, std::string> > mutableConfiguration; // If present, rawConfiguration is not valid
|
|
|
|
Standalone<VectorRef<KeyValueRef>> rawConfiguration; // sorted by key
|
|
|
|
|
|
|
|
void makeConfigurationMutable();
|
|
|
|
void makeConfigurationImmutable();
|
|
|
|
|
|
|
|
bool setInternal( KeyRef key, ValueRef value );
|
|
|
|
void resetInternal();
|
|
|
|
void setDefaultReplicationPolicy();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|