public async Task CreateStatefullServiceIfNotExists(ModelBase entity,
                                                            string entityTypeRootPath,
                                                            string serviceTypeName,
                                                            int minReplicaSetSize    = 3,
                                                            int targetReplicaSetSize = 3,
                                                            PartitionSchemeDescription partitionScheme = null
                                                            )
        {
            var client = new FabricClient();

            try
            {
                await client.ServiceManager.GetServiceDescriptionAsync(
                    new Uri($"fabric:/Dashboard/{entityTypeRootPath}/{entity.Key.ToLower()}"));
            }
            catch (FabricServiceNotFoundException)
            {
                await client.ServiceManager.CreateServiceAsync(new StatefulServiceDescription
                {
                    ApplicationName            = new Uri("fabric:/Dashboard"),
                    ServiceName                = new Uri($"fabric:/Dashboard/{entityTypeRootPath}/{entity.Key.ToLower()}"),
                    MinReplicaSetSize          = minReplicaSetSize,
                    TargetReplicaSetSize       = targetReplicaSetSize,
                    ServiceTypeName            = serviceTypeName,
                    PartitionSchemeDescription = partitionScheme ?? new SingletonPartitionSchemeDescription(),
                    HasPersistedState          = true
                });
            }
        }
示例#2
0
        public static PartitionSchemeDescription GetClone(this PartitionSchemeDescription partitionSchemeDesc)
        {
            if (partitionSchemeDesc is SingletonPartitionSchemeDescription)
            {
                return(new SingletonPartitionSchemeDescription());
            }

            if (partitionSchemeDesc is NamedPartitionSchemeDescription)
            {
                var n1 = partitionSchemeDesc as NamedPartitionSchemeDescription;
                var n2 = new NamedPartitionSchemeDescription();

                foreach (var name in n1.PartitionNames)
                {
                    n2.PartitionNames.Add(name);
                }

                return(n2);
            }

            var u1 = partitionSchemeDesc as UniformInt64RangePartitionSchemeDescription;
            var u2 = new UniformInt64RangePartitionSchemeDescription()
            {
                PartitionCount = u1.PartitionCount, LowKey = u1.LowKey, HighKey = u1.HighKey
            };

            return(u2);
        }
        public async Task CreateStatelessServiceIfNotExists(ModelBase entity,
                                                            string entityTypeRootPath,
                                                            string serviceTypeName,
                                                            PartitionSchemeDescription partitionScheme = null
                                                            )
        {
            var client = new FabricClient();

            try
            {
                await client.ServiceManager.GetServiceDescriptionAsync(
                    new Uri($"fabric:/Dashboard/{entityTypeRootPath}/{entity.Key.ToLower()}"));
            }
            catch (FabricServiceNotFoundException)
            {
                await client.ServiceManager.CreateServiceAsync(new StatelessServiceDescription()
                {
                    ApplicationName            = new Uri("fabric:/Dashboard"),
                    ServiceName                = new Uri($"fabric:/Dashboard/{entityTypeRootPath}/{entity.Key.ToLower()}"),
                    ServiceTypeName            = serviceTypeName,
                    PartitionSchemeDescription = partitionScheme ?? new SingletonPartitionSchemeDescription(),
                    InstanceCount              = -1
                });
            }
        }
        /// <summary>
        /// Gets the object from Json properties.
        /// </summary>
        /// <param name="reader">The <see cref="T: Newtonsoft.Json.JsonReader" /> to read from.</param>
        /// <returns>The object Value.</returns>
        internal static PartitionSchemeDescription GetFromJsonProperties(JsonReader reader)
        {
            PartitionSchemeDescription obj = null;
            var propName = reader.ReadPropertyName();

            if (!propName.Equals("PartitionScheme", StringComparison.Ordinal))
            {
                throw new JsonReaderException($"Incorrect discriminator property name {propName}, Expected discriminator property name is PartitionScheme.");
            }

            var propValue = reader.ReadValueAsString();

            if (propValue.Equals("Named", StringComparison.Ordinal))
            {
                obj = NamedPartitionSchemeDescriptionConverter.GetFromJsonProperties(reader);
            }
            else if (propValue.Equals("Singleton", StringComparison.Ordinal))
            {
                obj = SingletonPartitionSchemeDescriptionConverter.GetFromJsonProperties(reader);
            }
            else if (propValue.Equals("UniformInt64Range", StringComparison.Ordinal))
            {
                obj = UniformInt64RangePartitionSchemeDescriptionConverter.GetFromJsonProperties(reader);
            }
            else
            {
                throw new InvalidOperationException("Unknown PartitionScheme.");
            }

            return(obj);
        }
 public RestartPool(
     string poolId                              = Constants.NoOpServiceTypeUri,
     string serviceTypeUri                      = Constants.NoOpServiceTypeUri,
     bool isServiceStateful                     = true,
     bool hasPersistedState                     = true,
     int minReplicas                            = 1,
     int targetReplicas                         = 3,
     int maxPoolSize                            = 20,
     int idleServicesPoolSize                   = 3,
     int servicesAllocationBlockSize            = 2,
     PartitionSchemeDescription partitionScheme = PartitionSchemeDescription.Singleton,
     double expirationQuanta                    = 15
     )
 {
     PoolId                      = poolId;
     ServiceTypeUri              = serviceTypeUri;
     IsServiceStateful           = isServiceStateful;
     HasPersistedState           = hasPersistedState;
     MinReplicas                 = minReplicas;
     TargetReplicas              = targetReplicas;
     MaxPoolSize                 = maxPoolSize;
     IdleServicesPoolSize        = idleServicesPoolSize;
     PartitionScheme             = partitionScheme;
     ServicesAllocationBlockSize = servicesAllocationBlockSize;
     ExpirationQuanta            = expirationQuanta;
 }
 public ServiceDescriptionFactory(string serviceTypeUri, Guid instanceId, PartitionSchemeDescription partitionSchemeDescription)
 {
     ParseServiceTypeUri(serviceTypeUri, out var applicationName, out var serviceTypeName);
     ApplicationName            = new Uri(applicationName, UriKind.RelativeOrAbsolute);
     ServiceTypeName            = serviceTypeName;
     ServiceName                = CreateServiceName(serviceTypeUri, instanceId);
     PartitionSchemeDescription = partitionSchemeDescription;
 }
        public void PartitionDescription_SingletonPartitionDescriptionIsParsed()
        {
            var expected = SingletonPartitionDescriptionTest.HelperInstance.CreateDefaultDescription();

            using (var pc = new PinCollection())
            {
                var actual = PartitionSchemeDescription.CreateFromNative((NativeTypes.FABRIC_PARTITION_SCHEME)expected.Scheme, expected.ToNative(pc));
                HelperInstance.Compare(expected, actual);
            }
        }
示例#8
0
        private bool TryCreatePartitionDescription(out PartitionSchemeDescription psd)
        {
            psd = null;

            if (this.control.Package.Data.ContainsKey("PartitionSingleton"))
            {
                psd = new SingletonPartitionSchemeDescription();
                return(true);
            }

            if (this.control.Package.Data.ContainsKey("PartitionNamed"))
            {
                NamedPartitionSchemeDescription d = new NamedPartitionSchemeDescription();

                string nameListValue;
                if (!this.control.Package.Data.TryGetValue("PartitionNamedList", out nameListValue))
                {
                    log.Error("named partition specified, but no named list was found");
                    return(false);
                }

                string[] names = nameListValue.Split(PackageSettings.NamedPartitionSplitChar);
                for (int i = 0; i < names.Length; i++)
                {
                    d.PartitionNames.Add(names[i]);
                }

                psd = d;
                return(true);
            }

            if (this.control.Package.Data.ContainsKey("PartitionUniform"))
            {
                UniformInt64RangePartitionSchemeDescription d = new UniformInt64RangePartitionSchemeDescription();

                long low;
                long high;
                int  count;

                if (!this.TryGetData("PartitionUniformLowKey", out low) ||
                    !this.TryGetData("PartitionUniformHighKey", out high) ||
                    !this.TryGetData("PartitionUniformCount", out count))
                {
                    return(false);
                }

                d.LowKey         = low;
                d.HighKey        = high;
                d.PartitionCount = count;
                psd = d;
                return(true);
            }

            return(false);
        }
示例#9
0
        public static int TotalPartitions(this PartitionSchemeDescription partitionSchemeDesc)
        {
            if (partitionSchemeDesc is SingletonPartitionSchemeDescription)
            {
                return(1);
            }

            if (partitionSchemeDesc is NamedPartitionSchemeDescription)
            {
                return((partitionSchemeDesc as NamedPartitionSchemeDescription).PartitionNames.Count);
            }

            return((partitionSchemeDesc as UniformInt64RangePartitionSchemeDescription).PartitionCount);
        }
示例#10
0
 public PoolConfiguration(TimeSpan expirationQuanta, bool hasPersistedState, int idleServicesPoolSize, bool isServiceStateful,
                          int maxPoolSize, int minReplicaSetSize, PartitionSchemeDescription partitionScheme, int servicesAllocationBlockSize,
                          int targetReplicasetSize)
 {
     ExpirationQuanta            = expirationQuanta;
     HasPersistedState           = hasPersistedState;
     IdleServicesPoolSize        = idleServicesPoolSize;
     IsServiceStateful           = isServiceStateful;
     MaxPoolSize                 = maxPoolSize;
     MinReplicaSetSize           = minReplicaSetSize;
     PartitionScheme             = partitionScheme;
     ServicesAllocationBlockSize = servicesAllocationBlockSize;
     TargetReplicasetSize        = targetReplicasetSize;
 }
示例#11
0
        public static ServicePartitionKey ToServicePartitionKey(this PartitionSchemeDescription desc, string instanceId)
        {
            switch (desc)
            {
            case PartitionSchemeDescription.UniformInt64Name:
                return(new ServicePartitionKey(1));

            case PartitionSchemeDescription.Named:
                return(new ServicePartitionKey(instanceId));

            default:
                return(null);
            }
        }
 public StartInstanceRequest(
     string serviceTypeUri,
     bool isServiceStateful = true,
     bool hasPersistedState = true,
     int minReplicas        = 1,
     int targetReplicas     = 3,
     PartitionSchemeDescription partitionScheme = PartitionSchemeDescription.UniformInt64Name,
     TimeSpan?expirationQuanta = null
     )
 {
     ServiceTypeUri    = serviceTypeUri;
     IsServiceStateful = isServiceStateful;
     HasPersistedState = hasPersistedState;
     MinReplicas       = minReplicas;
     TargetReplicas    = targetReplicas;
     PartitionScheme   = partitionScheme;
     ExpirationQuanta  = expirationQuanta ?? new TimeSpan(24, 0, 0);
 }
 public CreateService(
     Guid instanceId,
     string serviceTypeUri,
     bool isServiceStateful,
     bool hasPersistedState,
     int minReplicas,
     int targetReplicas,
     PartitionSchemeDescription partitionScheme
     )
 {
     InstanceId        = instanceId;
     ServiceTypeUri    = serviceTypeUri;
     IsServiceStateful = isServiceStateful;
     HasPersistedState = hasPersistedState;
     MinReplicas       = minReplicas;
     TargetReplicas    = targetReplicas;
     PartitionScheme   = partitionScheme;
 }
 public StartInstance(
     Guid instanceId,
     string serviceTypeUri,
     bool isServiceStateful,
     bool hasPersistedState,
     int minReplicas,
     int targetReplicas,
     PartitionSchemeDescription partitionScheme,
     TimeSpan expirationQuanta
     )
 {
     InstanceId        = instanceId;
     ServiceTypeUri    = serviceTypeUri;
     IsServiceStateful = isServiceStateful;
     HasPersistedState = hasPersistedState;
     MinReplicas       = minReplicas;
     TargetReplicas    = targetReplicas;
     PartitionScheme   = partitionScheme;
     ExpirationQuanta  = expirationQuanta;
 }
        /// <summary>
        /// Serializes the object to JSON.
        /// </summary>
        /// <param name="writer">The <see cref="T: Newtonsoft.Json.JsonWriter" /> to write to.</param>
        /// <param name="obj">The object to serialize to JSON.</param>
        internal static void Serialize(JsonWriter writer, PartitionSchemeDescription obj)
        {
            var kind = obj.PartitionScheme;

            if (kind.Equals(PartitionScheme.Named))
            {
                NamedPartitionSchemeDescriptionConverter.Serialize(writer, (NamedPartitionSchemeDescription)obj);
            }
            else if (kind.Equals(PartitionScheme.Singleton))
            {
                SingletonPartitionSchemeDescriptionConverter.Serialize(writer, (SingletonPartitionSchemeDescription)obj);
            }
            else if (kind.Equals(PartitionScheme.UniformInt64Range))
            {
                UniformInt64RangePartitionSchemeDescriptionConverter.Serialize(writer, (UniformInt64RangePartitionSchemeDescription)obj);
            }
            else
            {
                throw new InvalidOperationException("Unknown PartitionScheme.");
            }
        }
 public StartPoolRequest(
     bool isServiceStateful = true,
     bool hasPersistedState = true,
     int minReplicas        = 1,
     int targetReplicas     = 3,
     PartitionSchemeDescription partitionScheme = PartitionSchemeDescription.UniformInt64Name,
     int maxPoolSize                 = Int32.MaxValue,
     int idleServicesPoolSize        = 10,
     int servicesAllocationBlockSize = 5,
     TimeSpan?expirationQuanta       = null
     )
 {
     IsServiceStateful           = isServiceStateful;
     HasPersistedState           = hasPersistedState;
     MinReplicas                 = minReplicas;
     TargetReplicas              = targetReplicas;
     PartitionScheme             = partitionScheme;
     MaxPoolSize                 = maxPoolSize;
     IdleServicesPoolSize        = idleServicesPoolSize;
     ServicesAllocationBlockSize = servicesAllocationBlockSize;
     ExpirationQuanta            = expirationQuanta ?? new TimeSpan(24, 0, 0);
 }
示例#17
0
 public StartPool(
     string serviceTypeUri                      = Defaults.ServiceTypeUri,
     bool isServiceStateful                     = Defaults.IsServiceStateful,
     bool hasPersistedState                     = Defaults.HasPersistedState,
     int minReplicas                            = Defaults.MinReplicas,
     int targetReplicas                         = Defaults.TargetReplicas,
     int maxPoolSize                            = Defaults.MaxPoolSize,
     int idleServicesPoolSize                   = Defaults.IdleServicesPoolSize,
     int servicesAllocationBlockSize            = Defaults.ServicesAllocationBlockSize,
     PartitionSchemeDescription partitionScheme = Defaults.PartitionScheme,
     double expirationQuanta                    = Defaults.ExpirationQuanta
     )
 {
     ServiceTypeUri              = serviceTypeUri;
     IsServiceStateful           = isServiceStateful;
     HasPersistedState           = hasPersistedState;
     MinReplicas                 = minReplicas;
     TargetReplicas              = targetReplicas;
     MaxPoolSize                 = maxPoolSize;
     IdleServicesPoolSize        = idleServicesPoolSize;
     PartitionScheme             = partitionScheme;
     ServicesAllocationBlockSize = servicesAllocationBlockSize;
     ExpirationQuanta            = expirationQuanta;
 }
        /// <inheritdoc/>
        protected override void ProcessRecordInternal()
        {
            PartitionSchemeDescription partitionSchemeDescription = null;

            if (this.Named.IsPresent)
            {
                partitionSchemeDescription = new NamedPartitionSchemeDescription(
                    count: this.Count,
                    names: this.Names);
            }
            else if (this.Singleton.IsPresent)
            {
                partitionSchemeDescription = new SingletonPartitionSchemeDescription();
            }
            else if (this.UniformInt64Range.IsPresent)
            {
                partitionSchemeDescription = new UniformInt64RangePartitionSchemeDescription(
                    count: this.Count,
                    lowKey: this.LowKey,
                    highKey: this.HighKey);
            }

            ServiceDescription serviceDescription = null;

            if (this.Stateful.IsPresent)
            {
                serviceDescription = new StatefulServiceDescription(
                    serviceName: this.ServiceName,
                    serviceTypeName: this.ServiceTypeName,
                    partitionDescription: partitionSchemeDescription,
                    targetReplicaSetSize: this.TargetReplicaSetSize,
                    minReplicaSetSize: this.MinReplicaSetSize,
                    hasPersistedState: this.HasPersistedState,
                    applicationName: this.ApplicationName,
                    initializationData: this.InitializationData,
                    placementConstraints: this.PlacementConstraints,
                    correlationScheme: this.CorrelationScheme,
                    serviceLoadMetrics: this.ServiceLoadMetrics,
                    servicePlacementPolicies: this.ServicePlacementPolicies,
                    defaultMoveCost: this.DefaultMoveCost,
                    isDefaultMoveCostSpecified: this.IsDefaultMoveCostSpecified,
                    servicePackageActivationMode: this.ServicePackageActivationMode,
                    serviceDnsName: this.ServiceDnsName,
                    scalingPolicies: this.ScalingPolicies,
                    tagsRequiredToPlace: this.TagsRequiredToPlace,
                    tagsRequiredToRun: this.TagsRequiredToRun,
                    flags: this.Flags,
                    replicaRestartWaitDurationSeconds: this.ReplicaRestartWaitDurationSeconds,
                    quorumLossWaitDurationSeconds: this.QuorumLossWaitDurationSeconds,
                    standByReplicaKeepDurationSeconds: this.StandByReplicaKeepDurationSeconds,
                    servicePlacementTimeLimitSeconds: this.ServicePlacementTimeLimitSeconds,
                    dropSourceReplicaOnMove: this.DropSourceReplicaOnMove,
                    replicaLifecycleDescription: this.ReplicaLifecycleDescription,
                    auxiliaryReplicaCount: this.AuxiliaryReplicaCount);
            }
            else if (this.Stateless.IsPresent)
            {
                serviceDescription = new StatelessServiceDescription(
                    serviceName: this.ServiceName,
                    serviceTypeName: this.ServiceTypeName,
                    partitionDescription: partitionSchemeDescription,
                    instanceCount: this.InstanceCount,
                    applicationName: this.ApplicationName,
                    initializationData: this.InitializationData,
                    placementConstraints: this.PlacementConstraints,
                    correlationScheme: this.CorrelationScheme,
                    serviceLoadMetrics: this.ServiceLoadMetrics,
                    servicePlacementPolicies: this.ServicePlacementPolicies,
                    defaultMoveCost: this.DefaultMoveCost,
                    isDefaultMoveCostSpecified: this.IsDefaultMoveCostSpecified,
                    servicePackageActivationMode: this.ServicePackageActivationMode,
                    serviceDnsName: this.ServiceDnsName,
                    scalingPolicies: this.ScalingPolicies,
                    tagsRequiredToPlace: this.TagsRequiredToPlace,
                    tagsRequiredToRun: this.TagsRequiredToRun,
                    minInstanceCount: this.MinInstanceCount,
                    minInstancePercentage: this.MinInstancePercentage,
                    flags: this.Flags,
                    instanceCloseDelayDurationSeconds: this.InstanceCloseDelayDurationSeconds,
                    instanceLifecycleDescription: this.InstanceLifecycleDescription,
                    instanceRestartWaitDurationSeconds: this.InstanceRestartWaitDurationSeconds);
            }

            this.ServiceFabricClient.Services.CreateServiceAsync(
                applicationId: this.ApplicationId,
                serviceDescription: serviceDescription,
                serverTimeout: this.ServerTimeout,
                cancellationToken: this.CancellationToken).GetAwaiter().GetResult();

            Console.WriteLine("Success!");
        }
示例#19
0
        private bool TryCreatePartitionDescription(out PartitionSchemeDescription psd)
        {
            psd = null;

            if (this.control.Package.Data.ContainsKey("PartitionSingleton"))
            {
                psd = new SingletonPartitionSchemeDescription();
                return true;
            }

            if (this.control.Package.Data.ContainsKey("PartitionNamed"))
            {
                NamedPartitionSchemeDescription d = new NamedPartitionSchemeDescription();

                string nameListValue;
                if (!this.control.Package.Data.TryGetValue("PartitionNamedList", out nameListValue))
                {
                    log.Error("named partition specified, but no named list was found");
                    return false;
                }

                string[] names = nameListValue.Split(PackageSettings.NamedPartitionSplitChar);
                for (int i = 0; i < names.Length; i++)
                {
                    d.PartitionNames.Add(names[i]);
                }

                psd = d;
                return true;
            }

            if (this.control.Package.Data.ContainsKey("PartitionUniform"))
            {
                UniformInt64RangePartitionSchemeDescription d = new UniformInt64RangePartitionSchemeDescription();

                long low;
                long high;
                int count;

                if (!this.TryGetData("PartitionUniformLowKey", out low)
                    || !this.TryGetData("PartitionUniformHighKey", out high)
                    || !this.TryGetData("PartitionUniformCount", out count))
                {
                    return false;
                }

                d.LowKey = low;
                d.HighKey = high;
                d.PartitionCount = count;
                psd = d;
                return true;
            }

            return false;
        }
示例#20
0
        public Task CreateStatelessServiceAsync(Guid instanceId, string serviceTypeUri, PartitionSchemeDescription partitionSchemeDescription, int instanceCount = 1, byte[] initializationData = null)
        {
            var serviceDescriptionFactory = new ServiceDescriptionFactory(serviceTypeUri, instanceId, partitionSchemeDescription);

            return(CreateStatelessServiceAsync(serviceDescriptionFactory, instanceCount, initializationData));
        }
示例#21
0
        public Task CreateStatefulServiceAsync(Guid instanceId, string serviceTypeUri, PartitionSchemeDescription partitionSchemeDescription, int minReplicas = 1, int targetReplicas = 3, bool hasPersistedState = true)
        {
            var serviceDescriptionFactory = new ServiceDescriptionFactory(serviceTypeUri, instanceId, partitionSchemeDescription);

            return(CreateStatefulServiceAsync(serviceDescriptionFactory, minReplicas, targetReplicas, hasPersistedState));
        }
示例#22
0
 private static void SetPartitionDescription(ServiceModel.ServiceType description, PartitionSchemeDescription partitionDescription)
 {
     if (partitionDescription is SingletonPartitionSchemeDescription)
     {
         description.SingletonPartition = new ServiceModel.ServiceTypeSingletonPartition();
     }
     else if (partitionDescription is UniformInt64RangePartitionSchemeDescription)
     {
         var uniform = (UniformInt64RangePartitionSchemeDescription)partitionDescription;
         description.UniformInt64Partition = new ServiceModel.ServiceTypeUniformInt64Partition
         {
             HighKey        = uniform.HighKey.ToString(System.Globalization.CultureInfo.InvariantCulture),
             LowKey         = uniform.LowKey.ToString(System.Globalization.CultureInfo.InvariantCulture),
             PartitionCount = uniform.PartitionCount.ToString(System.Globalization.CultureInfo.InvariantCulture)
         };
     }
     else if (partitionDescription is NamedPartitionSchemeDescription)
     {
         var named = (NamedPartitionSchemeDescription)partitionDescription;
         description.NamedPartition = new ServiceModel.ServiceTypeNamedPartition
         {
             Partition = named.PartitionNames.Select(n => new ServiceModel.ServiceTypeNamedPartitionPartition {
                 Name = n
             }).ToArray()
         };
     }
 }
示例#23
0
        public static System.Fabric.Description.PartitionSchemeDescription ToServiceFabricDescription(this PartitionSchemeDescription desc)
        {
            switch (desc)
            {
            case PartitionSchemeDescription.UniformInt64Name:
                return(new UniformInt64RangePartitionSchemeDescription());

            case PartitionSchemeDescription.Named:
                return(new NamedPartitionSchemeDescription());

            default:
                return(new SingletonPartitionSchemeDescription());
            }
        }