/// <summary> /// Ensures that the provided connection string is valid and builds the connection string /// to be used for DDR connection to the given shard provider. /// </summary> /// <param name="shardProvider">Shard provider containing shard to be connected to.</param> /// <param name="connectionString">Input connection string.</param> /// <returns>Connection string for DDR connection.</returns> private string ValidateAndPrepareConnectionString(IShardProvider shardProvider, string connectionString) { Debug.Assert(shardProvider != null); Debug.Assert(connectionString != null); // Devnote: If connection string specifies Active Directory authentication and runtime is not // .NET 4.6 or higher, then below call will throw. SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(connectionString); // DataSource must not be set. if (!string.IsNullOrEmpty(connectionStringBuilder.DataSource)) { throw new ArgumentException( StringUtils.FormatInvariant( Errors._ShardMap_OpenConnection_ConnectionStringPropertyDisallowed, "DataSource"), "connectionString"); } // InitialCatalog must not be set. if (!string.IsNullOrEmpty(connectionStringBuilder.InitialCatalog)) { throw new ArgumentException( StringUtils.FormatInvariant( Errors._ShardMap_OpenConnection_ConnectionStringPropertyDisallowed, "Initial Catalog"), "connectionString"); } // ConnectRetryCount must not be set (default value is 1) if (ShardMapUtils.IsConnectionResiliencySupported && (int)connectionStringBuilder[ShardMapUtils.ConnectRetryCount] > 1) { throw new ArgumentException( StringUtils.FormatInvariant( Errors._ShardMap_OpenConnection_ConnectionStringPropertyDisallowed, ShardMapUtils.ConnectRetryCount), "connectionString"); } // Verify that either UserID/Password or provided or integrated authentication is enabled. SqlShardMapManagerCredentials.EnsureCredentials(connectionStringBuilder, "connectionString"); Shard s = shardProvider.ShardInfo; connectionStringBuilder.DataSource = s.Location.DataSource; connectionStringBuilder.InitialCatalog = s.Location.Database; // Append the proper post-fix for ApplicationName connectionStringBuilder.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix( connectionStringBuilder.ApplicationName, this.ApplicationNameSuffix); // Disable connection resiliency if necessary if (ShardMapUtils.IsConnectionResiliencySupported) { connectionStringBuilder[ShardMapUtils.ConnectRetryCount] = 0; } return(connectionStringBuilder.ConnectionString); }
private static ShardMapManager GetSqlShardMapManager( string connectionString, ShardMapManagerLoadPolicy loadPolicy, RetryBehavior retryBehavior, EventHandler <RetryingEventArgs> retryEventHandler, bool throwOnFailure) { Debug.Assert(connectionString != null); Debug.Assert(retryBehavior != null); SqlShardMapManagerCredentials credentials = new SqlShardMapManagerCredentials(connectionString); StoreOperationFactory storeOperationFactory = new StoreOperationFactory(); IStoreResults result; TransientFaultHandling.RetryPolicy retryPolicy = new TransientFaultHandling.RetryPolicy( new ShardManagementTransientErrorDetectionStrategy(retryBehavior), RetryPolicy.DefaultRetryPolicy.GetRetryStrategy()); EventHandler <TransientFaultHandling.RetryingEventArgs> handler = (sender, args) => { if (retryEventHandler != null) { retryEventHandler(sender, new RetryingEventArgs(args)); } }; try { retryPolicy.Retrying += handler; using (IStoreOperationGlobal op = storeOperationFactory.CreateGetShardMapManagerGlobalOperation( credentials, retryPolicy, throwOnFailure ? "GetSqlShardMapManager" : "TryGetSqlShardMapManager", throwOnFailure)) { result = op.Do(); } } finally { retryPolicy.Retrying -= handler; } return(result.Result == StoreResult.Success ? new ShardMapManager( credentials, new SqlStoreConnectionFactory(), storeOperationFactory, new CacheStore(), loadPolicy, RetryPolicy.DefaultRetryPolicy, retryBehavior, retryEventHandler) : null); }
/// <summary> /// Constructs an instance of SqlOperationLocal. /// </summary> /// <param name="credentials">Credentials for connecting to SMM databases.</param> /// <param name="retryPolicy">Retry policy for requests.</param> /// <param name="location">Shard location where the operation is to be performed.</param> /// <param name="operationName">Operation name.</param> internal StoreOperationLocal( SqlShardMapManagerCredentials credentials, TransientFaultHandling.RetryPolicy retryPolicy, ShardLocation location, string operationName) { _credentials = credentials; _retryPolicy = retryPolicy; this.OperationName = operationName; this.Location = location; }
/// <summary> /// Constructs request for deploying SMM storage objects to target GSM database. /// </summary> /// <param name="credentials">Credentials for connection.</param> /// <param name="retryPolicy">Retry policy.</param> /// <param name="operationName">Operation name, useful for diagnostics.</param> /// <param name="createMode">Creation mode.</param> /// <param name="targetVersion">target version of store to deploy</param> internal CreateShardMapManagerGlobalOperation( SqlShardMapManagerCredentials credentials, TransientFaultHandling.RetryPolicy retryPolicy, string operationName, ShardMapManagerCreateMode createMode, Version targetVersion) : base(credentials, retryPolicy, operationName) { _createMode = createMode; _targetVersion = targetVersion; }
/// <summary> /// Given the connection string, opens up the corresponding data source and obtains the ShardMapManager. /// </summary> /// <param name="credentials">Credentials for performing ShardMapManager operations.</param> /// <param name="storeConnectionFactory">Factory for store connections.</param> /// <param name="storeOperationFactory">Factory for store operations.</param> /// <param name="cacheStore">Cache store.</param> /// <param name="loadPolicy">Initialization policy.</param> /// <param name="retryPolicy">Policy for performing retries on connections to shard map manager database.</param> /// <param name="retryBehavior">Policy for detecting transient errors.</param> internal ShardMapManager( SqlShardMapManagerCredentials credentials, IStoreConnectionFactory storeConnectionFactory, IStoreOperationFactory storeOperationFactory, ICacheStore cacheStore, ShardMapManagerLoadPolicy loadPolicy, RetryPolicy retryPolicy, RetryBehavior retryBehavior) : this(credentials, storeConnectionFactory, storeOperationFactory, cacheStore, loadPolicy, retryPolicy, retryBehavior, null) { }
/// <summary> /// Constructs request for obtaining shard map manager object if the GSM has the SMM objects in it. /// </summary> /// <param name="credentials">Credentials for connection.</param> /// <param name="retryPolicy">Retry policy.</param> /// <param name="operationName">Operation name, useful for diagnostics.</param> /// <param name="throwOnFailure">Whether to throw exception on failure or return error code.</param> /// <returns>The store operation.</returns> public virtual IStoreOperationGlobal CreateGetShardMapManagerGlobalOperation( SqlShardMapManagerCredentials credentials, TransientFaultHandling.RetryPolicy retryPolicy, string operationName, bool throwOnFailure) { return(new GetShardMapManagerGlobalOperation( credentials, retryPolicy, operationName, throwOnFailure)); }
/// <summary> /// Instantiates the object that holds the credentials for accessing SQL Servers /// containing the shard map manager data. /// </summary> /// <param name="connectionString">Connection string for Shard map manager data source.</param> public SqlShardMapManagerCredentials(string connectionString) { ExceptionUtils.DisallowNullArgument(connectionString, "connectionString"); // Devnote: If connection string specifies Active Directory authentication and runtime is not // .NET 4.6 or higher, then below call will throw. SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(connectionString); #region GSM Validation // DataSource must be set. if (string.IsNullOrEmpty(connectionStringBuilder.DataSource)) { throw new ArgumentException( StringUtils.FormatInvariant( Errors._SqlShardMapManagerCredentials_ConnectionStringPropertyRequired, "DataSource"), "connectionString"); } // InitialCatalog must be set. if (string.IsNullOrEmpty(connectionStringBuilder.InitialCatalog)) { throw new ArgumentException( StringUtils.FormatInvariant( Errors._SqlShardMapManagerCredentials_ConnectionStringPropertyRequired, "Initial Catalog"), "connectionString"); } // Ensure credentials are specified for GSM connectivity. SqlShardMapManagerCredentials.EnsureCredentials(connectionStringBuilder, "connectionString"); #endregion GSM Validation // Copy the input connection strings. _connectionStringShardMapManager = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString); _connectionStringShardMapManager.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix( _connectionStringShardMapManager.ApplicationName, GlobalConstants.ShardMapManagerInternalConnectionSuffixGlobal); _connectionStringShard = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString); _connectionStringShard.Remove("Data Source"); _connectionStringShard.Remove("Initial Catalog"); _connectionStringShard.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix( _connectionStringShard.ApplicationName, GlobalConstants.ShardMapManagerInternalConnectionSuffixLocal); }
/// <summary> /// Constructs request for deploying SMM storage objects to target GSM database. /// </summary> /// <param name="credentials">Credentials for connection.</param> /// <param name="retryPolicy">Retry policy.</param> /// <param name="operationName">Operation name, useful for diagnostics.</param> /// <param name="createMode">Creation mode.</param> /// <param name="targetVersion">target version of store to deploy, this will be used mainly for upgrade testing purposes.</param> /// <returns>The store operation.</returns> public virtual IStoreOperationGlobal CreateCreateShardMapManagerGlobalOperation( SqlShardMapManagerCredentials credentials, TransientFaultHandling.RetryPolicy retryPolicy, string operationName, ShardMapManagerCreateMode createMode, Version targetVersion) { return(new CreateShardMapManagerGlobalOperation( credentials, retryPolicy, operationName, createMode, targetVersion)); }
/// <summary> /// Given the connection string, opens up the corresponding data source and obtains the ShardMapManager. /// </summary> /// <param name="credentials">Credentials for performing ShardMapManager operations.</param> /// <param name="storeConnectionFactory">Factory for store connections.</param> /// <param name="storeOperationFactory">Factory for store operations.</param> /// <param name="cacheStore">Cache store.</param> /// <param name="loadPolicy">Initialization policy.</param> /// <param name="retryPolicy">Policy for performing retries on connections to shard map manager database.</param> /// <param name="retryBehavior">Policy for detecting transient errors.</param> /// <param name="retryEventHandler">Event handler for store operation retry events.</param> internal ShardMapManager( SqlShardMapManagerCredentials credentials, IStoreConnectionFactory storeConnectionFactory, IStoreOperationFactory storeOperationFactory, ICacheStore cacheStore, ShardMapManagerLoadPolicy loadPolicy, RetryPolicy retryPolicy, RetryBehavior retryBehavior, EventHandler <RetryingEventArgs> retryEventHandler) { Debug.Assert(credentials != null); this.Credentials = credentials; this.StoreConnectionFactory = storeConnectionFactory; this.StoreOperationFactory = storeOperationFactory; this.Cache = cacheStore; this.RetryPolicy = new TransientFaultHandling.RetryPolicy( new ShardManagementTransientErrorDetectionStrategy(retryBehavior), retryPolicy.GetRetryStrategy()); // Register for TfhImpl.RetryPolicy.Retrying event. this.RetryPolicy.Retrying += this.ShardMapManagerRetryingEventHandler; // Add user specified event handler. if (retryEventHandler != null) { this.ShardMapManagerRetrying += retryEventHandler; } if (loadPolicy == ShardMapManagerLoadPolicy.Eager) { // We eagerly load everything from ShardMapManager. In case of lazy // loading policy, we will add things to local caches based on cache // misses on lookups. this.LoadFromStore(); } }
private static ShardMapManager CreateSqlShardMapManagerImpl( string connectionString, ShardMapManagerCreateMode createMode, RetryBehavior retryBehavior, EventHandler <RetryingEventArgs> retryEventHandler, Version targetVersion) { ExceptionUtils.DisallowNullArgument(connectionString, "connectionString"); ExceptionUtils.DisallowNullArgument(retryBehavior, "retryBehavior"); if (createMode != ShardMapManagerCreateMode.KeepExisting && createMode != ShardMapManagerCreateMode.ReplaceExisting) { throw new ArgumentException( StringUtils.FormatInvariant( Errors._General_InvalidArgumentValue, createMode, "createMode"), "createMode"); } using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid())) { Tracer.TraceInfo( TraceSourceConstants.ComponentNames.ShardMapManagerFactory, "CreateSqlShardMapManager", "Start; "); Stopwatch stopwatch = Stopwatch.StartNew(); SqlShardMapManagerCredentials credentials = new SqlShardMapManagerCredentials(connectionString); TransientFaultHandling.RetryPolicy retryPolicy = new TransientFaultHandling.RetryPolicy( new ShardManagementTransientErrorDetectionStrategy(retryBehavior), RetryPolicy.DefaultRetryPolicy.GetRetryStrategy()); EventHandler <TransientFaultHandling.RetryingEventArgs> handler = (sender, args) => { if (retryEventHandler != null) { retryEventHandler(sender, new RetryingEventArgs(args)); } }; try { retryPolicy.Retrying += handler; // specifying targetVersion as GlobalConstants.GsmVersionClient to deploy latest store by default. using (IStoreOperationGlobal op = new StoreOperationFactory().CreateCreateShardMapManagerGlobalOperation( credentials, retryPolicy, "CreateSqlShardMapManager", createMode, targetVersion)) { op.Do(); } stopwatch.Stop(); Tracer.TraceInfo( TraceSourceConstants.ComponentNames.ShardMapManagerFactory, "CreateSqlShardMapManager", "Complete; Duration: {0}", stopwatch.Elapsed); } finally { retryPolicy.Retrying -= handler; } return(new ShardMapManager( credentials, new SqlStoreConnectionFactory(), new StoreOperationFactory(), new CacheStore(), ShardMapManagerLoadPolicy.Lazy, RetryPolicy.DefaultRetryPolicy, retryBehavior, retryEventHandler)); } }
/// <summary> /// Constructs request for obtaining shard map manager object if the GSM has the SMM objects in it. /// </summary> /// <param name="credentials">Credentials for connection.</param> /// <param name="retryPolicy">Retry policy.</param> /// <param name="operationName">Operation name, useful for diagnostics.</param> /// <param name="throwOnFailure">Whether to throw exception on failure or return error code.</param> internal GetShardMapManagerGlobalOperation(SqlShardMapManagerCredentials credentials, TransientFaultHandling.RetryPolicy retryPolicy, string operationName, bool throwOnFailure) : base(credentials, retryPolicy, operationName) { _throwOnFailure = throwOnFailure; }
/// <summary> /// Constructs an instance of SqlOperationGlobal. /// </summary> /// <param name="credentials">Credentials for connecting to SMM GSM database.</param> /// <param name="retryPolicy">Retry policy for requests.</param> /// <param name="operationName">Operation name, useful for diagnostics.</param> internal StoreOperationGlobal(SqlShardMapManagerCredentials credentials, TransientFaultHandling.RetryPolicy retryPolicy, string operationName) { this.OperationName = operationName; _credentials = credentials; _retryPolicy = retryPolicy; }