根據自己的邏輯移動碎片可以通過實現自定義ShardAllocationStrategy
來實現。
你只需要擴展ShardAllocationStrategy和執行這些2種方法:
def allocateShard(requester: ActorRef, shardId: ShardId,
currentShardAllocations: Map[ActorRef, immutable.IndexedSeq[ShardId]])
: Future[ActorRef]
def rebalance(currentShardAllocations: Map[ActorRef,
immutable.IndexedSeq[ShardId]], rebalanceInProgress: Set[ShardId])
: Future[Set[ShardId]]
第一個決定哪些區域將分配一個新片段時選擇,併爲您提供已分配的碎片。第二個是定期調用的,並允許您控制哪些碎片重新平衡到另一個區域(例如,如果它們變得太不平衡)。
這兩個函數都會返回一個Future,這意味着您甚至可以查詢另一個actor來獲取所需的信息(例如,具有您的actor之間的親和力信息的actor)。
對於親和力本身,我認爲你必須自己實現一些東西。例如,參與者可以收集有關其發件人節點的統計信息,並定期將其發佈到集羣單例中,以確定哪些參與者應該移動到同一個節點。