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: