Ejemplo n.º 1
0
 private void ClusterClient_must_startup_cluster_with_single_node()
 {
     Within(TimeSpan.FromSeconds(30), () =>
     {
         Join(_config.First, _config.First);
         RunOn(() =>
         {
             var service = Sys.ActorOf(EchoActor.Props(this), "testService");
             ClusterClientReceptionist.Get(Sys).RegisterService(service);
             AwaitMembersUp(1);
         }, _config.First);
         EnterBarrier("cluster-started");
     });
 }
Ejemplo n.º 2
0
        public void ClusterClient_must_demonstrate_usage()
        {
            var host1 = _config.First;
            var host2 = _config.Second;
            var host3 = _config.Third;

            Within(15.Seconds(), () =>
            {
                //#server
                RunOn(() =>
                {
                    var serviceA = Sys.ActorOf(Props.Create <ClusterClientSpecConfig.Service>(), "serviceA");
                    ClusterClientReceptionist.Get(Sys).RegisterService(serviceA);
                }, host1);

                RunOn(() =>
                {
                    var serviceB = Sys.ActorOf(Props.Create <ClusterClientSpecConfig.Service>(), "serviceB");
                    ClusterClientReceptionist.Get(Sys).RegisterService(serviceB);
                }, host2, host3);
                //#server

                RunOn(() =>
                {
                    AwaitCount(4);
                }, host1, host2, host3, _config.Fourth);
                EnterBarrier("services-replicated");

                //#client
                RunOn(() =>
                {
                    var c = Sys.ActorOf(ClusterClient.Props(ClusterClientSettings.Create(Sys).WithInitialContacts(InitialContacts)), "client");
                    c.Tell(new ClusterClient.Send("/user/serviceA", "hello", localAffinity: true));
                    c.Tell(new ClusterClient.SendToAll("/user/serviceB", "hi"));
                }, _config.Client);
                //#client

                RunOn(() =>
                {
                    // note that "hi" was sent to 2 "serviceB"
                    var received = ReceiveN(3);
                    received.ToImmutableHashSet().Should().BeEquivalentTo(ImmutableHashSet.Create("hello", "hi"));
                }, _config.Client);

                // strange, barriers fail without this sleep
                Thread.Sleep(1000);
                EnterBarrier("after-4");
            });
        }
Ejemplo n.º 3
0
        public void ClusterClient_must_reestablish_connection_to_receptionist_after_server_restart()
        {
            Within(30.Seconds(), () =>
            {
                RunOn(() =>
                {
                    _remainingServerRoleNames.Count.Should().Be(1);
                    var remainingContacts = _remainingServerRoleNames.Select(r => Node(r) / "system" / "receptionist").ToImmutableHashSet();
                    var c = Sys.ActorOf(ClusterClient.Props(ClusterClientSettings.Create(Sys).WithInitialContacts(remainingContacts)), "client4");

                    c.Tell(new ClusterClient.Send("/user/service2", "bonjour4", localAffinity: true));
                    var reply = ExpectMsg <ClusterClientSpecConfig.Reply>(10.Seconds());
                    reply.Msg.Should().Be("bonjour4-ack");
                    reply.Node.Should().Be(remainingContacts.First().Address);

                    // TODO: bug, cannot compare with a logsource
                    var logSource = $"{Sys.AsInstanceOf<ExtendedActorSystem>().Provider.DefaultAddress}/user/client4";

                    EventFilter.Info(start: "Connected to").ExpectOne(() =>
                    {
                        EventFilter.Info(start: "Lost contact").ExpectOne(() =>
                        {
                            // shutdown server
                            TestConductor.Shutdown(_remainingServerRoleNames.First()).Wait();
                        });
                    });

                    c.Tell(new ClusterClient.Send("/user/service2", "shutdown", localAffinity: true));
                    Thread.Sleep(2000); // to ensure that it is sent out before shutting down system
                }, _config.Client);

                RunOn(() =>
                {
                    Sys.WhenTerminated.Wait(20.Seconds());
                    // start new system on same port
                    var port = Cluster.Get(Sys).SelfAddress.Port;
                    var sys2 = ActorSystem.Create(
                        Sys.Name,
                        ConfigurationFactory.ParseString($"akka.remote.dot-netty.tcp.port={port}").WithFallback(Sys.Settings.Config));
                    Cluster.Get(sys2).Join(Cluster.Get(sys2).SelfAddress);
                    var service2 = sys2.ActorOf(Props.Create(() => new ClusterClientSpecConfig.TestService(TestActor)), "service2");
                    ClusterClientReceptionist.Get(sys2).RegisterService(service2);
                    sys2.WhenTerminated.Wait(20.Seconds());
                }, _remainingServerRoleNames.ToArray());
            });
        }
        public void Start()
        {
            Logger.Info("Starting shard");

            var system = ActorSystem.Create(Constants.ActorSystemName, ConfigurationFactory.Load()
                                            .WithFallback(config)
                                            .WithFallback(ClusterSingletonManager.DefaultConfig()));
            var sharding = ClusterSharding.Get(system);

            shardRegion = sharding.Start(
                typeName: nameof(MyActor),
                entityProps: Props.Create(() => new MyActor(nodeId)),
                settings: ClusterShardingSettings.Create(system),
                messageExtractor: new MessageExtractor(Constants.MaxNumberOfNodes * 10)
                );
            ClusterClientReceptionist.Get(system).RegisterService(shardRegion);
        }
Ejemplo n.º 5
0
        public static void StartUp(string port)
        {
            var section = (AkkaConfigurationSection)ConfigurationManager.GetSection("akka");

            //Override the configuration of the port
            var config =
                ConfigurationFactory.ParseString("akka.remote.dot-netty.tcp.port=" + port)
                .WithFallback(section.AkkaConfig);

            //create an Akka system
            var system = ActorSystem.Create("cluster-playground", config);

            //create an actor that handles cluster domain events
            var node = system.ActorOf(Node.MyProps(port), "node");

            ClusterClientReceptionist.Get(system).RegisterService(node);
            system.WhenTerminated.Wait();
        }
Ejemplo n.º 6
0
        //[MultiNodeFact(Skip = "TODO")]
        public void ClusterClient_should_demonstrate_usage()
        {
            ClusterClient_should_communicate_to_any_node_in_cluster();

            Within(TimeSpan.FromSeconds(15), () =>
            {
                RunOn(() =>
                {
                    var serviceA = Sys.ActorOf(Props.Create <Service>(), "serviceA");
                    ClusterClientReceptionist.Get(Sys).RegisterService(serviceA);
                }, _first);

                RunOn(() =>
                {
                    var serviceB = Sys.ActorOf(Props.Create <Service>(), "serviceB");
                    ClusterClientReceptionist.Get(Sys).RegisterService(serviceB);
                }, _second, _third);

                RunOn(() =>
                {
                    AwaitCount(4);
                }, _first, _second, _third, _fourth);

                RunOn(() =>
                {
                    var c = Sys.ActorOf(Client.ClusterClient.Props(ClusterClientSettings.Create(Sys).WithInitialContacts(InitialContacts)), "client");
                    c.Tell(new Client.ClusterClient.Send("/user/serviceA", "hello", localAffinity: true));
                    c.Tell(new Client.ClusterClient.SendToAll("/user/serviceB", "hi"));
                }, _client);

                RunOn(() =>
                {
                    // note that "hi" was sent to 2 "serviceB"
                    var received = ReceiveN(3);
                    Assert.True(received.Contains("hello"));
                    Assert.True(received.Contains("hi"));
                }, _client);

                // strange, barriers fail without this sleep
                Thread.Sleep(1000);
                EnterBarrier("after-3");
            });
        }
        protected override void PreStart()
        {
            base.PreStart();

            _receptionistActor = ClusterClientReceptionist.Get(Context.System).Underlying;

            //
            // 이벤트 등록: SubscribeClusterClients.Instance
            //      -> Receive<ClusterClients>
            //      -> Receive<ClusterClientUp>
            //      -> Receive<ClusterClientUnreachable >
            // 이벤트 제거: UnsubscribeClusterClients.Instance
            //
            //   vs.
            //
            // 명시적 확인 : GetClusterClients.Instance
            //      ->
            _receptionistActor.Tell(SubscribeClusterClients.Instance);
        }
Ejemplo n.º 8
0
        public async Task StartAsync()
        {
            var setup = ActorSystemSetup.Create(
                BootstrapSetup.Create()
                .WithConfig(Util.Config(_port))
                .WithConfigFallback(DistributedPubSub.DefaultConfig())
                .WithConfigFallback(ClusterClientReceptionist.DefaultConfig())
                .WithActorRefProvider(ProviderSelection.Cluster.Instance));

            _actorSystem = ActorSystem.Create(Consts.NodeName, setup);
            var cluster = Akka.Cluster.Cluster.Get(_actorSystem);
            await cluster.JoinSeedNodesAsync(Consts.Seeds);

            _receptionist = ClusterClientReceptionist.Get(_actorSystem);
            foreach (var id in Enumerable.Range(_nodeId * Consts.ActorCount, Consts.ActorCount))
            {
                var actor = _actorSystem.ActorOf(Props.Create(() => new ServiceActor(id)), Util.ActorName(id));
                _receptionist.RegisterService(actor);
            }
        }
Ejemplo n.º 9
0
        public void ClusterClient_must_startup_cluster()
        {
            Within(30.Seconds(), () =>
            {
                Join(_config.First, _config.First);
                Join(_config.Second, _config.First);

                RunOn(() =>
                {
                    var service = Sys.ActorOf(Props.Create(() => new ClusterClientStopSpecConfig.Service()), "testService");
                    ClusterClientReceptionist.Get(Sys).RegisterService(service);
                }, _config.First);

                RunOn(() =>
                {
                    AwaitCount(1);
                }, _config.First, _config.Second);

                EnterBarrier("cluster-started");
            });
        }
Ejemplo n.º 10
0
        static void Main(string[] args)
        {
            var port    = args.Length > 0 ? int.Parse(args[0]) : 0;
            var section = (AkkaConfigurationSection)ConfigurationManager.GetSection("akka");

            var config = ConfigurationFactory
                         .ParseString($"akka.remote.dot-netty.tcp.port={port}")
                         .WithFallback(section.AkkaConfig);

            using (var system = ActorSystem.Create("Cluster", config))
            {
                Props props = Props.Create(() => new WorkerActor());
                // Uocite da je receptionist takodjer IActorRef
                var service = system.ActorOf(props, "service");
                //Retrieve the cluster receptionist extension
                var receptionist = ClusterClientReceptionist.Get(system);
                //Register the service with the cluster client
                receptionist.RegisterService(service);

                Console.ReadLine();
            }
        }
Ejemplo n.º 11
0
        //[MultiNodeFact(Skip = "TODO")]
        public void ClusterClient_should_startup_cluster()
        {
            Within(TimeSpan.FromSeconds(30), () =>
            {
                Join(_first, _first);
                Join(_second, _first);
                Join(_third, _first);
                Join(_fourth, _first);

                RunOn(() =>
                {
                    var service = Sys.ActorOf(Props.Create(() => new TestService(TestActor)), "testService");
                    ClusterClientReceptionist.Get(Sys).RegisterService(service);
                }, _fourth);

                RunOn(() =>
                {
                    AwaitCount(1);
                }, _first, _second, _third, _fourth);
                EnterBarrier("after-1");
            });
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Starts a job, which establishes cluster client receptionist for target <see cref="EchoReceiver"/> actor,
        /// making it visible from outside of the cluster.
        /// </summary>
        static void RunClusterClientSeed(ActorSystem system)
        {
            var receptionist = ClusterClientReceptionist.Get(system);

            receptionist.RegisterService(system.ActorOf(Props.Create <EchoReceiver>(), "my-service"));
        }
Ejemplo n.º 13
0
 protected override void PreStart()
 {
     var c = ClusterClientReceptionist.Get(Context.System);
     //c.RegisterService(Self);
 }
Ejemplo n.º 14
0
 private void CreateReceptionist()
 {
     var x = ClusterClientReceptionist.Get(Sys);
 }
Ejemplo n.º 15
0
        public void ClusterClient_must_report_events()
        {
            Within(15.Seconds(), () =>
            {
                RunOn(() =>
                {
                    var c = Sys.ActorSelection("/user/client").ResolveOne(Dilated(1.Seconds())).Result;
                    var l = Sys.ActorOf(
                        Props.Create(() => new ClusterClientSpecConfig.TestClientListener(c)),
                        "reporter-client-listener");

                    var expectedContacts = ImmutableHashSet.Create(_config.First, _config.Second, _config.Third, _config.Fourth)
                                           .Select(_ => Node(_) / "system" / "receptionist");

                    Within(10.Seconds(), () =>
                    {
                        AwaitAssert(() =>
                        {
                            var probe = CreateTestProbe();
                            l.Tell(ClusterClientSpecConfig.TestClientListener.GetLatestContactPoints.Instance, probe.Ref);
                            probe.ExpectMsg <ClusterClientSpecConfig.TestClientListener.LatestContactPoints>()
                            .ContactPoints.Should()
                            .BeEquivalentTo(expectedContacts);
                        });
                    });
                }, _config.Client);


                EnterBarrier("reporter-client-listener-tested");

                RunOn(() =>
                {
                    // Only run this test on a node that knows about our client. It could be that no node knows
                    // but there isn't a means of expressing that at least one of the nodes needs to pass the test.
                    var r = ClusterClientReceptionist.Get(Sys).Underlying;
                    r.Tell(GetClusterClients.Instance);
                    var cps = ExpectMsg <ClusterClients>();
                    if (cps.ClusterClientsList.Any(c => c.Path.Name.Equals("client")))
                    {
                        Log.Info("Testing that the receptionist has just one client");
                        var l = Sys.ActorOf(
                            Props.Create(() => new ClusterClientSpecConfig.TestReceptionistListener(r)),
                            "reporter-receptionist-listener");

                        var c = Sys
                                .ActorSelection(Node(_config.Client) / "user" / "client")
                                .ResolveOne(Dilated(2.Seconds())).Result;

                        var expectedClients = ImmutableHashSet.Create(c);
                        Within(10.Seconds(), () =>
                        {
                            AwaitAssert(() =>
                            {
                                var probe = CreateTestProbe();
                                l.Tell(ClusterClientSpecConfig.TestReceptionistListener.GetLatestClusterClients.Instance, probe.Ref);

                                // "ask-client" might still be around, filter
                                probe.ExpectMsg <ClusterClientSpecConfig.TestReceptionistListener.LatestClusterClients>()
                                .ClusterClients.Should()
                                .Contain(expectedClients);
                            });
                        });
                    }
                }, _config.First, _config.Second, _config.Third);

                EnterBarrier("after-5");
            });
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Creates a new actor system according to the supplied parameters and the
        /// "hub.hocon" file
        ///
        /// Assumed transport used is tcp
        /// </summary>
        /// <param name="systemName">System name</param>
        /// <param name="hostname">IP address or hostname</param>
        /// <param name="requestedPort">Non-zero port</param>
        /// <returns>The Actor System</returns>
        public static ActorSystem Create(string systemName, string hostname, int?requestedPort)
        {
            if (!File.Exists("hub.hocon"))
            {
                throw new FileLoadException($"Could not find 'hub.hocon' file in {Directory.GetCurrentDirectory()}");
            }

            // Reads in the entire hocon configuration and parses to a Config object
            var configText    = File.ReadAllText("hub.hocon");
            var clusterConfig = ConfigurationFactory.ParseString(configText);

            // Find the "hub" specific configuration
            var hubConfig = clusterConfig.GetConfig("hub");

            // Set the actor system name if supplied value is null or empty
            if (hubConfig != null && string.IsNullOrEmpty(systemName))
            {
                systemName = hubConfig.GetString("actorsystem");
            }

            var remoteConfig = clusterConfig.GetConfig("akka.remote");

            if (string.IsNullOrEmpty(hostname))
            {
                // Defaults to localhost as final step
                hostname = remoteConfig.GetString("dot-netty.tcp.public-hostname") ?? "127.0.0.1";
            }

            int port = requestedPort ?? remoteConfig.GetInt("dot-netty.tcp.port");

            if (port == 0)
            {
                throw new ConfigurationException("An explicit port must be specified for the Hub. Zero is unnacceptable.");
            }

            // Display configuration
            Log.Information($"[Cluster Hub] ActorSystem: {systemName}; IP: {hostname}; PORT: {port}");
            Log.Information($"[Cluster Hub] Parsing address");
            Log.Information("[Cluster Hub] Parse successful");
            Log.Information("[Cluster Hub] Public hostname {PublicHubHostname}", hostname);

            var selfAddress = new Address("akka.tcp", systemName, hostname.Trim(), port).ToString();

            var seeds = clusterConfig.GetStringList("akka.cluster.seed-nodes");

            seeds.Add(selfAddress);

            var injectedClusterConfigString = seeds.Aggregate("akka.cluster.seed-nodes = [", (current, seed) => current + (@"""" + seed + @""","));

            injectedClusterConfigString += "]";

            var finalConfig = ConfigurationFactory.ParseString(
                string.Format("akka.remote.dot-netty.tcp.public-hostname = {0}, akka.remote.dot-netty.tcp.port = {1}", hostname, port)
                )
                              .WithFallback(ConfigurationFactory.ParseString(injectedClusterConfigString))
                              .WithFallback(clusterConfig);

            // Create the actor system
            var system = ActorSystem.Create(systemName, finalConfig);

            var proxySettings = ClusterSingletonProxySettings.Create(system).WithBufferSize(1000);

            var name = "manager";

            // Create the cluster singleton proxy for the node manager
            var managerProxy = system.ActorOf(
                ClusterSingletonProxy.Props(
                    singletonManagerPath: $"/user/{name}",
                    settings: proxySettings
                    ),
                $"{name}-proxy");

            // Subscribe the forwarder to the client receptionist topic
            var forwarder = system.ActorOf(Props.Create(() => new PublisherActor("client-messages")), "forwarder");

            ClusterClientReceptionist.Get(system).RegisterSubscriber("client-messages", forwarder);
            // ClusterClientReceptionist.Get(system).RegisterService(managerProxy);

            return(system);
        }
Ejemplo n.º 17
0
        public void ClusterClient_must_reestablish_connection_to_another_receptionist_when_server_is_shutdown()
        {
            Within(30.Seconds(), () =>
            {
                RunOn(() =>
                {
                    var service2 = Sys.ActorOf(Props.Create(() => new ClusterClientSpecConfig.TestService(TestActor)), "service2");
                    ClusterClientReceptionist.Get(Sys).RegisterService(service2);
                    AwaitCount(8);
                }, _config.First, _config.Second, _config.Third, _config.Fourth);
                EnterBarrier("service2-replicated");

                RunOn(() =>
                {
                    var c = Sys.ActorOf(ClusterClient.Props(ClusterClientSettings.Create(Sys).WithInitialContacts(InitialContacts)), "client2");
                    c.Tell(new ClusterClient.Send("/user/service2", "bonjour", localAffinity: true));
                    var reply = ExpectMsg <ClusterClientSpecConfig.Reply>();
                    reply.Msg.Should().Be("bonjour-ack");

                    RoleName receptionistRoleName = GetRoleName(reply.Node);
                    if (receptionistRoleName == null)
                    {
                        throw new Exception("Unexpected missing role name: " + reply.Node);
                    }

                    TestConductor.Exit(receptionistRoleName, 0).Wait();
                    _remainingServerRoleNames = _remainingServerRoleNames.Remove(receptionistRoleName);

                    Within(Remaining - 3.Seconds(), () =>
                    {
                        AwaitAssert(() =>
                        {
                            c.Tell(new ClusterClient.Send("/user/service2", "hi again", localAffinity: true));
                            ExpectMsg <ClusterClientSpecConfig.Reply>(1.Seconds()).Msg.Should().Be("hi again-ack");
                        });
                    });
                    Sys.Stop(c);
                }, _config.Client);
                EnterBarrier("verified-3");
                ReceiveWhile(2.Seconds(), msg =>
                {
                    if (msg.Equals("hi again"))
                    {
                        return(msg);
                    }
                    else
                    {
                        throw new Exception("Unexpected message: " + msg);
                    }
                });
                EnterBarrier("verified-4");

                RunOn(() =>
                {
                    // Locate the test listener from a previous test and see that it agrees
                    // with what the client is telling it about what receptionists are alive
                    var l = Sys.ActorSelection("/user/reporter-client-listener");
                    var expectedContacts = _remainingServerRoleNames.Select(c => Node(c) / "system" / "receptionist");
                    Within(10.Seconds(), () =>
                    {
                        AwaitAssert(() =>
                        {
                            var probe = CreateTestProbe();
                            l.Tell(ClusterClientSpecConfig.TestClientListener.GetLatestContactPoints.Instance, probe.Ref);
                            probe.ExpectMsg <ClusterClientSpecConfig.TestClientListener.LatestContactPoints>()
                            .ContactPoints.Should()
                            .BeEquivalentTo(expectedContacts);
                        });
                    });
                }, _config.Client);

                EnterBarrier("after-4");
            });
        }