I have service. It makes request to 3rd-party API. As that API has set amount of requests per second, I need to set limits to my requests.
<DefaultServices>
<Service Name="MyService">
<StatelessService ServiceTypeName="MyService" InstanceCount="-1">
<UniformInt64Partition PartitionCount="5" LowKey="0" HighKey="5" />
</StatelessService>
</Service>
</DefaultServices>
I have this configuration. "-1" means max available amount if I'm not wrong.
How can I get amount of instances of "MyService" ? Should I also count amount of partitions to get real amount of instanses?
In simple terms, is just getting the number of Partitions multiplied by the number of allocated replicas(stateful) or instances(stateless) a service has:
In your case, you set the instance count to -1, assuming the number of nodes is 3, the math would be:
3(instances) * 5(partitions) = 15 (instances)
Using PowerShell
$instances = 0
foreach ($partition in Get-ServiceFabricPartition -ServiceName "fabric:/AppName/ServiceName") {
foreach ($replica in Get-ServiceFabricReplica -PartitionId $partition.PartitionId)
{
if($replica.ReplicaStatus -eq 'Ready'){
$instances++
}
}
}
echo $instances
Using FabricClient:
int instances = 0;
var fabricClient = new FabricClient();
var partitions = await fabricClient.QueryManager.GetPartitionListAsync(new Uri("fabric:/AppName/ServiceName"));
foreach (var partition in partitions)
{
instances += (await fabricClient.QueryManager.GetReplicaListAsync(partition.PartitionInformation.Id)).Where(r => r.ReplicaStatus == ServiceReplicaStatus.Ready).Count();
}
The scripts above should works for Stateful and Stateless.
There are some caveats:
An InstanceCount of -1 means that an instance of the service is running on each node in the cluster. So a 5 node cluster will have 5 instances of MyService
running, one on each node. Stateless services do not have partitions (well, just one, the SingletonPartition
)
So, the get the number of instances you can just look at the value of InstanceCount. If it is <> -1 then it is the node count, otherwise the value is the number of instances.
If you want this programmatically try the FabricClient class:
var fabricClient = new FabricClient();
var partitions = await fabricClient.QueryManager.GetPartitionListAsync(new Uri("fabric:/Application2/Stateless1"));
var singletonPartition = (StatelessServicePartition)partitions.First(); // stateless service has just one partition
Console.WriteLine(singletonPartition.InstanceCount);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.