/// <summary>
        /// Creates and adds a range mapping to ShardMap.
        /// </summary>
        /// <param name="range">Range for which to create the mapping.</param>
        /// <param name="shard">Shard associated with the range mapping.</param>
        /// <returns>Newly created mapping.</returns>
        public RangeMapping <TKey> CreateRangeMapping(Range <TKey> range, Shard shard)
        {
            ExceptionUtils.DisallowNullArgument(range, "range");
            ExceptionUtils.DisallowNullArgument(shard, "shard");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                RangeMappingCreationInfo <TKey> args = new RangeMappingCreationInfo <TKey>(range, shard, MappingStatus.Online);

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "CreateRangeMapping", "Start; Shard: {0}", shard.Location);

                Stopwatch stopwatch = Stopwatch.StartNew();

                RangeMapping <TKey> rangeMapping = this.rsm.Add(new RangeMapping <TKey>(this.Manager, args));

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "CreateRangeMapping", "Complete; Shard: {0}; Duration: {1}", shard.Location, stopwatch.Elapsed);

                return(rangeMapping);
            }
        }
 /// <summary>
 /// Gets all the mappings that exist within given range.
 /// </summary>
 /// <param name="range">Optional range value, if null, we cover everything.</param>
 /// <param name="shard">Optional shard parameter, if null, we cover all shards.</param>
 /// <returns>Read-only collection of mappings that overlap with given range.</returns>
 public IReadOnlyList <PointMapping <TKey> > GetMappingsForRange(Range <TKey> range, Shard shard)
 {
     return(this.GetMappingsForRange <PointMapping <TKey>, TKey>(
                range,
                shard,
                (smm, sm, ssm) => new PointMapping <TKey>(smm, sm, ssm),
                ShardManagementErrorCategory.ListShardMap,
                "PointMapping"));
 }
        /// <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="connectionInfo">Input connection info.</param>
        /// <returns>Connection string for DDR connection.</returns>
        private SqlConnectionInfo ValidateAndPrepareConnectionString(
            IShardProvider shardProvider,
            SqlConnectionInfo connectionInfo)
        {
            Debug.Assert(shardProvider != null);
            Debug.Assert(connectionInfo != null);
            Debug.Assert(connectionInfo.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(connectionInfo.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",
                connectionInfo.Credential,
                connectionInfo.AccessTokenFactory);

            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(connectionInfo.CloneWithUpdatedConnectionString(connectionStringBuilder.ConnectionString));
        }
示例#4
0
 /// <summary>
 /// Gets all the mappings that exist within given range.
 /// </summary>
 /// <param name="range">Optional range value, if null, we cover everything.</param>
 /// <param name="shard">Optional shard parameter, if null, we cover all shards.</param>
 /// <returns>Read-only collection of mappings that overlap with given range.</returns>
 internal IReadOnlyList <RangeMapping <TKey> > GetMappingsForRange(Range <TKey> range, Shard shard)
 {
     return(this.GetMappingsForRange <RangeMapping <TKey>, TKey>(
                range,
                shard,
                (smm, sm, ssm) => new RangeMapping <TKey>(smm, sm, ssm),
                ShardManagementErrorCategory.RangeShardMap,
                "RangeMapping"));
 }