For a stateful service, is there a way a replica can determine from its partition ID (a GUID value) the partition key (string or number/number range)? Looking at the .NET API, it's only possible to get replica ID from a ServicePartitionKey
, but not the other way around.

- 23,443
- 7
- 55
- 80
2 Answers
No, there is no way you can get it directly from the partition id (guid). Depending on which partitioning kind (Singleton, Int64Range or Named) you use and how that is setup it might be possible to derive it, but there is no way to do it directly from the service.
It makes sense that it is this way, consider a scenario where you have a service with an Int64 partitioning scheme with low key = 0
and high key = 9
and 2 partitions. Any partition key from 0 to 4 would end up on partition no. 1 and keys 5 - 9 on partition no. 2. But since any call with a key from 0 to 4 would end up on the same instance of that service then the key would really be per call, not per service, so from the service's point of view, how would it know which key it's partition corresponds to, unless that was something baked into the client's call to the service and you could say "for this call to partition XXX the key is 123". Currently there is no such information baked into fabric transport communication, the partition is resolved on the client side and never passed to the service.
What you could do is figure out the range a partition id is in. For a Singleton partition scheme, well you are on 'the partition', not much more to discuss there. For Named and Int64 you can enumerate the partitions using FabricClient.QueryManager
:
var fabricClient = new FabricClient();
var partitionList = await fabricClient.QueryManager.GetPartitionListAsync(serviceName);
foreach (var partition in partitionList)
{
// Partition Guid
var partitionId = partition.PartitionInformation.Id;
// Int 64 Range
var int64PartitionInfo = partition.PartitionInformation as Int64RangePartitionInformation;
var lowKey = int64PartitionInfo?.LowKey;
var highKey = int64PartitionInfo?.HighKey;
// Named Range
var namedPartitionInfo = partition.PartitionInformation as NamedPartitionInformation;
var name = namedPartitionInfo.Name;
}
For Named range if actually does give you the partition key since it's a one-to-one mapping there, but for Int64 range you will only get the span of ints for that partition.
The above code is also something you probably don't want to run every time you want to look for a partition key as the FabricClient.QueryManager
is relatively slow.
The other option you have, if it is important for you to be able to know the partition key in each service call is to simply add it as a message parameter or a message header in your calls to the client (similar to this Passing user and auditing information in calls to Reliable Services in Service Fabric transport)
-
Thank you. Going with Name or HighKey is exactly what I needed. – Sean Feldman Feb 09 '17 at 06:48
yoape answer is really good and should be considered from scenarios where the client need to know the partition information from outside the service. But it suggest it is not possible from within the service itself.
By the question, it request to get this information from within the service itself, and it is possible to get the partition information like below.
Assuming you have a StatefulService like this:
class WorkerService : StatefulService {}
Within the service you can get the partition information like this:
switch (this.Partition.PartitionInfo.Kind)
{
case ServicePartitionKind.Int64Range:
var rangePartition = this.Partition.PartitionInfo as Int64RangePartitionInformation;
var lowKey = rangePartition.LowKey;
var highKey = rangePartition.HighKey;
break;
case ServicePartitionKind.Named:
var namedPartition = this.Partition.PartitionInfo as NamedPartitionInformation;
var name = namedPartition.Name;
break;
case ServicePartitionKind.Singleton:
var singleton = this.Partition.PartitionInfo as SingletonPartitionInformation;
var PartitionId = singleton.Id;
break;
default:
var PartitionId2 = this.Partition.PartitionInfo.Id;
break;
}
Singleton is the same as default where both uses the id of the partition.

- 10,631
- 2
- 32
- 36
-
-
if you are using RangePartition the key is a range defined by low key and high key, there is not unique key, if you want a unique value to identify the range you should use the partition ID. All partitions have an ID. – Diego Mendes May 03 '19 at 15:19