Cherry
Cherry

Reputation: 33618

How check that Cluster sharding is started properly?

I want to check whether ClusterSharding started on not for one region. Here is the code:

def someMethod: {
    val system = ActorSystem("ClusterSystem", ConfigFactory.load())
    val region: ActorRef = ClusterSharding(system).shardRegion("someActorName")
}

Method akka.contrib.pattern.ClusterSharding#shardRegion throws IllegalArgumentException if it do not find shardRegion. I do not like approach to catch IllegalArgumentException just to check that ClusterSharding did not started.

Is there another approach like ClusterSharding(system).isStarted(shardRegionName = "someActorName")? Or it is assumed that I should start all shardingRegion at ActorSystem start up?

Upvotes: 2

Views: 1092

Answers (1)

Diego Martinoia
Diego Martinoia

Reputation: 4662

You should indeed start all regions as soon as possible. According to the docs:

"When using the sharding extension you are first, typically at system startup on each node in the cluster, supposed to register the supported entry types with the ClusterSharding.start method."

Startup of a region is not immediate. In particular, even in local cases, it would take at the very least the time specified in the akka.contrib.cluster.sharding.retry-interval (the name is misleading: this value is both the initial delay of registration and the retry interval) parameter of your configuration before your sharded actors can effectively receive messages (the messages sent in that period are not lost, but not delivered until after a while).

If you want to be 100% sure that your region started, you should have one of your sharded actor respond to an identify message after you call cluster.start . Once it replies, you are guaranteed that your region is up and running. You can use a ask pattern if you want to be blocking and await on the ask future.

Upvotes: 1

Related Questions