From 6fd726f5588f0e162e3ac794f1a41605d4fbf2a1 Mon Sep 17 00:00:00 2001 From: Jared Moore Date: Thu, 30 May 2019 22:43:28 -0700 Subject: [PATCH] Integrate SqlConnectionInfo into SqlShardMapManagerCredentials (#190) --- .../SqlStore/SqlShardMapManagerCredentials.cs | 72 ++++++++----------- .../StoreOperations/Base/StoreOperation.cs | 11 ++- .../Base/StoreOperationGlobal.cs | 8 +-- .../Base/StoreOperationLocal.cs | 10 +-- 4 files changed, 41 insertions(+), 60 deletions(-) diff --git a/Src/ElasticScale.Client/ShardManagement/SqlStore/SqlShardMapManagerCredentials.cs b/Src/ElasticScale.Client/ShardManagement/SqlStore/SqlShardMapManagerCredentials.cs index 9e79d30..d5eea78 100644 --- a/Src/ElasticScale.Client/ShardManagement/SqlStore/SqlShardMapManagerCredentials.cs +++ b/Src/ElasticScale.Client/ShardManagement/SqlStore/SqlShardMapManagerCredentials.cs @@ -12,19 +12,14 @@ namespace Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement internal sealed class SqlShardMapManagerCredentials { /// - /// Connection string for shard map manager database. + /// Shard map manager data source /// - private SqlConnectionStringBuilder _connectionStringShardMapManager; + private readonly string _smmDataSource; /// - /// Connection string for individual shards. + /// Shard map manager database /// - private SqlConnectionStringBuilder _connectionStringShard; - - /// - /// Secure credential for shard map manager data source. - /// - private SqlCredential _secureCredential; + private readonly string _smmInitialCatalog; /// /// Instantiates the object that holds the credentials for accessing SQL Servers @@ -49,8 +44,6 @@ public SqlShardMapManagerCredentials(SqlConnectionInfo connectionInfo) { ExceptionUtils.DisallowNullArgument(connectionInfo, "connectionInfo"); - this._secureCredential = connectionInfo.Credential; - // 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(connectionInfo.ConnectionString); @@ -81,53 +74,46 @@ public SqlShardMapManagerCredentials(SqlConnectionInfo connectionInfo) SqlShardMapManagerCredentials.EnsureCredentials( connectionStringBuilder, "connectionString", - this._secureCredential); + connectionInfo.Credential); #endregion GSM Validation - // Copy the input connection strings. - _connectionStringShardMapManager = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString); + // Generate connectionInfoShardMapManager + SqlConnectionStringBuilder connectionStringShardMapManager = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString); - _connectionStringShardMapManager.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix( - _connectionStringShardMapManager.ApplicationName, + connectionStringShardMapManager.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix( + connectionStringShardMapManager.ApplicationName, GlobalConstants.ShardMapManagerInternalConnectionSuffixGlobal); - _connectionStringShard = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString); + _smmDataSource = connectionStringShardMapManager.DataSource; + _smmInitialCatalog = connectionStringShardMapManager.InitialCatalog; + + this.ConnectionInfoShardMapManager = + connectionInfo.CloneWithUpdatedConnectionString(connectionStringShardMapManager.ConnectionString); - _connectionStringShard.Remove("Data Source"); - _connectionStringShard.Remove("Initial Catalog"); + // Generate connectionInfoShard + SqlConnectionStringBuilder connectionStringShard = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString); - _connectionStringShard.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix( - _connectionStringShard.ApplicationName, + connectionStringShard.Remove("Data Source"); + connectionStringShard.Remove("Initial Catalog"); + + connectionStringShard.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix( + connectionStringShard.ApplicationName, GlobalConstants.ShardMapManagerInternalConnectionSuffixLocal); - } - /// - /// Connection string for shard map manager database. - /// - public string ConnectionStringShardMapManager - { - get - { - return _connectionStringShardMapManager.ConnectionString; - } + this.ConnectionInfoShard = + connectionInfo.CloneWithUpdatedConnectionString(connectionStringShard.ConnectionString); } /// - /// Secure Credential for shard map manager database. + /// Connection info for shard map manager database. /// - public SqlCredential SecureCredentialShardMapManager => this._secureCredential; + public SqlConnectionInfo ConnectionInfoShardMapManager { get; private set; } /// - /// Connection string for shards. + /// Connection info for shards. /// - public string ConnectionStringShard - { - get - { - return _connectionStringShard.ConnectionString; - } - } + public SqlConnectionInfo ConnectionInfoShard { get; private set; } /// /// Location of Shard Map Manager used for logging purpose. @@ -138,8 +124,8 @@ public string ShardMapManagerLocation { return StringUtils.FormatInvariant( "[DataSource={0} Database={1}]", - _connectionStringShardMapManager.DataSource, - _connectionStringShardMapManager.InitialCatalog); + _smmDataSource, + _smmInitialCatalog); } } diff --git a/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperation.cs b/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperation.cs index 1f5ca34..8da9913 100644 --- a/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperation.cs +++ b/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperation.cs @@ -552,7 +552,7 @@ protected abstract ShardManagementErrorCategory ErrorCategory /// Connection string for LSM given its location. protected string GetConnectionStringForShardLocation(ShardLocation location) { - return new SqlConnectionStringBuilder(this.Manager.Credentials.ConnectionStringShard) + return new SqlConnectionStringBuilder(this.Manager.Credentials.ConnectionInfoShardMapManager.ConnectionString) { DataSource = location.DataSource, InitialCatalog = location.Database @@ -566,9 +566,8 @@ protected string GetConnectionStringForShardLocation(ShardLocation location) /// Connection string for LSM given its location. protected SqlConnectionInfo GetSqlStoreConnectionInfoForShardLocation(ShardLocation location) { - return new SqlConnectionInfo( - this.GetConnectionStringForShardLocation(location), - this.Manager.Credentials.SecureCredentialShardMapManager); + return this.Manager.Credentials.ConnectionInfoShardMapManager.CloneWithUpdatedConnectionString( + this.GetConnectionStringForShardLocation(location)); } /// @@ -655,9 +654,7 @@ private void EstablishConnnections(bool undo) // Open global & local connections and acquire application level locks for the corresponding scope. _globalConnection = this.Manager.StoreConnectionFactory.GetConnection( StoreConnectionKind.Global, - new SqlConnectionInfo( - this.Manager.Credentials.ConnectionStringShardMapManager, - this.Manager.Credentials.SecureCredentialShardMapManager)); + this.Manager.Credentials.ConnectionInfoShardMapManager); _globalConnection.OpenWithLock(this.Id); diff --git a/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperationGlobal.cs b/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperationGlobal.cs index 211fafb..3e063a7 100644 --- a/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperationGlobal.cs +++ b/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperationGlobal.cs @@ -316,9 +316,7 @@ private void EstablishConnnection() { _globalConnection = new SqlStoreConnection( StoreConnectionKind.Global, - new SqlConnectionInfo( - _credentials.ConnectionStringShardMapManager, - _credentials.SecureCredentialShardMapManager)); + _credentials.ConnectionInfoShardMapManager); _globalConnection.Open(); } @@ -330,9 +328,7 @@ private Task EstablishConnnectionAsync() { _globalConnection = new SqlStoreConnection( StoreConnectionKind.Global, - new SqlConnectionInfo( - _credentials.ConnectionStringShardMapManager, - _credentials.SecureCredentialShardMapManager)); + _credentials.ConnectionInfoShardMapManager); return _globalConnection.OpenAsync(); } diff --git a/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperationLocal.cs b/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperationLocal.cs index c35f35b..2ad5dfe 100644 --- a/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperationLocal.cs +++ b/Src/ElasticScale.Client/ShardManagement/StoreOperations/Base/StoreOperationLocal.cs @@ -173,17 +173,19 @@ private void EstablishConnnection() { // Open connection. SqlConnectionStringBuilder localConnectionString = - new SqlConnectionStringBuilder(_credentials.ConnectionStringShard) + new SqlConnectionStringBuilder(_credentials.ConnectionInfoShard.ConnectionString) { DataSource = this.Location.DataSource, InitialCatalog = this.Location.Database }; + SqlConnectionInfo localConnectionInfo = + _credentials.ConnectionInfoShard.CloneWithUpdatedConnectionString( + localConnectionString.ConnectionString); + _localConnection = new SqlStoreConnection( StoreConnectionKind.LocalSource, - new SqlConnectionInfo( - localConnectionString.ConnectionString, - _credentials.SecureCredentialShardMapManager)); + localConnectionInfo); _localConnection.Open(); }