@@ -15,21 +15,27 @@ import java.util.concurrent.ConcurrentHashMap
1515
1616private [entityreplication] class ClusterReplicationImpl (system : ActorSystem [_]) extends ClusterReplication {
1717
18- private [this ] val regions : concurrent.Map [ReplicatedEntityTypeKey [Nothing ], ActorRef [Nothing ]] =
19- new ConcurrentHashMap [ReplicatedEntityTypeKey [_], ActorRef [_]].asScala
18+ private case class ReplicationRegionEntry (settings : ClusterReplicationSettings , regionRef : ActorRef [Nothing ])
19+
20+ private [this ] val regions : concurrent.Map [ReplicatedEntityTypeKey [Nothing ], ReplicationRegionEntry ] =
21+ new ConcurrentHashMap [ReplicatedEntityTypeKey [_], ReplicationRegionEntry ].asScala
2022
2123 override def init [M , E ](entity : ReplicatedEntity [M , E ]): ActorRef [E ] =
22- regions.getOrElseUpdate(entity.typeKey, internalInit(entity)).unsafeUpcast[E ]
24+ regions
25+ .getOrElseUpdate(
26+ entity.typeKey,
27+ internalInit(entity),
28+ ).regionRef.unsafeUpcast[E ]
2329
24- private [this ] def internalInit [M , E ](entity : ReplicatedEntity [M , E ]): ActorRef [ E ] = {
30+ private [this ] def internalInit [M , E ](entity : ReplicatedEntity [M , E ]): ReplicationRegionEntry = {
2531 val classicSystem = system.toClassic
26- val settings = entity.settings.getOrElse(untyped. ClusterReplicationSettings .create(classicSystem ))
32+ val settings = entity.settings.getOrElse(ClusterReplicationSettings (system ))
2733 val extractEntityId : untyped.ReplicationRegion .ExtractEntityId = {
2834 case ReplicationEnvelope (entityId, message) => (entityId, message)
2935 }
3036 val extractShardId : untyped.ReplicationRegion .ExtractShardId = {
3137 case ReplicationEnvelope (entityId, _) =>
32- Math .abs(entityId.hashCode % settings.raftSettings.numberOfShards).toString
38+ shardIdOf( settings, entityId)
3339 }
3440 val possibleShardIds : Set [untyped.ReplicationRegion .ShardId ] = {
3541 (0 until settings.raftSettings.numberOfShards).map(_.toString).toSet
@@ -67,13 +73,31 @@ private[entityreplication] class ClusterReplicationImpl(system: ActorSystem[_])
6773 extractShardId = extractShardId,
6874 possibleShardIds = possibleShardIds,
6975 )
70- region.toTyped
76+ ReplicationRegionEntry (settings, region.toTyped)
7177 }
7278
7379 override def entityRefFor [M ](typeKey : ReplicatedEntityTypeKey [M ], entityId : String ): ReplicatedEntityRef [M ] =
7480 regions.get(typeKey) match {
75- case Some (region) =>
81+ case Some (ReplicationRegionEntry (_, region) ) =>
7682 new ReplicatedEntityRefImpl [M ](typeKey, entityId, region.unsafeUpcast[ReplicationEnvelope [M ]], system)
7783 case None => throw new IllegalStateException (s " The type [ ${typeKey}] must be init first " )
7884 }
85+
86+ override def shardIdOf [M ](
87+ typeKey : ReplicatedEntityTypeKey [M ],
88+ entityId : String ,
89+ ): untyped.ReplicationRegion .ShardId = {
90+ regions.get(typeKey) match {
91+ case Some (ReplicationRegionEntry (settings, _)) =>
92+ shardIdOf(settings, entityId)
93+ case None =>
94+ throw new IllegalStateException (s " The type [ ${typeKey}] must be init first " )
95+ }
96+ }
97+
98+ private def shardIdOf (
99+ settings : ClusterReplicationSettings ,
100+ entityId : String ,
101+ ): String =
102+ Math .abs(entityId.hashCode % settings.raftSettings.numberOfShards).toString
79103}
0 commit comments