确定有状态副本在服务结构中的角色 [英] Determine role of stateful replica in service fabric
问题描述
我在Service Fabric中有一个状态服务,其中包含多个分区和副本.我已经对其进行了配置,以便主副本和ActiveSecondary副本都公开其端点.目的是使我可以利用辅助副本进行读取操作.
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.
我遇到的问题是,在服务内部我希望能够告诉它是Primary还是ActiveSecondary,因为不需要为辅助副本运行某些共享的默认代码. (因为将默认值插入ReliableStateManager会引发次要事件.)
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?
推荐答案
您可以覆盖OnChangeRoleAsync
并检查ReplicaRole
参数.请注意:
You can override OnChangeRoleAsync
and check the ReplicaRole
parameter. Note that:
- 角色可以在服务的生命周期内发生变化(例如,将辅助角色提升为主要角色)
-
RunAsync
仅在主副本上执行(如果角色更改,该副本将被取消)-因此您可以安全地将初始化代码放在此处 - 对于更高级的方案,您还可以检查
Partition
的ReadStatus
和WriteStatus
- 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
这篇关于确定有状态副本在服务结构中的角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!