Hi,
I am using cluster sharding with four nodes (N1, N2, N3 and N4).
N1 and N2 - started with default role.
N3 and N4 - started with function role.
Below is the code,
ActorRef shardRegion = setupClusterSharding(system, "shardRegion", ReactorProperty.getRole());
private static ActorRef setupClusterSharding(ActorSystem actorSystem, String entityName, String role) {
ClusterShardingSettings settings = ClusterShardingSettings.create(actorSystem).withRole(role);
return ClusterSharding.get(actorSystem).start(
entityName,
ShardingActor.props(),
settings,
Message.messageExtractor()
);
}
whenever I send message to shardRegion it is distributing actor creation across all the four nodes not with roles. I mean, few actor creation I wanted to distribute only on N3 and N4 since the role is function but the distribution happens on all four nodes.
shardActor.tell(message, getSelf());
I don’t know how to control this so I am using ActorSelection to achieve this.
I am subscribing to cluster events so that I have control over Member Up and aExit events
private void addMember(Member member) {
Set<Address> value = new HashSet<>();
String role = member.getRoles().iterator().next();
if (member.roles() != null && role!= null && nodes.get(role) != null) {
value = nodes.get(member.roles().head());
value.add(member.address());
} else
value.add(member.address());
nodes.put(role, value);
}
private void removeMember(Member member) {
Set<Address> value = new HashSet<>();
String role = member.getRoles().iterator().next();
if (member.roles() != null && role != null && nodes.get(role) != null) {
value = nodes.get(role);
value.remove(member.address());
}
nodes.put(role, value);
}
private ActorSelection getShardActor(Set<Address> addresses) {
if (addresses != null) {
Iterator<Address> itr = addresses.iterator();
if (itr.hasNext())
return getContext().actorSelection(addresses.iterator().next() + "/system/sharding/shardRegion");
}
throw new NoSuchElementException();
}
I am using below snippet to send messages
ActorSelection shardActor
if (isFunction)
shardActor = getShardActor(SupervisorActor.getNodes().get("function"));
else
shardActor = getShardActor(SupervisorActor.getNodes().get("default"));
shardActor.tell(event, getSelf());
Is it correct way of implementation ?
Please guide me on this!!