0

How can I deploy actors to separate processes / machines and use ConsistentHashingGroup router?

My strategy is to leverage the ConsistentHashingGroup router by dynamically constructing a config file with the addresses of nodes on my system. These nodes are not all on the same process but could also be running on separate processes as well.

The config file example below relies on static addresses. However, I believe I need to generate a config file dynamically because actor paths on other nodes / machines are not known at compile time.

     let config = ConfigurationFactory.ParseString(@"
         routees.paths = [
             ""akka://ClusterSystem/user/Worker1"" #testing full path
             ""akka://ClusterSystem/user/Worker2""
             ""akka://ClusterSystem/user/Worker3""
             user/Worker4
         ]")

Any suggestions?

Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118

2 Answers2

1

You know. In a real world scenario. Actors don't magically appear. You either deploy them as part of a pool router for example, or some other mechanism. Or you start them explicitly.

If your running your hashgroup from a specific node. And you know where it starts at design time. You could also have actors utilise pub/sub to publish their own address to a well known topic. Which another actor subscribes to and registers them in your hashgroup.

That way you define sort of a protocol/conversation inside your cluster that actors can use to make themselves part of a specific hashgroup.

Does that make sense ?

Danthar
  • 1,068
  • 7
  • 13
0

I first had to reference the remote actor names that I wanted to use within my configuration:

var config = ConfigurationFactory.ParseString(@"
akka {  
    log-config-on-start = on
    stdout-loglevel = DEBUG
    loglevel = DEBUG
    actor {
        provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote""

        debug {  
          receive = on 
          autoreceive = on
          lifecycle = on
          event-stream = on
          unhandled = on
        }

        deployment {
            /localactor {
                router = consistent-hashing-pool
                nr-of-instances = 5
                virtual-nodes-factor = 10
            }
            /remoteactor1 {
                router = consistent-hashing-pool
                nr-of-instances = 5
                remote = ""akka.tcp://system2@localhost:8080""
            }
            /remoteactor2 {
                router = consistent-hashing-pool
                nr-of-instances = 5
                remote = ""akka.tcp://system2@localhost:8080""
            }
            /remoteactor3 {
                router = consistent-hashing-pool
                nr-of-instances = 5
                remote = ""akka.tcp://system2@localhost:8080""
            }
        }
    }
    remote {
        dot-netty.tcp {
            port = 8090
            hostname = localhost
        }
    }
}
");

I then remote deployed the actors:

  //create a remote deployed actor
  var remote1 = system.ActorOf(Props.Create(() => new SomeActor(null, 123)).WithRouter(FromConfig.Instance), "remoteactor1");
  var remote2 = system.ActorOf(Props.Create(() => new SomeActor(null, 456)).WithRouter(FromConfig.Instance), "remoteactor2");
  var remote3 = system.ActorOf(Props.Create(() => new SomeActor(null, 789)).WithRouter(FromConfig.Instance), "remoteactor3");

I then had to add the actors that were recently deployed to a ConsistentHashingGroup instance:

   var hashGroup = system.ActorOf(Props.Empty.WithRouter(new ConsistentHashingGroup(config)));

   Task.Delay(500).Wait();

   var routee1 = Routee.FromActorRef(remote1);
   hashGroup.Tell(new AddRoutee(routee1));

   var routee2 = Routee.FromActorRef(remote2);
   hashGroup.Tell(new AddRoutee(routee2));

   var routee3 = Routee.FromActorRef(remote3);
   hashGroup.Tell(new AddRoutee(routee3));

I was then able to use the router instance to send messages:

   for (var i = 0; i < 5; i++)
   {
       for (var j = 0; j < 7; j++)
       {
           var message = new SomeMessage(j, $"remote message: {j}");
           hashGroup.Tell(message, someSender);
       }
   }
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118