1

In Scala, if I register a remote actor using alive(0), the actor is registered at a random port. I can do the registration like this: register('fooParActor, self) in the act method.

Now, on the master/server side, I can select an actor by supplying the port. Do I need to manually scan the ports in order to use random ports?

The problem I am trying to solve is to create n actors on a node and then select them all at a master/server program, e.g. start 10 slaves on node x and get a list 10 remote actors at node y.

How is this done?

Felix
  • 8,385
  • 10
  • 40
  • 59

2 Answers2

1

There's no need to register various ports for the actors. Instead you need one port for the whole actor system - more precisely the akka kernel (that the server needs to know too). See this page of the documentation for how all of this works in detail.

In order to select a remote actor you can then look it up via its path in the remote actor system, similarly to something like this:

context.actorFor("akka://actorSystemName@10.0.0.1:2552/user/someActorName/1")

In that case, you would have created the n actors as children of the someActorName actor and given them the names 1 to n (so you could get the others via .../someActorName/2, .../someActorName/3 and so on).

There's no need to randomize anything at all here and given how you described the problem, there is also no need for randomization within that. You simply start the 10 actors up and number them from 1 to 10. Any random numbers would just unnecessarily complicate things.

As for really random ports I can only agree with sourcedelica. You need a fixed port to communicate the random ones, or some other way of communications. If someone doesn't know where to communicate to due to the random port it simply won't work.

Frank
  • 10,461
  • 2
  • 31
  • 46
  • I think the main advantage is that I get a free port even if I don't know any free ports in advance. – Felix Feb 04 '13 at 16:43
  • BTW I am using remote actors from 2.10 standard library – Felix Feb 04 '13 at 16:44
  • If you're working with 2.10 I should suggest that you switch to akka, as the standard library's actos are deprecated since 2.10. – Frank Feb 04 '13 at 20:12
  • Where do you get that? I don't see any deprecation marks anywhere: http://www.scala-lang.org/api/current/index.html#scala.actors.Actor http://www.scala-lang.org/api/current/index.html#scala.actors.remote.RemoteActor$ – Felix Feb 06 '13 at 08:59
  • I found it: http://docs.scala-lang.org/overviews/core/actors-migration-guide.html :) If you have an idea of how I can have 2 machines agree on a random port with akka and remote actors, I will accept your answer ;) – Felix Feb 06 '13 at 14:10
0

You need to have at least one ActorSystem with a well known port. Then the other ActorSystems can use port 0 to have Akka assign a random port. The slave ActorSystems will have actors register with an actor on the Master so it knows all of the remote systems.

If you absolutely need to have your master use a random port it will need to communicate its port out of band (using a shared filesystem or database).

sourcedelica
  • 23,940
  • 7
  • 66
  • 74
  • I imagine running the program on homogenous machines in a cluster, so they simply need to agree on a working port. They can easily communicate it via network file system once found. Will the random port be available? – Felix Feb 06 '13 at 19:52
  • Yes, if you use a port of 0 for your remote configuration then Akka will assign a random port (see http://doc.akka.io/docs/akka/2.1.0/general/configuration.html#akka-remote, the akka.remote.netty.port entry). On your master you can write this port # to a network filesystem file so the slaves can connect. See my answer at http://stackoverflow.com/questions/14288068/how-do-i-get-the-absolute-remote-actor-url-from-inside-the-actor/14289707#14289707 for how to get the port of the local actor system. – sourcedelica Feb 06 '13 at 23:10