/* * DatabaseContext.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 DatabaseContext_h #define DatabaseContext_h #pragma once #include "fdbclient/NativeAPI.actor.h" #include "fdbclient/KeyRangeMap.h" #include "fdbclient/MasterProxyInterface.h" #include "fdbclient/ClientDBInfo.h" #include "fdbrpc/QueueModel.h" #include "fdbrpc/MultiInterface.h" #include "flow/TDMetric.actor.h" #include "fdbclient/EventTypes.actor.h" #include "fdbrpc/ContinuousSample.h" class StorageServerInfo : public ReferencedInterface { public: static Reference getInterface( DatabaseContext *cx, StorageServerInterface const& interf, LocalityData const& locality ); void notifyContextDestroyed(); virtual ~StorageServerInfo(); private: DatabaseContext *cx; StorageServerInfo( DatabaseContext *cx, StorageServerInterface const& interf, LocalityData const& locality ) : cx(cx), ReferencedInterface(interf, locality) {} }; typedef MultiInterface> LocationInfo; typedef MultiInterface ProxyInfo; class DatabaseContext : public ReferenceCounted, public FastAllocated, NonCopyable { public: static DatabaseContext* allocateOnForeignThread() { return (DatabaseContext*)DatabaseContext::operator new(sizeof(DatabaseContext)); } // For internal (fdbserver) use only static Database create( Reference>> clusterInterface, Reference connFile, LocalityData const& clientLocality ); static Database create( Reference> clientInfo, Future clientInfoMonitor, LocalityData clientLocality, bool enableLocalityLoadBalance, int taskID=TaskDefaultEndpoint, bool lockAware=false, int apiVersion=Database::API_VERSION_LATEST ); ~DatabaseContext(); Database clone() const { return Database(new DatabaseContext( cluster, clientInfo, clientInfoMonitor, dbId, taskID, clientLocality, enableLocalityLoadBalance, lockAware, apiVersion )); } std::pair> getCachedLocation( const KeyRef&, bool isBackward = false ); bool getCachedLocations( const KeyRangeRef&, vector>>&, int limit, bool reverse ); Reference setCachedLocation( const KeyRangeRef&, const vector& ); void invalidateCache( const KeyRef&, bool isBackward = false ); void invalidateCache( const KeyRangeRef& ); Reference getMasterProxies(bool useProvisionalProxies); Future> getMasterProxiesFuture(bool useProvisionalProxies); Future onMasterProxiesChanged(); Future getHealthMetrics(bool detailed); // Update the watch counter for the database void addWatch(); void removeWatch(); void setOption( FDBDatabaseOptions::Option option, Optional value ); Error deferredError; bool lockAware; bool isError() { return deferredError.code() != invalid_error_code; } void checkDeferredError() { if(isError()) { throw deferredError; } } int apiVersionAtLeast(int minVersion) { return apiVersion < 0 || apiVersion >= minVersion; } Future onConnected(); // Returns after a majority of coordination servers are available and have reported a leader. The cluster file therefore is valid, but the database might be unavailable. Reference getConnectionFile(); //private: explicit DatabaseContext( Reference cluster, Reference> clientDBInfo, Future clientInfoMonitor, Standalone dbId, int taskID, LocalityData const& clientLocality, bool enableLocalityLoadBalance, bool lockAware, int apiVersion = Database::API_VERSION_LATEST ); explicit DatabaseContext( const Error &err ); // Key DB-specific information AsyncTrigger masterProxiesChangeTrigger; Future monitorMasterProxiesInfoChange; Reference masterProxies; bool provisional; UID masterProxiesLastChange; LocalityData clientLocality; QueueModel queueModel; bool enableLocalityLoadBalance; // Transaction start request batching struct VersionBatcher { PromiseStream< std::pair< Promise, Optional > > stream; Future actor; }; std::map versionBatcher; // Client status updater struct ClientStatusUpdater { std::vector< std::pair > inStatusQ; std::vector< std::pair > outStatusQ; Future actor; }; ClientStatusUpdater clientStatusUpdater; // Cache of location information int locationCacheSize; CoalescedKeyRangeMap< Reference > locationCache; std::map< UID, StorageServerInfo* > server_interf; Standalone dbId; int64_t transactionReadVersions; int64_t transactionLogicalReads; int64_t transactionPhysicalReads; int64_t transactionCommittedMutations; int64_t transactionCommittedMutationBytes; int64_t transactionsCommitStarted; int64_t transactionsCommitCompleted; int64_t transactionsTooOld; int64_t transactionsFutureVersions; int64_t transactionsNotCommitted; int64_t transactionsMaybeCommitted; int64_t transactionsResourceConstrained; int64_t transactionsProcessBehind; int64_t transactionWaitsForFullRecovery; ContinuousSample latencies, readLatencies, commitLatencies, GRVLatencies, mutationsPerCommit, bytesPerCommit; int outstandingWatches; int maxOutstandingWatches; double transactionTimeout; int transactionMaxRetries; double transactionMaxBackoff; int transactionMaxBytes; int snapshotRywEnabled; Future logger; int taskID; Int64MetricHandle getValueSubmitted; EventMetricHandle getValueCompleted; Reference> clientInfo; Future clientInfoMonitor; Reference cluster; int apiVersion; int mvCacheInsertLocation; std::vector>> metadataVersionCache; HealthMetrics healthMetrics; double healthMetricsLastUpdated; double detailedHealthMetricsLastUpdated; }; #endif