1
votes

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

1 Answers

3
votes

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's ReadStatus and WriteStatus