how to control lifetime of a sharded entity

46 views
Skip to first unread message

Bert Robben

unread,
Feb 11, 2016, 11:36:17 AM2/11/16
to Akka User List
Hi,

I'm trying to use sharding and that works reasonably well (my entities are being created across my cluster and they respond to my requests).

However, I notice that after each call to my entity, it is destroyed. Every next call on the entity then recreates it. This is not ideal for me since restarting my entity is a heavy process (I might need to recover lots of state from the database). So I would like to tune the system such that my entities are survive for much longer (they are perfectly suited to handle many concurrent requests). Is this somehow possible with the current implementation?

Note that I use state-store-mode = ddata and I don't make use of Akka Persistence.


Also, in my case it is vital for my entity to know its id, because that's how it discerns itself from the other entities. The only way I've found to get to the id now is to do context().parent().path().name(). That works, but feels very implementation dependent. Am I missing some easier API?


thanks,

Bert

Filippo De Luca

unread,
Feb 11, 2016, 3:52:51 PM2/11/16
to akka...@googlegroups.com
Hi Bert,
the only way that I am aware of to get the id is using the one you have posted:

   context().parent().path().
name()

It is weird that the Actors get destroyed. They should not by default.

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.



--

Patrik Nordwall

unread,
Feb 12, 2016, 4:48:55 AM2/12/16
to akka...@googlegroups.com
The name of the actor is the entity identifier (utf-8 URL-encoded), i.e. `self().path().name()`.
I don't know why you are suggesting `parent()`.

Those actors are not stopped automatically unless there is a rebalance, so that must be something you do.

Patrik Nordwall
Typesafe Reactive apps on the JVM
Twitter: @patriknw

Bert Robben

unread,
Feb 12, 2016, 5:43:55 AM2/12/16
to Akka User List
Hi Patrik,

Yes, I've found out my mistake. I was confusing shardId with entityId.

shardId = context().parent().path().name()
entityId = self().path().name()

(you might want to add this to the documentation, because I was confused for a while in how the entity would be able to know its own id)

In my app, I wanted to have entityId = hash(messageId) and shardId = hash(hash(entityId)). But I had it as entityId = messageId and shardId = hash(entityId). Because of this always a new entity was being created as the messageId is always different. I've now fixed the messageExtractor and now everything works as expected and as it should.

thanks,

Bert

PS: I thought I already sent a reply for this this morning, so if that one turns up again this is a duplicate ...

Bert Robben

unread,
Feb 12, 2016, 5:43:55 AM2/12/16
to Akka User List
Thanks Filippo. I think I figured out now what was going wrong.

I was confused between the shardId and the entityId.

shardId == context().parent().path().name()
entityId == self().parth().name()

My entities were being created again and again because I was computing the entityId wrongly. In my case, all messages carry an id. What I need is entityId = hash(messageId) and shardId = hash(entityId). However, I was doing entityId = messageId and shardId = hash(messageId). Therefore I was always creating a new entity because the messageId is unique. I've fixed my MessageExtractor now and everything goes as it should.

thanks,

Bert

Patrik Nordwall

unread,
Feb 12, 2016, 6:14:16 AM2/12/16
to Akka User List
I think it is documented that the entity id is the actor name. You are welcome to contribute with clarifications with a pull request if you like.

The entity type name should be well know by your application. A constant or you can pass it as a constructor parameter via the Props. No need to involve grandpa.

--

Bert Robben

unread,
Feb 12, 2016, 6:28:06 AM2/12/16
to akka...@googlegroups.com
I'm not sure what you mean with "entity type name" and grandpa.


But I checked the docs again, and it indeed is already mentioned that the name is the entityId. I must have overlooked that.

Bert

You received this message because you are subscribed to a topic in the Google Groups "Akka User List" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/akka-user/njCIQqOJTrw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to akka-user+...@googlegroups.com.

Patrik Nordwall

unread,
Feb 12, 2016, 6:32:03 AM2/12/16
to akka...@googlegroups.com
You do something like this:
```
ClusterSharding.get(system).start("Counter",
  Props.create(Counter.class), settings, messageExtractor);
```

That "Counter" name is what I call the entity type name. I thought you were trying to get to that via context().parent().parent(), which should not be necessary.

Bert Robben

unread,
Feb 12, 2016, 6:49:15 AM2/12/16
to akka...@googlegroups.com
Ah, no. I was only using context().parent().name() because I incorrectly thought that this was my entityId. Creating my entity props never was a problem.
Reply all
Reply to author
Forward
0 new messages