user3587180
user3587180

Reputation: 1397

Why is the parent's name used as part of the PersistenceId in Akka Sharding?

I was looking at sharding example and noticed that the parent's name is used as part of the PersistenceId?

PersistenceId = Context.Parent.Path.Name + "-" + Self.Path.Name;

My questions:

  1. Is the parent's name the ShardId?

  2. What is the purpose of using it? Does it act like a composite key?

  3. My EntityId is a guid and my MessageExtractor inherits from HashCodeMessageExtractor class. Since that will give me a consistent hash for ShardId based on the PersistenceId/Guid, do I still need to use the parents name as part of the PersistenceId? Also, does the hash value change for the same guid depending on different constructor values (maxNumberOfShards)?

Upvotes: 0

Views: 201

Answers (1)

Bartosz Sypytkowski
Bartosz Sypytkowski

Reputation: 7542

This example is specific to Akka.Cluster.Sharding. While being managed by a cluster sharding extension, sharded actors (called entities) are still placed under standard akka actor hierarchy tree. It looks more or less like this:

  • ShardRegion (name=typeName)
    • Shard (name=shardId)
      • Entity (name=entityId)

While your actor is entity part in this tree, the rest is created by Akka.Cluster.Sharding plugin.

Another part is a way of localizing entity in the cluster. For this Akka.Cluster.Sharding uses a composite key in form of (shardId,entityId). Always.

  1. In some scenarios - like when using HashCodeMessageExtractor, you've mentioned - the shardId is generated/computed based solely on the entityId. In that case you don't need to include it anywhere, entityId is enough. Downside of using HashCodeMessageExtractor is that you must provide max number of shards up front: brain-dead rule is to use 10 * max-number-of-nodes you expect to ever have in your cluster.
  2. Other scenarios may require both IDs to uniquely identify an entity. This is the case from the example, and for this reason we do a lookup for shardId (which by looking at the hierarchy I've presented, is encoded in parent's name) to compose into persistentId.

You can find more detailed info about building shard ids here.

Upvotes: 1

Related Questions