Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding partition key for stateful service in Azure Service Fabric

I've followed the steps here to create a reverse proxy on my resource group in Azure. https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-reverseproxy

I believe the reverse proxy is working because I used to receive a 404 error, now I am receiving a 400 Bad Request.

The error code is FABRIC_E_INVALID_PARTITION_KEY

This kind of makes sense, because in the documentation, it says for services not using the singleton partition, you must specify a partition key and partition kind, like this:

http://mycluster.eastus.cloudapp.azure.com:19008/MyApp/MyService?PartitionKey=3&PartitionKind=Int64Range

My question is, how do I get that partition key? The documentation explicitly states it is not the GUID generated in the explorer, so I can't use that. I am contacting this stateful service from an external application, outside of the fabric.

like image 930
adam3039 Avatar asked Jan 09 '17 22:01

adam3039


Video Answer


1 Answers

The PartitionId you see in the Service Explorer is the unique id of the Partition that your service request ends up on. The PartitionKey is not the same as the PartitionId, it is rather the the key that goes into the Partitioning Hash and based on that the partition that a request with that key ends up on is calculated from.

In the ApplicationManifest.xml in the Application project the partitioning key for a newly created service looks like this:

<Service Name="MyService">
  <StatefulService ServiceTypeName="MyServiceType" 
                   TargetReplicaSetSize="[MyService_TargetReplicaSetSize]"
                   MinReplicaSetSize="[MyService_MinReplicaSetSize]">
    <UniformInt64Partition PartitionCount="[MyService_PartitionCount]"
                           LowKey="-9223372036854775808"
                           HighKey="9223372036854775807" />
  </StatefulService>
</Service>

Here the UniformInt64Partition indicates that it is an Int64Range that is used. The LowKey and HighKey gives the range of accepted PartitionKeys. The PartitionCount gives the number of partitions that runs this service. In the uniform range then the partitions are uniformly mapped from low to high keys. You should probably change the range to something that makes more sense for your specifik service. E.g.:

  <Parameters>
      ...
      <Parameter Name="MyService_PartitionCount" DefaultValue="2" />
      ...
  </Parameters>
  ...
<Service Name="MyService">
  <StatefulService ServiceTypeName="MyServiceType" 
                   TargetReplicaSetSize="[MyService_TargetReplicaSetSize]"
                   MinReplicaSetSize="[MyService_MinReplicaSetSize]">
    <UniformInt64Partition PartitionCount="[MyService_PartitionCount]"
                           LowKey="0"
                           HighKey="11" />
  </StatefulService>
</Service>

This would give us 2 partitions that are mapped:

  • 0 - 5: Partition 0
  • 6 - 11: Partition 1

Having a partition range that is greater than the number of partitions is integral in allowing us to scale out later on without having to change our partition keys. If we in the example above double the number of partitions (scale out) to 4 partitions then the mapping would become:

  • 0 - 2: Partition 0
  • 3 - 5: Partition 1
  • 6 - 8: Partition 2
  • 9 - 11: Partition 3

The partition keys we use would however not change, meaning that clients of the service are not affected. Similar reasoning goes for Named scheme. The other possible scheme is Singleton and that one is usually used for Stateless services.

Microsoft's documentation on partitioning for services can be found here: https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-concepts-partitioning

Back to your question, your partition key could be any integer within the range LowKey and HighKey as you have specified in your manifest (if you use the UniformInt64Partition). That you get the FABRIC_E_INVALID_PARTITION_KEY error is indicating that the provided key is not a valid integer. If it were outside the accepted range you would likely get FABRIC_E_KEY_NOT_FOUND as an error instead.

like image 69
yoape Avatar answered Sep 27 '22 20:09

yoape