Martin Andersson
Martin Andersson

Reputation: 19563

Does Akka actor incarnation matter for remote calls?

On this page, I read (emphasize mine):

You can create an actor, terminate it, and then create a new actor with the same actor path. The newly created actor is a new incarnation of the actor. It is not the same actor. An actor reference to the old incarnation is not valid for the new incarnation. Messages sent to the old actor reference will not be delivered to the new incarnation even though they have the same path.*

*for completeness, a message sent to the "old" reference would go to a dead letter mailbox/actor/queue/whatever.

They further state (emphasize mine):

Two actor references are compared equal when they have the same path and point to the same actor incarnation.

Okay, so that's pretty clear. An actor reference points to a particular instance of an object, so to speak.

But the instance/"incarnation" doesn't seem to matter at all when doing a remote call. Further down the same page it is said (emphasize mine):

When sending an actor reference across the network, it is represented by its path. Hence, the path must fully encode all information necessary to send messages to the underlying actor. This is achieved by encoding protocol, host and port in the address part of the path string. When an actor system receives an actor path from a remote node, it checks whether that path’s address matches the address of this actor system, in which case it will be resolved to the actor’s local reference.

Not a word on the incarnation - or, instance identifier of sorts - being a part of the "information necessary".

This is slightly contradicting, at the least, confusing. Let me elaborate with an example.

Suppose I have a reference a and a reference b, both sharing the same path. a is the old, dead "incarnation". b is the new replacement incarnation. Using a locally will send all messages to the dead letter queue. Cool. But if I send a message containing a as a reply-to actor in a message to a remote actor, and this remote actor writes a message back, then his message automagically goes to b??

I have a hard time understanding this, or rather, the rationale. Why the difference. I mean if this is true - there is a difference - then I can write some really funny test code where I am not able to communicate with a superseded/dead guy a but if I put dead guy a on the wire to anyone else then they can happily communicate back to him without a problem lol. Obviously I have to be missing something!

Upvotes: 0

Views: 71

Answers (1)

johanandren
johanandren

Reputation: 11479

An actor that has crashed and is restarted by supervision, is not a new "incarnation" but will answer to the same ActorRef(s). It is the same actor even though it is actually a newly instantiated object, starting from a clean sheet to avoid dealing with any corruption of the internal state because of the crash. Other actors can keep interacting with it as though nothing happened.

An actor that stops, and later the parent starts a new (potentially completely different actor with a different protocol) but using the same name, and therefore path, is a new incarnation, existing actor refs to the previous "incarnation" does not point to this new actor.

Supervision restart (and resume) also does not trigger deathwatch, while stopping the actor does.

This is works the same both for remote and local ActorRefs.

With Typed Cluster, the only way to get a hold of a remote actor is to get a message with its ActorRef, either from the receptionist or from a remote actor you already have a reference to.

With remoting in Akka Classic APIs you can use the lower level actorSelection which is only about path, and then either accept that you don't know anything about the "incarnation" or if there has ever been an actor running at that path when you send messages to it (if not the messages goes to dead letters), or you resolve it and get an ActorRef for the selection at that point in time, which will then no longer be pointing to an actor if the actor stops (and messages go to dead letters).

Upvotes: 1

Related Questions