Skip to content

Commit 279b51b

Browse files
authored
Merge pull request #82 from lerna-stack/improve-raft-actor-allocation
✨feat: Distribute the shards to nodes fairly
2 parents 08d6f3f + 6bffac3 commit 279b51b

File tree

3 files changed

+18
-5
lines changed

3 files changed

+18
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88
[Unreleased]: https://github.com/lerna-stack/akka-entity-replication/compare/.v1.0.0...master
99

10+
### Breaking Change
11+
12+
- Change the shard-distribution-strategy to distribute shard (`RaftActor`) more evenly [PR#82](https://github.com/lerna-stack/akka-entity-replication/pull/82)
13+
14+
⚠️ This change does not allow rolling updates. You have to update your system by stopping the whole cluster.
15+
1016
### Added
1117
- Java11 support
1218
- Add new typed API based on Akka Typed [PR#79](https://github.com/lerna-stack/akka-entity-replication/pull/79)

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ To use this library, you must add a dependency into your sbt project, add the fo
2929

3030
**Stable Release**
3131

32-
[![Maven Central](https://img.shields.io/maven-central/v/com.lerna-stack/akka-entity-replication_2.13?color=%23005cb2&label=stable)](https://mvnrepository.com/artifact/com.lerna-stack/akka-entity-replication)
32+
[![Maven Central](https://img.shields.io/maven-central/v/com.lerna-stack/akka-entity-replication_2.13?color=%23005cb2&label=stable)](https://mvnrepository.com/artifact/com.lerna-stack/akka-entity-replication)
33+
34+
⚠️ `v2.0.0` will contain breaking changes. For more details see [CHANGELOG](./CHANGELOG.md).
3335

3436
```scala
3537
libraryDependencies += "com.lerna-stack" %% "akka-entity-replication" % "X.X.X"

src/main/scala/lerna/akka/entityreplication/ReplicationRegion.scala

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package lerna.akka.entityreplication
22

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

119119
// TODO 変数名を実態にあったものに変更
120120
private[this] val shardingRouters: Map[MemberIndex, ActorRef] = allMemberIndexes.map { memberIndex =>
121+
def clusterReplicationShardId(message: Any): String = extractNormalizedShardIdInternal(message).raw
122+
val extractEntityId: ShardRegion.ExtractEntityId = message => (clusterReplicationShardId(message), message)
123+
val extractShardId: ShardRegion.ExtractShardId = {
124+
case ShardRegion.StartEntity(id) => id
125+
case message => clusterReplicationShardId(message)
126+
}
121127
memberIndex -> {
122128
ClusterSharding(context.system).start(
123129
typeName = s"raft-shard-$typeName-${memberIndex.role}",
124130
entityProps = createRaftActorProps(),
125131
settings = ClusterShardingSettings(settings.raftSettings.clusterShardingConfig)
126132
.withRole(memberIndex.role),
127-
messageExtractor = new HashCodeMessageExtractor(maxNumberOfShards = 50) {
128-
override def entityId(message: Any): String = extractNormalizedShardIdInternal(message).raw
129-
},
133+
extractEntityId,
134+
extractShardId,
130135
)
131136
}
132137
}.toMap

0 commit comments

Comments
 (0)