示例#1
0
        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);
        }
示例#2
0
        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));
            }
        }