public static async Task <ServicePartitionList> PerformAsync(
            FabricClient fabricClient,
            Uri serviceName,
            TimeSpan requestTimeout,
            int maxRetryCount,
            TimeSpan initialRetryInterval)
        {
            ServicePartitionList partitionList = null;
            await RetriableOperation.PerformAsync(
                async() =>
            {
                try
                {
                    // Get the list of partitions for the service
                    partitionList = await fabricClient.QueryManager.GetPartitionListAsync(
                        serviceName,
                        null,
                        requestTimeout,
                        CancellationToken.None);
                }
                catch (Exception e)
                {
                    if ((e is TimeoutException) || (e is FabricTransientException))
                    {
                        // For these exceptions, we would like to retry until the maximum
                        // retry count has been reached.
                        return(false);
                    }
                    throw;
                }

                // Check if all partitions are in ready state
                if (partitionList.Any(p => (p.PartitionStatus != ServicePartitionStatus.Ready)))
                {
                    // Some partitions are not ready. Let's check again after some time (unless
                    // the maximum retry count has been reached).
                    return(false);
                }

                // All partitions are in ready state
                return(true);
            },
                maxRetryCount,
                initialRetryInterval);

            return(partitionList);
        }
コード例 #2
0
        private async Task <long> GetRandomPartitionKeyAsync(Uri serviceUri)
        {
            ServicePartitionList partitions = await _fabricClient.QueryManager.GetPartitionListAsync(serviceUri);

            if ((partitions == null) || (!partitions.Any()))
            {
                throw new PartitionNotAvailableException($"{serviceUri.ToString()} is not Available now.");
            }

            List <long> list = partitions.Select(c => ((Int64RangePartitionInformation)c.PartitionInformation).LowKey).ToList();

            int max = list.Count;

            int index;

            lock (_syncLock)
            {
                index = _random.Next(0, max);
            }
            return(list[index]);
        }
コード例 #3
0
        public static void Initialize(StatefulServiceContext parameters)
        {
            lock (Semaphore)
            {
                if (RegistryServiceUri != null)
                {
                    return;
                }
                if (parameters == null)
                {
                    return;
                }

                try
                {
                    // Read settings from the DeviceActorServiceConfig section in the Settings.xml file
                    ICodePackageActivationContext activationContext = parameters.CodePackageActivationContext;
                    ConfigurationPackage          config            = activationContext.GetConfigurationPackageObject(ConfigurationPackage);
                    ConfigurationSection          section           = config.Settings.Sections[ConfigurationSection];

                    // Read the MessageBoxServiceUri setting from the Settings.xml file
                    if (section.Parameters.Any(
                            p => string.Compare(
                                p.Name,
                                MessageBoxServiceUriParameter,
                                StringComparison.InvariantCultureIgnoreCase) == 0))
                    {
                        ConfigurationProperty parameter = section.Parameters[MessageBoxServiceUriParameter];
                        if (!string.IsNullOrWhiteSpace(parameter?.Value))
                        {
                            MessageBoxServiceUri = new Uri(parameter.Value);
                        }
                    }
                    else
                    {
                        MessageBoxServiceUri = new Uri($"fabric:/{parameters.ServiceName.Segments[1]}MessageBoxService");
                    }
                    ActorEventSource.Current.Message($"[{MessageBoxServiceUriParameter}] = [{MessageBoxServiceUri}]");

                    // Read the RegistryServiceUri setting from the Settings.xml file
                    if (section.Parameters.Any(
                            p => string.Compare(
                                p.Name,
                                RegistryServiceUriParameter,
                                StringComparison.InvariantCultureIgnoreCase) == 0))
                    {
                        ConfigurationProperty parameter = section.Parameters[RegistryServiceUriParameter];
                        if (!string.IsNullOrWhiteSpace(parameter?.Value))
                        {
                            RegistryServiceUri = new Uri(parameter.Value);
                        }
                    }
                    else
                    {
                        RegistryServiceUri = new Uri($"fabric:/{parameters.ServiceName.Segments[1]}RegistryService");
                    }
                    ActorEventSource.Current.Message($"[{RegistryServiceUriParameter}] = [{RegistryServiceUri}]");

                    // Read the MaxQueryRetryCount setting from the Settings.xml file
                    if (section.Parameters.Any(
                            p => string.Compare(
                                p.Name,
                                MaxQueryRetryCountParameter,
                                StringComparison.InvariantCultureIgnoreCase) == 0))
                    {
                        ConfigurationProperty parameter = section.Parameters[MaxQueryRetryCount];
                        if (!string.IsNullOrWhiteSpace(parameter?.Value))
                        {
                            int.TryParse(parameter.Value, out MaxQueryRetryCount);
                        }
                    }
                    ActorEventSource.Current.Message($"[{MaxQueryRetryCountParameter}] = [{MaxQueryRetryCount}]");

                    // Read the MaxQueryRetryCount setting from the Settings.xml file
                    if (section.Parameters.Any(
                            p => string.Compare(
                                p.Name,
                                BackoffQueryDelayInSecondsParameter,
                                StringComparison.InvariantCultureIgnoreCase) == 0))
                    {
                        ConfigurationProperty parameter = section.Parameters[BackoffQueryDelayInSecondsParameter];
                        if (!string.IsNullOrWhiteSpace(parameter?.Value))
                        {
                            int value;
                            if (int.TryParse(parameter.Value, out value))
                            {
                                BackoffQueryDelay = TimeSpan.FromSeconds(value);
                            }
                        }
                    }
                    ActorEventSource.Current.Message($"[{BackoffQueryDelayInSecondsParameter}] = [{BackoffQueryDelay.TotalSeconds}]");
                }
                catch (KeyNotFoundException)
                {
                    RegistryServiceUri = new Uri($"fabric:/{parameters.ServiceName.Segments[1]}RegistryService");
                    ActorEventSource.Current.Message($"[{MessageBoxServiceUriParameter}] = [{MessageBoxServiceUri}]");
                    ActorEventSource.Current.Message($"[{RegistryServiceUriParameter}] = [{RegistryServiceUri}]");
                    ActorEventSource.Current.Message($"[{MaxQueryRetryCountParameter}] = [{MaxQueryRetryCount}]");
                    ActorEventSource.Current.Message($"[{BackoffQueryDelayInSecondsParameter}] = [{BackoffQueryDelay.TotalSeconds}]");
                }
                if (RegistryServiceUri == null)
                {
                    return;
                }
                FabricClient fabricClient = new FabricClient();

                ServicePartitionList list = fabricClient.QueryManager.GetPartitionListAsync(RegistryServiceUri).Result;
                RegistryServicePartitionCount = (list != null) && list.Any() ? list.Count : 1;
                ActorEventSource.Current.Message($"[{nameof(RegistryServicePartitionCount)}] = [{RegistryServicePartitionCount}]");

                list = fabricClient.QueryManager.GetPartitionListAsync(MessageBoxServiceUri).Result;
                MessageBoxServicePartitionCount = (list != null) && list.Any() ? list.Count : 1;
                ActorEventSource.Current.Message($"[{nameof(MessageBoxServicePartitionCount)}] = [{MessageBoxServicePartitionCount}]");
            }
        }
コード例 #4
0
        public Task <string> OpenAsync(CancellationToken cancellationToken)
        {
            try
            {
                // Read settings from the DeviceActorServiceConfig section in the Settings.xml file
                ICodePackageActivationContext activationContext = this.statelessServiceContext.CodePackageActivationContext;
                ConfigurationPackage          config            = activationContext.GetConfigurationPackageObject(ConfigurationPackage);
                ConfigurationSection          section           = config.Settings.Sections[ConfigurationSection];

                // Read the MessageBoxServiceUri setting from the Settings.xml file
                if (section.Parameters.Any(
                        p => string.Compare(
                            p.Name,
                            MessageBoxServiceUriParameter,
                            StringComparison.InvariantCultureIgnoreCase) == 0))
                {
                    ConfigurationProperty parameter = section.Parameters[MessageBoxServiceUriParameter];
                    if (!string.IsNullOrWhiteSpace(parameter?.Value))
                    {
                        MessageBoxServiceUri = new Uri(parameter.Value);
                    }
                }
                else
                {
                    MessageBoxServiceUri = new Uri($"fabric:/{this.statelessServiceContext.ServiceName.Segments[1]}MessageBoxService");
                }
                ActorEventSource.Current.Message($"[{MessageBoxServiceUriParameter}] = [{MessageBoxServiceUri}]");

                // Read the RegistryServiceUri setting from the Settings.xml file
                if (section.Parameters.Any(
                        p => string.Compare(
                            p.Name,
                            RegistryServiceUriParameter,
                            StringComparison.InvariantCultureIgnoreCase) == 0))
                {
                    ConfigurationProperty parameter = section.Parameters[RegistryServiceUriParameter];
                    if (!string.IsNullOrWhiteSpace(parameter?.Value))
                    {
                        RegistryServiceUri = new Uri(parameter.Value);
                    }
                }
                else
                {
                    RegistryServiceUri = new Uri($"fabric:/{this.statelessServiceContext.ServiceName.Segments[1]}RegistryService");
                }
                ActorEventSource.Current.Message($"[{RegistryServiceUriParameter}] = [{RegistryServiceUri}]");

                FabricClient fabricClient = new FabricClient();

                ServicePartitionList list = fabricClient.QueryManager.GetPartitionListAsync(RegistryServiceUri).Result;
                RegistryServicePartitionCount = (list != null) && list.Any() ? list.Count : 1;
                ActorEventSource.Current.Message($"[{nameof(RegistryServicePartitionCount)}] = [{RegistryServicePartitionCount}]");

                list = fabricClient.QueryManager.GetPartitionListAsync(MessageBoxServiceUri).Result;
                MessageBoxServicePartitionCount = (list != null) && list.Any() ? list.Count : 1;
                ActorEventSource.Current.Message($"[{nameof(MessageBoxServicePartitionCount)}] = [{MessageBoxServicePartitionCount}]");

                EndpointResourceDescription serviceEndpoint = this.statelessServiceContext.CodePackageActivationContext.GetEndpoint("ServiceEndpoint");
                int port = serviceEndpoint.Port;

                this.listeningAddress = string.Format(
                    CultureInfo.InvariantCulture,
                    "http://+:{0}/{1}",
                    port,
                    string.IsNullOrWhiteSpace(this.appRoot)
                        ? string.Empty
                        : this.appRoot.TrimEnd('/') + '/');

                this.serverHandle = WebApp.Start(this.listeningAddress, appBuilder => this.startup.Configuration(appBuilder));
                string publishAddress = this.listeningAddress.Replace("+", FabricRuntime.GetNodeContext().IPAddressOrFQDN);

                ServiceEventSource.Current.Message($"Listening on {publishAddress}");

                return(Task.FromResult(publishAddress));
            }
            catch (Exception ex)
            {
                ServiceEventSource.Current.Message(ex.Message);
                if (!string.IsNullOrWhiteSpace(ex.InnerException?.Message))
                {
                    ServiceEventSource.Current.Message(ex.InnerException.Message);
                }
                throw;
            }
        }