问题
I have a stateful service in Service Fabric with multiple partitions and replicas. I have configured it so that both the Primary and ActiveSecondary replicas expose their endpoints. The purpose is so that I can leverage the secondary replicas for read operations.
The problem I'm having is that inside the service I want to be able to tell it is a Primary or ActiveSecondary because some shared defaulting code needs to not run for the secondary replicas. (Because inserting defaults into the ReliableStateManager throws on secondaries.)
Can I determine the Replica Role at runtime?
回答1:
You can override OnChangeRoleAsync
and check the ReplicaRole
parameter. Note that:
- The role can change during the lifetime of a service (e.g. secondary promoted to primary)
RunAsync
is only executed on primary replicas (will be cancelled if the role changes) - so you can safely place your initialization code there- For more advanced scenarios, you can also check the
Partition
'sReadStatus
andWriteStatus
来源:https://stackoverflow.com/questions/37636023/determine-role-of-stateful-replica-in-service-fabric