コード例 #1
0
        private static void Run()
        {
            var config = ConfigurationFactory.ParseString(@"
	            akka {
		            actor.provider = cluster
                    remote.dot-netty.tcp {
                        hostname = ""127.0.0.1""
                        port = 0
                    }
		            cluster.discovery {
			            provider = akka.cluster.discovery.consul
			            consul {
				            listener-url = ""http://127.0.0.1:8500""
		                    refresh-interval = 1m
			            }
		            }
	            }"    );

            using (var system = ActorSystem.Create("sample", config))
            {
                ClusterDiscovery.Join(system);

                system.ActorOf(Props.Create <SampleActor>());
                Console.ReadLine();
            }
        }
コード例 #2
0
            public async Task ShouldCarryOutRediscoveryWith40Server(int routerCount, int writerCount, int readerCount,
                                                                    string database, params string[] bookmarks)
            {
                // Given
                var routingContext = new Dictionary <string, string>
                {
                    { "name", "molly" },
                    { "age", "1" },
                    { "color", "white" }
                };
                var recordFields = CreateGetServersResponseRecordFields(routerCount, writerCount, readerCount);
                var mockConn     =
                    Setup40SocketConnection(routingContext, database, Bookmark.From(bookmarks), recordFields);
                var manager = new ClusterDiscovery(routingContext, null);

                // When
                var table = await manager.DiscoverAsync(mockConn.Object, database, Bookmark.From(bookmarks));

                // Then
                table.Database.Should().Be(database ?? "");
                table.Readers.Count().Should().Be(readerCount);
                table.Writers.Count().Should().Be(writerCount);
                table.Routers.Count().Should().Be(routerCount);
                table.ExpireAfterSeconds.Should().Be(15000L);
                mockConn.Verify(x => x.CloseAsync(), Times.Once);
            }
コード例 #3
0
            public async Task ShouldCarryOutRediscoveryWith43Server(int routerCount, int writerCount, int readerCount,
                                                                    string database, params string[] bookmarks)
            {
                // Given
                var routingContext = new Dictionary <string, string>
                {
                    { "address", "127.0.0.1:9001" },
                    { "region", "china" },
                    { "policy", "myp_policy" }
                };
                var recordFields = CreateGetServersDictionary(routerCount, writerCount, readerCount);
                var mockConn     = Setup43SocketConnection(routingContext, database, Bookmark.From(bookmarks), recordFields);

                mockConn.Setup(m => m.RoutingContext).Returns(routingContext);
                var manager = new ClusterDiscovery(routingContext, null);

                // When
                var table = await manager.DiscoverAsync(mockConn.Object, database, Bookmark.From(bookmarks));

                // Then
                table.Database.Should().Be(database ?? "");
                table.Readers.Count().Should().Be(readerCount);
                table.Writers.Count().Should().Be(writerCount);
                table.Routers.Count().Should().Be(routerCount);
                table.ExpireAfterSeconds.Should().Be(15000L);
                mockConn.Verify(x => x.CloseAsync(), Times.Once);
            }
コード例 #4
0
            public void ShouldHaveLocalhost(string input, string host, int port)
            {
                var uri = ClusterDiscovery.BoltRoutingUri(input);

                uri.Scheme.Should().Be("neo4j");
                uri.Host.Should().Be(host);
                uri.Port.Should().Be(port);
            }
コード例 #5
0
        public override Task StartAsync(CancellationToken cancellationToken)
        {
            cluster = Akka.Cluster.Cluster.Get(ActorSystem);

            cluster.RegisterOnMemberUp(RegisterOnMemberUp);

            cluster.RegisterOnMemberRemoved(RegisterOnMemberRemoved);

            ClusterDiscovery.Join(ActorSystem);

            return(base.StartAsync(cancellationToken));
        }
コード例 #6
0
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            var myPodIp = GetLocalIPAddress();

            var config = ConfigurationFactory.ParseString(@"
	            akka {
		            actor.provider = cluster
                    remote.dot-netty.tcp {
                        hostname = """ + myPodIp + @"""
                        port = 2551 
                    }
                    cluster.roles = [sample,demo]
		            cluster.discovery {
			            provider = akka.cluster.discovery.k8s
			            k8s {
		                    refresh-interval = 10s
                            namespace = ""default""
                            label-selector = ""akka-cluster=sample,env=Development""
                        }
		            }
	            }"    );

            using var system = ActorSystem.Create("sample", config);
            await ClusterDiscovery.JoinAsync(system);

            system.ActorOf(Props.Create <SampleActor>());

            try
            {
                while (!stoppingToken.IsCancellationRequested)
                {
                    var sb = new StringBuilder();
                    sb.AppendLine($"My address: {myPodIp}");

                    var state = Cluster.Get(system).State;

                    foreach (var member in state.Members)
                    {
                        sb.AppendLine($"    Member: {member.Address} = {member.Status}, Seen by = {state.SeenBy.Contains(member.Address)} [{string.Join(", ", member.Roles)}]");
                    }
                    logger.LogInformation("Status:\r\n" + sb.ToString());

                    await Task.Delay(10000, stoppingToken);
                }
            }
            finally
            {
                var cluster = Cluster.Get(system);
                await cluster.LeaveAsync();
            }
        }
コード例 #7
0
        public override async Task StartAsync(CancellationToken cancellationToken)
        {

            cluster = Akka.Cluster.Cluster.Get(ActorSystem);

            cluster.RegisterOnMemberUp(RegisterOnMemberUp);

            cluster.RegisterOnMemberRemoved(RegisterOnMemberRemoved);
            ClusterDiscovery.Get(ActorSystem);
           

            await ClusterDiscovery.JoinAsync(ActorSystem);
           
        }
コード例 #8
0
            public async Task ShouldThrowWhenNoRecord()
            {
                // Given
                var connMock = SetupSocketConnection(new List <object[]>());
                var manager  = new ClusterDiscovery(null, null);

                // When
                var exception = await Record.ExceptionAsync(() => manager.DiscoverAsync(connMock.Object));

                // Then
                exception.Should().BeOfType <InvalidOperationException>().Which.Message.Should()
                .Be("The result is empty.");
                connMock.Verify(x => x.CloseAsync(), Times.Once);
            }
コード例 #9
0
            public async Task ShouldThrowWhenRecordUnparsable()
            {
                // Given
                var connMock = SetupSocketConnection(new object[] { 1 });
                var manager  = new ClusterDiscovery(null, null);

                // When
                var exception = await Record.ExceptionAsync(() => manager.DiscoverAsync(connMock.Object));

                // Then
                exception.Should().BeOfType <ProtocolException>().Which.Message.Should()
                .Be("keys (2) does not equal to values (1)");
                connMock.Verify(x => x.CloseAsync(), Times.Once);
            }
コード例 #10
0
            public async Task ShouldThrowExceptionIfReaderIsEmpty()
            {
                // Given
                var procedureReplyRecordFields = CreateGetServersResponseRecordFields(3, 1, 0);
                var connMock = SetupSocketConnection(procedureReplyRecordFields);
                var manager  = new ClusterDiscovery(null, null);

                // When
                var exception = await Record.ExceptionAsync(() => manager.DiscoverAsync(connMock.Object));

                // Then
                exception.Should().BeOfType <ProtocolException>().Which.Message.Should()
                .Contain("3 routers, 1 writers and 0 readers.");
                connMock.Verify(x => x.CloseAsync(), Times.Once);
            }
コード例 #11
0
            public void ShouldUseGetServersProcedure(string version)
            {
                // Given
                var discovery      = new ClusterDiscovery(null, null);
                var mock           = new Mock <IConnection>();
                var serverInfoMock = new Mock <IServerInfo>();

                serverInfoMock.Setup(m => m.Version).Returns(version);
                mock.Setup(m => m.Server).Returns(serverInfoMock.Object);
                // When
                var statement = discovery.DiscoveryProcedure(mock.Object);

                // Then
                statement.Text.Should().Be("CALL dbms.cluster.routing.getServers");
                statement.Parameters.Should().BeEmpty();
            }
コード例 #12
0
            public async Task ShouldCarryOutRediscovery(int routerCount, int writerCount, int readerCount)
            {
                // Given
                var recordFields = CreateGetServersResponseRecordFields(routerCount, writerCount, readerCount);
                var connMock     = SetupSocketConnection(recordFields);
                var manager      = new ClusterDiscovery(null, null);

                // When
                var table = await manager.DiscoverAsync(connMock.Object);

                // Then
                table.Readers.Count().Should().Be(readerCount);
                table.Writers.Count().Should().Be(writerCount);
                table.Routers.Count().Should().Be(routerCount);
                table.ExpireAfterSeconds.Should().Be(9223372036854775807);
                connMock.Verify(x => x.CloseAsync(), Times.Once);
            }
コード例 #13
0
            public async Task ShouldProtocolErrorWhenMultipleRecord()
            {
                // Given
                var connMock = SetupSocketConnection(new List <object[]>
                {
                    CreateGetServersResponseRecordFields(3, 2, 1),
                    CreateGetServersResponseRecordFields(3, 2, 1)
                });
                var manager = new ClusterDiscovery(null, null);

                // When
                var exception = await Record.ExceptionAsync(() => manager.DiscoverAsync(connMock.Object));

                // Then
                exception.Should().BeOfType <InvalidOperationException>().Which.Message.Should()
                .Be("The result contains more than one element.");
                connMock.Verify(x => x.CloseAsync(), Times.Once);
            }
コード例 #14
0
            public void ShouldUseGetRoutingTableProcedure(string version)
            {
                // Given
                var context = new Dictionary <string, string> {
                    { "context", string.Empty }
                };
                var discovery      = new ClusterDiscovery(context, null);
                var mock           = new Mock <IConnection>();
                var serverInfoMock = new Mock <IServerInfo>();

                serverInfoMock.Setup(m => m.Version).Returns(version);
                mock.Setup(m => m.Server).Returns(serverInfoMock.Object);
                // When
                var statement = discovery.DiscoveryProcedure(mock.Object, null);

                // Then
                statement.Text.Should()
                .Be("CALL dbms.cluster.routing.getRoutingTable($context)");
                statement.Parameters["context"].Should().Be(context);
            }
コード例 #15
0
            public void ShouldThrowWhenProcedureNotFound()
            {
                // Given
                var pairs = new List <Tuple <IRequestMessage, IResponseMessage> >
                {
                    MessagePair(
                        new RunWithMetadataMessage(new Statement("CALL dbms.cluster.routing.getRoutingTable($context)",
                                                                 new Dictionary <string, object> {
                        { "context", null }
                    }), AccessMode.Write),
                        new FailureMessage("Neo.ClientError.Procedure.ProcedureNotFound", "not found")),
                    MessagePair(PullAll, Ignored)
                };

                var connMock = new MockedConnection(AccessMode.Write, pairs).MockConn;
                var manager  = new ClusterDiscovery(null, null);

                // When & Then
                manager.Awaiting(m => m.DiscoverAsync(connMock.Object, null, Bookmark.Empty)).Should()
                .Throw <ClientException>().WithMessage("*not found*");
                connMock.Verify(x => x.CloseAsync(), Times.Once);
            }
コード例 #16
0
            public void ShouldUseGetRoutingTableForDatabaseProcedure(string version)
            {
                // Given
                var context = new Dictionary <string, string> {
                    { "context", string.Empty }
                };
                var discovery      = new ClusterDiscovery(context, null);
                var mock           = new Mock <IConnection>();
                var serverInfoMock = new Mock <IServerInfo>();

                serverInfoMock.Setup(m => m.Version).Returns(version);
                mock.Setup(m => m.Server).Returns(serverInfoMock.Object);
                mock.Setup(m => m.BoltProtocol).Returns(BoltProtocolV4.BoltV4);
                // When
                var query = discovery.DiscoveryProcedure(mock.Object, "foo");

                // Then
                query.Text.Should()
                .Be("CALL dbms.routing.getRoutingTable($context, $database)");
                query.Parameters["context"].Should().Be(context);
                query.Parameters["database"].Should().Be("foo");
            }
コード例 #17
0
            public async Task ShouldThrowWhenProcedureNotFound()
            {
                // Given
                var pairs = new List <Tuple <IRequestMessage, IResponseMessage> >
                {
                    MessagePair(
                        new RunMessage("CALL dbms.cluster.routing.getServers", new Dictionary <string, object>()),
                        new FailureMessage("Neo.ClientError.Procedure.ProcedureNotFound", "not found")),
                    MessagePair(PullAll, Ignored)
                };

                var connMock = new MockedConnection(pairs).MockConn;
                var manager  = new ClusterDiscovery(null, null);

                // When
                var exception = await Record.ExceptionAsync(() => manager.DiscoverAsync(connMock.Object));

                // Then
                exception.Should().BeOfType <ClientException>().Which.Message.Should()
                .Contain("not found");
                connMock.Verify(x => x.CloseAsync(), Times.Once);
            }
コード例 #18
0
            public async Task ShouldCarryOutRediscoveryWith32Server(int routerCount, int writerCount, int readerCount)
            {
                // Given
                var routingContext = new Dictionary <string, string>
                {
                    { "name", "molly" },
                    { "age", "1" },
                    { "color", "white" }
                };
                var recordFields = CreateGetServersResponseRecordFields(routerCount, writerCount, readerCount);
                var mockConn     = Setup32SocketConnection(routingContext, recordFields);
                var manager      = new ClusterDiscovery(routingContext, null);

                // When
                var table = await manager.DiscoverAsync(mockConn.Object);

                // Then
                table.Readers.Count().Should().Be(readerCount);
                table.Writers.Count().Should().Be(writerCount);
                table.Routers.Count().Should().Be(routerCount);
                table.ExpireAfterSeconds.Should().Be(9223372036854775807);
                mockConn.Verify(x => x.CloseAsync(), Times.Once);
            }
コード例 #19
0
        private static async Task RunAsync()
        {
            var config = ConfigurationFactory.ParseString(@"
	            akka {
		            actor.provider = cluster
                    remote.dot-netty.tcp {
                        hostname = ""127.0.0.1""
                        port = 0
                    }
		            cluster.discovery {
			            provider = akka.cluster.discovery.consul
			            consul {
				            listener-url = ""http://127.0.0.1:8500""
		                    refresh-interval = 10s
                            # restart consul client every 5 minutes
                            restart-interval = 5m
			            }
		            }
	            }"    );

            using (var system = ActorSystem.Create("sample", config))
            {
                await ClusterDiscovery.JoinAsync(system);

                system.ActorOf(Props.Create <SampleActor>());

                Console.WriteLine("Press enter to shutdown the current node...");
                Console.ReadLine();

                var cluster = Cluster.Get(system);
                await cluster.LeaveAsync();

                Console.WriteLine("Node terminated. Press enter to exit...");
                Console.ReadLine();
            }
        }