private async Task <ServiceInfo> CreateNewWorkerServiceAsync() { var count = await this.GetWorkerCountAsync() + 1; var appName = this.Context.CodePackageActivationContext.ApplicationName; var serviceBaseName = "Worker"; var serviceName = $"{serviceBaseName}{count}"; var serviceAddress = $"{appName}/{serviceName}"; var serviceType = $"{serviceBaseName}Type"; using (var client = new FabricClient()) { var serviceDescriptor = new StatelessServiceDescription() { ApplicationName = new Uri(appName), ServiceName = new Uri(serviceAddress), ServiceTypeName = serviceType, PartitionSchemeDescription = new SingletonPartitionSchemeDescription() }; await client.ServiceManager.CreateServiceAsync(serviceDescriptor); } return(new ServiceInfo { ServiceAddress = serviceAddress, ServiceName = serviceName }); }
internal StatelessServiceDescription GetStatelessDescription(Job j) { var ser = JsonSerializer.Create(); var sw = new StringWriter(); ser.Serialize(sw, j); var desc = new StatelessServiceDescription() { ApplicationName = new Uri("fabric:/Compute2"), DefaultMoveCost = MoveCost.High, ServiceName = new Uri(string.Format("fabric:/Compute2/ComputeInstance{0}", System.DateTime.Now.Ticks)), ServiceTypeName = "StatelessComputeServiceType", PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), InstanceCount = 1, InitializationData = Encoding.UTF8.GetBytes(sw.ToString()) }; var metric = new StatelessServiceLoadMetricDescription() { DefaultLoad = 1, Name = "TCU", Weight = ServiceLoadMetricWeight.Medium }; desc.Metrics.Add(metric); return(desc); }
protected override void ProcessRecord() { PartitionSchemeDescription partitionSchemeDescription; ServiceDescription serviceDescription; var client = new ServiceFabricClientBuilder().ConnectAsync(ClusterEndPoint, ThumbPrint).Result; if (PartitionSchemeUniformInt64) { partitionSchemeDescription = new UniformInt64RangePartitionSchemeDescription(PartitionCount, LowKey.ToString(), HighKey.ToString()); } else if (PartitionSchemeNamed) { partitionSchemeDescription = new NamedPartitionSchemeDescription(PartitionCount, PartitionNames); } else { partitionSchemeDescription = new SingletonPartitionSchemeDescription(); } if (Stateless.IsPresent) { serviceDescription = new StatelessServiceDescription(new ServiceName(ServiceName), ServiceType, partitionSchemeDescription, InstanceCount, initializationData: InitializationData); } else { serviceDescription = new StatefulServiceDescription(new ServiceName(ServiceName), ServiceType, partitionSchemeDescription, TargetReplicaSetSize, MinReplicaSetSize, PersistedState, initializationData: InitializationData); } client.Services.CreateServiceAsync(ApplicationId, serviceDescription).Wait(); }
/// <summary> /// Provisions a new backend and name it uniquely according to the name in param /// </summary> /// <param name="backendName"></param> /// <returns></returns> public async Task ProvisionNewBackend(string backendName) { Uri serviceName = new Uri($"{m_appName}/{backendName}"); var applicationName = new Uri(m_appName); var payload = backendName; StatelessServiceDescription serviceDescription = new StatelessServiceDescription() { ApplicationName = applicationName, ServiceTypeName = "BackgroundStatelessWorkerType", ServiceName = serviceName, PartitionSchemeDescription = new UniformInt64RangePartitionSchemeDescription(100, 0, 99), // to change the number of instaces of this service please change //100 to your desired number and similartly change 99 ServicePackageActivationMode = ServicePackageActivationMode.ExclusiveProcess, InitializationData = Encoding.UTF8.GetBytes($" Backend name is : {payload}") }; //launch service try { using (var fabricClient = new FabricClient()) { await fabricClient.ServiceManager.CreateServiceAsync(serviceDescription); } } catch (Exception e) { throw e; } }
/// <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, StatelessServiceDescription obj) { // Required properties are always serialized, optional properties are serialized when not null. writer.WriteStartObject(); writer.WriteProperty(obj.ServiceKind.ToString(), "ServiceKind", JsonWriterExtensions.WriteStringValue); writer.WriteProperty(obj.ServiceName, "ServiceName", ServiceNameConverter.Serialize); writer.WriteProperty(obj.ServiceTypeName, "ServiceTypeName", JsonWriterExtensions.WriteStringValue); writer.WriteProperty(obj.PartitionDescription, "PartitionDescription", PartitionSchemeDescriptionConverter.Serialize); writer.WriteProperty(obj.InstanceCount, "InstanceCount", JsonWriterExtensions.WriteIntValue); writer.WriteProperty(obj.DefaultMoveCost, "DefaultMoveCost", MoveCostConverter.Serialize); writer.WriteProperty(obj.ServicePackageActivationMode, "ServicePackageActivationMode", ServicePackageActivationModeConverter.Serialize); if (obj.ApplicationName != null) { writer.WriteProperty(obj.ApplicationName, "ApplicationName", ApplicationNameConverter.Serialize); } if (obj.InitializationData != null) { writer.WriteProperty(obj.InitializationData, "InitializationData", ByteArrayConverter.Serialize); } if (obj.PlacementConstraints != null) { writer.WriteProperty(obj.PlacementConstraints, "PlacementConstraints", JsonWriterExtensions.WriteStringValue); } if (obj.CorrelationScheme != null) { writer.WriteEnumerableProperty(obj.CorrelationScheme, "CorrelationScheme", ServiceCorrelationDescriptionConverter.Serialize); } if (obj.ServiceLoadMetrics != null) { writer.WriteEnumerableProperty(obj.ServiceLoadMetrics, "ServiceLoadMetrics", ServiceLoadMetricDescriptionConverter.Serialize); } if (obj.ServicePlacementPolicies != null) { writer.WriteEnumerableProperty(obj.ServicePlacementPolicies, "ServicePlacementPolicies", ServicePlacementPolicyDescriptionConverter.Serialize); } if (obj.IsDefaultMoveCostSpecified != null) { writer.WriteProperty(obj.IsDefaultMoveCostSpecified, "IsDefaultMoveCostSpecified", JsonWriterExtensions.WriteBoolValue); } if (obj.ServiceDnsName != null) { writer.WriteProperty(obj.ServiceDnsName, "ServiceDnsName", JsonWriterExtensions.WriteStringValue); } if (obj.ScalingPolicies != null) { writer.WriteEnumerableProperty(obj.ScalingPolicies, "ScalingPolicies", ScalingPolicyDescriptionConverter.Serialize); } writer.WriteEndObject(); }
public async Task IncreaseDecreaseServiceInstances(bool increase, string appName, string serviceName, string serviceType) { //string appName = "fabric:/MyApplication"; //string serviceName = "fabric:/MyApplication/Stateless1"; //string serviceType = "Stateless1Type"; using (var fabricClient = new FabricClient()) { if (increase) { // Create the stateless service description. For stateful services, use a StatefulServiceDescription object. StatelessServiceDescription serviceDescription = new StatelessServiceDescription { ApplicationName = new Uri(appName), InstanceCount = 1, PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), ServiceName = new Uri(serviceName), ServiceTypeName = serviceType }; // Create the service instance. try { fabricClient.ServiceManager.CreateServiceAsync(serviceDescription).Wait(); ServiceEventSource.Current.ServiceMessage(_serviceContext, "Created service instance {0}", serviceName); } catch (AggregateException ae) { ServiceEventSource.Current.ServiceMessage(_serviceContext, "CreateService failed."); foreach (Exception ex in ae.InnerExceptions) { ServiceEventSource.Current.ServiceMessage(_serviceContext, "HResult: {0} Message: {1}", ex.HResult, ex.Message); } } } else { // Delete a service instance. try { DeleteServiceDescription deleteServiceDescription = new DeleteServiceDescription(new Uri(serviceName)); await fabricClient.ServiceManager.DeleteServiceAsync(deleteServiceDescription); ServiceEventSource.Current.ServiceMessage(_serviceContext, "Deleted service instance {0}", serviceName); } catch (AggregateException ae) { ServiceEventSource.Current.ServiceMessage(_serviceContext, "DeleteService failed."); foreach (Exception ex in ae.InnerExceptions) { ServiceEventSource.Current.ServiceMessage(_serviceContext, "HResult: {0} Message: {1}", ex.HResult, ex.Message); } } } } }
public async Task <IActionResult> Post([FromRoute] string name, [FromBody] EventsProcessorApplicationParams parameters) { // Determine the number of IoT Hub partitions. // The events processing service will be created with the same number of partitions. EventHubClient eventHubClient = EventHubClient.CreateFromConnectionString(parameters.IotHubConnectionString, "messages/events"); EventHubRuntimeInformation eventHubInfo = await eventHubClient.GetRuntimeInformationAsync(); // Application parameters are passed to the Events Processing application instance. NameValueCollection appInstanceParameters = new NameValueCollection(); appInstanceParameters["IotHubConnectionString"] = parameters.IotHubConnectionString; appInstanceParameters["IotHubProcessOnlyFutureEvents"] = parameters.IotHubProcessOnlyFutureEvents; appInstanceParameters["PublishDataServiceURLs"] = parameters.PublishDataServiceURLs; ApplicationDescription application = new ApplicationDescription( new Uri($"{Names.EventsProcessorApplicationPrefix}/{name}"), Names.EventsProcessorApplicationTypeName, parameters.Version, appInstanceParameters); // Create a named application instance await this.fabricClient.ApplicationManager.CreateApplicationAsync(application, this.operationTimeout, this.appLifetime.ApplicationStopping); // Next, create named instances of the services that run in the application. ServiceUriBuilder serviceNameUriBuilder = new ServiceUriBuilder(application.ApplicationName.ToString(), Names.EventsProcessorRouterServiceName); StatefulServiceDescription service = new StatefulServiceDescription() { ApplicationName = application.ApplicationName, HasPersistedState = true, MinReplicaSetSize = 3, TargetReplicaSetSize = 3, PartitionSchemeDescription = new UniformInt64RangePartitionSchemeDescription(eventHubInfo.PartitionCount, 0, eventHubInfo.PartitionCount - 1), ServiceName = serviceNameUriBuilder.Build(), ServiceTypeName = Names.EventsProcessorRouterServiceTypeName }; await this.fabricClient.ServiceManager.CreateServiceAsync(service, this.operationTimeout, this.appLifetime.ApplicationStopping); if (parameters.PublishDataServiceURLs != null && parameters.PublishDataServiceURLs.Length > 0) { // Next, create named instances of the services that run in the application. serviceNameUriBuilder = new ServiceUriBuilder(application.ApplicationName.ToString(), Names.EventsProcessorExtenderServiceName); StatelessServiceDescription extenderService = new StatelessServiceDescription() { ApplicationName = application.ApplicationName, InstanceCount = 1, PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), ServiceName = serviceNameUriBuilder.Build(), ServiceTypeName = Names.EventsProcessorExtenderServiceTypeName }; await this.fabricClient.ServiceManager.CreateServiceAsync(extenderService, this.operationTimeout, this.appLifetime.ApplicationStopping); } return(this.Ok()); }
public override ServiceDescription Build() { var statelessDescription = new StatelessServiceDescription { InstanceCount = this.InstanceCount }; statelessDescription.ApplicationName = this.ApplicationName; statelessDescription.PartitionSchemeDescription = this.PartitionSchemeDescriptionBuilder.Build(); statelessDescription.ServiceName = this.ServiceName; statelessDescription.ServiceTypeName = this.ServiceTypeName; statelessDescription.PlacementConstraints = this.PlacementConstraints; statelessDescription.ServiceDnsName = this.ServiceDnsName; if (this.Correlations != null) { foreach (var str in this.Correlations) { statelessDescription.Correlations.Add(ServiceCmdletBase.ParseCorrelation(str)); } } if (this.PlacementPolicies != null) { foreach (var str in this.PlacementPolicies) { statelessDescription.PlacementPolicies.Add(ServiceCmdletBase.ParsePlacementPolicy(str)); } } if (this.DefaultMoveCost != null) { statelessDescription.DefaultMoveCost = ServiceCmdletBase.ParseMoveCost(this.DefaultMoveCost); } statelessDescription.ServicePackageActivationMode = this.ServicePackageActivationMode; if (this.Metrics != null) { foreach (var str in this.Metrics) { statelessDescription.Metrics.Add(ServiceCmdletBase.ParseStatelessMetric(str)); } } if (this.ScalingPolicies != null) { foreach (var scale in this.ScalingPolicies) { statelessDescription.ScalingPolicies.Add(scale); } } return(statelessDescription); }
public static StatelessServiceDescription GetCloneStateless(this StatelessServiceDescription other) { StatelessServiceDescription clone = new StatelessServiceDescription() { InstanceCount = other.InstanceCount }; clone.Metrics.AddRangeNullSafe(other.Metrics.Select(m => (m as StatelessServiceLoadMetricDescription).GetClone())); // Copy properties clone.CopyFrom(other); return(clone); }
private Task CreateService(string appName) { StatelessServiceDescription serviceDescription = new StatelessServiceDescription() { ApplicationName = new Uri(appName), PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), InstanceCount = 1, ServiceTypeName = "MailboxType", ServiceName = new Uri($"{appName}/Mailbox" + new Random().Next(100).ToString()) }; return(_fabricClient.ServiceManager.CreateServiceAsync(serviceDescription)); }
public string Get(int instances) { var fc = new FabricClient(); string appName = this.context.CodePackageActivationContext.ApplicationName; StatelessServiceDescription serviceDescription = new StatelessServiceDescription() { ApplicationName = new Uri(appName), PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), InstanceCount = 1, ServiceTypeName = "MailboxType", ServiceName = new Uri($"{appName}/test1") }; return(fc.ServiceManager.CreateServiceAsync(serviceDescription).ToString()); }
private bool TryCreateStatelessServiceDescription(out StatelessServiceDescription sd) { sd = null; try { sd = new StatelessServiceDescription(); sd.ApplicationName = new Uri(this.control.Package.ApplicationAddress, UriKind.Absolute); sd.ServiceName = new Uri(this.control.Package.ServiceAddress, UriKind.Absolute); sd.ServiceTypeName = this.control.Package.ServiceType; int count; if (!this.TryGetData("ServiceStatelessCount", out count)) { return(false); } sd.InstanceCount = count; // add special case for -1 when running in dev one-box, only start 1 instance if (sd.InstanceCount == -1) { if (this.control.Cluster.ClusterType == ClusterType.OneBox) { log.Info("special case override of -1 for onebox environment"); if (Environment.UserInteractive) { log.Warn("CreateService: special case override of instanceCount==-1 for onebox environment"); } sd.InstanceCount = 1; } } PartitionSchemeDescription psd; if (!this.TryCreatePartitionDescription(out psd)) { return(false); } sd.PartitionSchemeDescription = psd; return(true); } catch (Exception e) { log.Error("failed to create StatelessServiceDescription, exception={0}", e.Message); return(false); } }
public async Task Handle(PeriodEndStoppedEvent message, IMessageHandlerContext context) { logger.LogDebug($"Received period end stopped event. Details: {message.ToJson()}"); var fabricClient = new FabricClient(); var serviceDescription = new StatelessServiceDescription { ApplicationName = new Uri("fabric:/SFA.DAS.Payments.DataLocks.ServiceFabric"), PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), ServiceName = new Uri(ServiceNames.DatalockApprovalsService), ServiceTypeName = "SFA.DAS.Payments.DataLocks.ApprovalsServiceType", InstanceCount = instanceCount, }; await fabricClient.ServiceManager.CreateServiceAsync(serviceDescription); logger.LogInfo($"Finished period end stopped handler. Details: {message.ToJson()}"); }
public async Task <IActionResult> Post([FromRoute] string tenantName, [FromBody] InsightApplicationParams parameters) { // First create the application instance. // This won't actually create the services yet. ApplicationDescription application = new ApplicationDescription( new Uri($"{Names.InsightApplicationNamePrefix}/{tenantName}"), Names.InsightApplicationTypeName, parameters.Version); await this.fabricClient.ApplicationManager.CreateApplicationAsync(application, this.operationTimeout, this.appLifetime.ApplicationStopping); // Now create the data service in the new application instance. ServiceUriBuilder dataServiceNameUriBuilder = new ServiceUriBuilder(application.ApplicationName.ToString(), Names.InsightDataServiceName); StatefulServiceDescription dataServiceDescription = new StatefulServiceDescription() { ApplicationName = application.ApplicationName, HasPersistedState = true, MinReplicaSetSize = 3, TargetReplicaSetSize = 3, PartitionSchemeDescription = new UniformInt64RangePartitionSchemeDescription(parameters.DataPartitionCount, Int64.MinValue, Int64.MaxValue), ServiceName = dataServiceNameUriBuilder.Build(), ServiceTypeName = Names.InsightDataServiceTypeName }; await this.fabricClient.ServiceManager.CreateServiceAsync(dataServiceDescription, this.operationTimeout, this.appLifetime.ApplicationStopping); // And finally, create the web service in the new application instance. ServiceUriBuilder webServiceNameUriBuilder = new ServiceUriBuilder(application.ApplicationName.ToString(), Names.InsightWebServiceName); StatelessServiceDescription webServiceDescription = new StatelessServiceDescription() { ApplicationName = application.ApplicationName, InstanceCount = parameters.WebInstanceCount, PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), ServiceName = webServiceNameUriBuilder.Build(), ServiceTypeName = Names.InsightWebServiceTypeName }; await this.fabricClient.ServiceManager.CreateServiceAsync(webServiceDescription, this.operationTimeout, this.appLifetime.ApplicationStopping); return(this.Ok()); }
private void TestHelper(Action <StatelessServiceDescription> setup) { var expected = HelperInstance.CreateDefaultDescription(); if (setup != null) { setup(expected); } using (var pc = new PinCollection()) { NativeTypes.FABRIC_SERVICE_DESCRIPTION_KIND kind; IntPtr native = expected.ToNative(pc, out kind); var actual = StatelessServiceDescription.CreateFromNative(native); Assert.AreEqual(NativeTypes.FABRIC_SERVICE_DESCRIPTION_KIND.FABRIC_SERVICE_DESCRIPTION_KIND_STATELESS, kind); HelperInstance.Compare(expected, actual); } }
public Task PostStateless(string jobName, int?workload = null) { string applicationName = this.context.CodePackageActivationContext.ApplicationName; StatelessServiceDescription serviceDescription = new StatelessServiceDescription() { ApplicationName = new Uri(applicationName), InstanceCount = 1, PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), ServiceTypeName = "StatelessJobServiceType", ServiceName = new Uri($"{applicationName}/jobs/{jobName}") }; if (workload.HasValue) { serviceDescription.Metrics.Add(new StatelessServiceLoadMetricDescription() { Name = "WorkUnits", DefaultLoad = workload.Value, Weight = ServiceLoadMetricWeight.High }); } return(fabricClient.ServiceManager.CreateServiceAsync(serviceDescription)); }
public DeviceGenerator(StatelessServiceContext context) : base(context) { applicationPath = new Uri($"fabric:/DeviceSimulation/Devices"); var simulations = new List <SimulationItem>() { new SimulationItem() { Id = Guid.NewGuid(), DeviceName = "SimulatedTruck-001", DeviceType = "Truck", DefinitionPath = "Truck.json", Interval = 1 } }; serviceDescriptions = new Dictionary <string, StatelessServiceDescription>(); foreach (var simulation in simulations) { var json = JsonConvert.SerializeObject(simulation); var statelessServiceDescription = new StatelessServiceDescription() { ApplicationName = new Uri($"fabric:/DeviceSimulation/Devices"), ServiceName = new Uri($"fabric:/DeviceSimulation/Devices/{simulation.DeviceName}"), ServiceTypeName = "DeviceSimulatorType", PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), InitializationData = Encoding.ASCII.GetBytes(json), InstanceCount = 1, }; serviceDescriptions.Add("Truck", statelessServiceDescription); } fabricClient = new FabricClient(); }
protected override object FormatOutput(object output) { var result = new PSObject(output); if (output is ServiceGroupDescription) { var serviceGroupDescription = output as ServiceGroupDescription; // Adding a helper toString method for printing IList<MemberDescription> var memberDescriptionsPSObj = new PSObject(serviceGroupDescription.MemberDescriptions); memberDescriptionsPSObj.Members.Add( new PSCodeMethod( Constants.ToStringMethodName, typeof(OutputFormatter).GetMethod(Constants.FormatObjectMethodName))); result.Properties.Add(new PSNoteProperty(Constants.MemberDescriptionsPropertyName, memberDescriptionsPSObj)); result.Properties.Add(new PSNoteProperty( Constants.ApplicationNamePropertyName, serviceGroupDescription.ServiceDescription.ApplicationName)); result.Properties.Add(new PSNoteProperty( Constants.ServiceNamePropertyName, serviceGroupDescription.ServiceDescription.ServiceName)); result.Properties.Add(new PSNoteProperty( Constants.ServiceTypeNamePropertyName, serviceGroupDescription.ServiceDescription.ServiceTypeName)); result.Properties.Add(new PSNoteProperty( Constants.ServiceKindPropertyName, serviceGroupDescription.ServiceDescription.Kind)); if (serviceGroupDescription.ServiceDescription is StatefulServiceDescription) { StatefulServiceDescription statefulServiceDescription = serviceGroupDescription.ServiceDescription as StatefulServiceDescription; result.Properties.Add(new PSNoteProperty( Constants.HasPersistedStatePropertyName, statefulServiceDescription.HasPersistedState)); result.Properties.Add(new PSNoteProperty( Constants.MinReplicaSetSizePropertyName, statefulServiceDescription.MinReplicaSetSize)); result.Properties.Add(new PSNoteProperty( Constants.TargetReplicaSetSizePropertyName, statefulServiceDescription.TargetReplicaSetSize)); if (statefulServiceDescription.HasPersistedState) { result.Properties.Add(new PSNoteProperty( Constants.ReplicaRestartWaitDurationPropertyName, statefulServiceDescription.ReplicaRestartWaitDuration)); result.Properties.Add(new PSNoteProperty( Constants.QuorumLossWaitDurationPropertyName, statefulServiceDescription.QuorumLossWaitDuration)); result.Properties.Add(new PSNoteProperty( Constants.StandByReplicaKeepDurationPropertyName, statefulServiceDescription.StandByReplicaKeepDuration)); } } else if (serviceGroupDescription.ServiceDescription is StatelessServiceDescription) { StatelessServiceDescription statelessServiceDescription = serviceGroupDescription.ServiceDescription as StatelessServiceDescription; result.Properties.Add(new PSNoteProperty( Constants.InstanceCountPropertyName, statelessServiceDescription.InstanceCount)); } result.Properties.Add( new PSNoteProperty( Constants.PartitionSchemePropertyName, serviceGroupDescription.ServiceDescription.PartitionSchemeDescription.Scheme)); result.Properties.Add( new PSNoteProperty( Constants.PlacementConstraintsPropertyName, string.IsNullOrEmpty(serviceGroupDescription.ServiceDescription.PlacementConstraints) ? Constants.NoneResultOutput : serviceGroupDescription.ServiceDescription.PlacementConstraints)); if (serviceGroupDescription.ServiceDescription.PartitionSchemeDescription.Scheme == PartitionScheme.UniformInt64Range) { var info = serviceGroupDescription.ServiceDescription.PartitionSchemeDescription as UniformInt64RangePartitionSchemeDescription; if (info != null) { result.Properties.Add(new PSNoteProperty(Constants.PartitionLowKeyPropertyName, info.LowKey)); result.Properties.Add(new PSNoteProperty(Constants.PartitionHighKeyPropertyName, info.HighKey)); result.Properties.Add(new PSNoteProperty(Constants.PartitionCountPropertyName, info.PartitionCount)); } } else if (serviceGroupDescription.ServiceDescription.PartitionSchemeDescription.Scheme == PartitionScheme.Named) { var info = serviceGroupDescription.ServiceDescription.PartitionSchemeDescription as NamedPartitionSchemeDescription; if (info != null) { var partitionNamesPropertyPSObj = new PSObject(info.PartitionNames); partitionNamesPropertyPSObj.Members.Add( new PSCodeMethod( Constants.ToStringMethodName, typeof(OutputFormatter).GetMethod(Constants.FormatObjectMethodName))); result.Properties.Add(new PSNoteProperty(Constants.PartitionNamesPropertyName, partitionNamesPropertyPSObj)); } } return(result); } return(base.FormatOutput(output)); }
private async Task <SprocAddressStruct> GetOrCreateSproServiceInstanceAsync(string messageSessionType, string messageUserName) { var urlPath = $"SPROC_{messageSessionType}_{messageUserName}"; var url = new ServiceUriBuilder(urlPath).ToUri(); var fabricClient = new FabricClient(); ServiceDescription service = null; try { service = await fabricClient.ServiceManager.GetServiceDescriptionAsync(url); } catch (Exception ex) { // nothing to do -> exception mean no service with the requested namename service = null; } if (service == null) { StatelessServiceDescription newGeneratorDescription = new StatelessServiceDescription() { ApplicationName = new Uri(this.Context.CodePackageActivationContext.ApplicationName), ServiceName = url, InstanceCount = 1, ServiceTypeName = "com.mega.SproGuestExeType", PartitionSchemeDescription = new SingletonPartitionSchemeDescription() }; await fabricClient.ServiceManager.CreateServiceAsync(newGeneratorDescription).ConfigureAwait(false); service = await fabricClient.ServiceManager.GetServiceDescriptionAsync(url); var healthState = HealthState.Unknown; var count = 0; while (healthState != HealthState.Ok) { if (count++ >= 10) { throw new TimeoutException("Time out waiting for " + url); } var serviceList = await fabricClient.QueryManager.GetServiceListAsync(new Uri(this.Context.CodePackageActivationContext.ApplicationName)); healthState = serviceList.Single(services => services.ServiceName == url).HealthState; if (healthState != HealthState.Ok) { await Task.Delay(TimeSpan.FromSeconds(2)); } } } ServicePartitionResolver resolver = ServicePartitionResolver.GetDefault(); ResolvedServicePartition partition = await resolver.ResolveAsync(service.ServiceName, new ServicePartitionKey(), CancellationToken.None); var s = partition.Endpoints.First(); // s.Address == {"Endpoints":{"com.mega.SproGuestExeTypeEndpoint":"localhost:33039"}} // HACK : exytraction fqdn : should using json deserialization instead of this bad hack int start = s.Address.IndexOf(":\"") + 2; int stop = s.Address.IndexOf("\"", start); var fqdn = s.Address.Substring(start, stop - start); var parts = fqdn.Split(':'); return(new SprocAddressStruct { ServiceName = service.ServiceName, Ip = parts[0], Port = Convert.ToInt32(parts[1]) }); }
private ServiceDescription CreateServiceDescription(ServiceOperationDescription serviceOperationDescription) { string errorMessage; ServiceDescription serviceDescription; switch (serviceOperationDescription.ServiceKind) { case ArmServiceKind.Stateful: if (!this.ValidObjectType <StatefulServiceOperationDescription>(serviceOperationDescription, out errorMessage)) { throw new InvalidCastException(errorMessage); } var statefulOperation = (StatefulServiceOperationDescription)serviceOperationDescription; serviceDescription = new StatefulServiceDescription() { HasPersistedState = statefulOperation.HasPersistedState, MinReplicaSetSize = statefulOperation.MinReplicaSetSize, TargetReplicaSetSize = statefulOperation.TargetReplicaSetSize, QuorumLossWaitDuration = statefulOperation.QuorumLossWaitDuration, ReplicaRestartWaitDuration = statefulOperation.ReplicaRestartWaitDuration, StandByReplicaKeepDuration = statefulOperation.StandByReplicaKeepDuration }; break; case ArmServiceKind.Stateless: if (!this.ValidObjectType <StatelessServiceOperationDescription>(serviceOperationDescription, out errorMessage)) { throw new InvalidCastException(errorMessage); } var statelessOperation = (StatelessServiceOperationDescription)serviceOperationDescription; serviceDescription = new StatelessServiceDescription() { InstanceCount = statelessOperation.InstanceCount }; break; default: throw new ArgumentOutOfRangeException( nameof(serviceOperationDescription.ServiceKind), serviceOperationDescription.ServiceKind, $"{this.TraceType}: Unexpected ArmServiceKind"); } serviceDescription.ApplicationName = serviceOperationDescription.ApplicationName; serviceDescription.ServiceName = serviceOperationDescription.ServiceName; if (serviceOperationDescription.DefaultMoveCost.HasValue) { serviceDescription.DefaultMoveCost = (MoveCost)Enum.Parse( typeof(MoveCost), serviceOperationDescription.DefaultMoveCost.Value.ToString()); } serviceDescription.PlacementConstraints = serviceOperationDescription.PlacementConstraints; serviceDescription.ServiceTypeName = serviceOperationDescription.ServiceTypeName; if (serviceOperationDescription.CorrelationScheme != null) { foreach (var scheme in serviceOperationDescription.CorrelationScheme) { serviceDescription.Correlations.Add(this.GetServiceCorrelationDescription(scheme)); } } if (serviceOperationDescription.ServiceLoadMetrics != null) { foreach (var metric in serviceOperationDescription.ServiceLoadMetrics) { serviceDescription.Metrics.Add(this.GetServiceLoadMetricDescription(metric)); } } if (serviceOperationDescription.ServicePlacementPolicies != null) { foreach (var policy in serviceOperationDescription.ServicePlacementPolicies) { serviceDescription.PlacementPolicies.Add(this.GetServicePlacementPolicyDescription(policy)); } } PartitionSchemeDescription partitionSchemeDescription; switch (serviceOperationDescription.PartitionDescription.PartitionScheme) { case ArmPartitionScheme.Named: if (!this.ValidObjectType <ArmNamedPartitionDescription>(serviceOperationDescription.PartitionDescription, out errorMessage)) { throw new InvalidCastException(errorMessage); } var namedOperation = (ArmNamedPartitionDescription)serviceOperationDescription.PartitionDescription; partitionSchemeDescription = new NamedPartitionSchemeDescription(namedOperation.Names); break; case ArmPartitionScheme.Singleton: partitionSchemeDescription = new SingletonPartitionSchemeDescription(); break; case ArmPartitionScheme.UniformInt64Range: if (!this.ValidObjectType <ArmUniformInt64PartitionDescription>(serviceOperationDescription.PartitionDescription, out errorMessage)) { throw new InvalidCastException(errorMessage); } var uniformOperation = (ArmUniformInt64PartitionDescription)serviceOperationDescription.PartitionDescription; partitionSchemeDescription = new UniformInt64RangePartitionSchemeDescription() { HighKey = uniformOperation.HighKey, LowKey = uniformOperation.LowKey, PartitionCount = uniformOperation.Count }; break; default: throw new ArgumentOutOfRangeException( nameof(serviceOperationDescription.PartitionDescription.PartitionScheme), serviceOperationDescription.PartitionDescription.PartitionScheme, $"{this.TraceType}: Unexpected ArmPartitionScheme"); } serviceDescription.PartitionSchemeDescription = partitionSchemeDescription; return(serviceDescription); }
/// <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!"); }
private bool TryCreateStatelessServiceDescription(out StatelessServiceDescription sd) { sd = null; try { sd = new StatelessServiceDescription(); sd.ApplicationName = new Uri(this.control.Package.ApplicationAddress, UriKind.Absolute); sd.ServiceName = new Uri(this.control.Package.ServiceAddress, UriKind.Absolute); sd.ServiceTypeName = this.control.Package.ServiceType; int count; if (!this.TryGetData("ServiceStatelessCount", out count)) { return false; } sd.InstanceCount = count; // add special case for -1 when running in dev one-box, only start 1 instance if (sd.InstanceCount == -1) { if (this.control.Cluster.ClusterType == ClusterType.OneBox) { log.Info("special case override of -1 for onebox environment"); if (Environment.UserInteractive) { log.Warn("CreateService: special case override of instanceCount==-1 for onebox environment"); } sd.InstanceCount = 1; } } PartitionSchemeDescription psd; if (!this.TryCreatePartitionDescription(out psd)) { return false; } sd.PartitionSchemeDescription = psd; return true; } catch (Exception e) { log.Error("failed to create StatelessServiceDescription, exception={0}", e.Message); return false; } }
public void ServiceDescriptionSerializationTest() { StatelessServiceDescription svc = this.random.CreateRandom <StatelessServiceDescription>(); TestUsingSerializer(this.Serializer, svc); }
/// <summary> /// This is the main entry point for your service instance. /// </summary> /// <param name="cancellationToken">Canceled when Service Fabric needs to shut down this service instance.</param> protected override async Task RunAsync(CancellationToken cancellationToken) { // TODO: Replace the following sample code with your own logic // or remove this RunAsync override if it's not needed in your service. var simulationJson = await storageService.FetchFileAsync("run", "main-simulation.json"); var simulations = JsonConvert.DeserializeObject <IEnumerable <SimulationItem> >(simulationJson); var ramp = 1; var rampDelay = 1; serviceDescriptions = new Dictionary <string, StatelessServiceDescription>(); foreach (var simulation in simulations) { simulation.ScriptFile = await storageService.FetchFileAsync("scripts", $"{simulation.DeviceType}.cscript"); simulation.ScriptLanguage = ScriptLanguage.CSharp; simulation.InitialState = await storageService.FetchFileAsync("state", $"{simulation.DeviceType}.json"); var batchStart = 1; var batchSize = 10; batchSize = simulation.BatchSize ?? batchSize; var batches = Enumerable.Range(simulation.DeviceOffset ?? 1, (simulation.NumberOfDevices + (simulation.DeviceOffset ?? 1)) / batchSize); var serviceName = $"{simulation.DeviceStartRange}-{simulation.DeviceEndRange}"; var json = JsonConvert.SerializeObject(simulation); var statelessServiceDescription = new StatelessServiceDescription() { ApplicationName = new Uri($"fabric:/DeviceSimulation/Devices"), ServiceName = new Uri($"fabric:/DeviceSimulation/Devices/{serviceName}"), ServiceTypeName = "DeviceSimulatorType", PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), InitializationData = Encoding.ASCII.GetBytes(json), InstanceCount = 1, }; serviceDescriptions.Add(serviceName, statelessServiceDescription); batchStart += batchSize; } fabricClient = new FabricClient(); var applicationDescription = new ApplicationDescription(applicationPath, "DeviceSimulationType", "1.0.0", new NameValueCollection()); await fabricClient.ApplicationManager.CreateApplicationAsync(applicationDescription); int i = 1; foreach (var kvp in serviceDescriptions) { await fabricClient.ServiceManager.CreateServiceAsync(kvp.Value); // Stagger Service Start Times if (i % ramp == 0 || i == serviceDescriptions.Count()) { System.Diagnostics.Trace.WriteLine($"Waiting {rampDelay * 1000} s to start up next batch of applications."); Thread.Sleep(rampDelay * 1000); } i++; } }
public async Task<IActionResult> Post([FromRoute] string tenantName, [FromBody] TenantApplicationParams parameters) { // First create the application instance. // This won't actually create the services yet. ApplicationDescription application = new ApplicationDescription( new Uri($"{Names.TenantApplicationNamePrefix}/{tenantName}"), Names.TenantApplicationTypeName, parameters.Version); await this.fabricClient.ApplicationManager.CreateApplicationAsync(application, this.operationTimeout, this.serviceCancellationToken); // Now create the data service in the new application instance. ServiceUriBuilder dataServiceNameUriBuilder = new ServiceUriBuilder(application.ApplicationName.ToString(), Names.TenantDataServiceName); StatefulServiceDescription dataServiceDescription = new StatefulServiceDescription() { ApplicationName = application.ApplicationName, HasPersistedState = true, MinReplicaSetSize = 3, TargetReplicaSetSize = 3, PartitionSchemeDescription = new UniformInt64RangePartitionSchemeDescription(parameters.DataPartitionCount, Int64.MinValue, Int64.MaxValue), ServiceName = dataServiceNameUriBuilder.Build(), ServiceTypeName = Names.TenantDataServiceTypeName }; await this.fabricClient.ServiceManager.CreateServiceAsync(dataServiceDescription, this.operationTimeout, this.serviceCancellationToken); // And finally, create the web service in the new application instance. ServiceUriBuilder webServiceNameUriBuilder = new ServiceUriBuilder(application.ApplicationName.ToString(), Names.TenantWebServiceName); StatelessServiceDescription webServiceDescription = new StatelessServiceDescription() { ApplicationName = application.ApplicationName, InstanceCount = parameters.WebInstanceCount, PartitionSchemeDescription = new SingletonPartitionSchemeDescription(), ServiceName = webServiceNameUriBuilder.Build(), ServiceTypeName = Names.TenantWebServiceTypeName }; await this.fabricClient.ServiceManager.CreateServiceAsync(webServiceDescription, this.operationTimeout, this.serviceCancellationToken); return this.Ok(); }
/// <summary> /// Creates an instance of an application. /// </summary> /// <param name="applicationName"></param> /// <param name="applicationTypeName"></param> /// <param name="applicationTypeVersion"></param> /// <param name="token"></param> /// <param name="cluster"></param> /// <param name="applicationInstanceName"></param> /// <returns></returns> public async Task <ApplicationDescription> CreateApplicationAsync(ApplicationDefinition applicationDefinition, CancellationToken token) { var app = new ApplicationDescription() { ApplicationName = new Uri(applicationDefinition.ApplicationName), ApplicationTypeName = applicationDefinition.Type, ApplicationTypeVersion = applicationDefinition.Version }; //maybe move this some where else as seperate step? var accountName = Environment.GetEnvironmentVariable("STORAGE_ACCOUNT_NAME"); var accountKey = Environment.GetEnvironmentVariable("STORAGE_ACCOUNT_KEY"); var connectionstring = $"DefaultEndpointsProtocol=https;AccountName={accountName};AccountKey={accountKey};EndpointSuffix=core.windows.net"; var name = applicationDefinition.ApplicationName.GetName(); await CreateAzureFileStorage(name, connectionstring); // set up env varaialbes for save world files to azure storage app.ApplicationParameters.Add("worldtolink_env", $@"\\{accountName}.file.core.windows.net\{name}"); app.ApplicationParameters.Add("passcode_env", accountKey); app.ApplicationParameters.Add("MOTD_env", $"service fabric minecraft: {applicationDefinition.ApplicationName}"); // set up the ports. var serviceDefinition = applicationDefinition.Services.First(); var mcPort = serviceDefinition.Ports["MCPORT"]; var rconPort = serviceDefinition.Ports["RCONPORT"]; app.ApplicationParameters.Add("MCPORT", mcPort.ToString()); app.ApplicationParameters.Add("RCONPORT", rconPort.ToString()); try { await fabricClient.ApplicationManager.CreateApplicationAsync(app, writeOperationTimeout, token); } catch (Exception ex) { Console.WriteLine(ex); throw; } // Create the stateless service description. For stateful services, use a StatefulServiceDescription object. StatelessServiceDescription serviceDescription = new StatelessServiceDescription(); serviceDescription.ApplicationName = app.ApplicationName; serviceDescription.InstanceCount = 1; serviceDescription.PartitionSchemeDescription = new SingletonPartitionSchemeDescription(); serviceDescription.ServiceName = new Uri($"{app.ApplicationName}/{serviceDefinition.ServiceName}"); serviceDescription.ServiceTypeName = serviceDefinition.Type; try { await fabricClient.ServiceManager.CreateServiceAsync(serviceDescription, this.writeOperationTimeout, token); return(app); } catch (Exception e) { Console.WriteLine(e); throw; } }