Search code examples
c#monoakka.netakka.net-cluster

How can I turn ClusterRouterPool from C# to HOCON configuration?


I'm learning akka.net and potentially will use it to replace part of our traditional message driven app.

Basically I'm trying to have X number of nodes joined to a cluster. It's peer to peer type, and I might run X number of actors (same actor) on a node.

If I have 10 jobs (say SendEmailActor), ideally, I would like each of the 10 jobs be executed on different nodes (spread the load evenly).

I have an extremely simple console app to demonstrate.

using System;
using System.Configuration;
using Akka;
using Akka.Actor;
using Akka.Cluster;
using Akka.Cluster.Routing;
using Akka.Configuration;
using Akka.Configuration.Hocon;
using Akka.Routing;

namespace Console1
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.Write("Is this the seed node? (Y/n): ");

            var port = Console.ReadLine().ToLowerInvariant() == "y" ? 9999 : 0;

            var section = (AkkaConfigurationSection)ConfigurationManager.GetSection("akka");

            var config =
                ConfigurationFactory.ParseString("akka.remote.helios.tcp.port=" + port)
                    .WithFallback(section.AkkaConfig);

            var cluster = ActorSystem.Create("MyCluster", config);

            var worker = cluster.ActorOf(Props.Create<Worker>().WithRouter(
                                 new ClusterRouterPool(
                                     new RoundRobinPool(10),
                                     new ClusterRouterPoolSettings(30, true, 5))), "worker");

            while (true)
            {
                Console.Read();
                var i = DateTime.Now.Millisecond;
                Console.WriteLine("Announce: {0}", i);
                worker.Tell(i);

            }
        }
    }

    public class Worker : UntypedActor
    {
        protected override void OnReceive(object message)
        {
            System.Threading.Thread.Sleep(new Random().Next(1000, 2000));
            Console.WriteLine("WORKER ({0}) [{1}:{2}]", message, Context.Self.Path, Cluster.Get(Context.System).SelfUniqueAddress.Address.Port);
        }
    }
}

And my app.config looks like

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="akka" type="Akka.Configuration.Hocon.AkkaConfigurationSection, Akka" />
    </configSections>
    <akka>
    <hocon>
        <![CDATA[
            akka {
                actor {
                    provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
                }

                remote {
                    helios.tcp {
                        hostname = "127.0.0.1"
                        port = 0
                    }
                }            

                cluster {
                    seed-nodes = ["akka.tcp://MyCluster@127.0.0.1:9999"]
                }
            }
        ]]>
    </hocon>
    </akka>
</configuration>

I want to use HOCON and setup akka.actor.deployment, and I couldn't get it to work. I don't quite understand the routees.paths, and its relationship with the the actor.deployment/worker and how will routees.paths map to the actors created in C#.

actor {
    provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
    deployment {
        /worker {
            router = roundrobin-pool
            routees.paths = ["/worker"] # what's this?
            cluster {
                    enabled = on
                    max-nr-of-instances-per-node = 1
                    allow-local-routees = on
            }
        }
    }
}

Another question: with aka.net.cluster is it possible to "mirror" the nodes to provide redundancy? Or GuaranteedDeliveryActor (I think it's renamed to AtLeastOnceDelivery) is the way to go?


Solution

  • Thanks to @RogerAlsing.

    The C# line looks like this

    var worker = cluster.ActorOf(Props.Create(() => new Worker()).WithRouter(FromConfig.Instance), "worker");

    and the config looks like

    akka {
            actor {
                provider = "Akka.Cluster.ClusterActorRefProvider, Akka.Cluster"
                deployment {
                    /worker {
                        router = round-robin-pool #broadcast-pool also works
                        nr-of-instances = 10
                        cluster {
                                enabled = on
                                max-nr-of-instances-per-node = 2
                                allow-local-routees = on
                        }
                    }
                }
            }
    
            remote {
                helios.tcp {
                    hostname = "127.0.0.1"
                    port = 0
                }
            }            
    
            cluster {
                seed-nodes = ["akka.tcp://MyCluster@127.0.0.1:9999"]
            }
        }