2017-05-26 04:48:44 +08:00
/*
* LogSystemConfig . h
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013 - 2018 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 .
*/
# ifndef FDBSERVER_LOGSYSTEMCONFIG_H
# define FDBSERVER_LOGSYSTEMCONFIG_H
# pragma once
# include "TLogInterface.h"
# include "fdbrpc/ReplicationPolicy.h"
# include "DatabaseConfiguration.h"
template < class Interface >
struct OptionalInterface {
// Represents an interface with a known id() and possibly known actual endpoints.
// For example, an OptionalInterface<TLogInterface> represents a particular tlog by id, which you might or might not presently know how to communicate with
UID id ( ) const { return ident ; }
bool present ( ) const { return iface . present ( ) ; }
Interface const & interf ( ) const { return iface . get ( ) ; }
explicit OptionalInterface ( UID id ) : ident ( id ) { }
explicit OptionalInterface ( Interface const & i ) : ident ( i . id ( ) ) , iface ( i ) { }
OptionalInterface ( ) { }
std : : string toString ( ) const { return ident . toString ( ) ; }
bool operator = = ( UID const & r ) const { return ident = = r ; }
template < class Ar >
void serialize ( Ar & ar ) {
ar & iface ;
if ( ! iface . present ( ) ) ar & ident ;
else ident = iface . get ( ) . id ( ) ;
}
protected :
UID ident ;
Optional < Interface > iface ;
} ;
2017-07-10 05:46:16 +08:00
struct TLogSet {
std : : vector < OptionalInterface < TLogInterface > > tLogs ;
std : : vector < TLogInterface > logRouters ;
2017-05-26 04:48:44 +08:00
int32_t tLogWriteAntiQuorum , tLogReplicationFactor ;
std : : vector < LocalityData > tLogLocalities ; // Stores the localities of the log servers
2017-06-30 06:50:19 +08:00
IRepPolicyRef tLogPolicy ;
2017-07-10 05:46:16 +08:00
bool isLocal ;
bool hasBest ;
2017-05-26 04:48:44 +08:00
2017-07-10 05:46:16 +08:00
TLogSet ( ) : tLogWriteAntiQuorum ( 0 ) , tLogReplicationFactor ( 0 ) , isLocal ( true ) , hasBest ( true ) { }
2017-05-26 04:48:44 +08:00
std : : string toString ( ) const {
2017-07-10 05:46:16 +08:00
return format ( " anti: %d replication: %d local: %d best: %d routers: %d tLogs: %s " , tLogWriteAntiQuorum , tLogReplicationFactor , isLocal , hasBest , logRouters . size ( ) , describe ( tLogs ) . c_str ( ) ) ;
}
2017-05-26 04:48:44 +08:00
2017-07-10 05:46:16 +08:00
bool operator = = ( const TLogSet & rhs ) const {
if ( tLogWriteAntiQuorum ! = rhs . tLogWriteAntiQuorum | | tLogReplicationFactor ! = rhs . tLogReplicationFactor | | isLocal ! = rhs . isLocal | | hasBest ! = rhs . hasBest | | tLogs . size ( ) ! = rhs . tLogs . size ( ) ) {
return false ;
2017-05-26 04:48:44 +08:00
}
2017-07-10 05:46:16 +08:00
if ( ( tLogPolicy & & ! rhs . tLogPolicy ) | | ( ! tLogPolicy & & rhs . tLogPolicy ) | | ( tLogPolicy & & ( tLogPolicy - > info ( ) ! = rhs . tLogPolicy - > info ( ) ) ) ) {
return false ;
2017-05-26 04:48:44 +08:00
}
2017-07-10 05:46:16 +08:00
for ( int j = 0 ; j < tLogs . size ( ) ; j + + ) {
if ( tLogs [ j ] . id ( ) ! = rhs . tLogs [ j ] . id ( ) | | tLogs [ j ] . present ( ) ! = rhs . tLogs [ j ] . present ( ) | | ( tLogs [ j ] . present ( ) & & tLogs [ j ] . interf ( ) . commit . getEndpoint ( ) . token ! = rhs . tLogs [ j ] . interf ( ) . commit . getEndpoint ( ) . token ) ) {
return false ;
}
2017-06-30 06:50:19 +08:00
}
2017-07-10 05:46:16 +08:00
return true ;
}
bool isEqualIds ( TLogSet const & r ) const {
if ( tLogWriteAntiQuorum ! = r . tLogWriteAntiQuorum | | tLogReplicationFactor ! = r . tLogReplicationFactor | | isLocal ! = r . isLocal | | hasBest ! = r . hasBest | | tLogs . size ( ) ! = r . tLogs . size ( ) ) {
return false ;
2017-05-26 04:48:44 +08:00
}
2017-07-10 05:46:16 +08:00
if ( ( tLogPolicy & & ! r . tLogPolicy ) | | ( ! tLogPolicy & & r . tLogPolicy ) | | ( tLogPolicy & & ( tLogPolicy - > info ( ) ! = r . tLogPolicy - > info ( ) ) ) ) {
return false ;
2017-06-30 06:50:19 +08:00
}
2017-07-10 05:46:16 +08:00
for ( int i = 0 ; i < tLogs . size ( ) ; i + + ) {
if ( tLogs [ i ] . id ( ) ! = r . tLogs [ i ] . id ( ) ) {
return false ;
2017-05-26 04:48:44 +08:00
}
2017-07-10 05:46:16 +08:00
}
return true ;
}
template < class Ar >
void serialize ( Ar & ar ) {
ar & tLogs & logRouters & tLogWriteAntiQuorum & tLogReplicationFactor & tLogPolicy & tLogLocalities & isLocal & hasBest ;
}
} ;
struct OldTLogConf {
std : : vector < TLogSet > tLogs ;
Version epochEnd ;
OldTLogConf ( ) : epochEnd ( 0 ) { }
std : : string toString ( ) const {
return format ( " end: %d %s " , epochEnd , describe ( tLogs ) . c_str ( ) ) ;
}
bool operator = = ( const OldTLogConf & rhs ) const {
return tLogs = = rhs . tLogs & & epochEnd = = rhs . epochEnd ;
}
bool isEqualIds ( OldTLogConf const & r ) const {
if ( tLogs . size ( ) ! = r . tLogs . size ( ) ) {
return false ;
}
for ( int i = 0 ; i < tLogs . size ( ) ; i + + ) {
if ( ! tLogs [ i ] . isEqualIds ( r . tLogs [ i ] ) ) {
return false ;
2017-06-30 06:50:19 +08:00
}
2017-05-26 04:48:44 +08:00
}
2017-07-10 05:46:16 +08:00
return true ;
2017-05-26 04:48:44 +08:00
}
template < class Ar >
void serialize ( Ar & ar ) {
2017-07-10 05:46:16 +08:00
ar & tLogs & epochEnd ;
2017-05-26 04:48:44 +08:00
}
} ;
struct LogSystemConfig {
int logSystemType ;
2017-07-10 05:46:16 +08:00
std : : vector < TLogSet > tLogs ;
2017-05-26 04:48:44 +08:00
std : : vector < OldTLogConf > oldTLogs ;
2017-07-10 05:46:16 +08:00
LogSystemConfig ( ) : logSystemType ( 0 ) { }
2017-05-26 04:48:44 +08:00
2017-07-10 05:46:16 +08:00
std : : string toString ( ) const {
return format ( " type: %d oldGenerations: %d %s " , logSystemType , oldTLogs . size ( ) , describe ( tLogs ) . c_str ( ) ) ;
}
2017-05-26 04:48:44 +08:00
std : : vector < TLogInterface > allPresentLogs ( ) const {
std : : vector < TLogInterface > results ;
2017-07-10 05:46:16 +08:00
for ( int i = 0 ; i < tLogs . size ( ) ; i + + ) {
for ( int j = 0 ; j < tLogs [ i ] . tLogs . size ( ) ; j + + ) {
if ( tLogs [ i ] . tLogs [ j ] . present ( ) ) {
results . push_back ( tLogs [ i ] . tLogs [ j ] . interf ( ) ) ;
}
}
}
2017-05-26 04:48:44 +08:00
return results ;
}
bool operator = = ( const LogSystemConfig & rhs ) const { return isEqual ( rhs ) ; }
bool isEqual ( LogSystemConfig const & r ) const {
2017-07-10 05:46:16 +08:00
return logSystemType = = r . logSystemType & & tLogs = = r . tLogs & & oldTLogs = = r . oldTLogs ;
2017-05-26 04:48:44 +08:00
}
bool isEqualIds ( LogSystemConfig const & r ) const {
2017-07-10 05:46:16 +08:00
if ( logSystemType ! = r . logSystemType | | tLogs . size ( ) ! = r . tLogs . size ( ) | | oldTLogs . size ( ) ! = r . oldTLogs . size ( ) ) {
2017-05-26 04:48:44 +08:00
return false ;
}
for ( int i = 0 ; i < tLogs . size ( ) ; i + + ) {
2017-07-10 05:46:16 +08:00
if ( ! tLogs [ i ] . isEqualIds ( r . tLogs [ i ] ) ) {
2017-06-30 06:50:19 +08:00
return false ;
2017-07-10 05:46:16 +08:00
}
2017-06-30 06:50:19 +08:00
}
2017-05-26 04:48:44 +08:00
for ( int i = 0 ; i < oldTLogs . size ( ) ; i + + ) {
2017-07-10 05:46:16 +08:00
if ( ! oldTLogs [ i ] . isEqualIds ( r . oldTLogs [ i ] ) ) {
2017-05-26 04:48:44 +08:00
return false ;
2017-06-30 06:50:19 +08:00
}
2017-05-26 04:48:44 +08:00
}
return true ;
}
bool isNextGenerationOf ( LogSystemConfig const & r ) const {
2017-07-10 05:46:16 +08:00
if ( ! oldTLogs . size ( ) | | tLogs . size ( ) ! = oldTLogs [ 0 ] . tLogs . size ( ) ) {
2017-05-26 04:48:44 +08:00
return false ;
}
2017-07-10 05:46:16 +08:00
for ( int i = 0 ; i < tLogs . size ( ) ; i + + ) {
if ( ! tLogs [ i ] . isEqualIds ( oldTLogs [ 0 ] . tLogs [ i ] ) ) {
2017-06-30 06:50:19 +08:00
return false ;
2017-07-10 05:46:16 +08:00
}
2017-06-30 06:50:19 +08:00
}
2017-05-26 04:48:44 +08:00
return true ;
}
template < class Ar >
void serialize ( Ar & ar ) {
2017-07-10 05:46:16 +08:00
ar & logSystemType & tLogs & oldTLogs ;
2017-05-26 04:48:44 +08:00
}
} ;
# endif