Skip to content

Commit

Permalink
✨feat: Distribute the shards to nodes fairly
Browse files Browse the repository at this point in the history
  • Loading branch information
negokaz committed Jun 11, 2021
1 parent 6a79cc0 commit e64cab3
Showing 1 changed file with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lerna.akka.entityreplication

import akka.actor.{ Actor, ActorLogging, ActorPath, ActorRef, OneForOneStrategy, Props, Stash, SupervisorStrategy }
import akka.cluster.ClusterEvent._
import akka.cluster.sharding.ShardRegion.{ GracefulShutdown, HashCodeMessageExtractor }
import akka.cluster.sharding.ShardRegion.GracefulShutdown
import akka.cluster.sharding.{ ClusterSharding, ClusterShardingSettings, ShardRegion }
import akka.cluster.{ Cluster, Member, MemberStatus }
import akka.routing.{ ActorRefRoutee, ConsistentHashingRouter, ConsistentHashingRoutingLogic, Router }
Expand Down Expand Up @@ -118,15 +118,20 @@ private[entityreplication] class ReplicationRegion(

// TODO 変数名を実態にあったものに変更
private[this] val shardingRouters: Map[MemberIndex, ActorRef] = allMemberIndexes.map { memberIndex =>
def clusterReplicationShardId(message: Any): String = extractNormalizedShardIdInternal(message).raw
val extractEntityId: ShardRegion.ExtractEntityId = message => (clusterReplicationShardId(message), message)
val extractShardId: ShardRegion.ExtractShardId = {
case ShardRegion.StartEntity(id) => id
case message => clusterReplicationShardId(message)
}
memberIndex -> {
ClusterSharding(context.system).start(
typeName = s"raft-shard-$typeName-${memberIndex.role}",
entityProps = createRaftActorProps(),
settings = ClusterShardingSettings(settings.raftSettings.clusterShardingConfig)
.withRole(memberIndex.role),
messageExtractor = new HashCodeMessageExtractor(maxNumberOfShards = 50) {
override def entityId(message: Any): String = extractNormalizedShardIdInternal(message).raw
},
extractEntityId,
extractShardId,
)
}
}.toMap
Expand Down

0 comments on commit e64cab3

Please sign in to comment.