Ejemplo n.º 1
0
 public void RequestSuspend(IInfrastructureCoordinator coordinator, Guid jobId)
 {
     coordinator
     .RunCommandAsync(true, "RequestSuspend:{0}".ToString(jobId), TimeSpan.MaxValue, CancellationToken.None)
     .GetAwaiter()
     .GetResult();
 }
Ejemplo n.º 2
0
        private string GetReplicatorAddress(IInfrastructureCoordinator coordinator, IConfigSection configSection)
        {
            string replicatorAddress = null;

            bool loopbackOnly = (coordinator is TestInfrastructureCoordinator);

            if (!loopbackOnly)
            {
                replicatorAddress = string.Format(
                    CultureInfo.InvariantCulture,
                    "{0}:{1}",
                    GetReplicatorHost(),
                    GetReplicatorPort(configSection));
            }

            return(replicatorAddress);
        }
Ejemplo n.º 3
0
        private async Task <IInfrastructureCoordinator> InitializeCoordinator(TimeSpan timeout, CancellationToken token)
        {
            Stopwatch watch = Stopwatch.StartNew();

            while (true)
            {
                try
                {
                    IInfrastructureCoordinator innerCoordinator = factory(factoryArgs);
                    return(innerCoordinator);
                }
                catch (Exception e)
                {
                    string message = "Failed to create infrastructure coordinator: {0}".ToString(e);
                    traceType.WriteWarning("{0}", message);

                    if (this.configSection.ReadConfigValue(ConfigKeyWarnOnInitFailure, true))
                    {
                        this.UpdateCoordinatorStatusHealthProperty(
                            HealthState.Warning,
                            message);
                    }
                    else
                    {
                        this.UpdateCoordinatorStatusHealthProperty(
                            HealthState.Ok,
                            "Coordinator is not expected to run.");
                    }
                }

                if (watch.Elapsed > timeout)
                {
                    traceType.WriteWarning("Timed out trying to create coordinator; exiting process");
                    ProcessCloser.ExitEvent.Set();
                }

                TimeSpan retryDelay = GetRetryDelay();
                traceType.WriteInfo("Retrying in {0}; remaining until timeout = {1}", retryDelay, timeout - watch.Elapsed);
                await Task.Delay(retryDelay, token).ConfigureAwait(false);
            }
        }
Ejemplo n.º 4
0
 private static IInfrastructureCoordinator CreateCoordinatorByReflection(
     string assemblyName,
     string factoryTypeName,
     params object[] factoryCreateMethodArgs)
 {
     try
     {
         var coordinatorFactory = CreateCoordinatorFactoryByReflection(assemblyName, factoryTypeName, factoryCreateMethodArgs);
         IInfrastructureCoordinator coordinator = coordinatorFactory.Create();
         return(coordinator);
     }
     catch (Exception ex)
     {
         TraceType.WriteError(
             "Unable to create infrastructure coordinator by reflection. Assembly name: {0}, Factory type name: {1}, Error: {2}",
             assemblyName,
             factoryTypeName,
             ex);
         throw;
     }
 }
Ejemplo n.º 5
0
        public async Task RunAsync(int primaryEpoch, CancellationToken token)
        {
            lock (stateLock)
            {
                if (this.isRunning)
                {
                    throw new InvalidOperationException("Coordinator is already running");
                }

                this.isRunning = true;
            }

            try
            {
                TimeSpan initTimeout = GetInitTimeout();
                traceType.WriteInfo("Initializing coordinator (timeout = {0})", initTimeout);
                IInfrastructureCoordinator currentCoordinator = await InitializeCoordinator(initTimeout, token).ConfigureAwait(false);

                traceType.WriteInfo("Coordinator created successfully");

                lock (stateLock)
                {
                    this.coordinator = currentCoordinator;
                }

                await currentCoordinator.RunAsync(primaryEpoch, token).ConfigureAwait(false);

                traceType.WriteInfo("Coordinator RunAsync completed normally");
            }
            finally
            {
                lock (stateLock)
                {
                    this.coordinator = null;
                    this.isRunning   = false;
                }

                traceType.WriteInfo("Exiting RunAsync");
            }
        }
Ejemplo n.º 6
0
        internal ServiceReplica(
            IInfrastructureAgentWrapper infrastructureServiceAgent,
            IInfrastructureCoordinator coordinator,
            string replicatorAddress,
            bool useClusterSecuritySettingsForReplicator,
            IConfigSection configSection)
        {
            if (infrastructureServiceAgent == null)
            {
                throw new ArgumentNullException("infrastructureServiceAgent");
            }
            if (coordinator == null)
            {
                throw new ArgumentNullException("coordinator");
            }
            if (configSection == null)
            {
                throw new ArgumentNullException("configSection");
            }

            this.infrastructureServiceAgent = infrastructureServiceAgent;
            this.coordinator       = coordinator;
            this.replicatorAddress = replicatorAddress;
            this.useClusterSecuritySettingsForReplicator = useClusterSecuritySettingsForReplicator;
            this.configSection = configSection;

            this.replicaRoleLock = new object();
            this.replicaRole     = ReplicaRole.Unknown;

            TraceType.WriteInfo("Created replica: coordinator = {0}, replicator address = {1}, useClusterSecuritySettingsForReplicator = {2}",
                                this.coordinator.GetType().FullName,
                                this.replicatorAddress ?? "<null>",
                                this.useClusterSecuritySettingsForReplicator);

            if (this.useClusterSecuritySettingsForReplicator)
            {
                this.clusterEndpointSecuritySettingsChangeNotifier = new ClusterEndpointSecuritySettingsChangeNotifier(this.replicatorAddress, this.UpdateReplicatorSettings);
            }
        }
Ejemplo n.º 7
0
        public IStatefulServiceReplica CreateReplica(
            string serviceTypeName,
            Uri serviceName,
            byte[] initializationData,
            Guid partitionId,
            long replicaId)
        {
            string replicaConfigSectionName = this.configSectionName;

            if (replicaConfigSectionName == null)
            {
                // When run as a system service, the configuration section name is encoded
                // in the service initialization data, because there can be multiple
                // InfrastructureService instances, each with its own config section.
                // When run as an application, a fixed configuration section name is used,
                // and passed into the factory constructor.
                replicaConfigSectionName = Encoding.Unicode.GetString(initializationData);
            }

            Trace.WriteInfo(
                TraceType,
                "CreateReplica: serviceName = {0}, partitionId = {1}, configSectionName = {2}",
                serviceName,
                partitionId,
                replicaConfigSectionName);

            try
            {
                var args = new CoordinatorFactoryArgs()
                {
                    Agent         = this.infrastructureServiceAgent,
                    ConfigSection = GetConfigSection(replicaConfigSectionName),
                    PartitionId   = partitionId,
                    ReplicaId     = replicaId,
                    ServiceName   = serviceName,
                };

                IInfrastructureCoordinator coordinator = CreateCoordinator(args);

                var replica = new ServiceReplica(
                    this.infrastructureServiceAgent,
                    coordinator,
                    GetReplicatorAddress(coordinator, args.ConfigSection),
                    UseClusterSecuritySettingsForReplicator(args.ConfigSection),
                    args.ConfigSection);

                return(replica);
            }
            catch (TargetInvocationException ex)
            {
                TraceType.WriteError("Error creating infrastructure coordinator. Cannot continue further. Exception: {0}", ex);

                if (ex.InnerException != null)
                {
                    // Rethrow, preserving the original stack trace from exceptions that
                    // come from dynamic invoke (e.g. factory creation) via reflection.
                    ExceptionDispatchInfo.Capture(ex.InnerException).Throw();
                }

                throw;
            }
            catch (Exception ex)
            {
                TraceType.WriteError("Error creating infrastructure coordinator. Cannot continue further. Exception: {0}", ex);
                throw;
            }
        }