Skip to content

Commit 0d25029

Browse files
dfakhritdinovDenys Fakhritdinov
andauthored
ClusterShardingLocal: call allocation strategy correctly (#325)
Co-authored-by: Denys Fakhritdinov <dfakhritdinov@evolution.com>
1 parent 22ba163 commit 0d25029

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

cluster-sharding/src/main/scala/com/evolutiongaming/akkaeffect/cluster/sharding/ClusterShardingLocal.scala

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ trait ClusterShardingLocal[F[_]] {
2626
/** Provides the actual stub */
2727
def clusterSharding: ClusterSharding[F]
2828

29-
/** Simulate cluster rebalacing.
29+
/** Simulate cluster rebalancing.
3030
*
3131
* I.e. send `handOffStopMessage` from [[ClusterSharding#startProxy]] to the actors that need rebalancing according
3232
* to `shardAllocationStrategy`.
@@ -119,8 +119,14 @@ object ClusterShardingLocal {
119119
val entityName = actorNameOf(entityId)
120120
context
121121
.child(entityId)
122-
.getOrElse(context.actorOf(props, entityName))
122+
.getOrElse(context.watch(context.actorOf(props, entityName)))
123123
.forward(msg)
124+
125+
case _: akka.actor.Terminated =>
126+
// stop itself when all children are stopped
127+
if (context.children.isEmpty) {
128+
context.stop(self)
129+
}
124130
}
125131
}
126132

@@ -172,15 +178,16 @@ object ClusterShardingLocal {
172178
val shardId = extractShardId(msg)
173179
val shardName = actorNameOf(shardId)
174180

175-
def allocate = allocationStrategy.allocateShard(sender, shardId, allocation())
176-
177181
context
178182
.child(shardName)
179183
.fold {
184+
val shard = context.actorOf(Props(shardActor()), shardName)
185+
val future = allocationStrategy.allocateShard(shard, shardId, allocation())
186+
180187
FromFuture[F]
181-
.apply(allocate)
188+
.apply(future)
182189
.toTry
183-
.as(context.actorOf(Props(shardActor()), shardName))
190+
.as(shard)
184191
} { shard =>
185192
shard.pure[Try]
186193
}

0 commit comments

Comments
 (0)