Skip to content

Commit

Permalink
Integrate SqlConnectionInfo into SqlShardMapManagerCredentials (#190)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredmoo authored May 31, 2019
1 parent 8338abd commit 6fd726f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,14 @@ namespace Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement
internal sealed class SqlShardMapManagerCredentials
{
/// <summary>
/// Connection string for shard map manager database.
/// Shard map manager data source
/// </summary>
private SqlConnectionStringBuilder _connectionStringShardMapManager;
private readonly string _smmDataSource;

/// <summary>
/// Connection string for individual shards.
/// Shard map manager database
/// </summary>
private SqlConnectionStringBuilder _connectionStringShard;

/// <summary>
/// Secure credential for shard map manager data source.
/// </summary>
private SqlCredential _secureCredential;
private readonly string _smmInitialCatalog;

/// <summary>
/// Instantiates the object that holds the credentials for accessing SQL Servers
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

/// <summary>
/// Connection string for shard map manager database.
/// </summary>
public string ConnectionStringShardMapManager
{
get
{
return _connectionStringShardMapManager.ConnectionString;
}
this.ConnectionInfoShard =
connectionInfo.CloneWithUpdatedConnectionString(connectionStringShard.ConnectionString);
}

/// <summary>
/// Secure Credential for shard map manager database.
/// Connection info for shard map manager database.
/// </summary>
public SqlCredential SecureCredentialShardMapManager => this._secureCredential;
public SqlConnectionInfo ConnectionInfoShardMapManager { get; private set; }

/// <summary>
/// Connection string for shards.
/// Connection info for shards.
/// </summary>
public string ConnectionStringShard
{
get
{
return _connectionStringShard.ConnectionString;
}
}
public SqlConnectionInfo ConnectionInfoShard { get; private set; }

/// <summary>
/// Location of Shard Map Manager used for logging purpose.
Expand All @@ -138,8 +124,8 @@ public string ShardMapManagerLocation
{
return StringUtils.FormatInvariant(
"[DataSource={0} Database={1}]",
_connectionStringShardMapManager.DataSource,
_connectionStringShardMapManager.InitialCatalog);
_smmDataSource,
_smmInitialCatalog);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ protected abstract ShardManagementErrorCategory ErrorCategory
/// <returns>Connection string for LSM given its location.</returns>
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
Expand All @@ -566,9 +566,8 @@ protected string GetConnectionStringForShardLocation(ShardLocation location)
/// <returns>Connection string for LSM given its location.</returns>
protected SqlConnectionInfo GetSqlStoreConnectionInfoForShardLocation(ShardLocation location)
{
return new SqlConnectionInfo(
this.GetConnectionStringForShardLocation(location),
this.Manager.Credentials.SecureCredentialShardMapManager);
return this.Manager.Credentials.ConnectionInfoShardMapManager.CloneWithUpdatedConnectionString(
this.GetConnectionStringForShardLocation(location));
}

/// <summary>
Expand Down Expand Up @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,7 @@ private void EstablishConnnection()
{
_globalConnection = new SqlStoreConnection(
StoreConnectionKind.Global,
new SqlConnectionInfo(
_credentials.ConnectionStringShardMapManager,
_credentials.SecureCredentialShardMapManager));
_credentials.ConnectionInfoShardMapManager);
_globalConnection.Open();
}

Expand All @@ -330,9 +328,7 @@ private Task EstablishConnnectionAsync()
{
_globalConnection = new SqlStoreConnection(
StoreConnectionKind.Global,
new SqlConnectionInfo(
_credentials.ConnectionStringShardMapManager,
_credentials.SecureCredentialShardMapManager));
_credentials.ConnectionInfoShardMapManager);
return _globalConnection.OpenAsync();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down

0 comments on commit 6fd726f

Please sign in to comment.