public void ShouldRetryOnSniffConnectionException_Async()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                var uris = new[]
                {
                    new Uri("http://localhost:9200"),
                    new Uri("http://localhost:9201"),
                    new Uri("http://localhost:9202")
                };
                var connectionPool = new SniffingConnectionPool(uris, randomizeOnStartup: false);
                var config         = new ConnectionConfiguration(connectionPool)
                                     .SniffOnConnectionFault();

                fake.Provide <IConnectionConfigurationValues>(config);

                var pingAsyncCall = FakeCalls.PingAtConnectionLevelAsync(fake);
                pingAsyncCall.Returns(FakeResponse.OkAsync(config));

                //sniffing is always synchronous and in turn will issue synchronous pings
                var pingCall = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(FakeResponse.Ok(config));

                var sniffCall = FakeCalls.Sniff(fake);
                var seenPorts = new List <int>();
                sniffCall.ReturnsLazily((Uri u, IRequestConfiguration c) =>
                {
                    seenPorts.Add(u.Port);
                    throw new Exception("Something bad happened");
                });

                var getCall = FakeCalls.GetCall(fake);
                getCall.Returns(FakeResponse.BadAsync(config));

                FakeCalls.ProvideDefaultTransport(fake);

                var client = fake.Resolve <ElasticsearchClient>();

                var e = Assert.Throws <MaxRetryException>(async() => await client.NodesHotThreadsAsync("nodex"));

                //all nodes must be tried to sniff for more information
                sniffCall.MustHaveHappened(Repeated.Exactly.Times(uris.Count()));
                //make sure we only saw one call to hot threads (the one that failed initially)
                getCall.MustHaveHappened(Repeated.Exactly.Once);

                //make sure the sniffs actually happened on all the individual nodes
                seenPorts.ShouldAllBeEquivalentTo(uris.Select(u => u.Port));
                e.InnerException.Message.Should().Contain("Sniffing known nodes");
            }
        }
示例#2
0
        public void ShouldThrowAndNotRetrySniffOnConnectionFault401_Async()
        {
            using (var fake = new AutoFake(callsDoNothing: true))
            {
                var uris = new[]
                {
                    new Uri("http://localhost:9200"),
                    new Uri("http://localhost:9201"),
                    new Uri("http://localhost:9202")
                };
                var connectionPool = new SniffingConnectionPool(uris, randomizeOnStartup: false);
                var config         = new ConnectionConfiguration(connectionPool)
                                     .SniffOnConnectionFault()
                                     .ThrowOnElasticsearchServerExceptions();

                fake.Provide <IConnectionConfigurationValues>(config);
                FakeCalls.ProvideDefaultTransport(fake);

                var pingAsyncCall = FakeCalls.PingAtConnectionLevelAsync(fake);
                pingAsyncCall.Returns(FakeResponse.OkAsync(config));

                var pingCall = FakeCalls.PingAtConnectionLevel(fake);
                pingCall.Returns(FakeResponse.Ok(config));

                var sniffCall = FakeCalls.Sniff(fake);
                var seenPorts = new List <int>();
                sniffCall.ReturnsLazily((Uri u, IRequestConfiguration c) =>
                {
                    seenPorts.Add(u.Port);
                    return(FakeResponse.Any(config, 401));
                });

                var getCall = FakeCalls.GetCall(fake);
                getCall.Returns(FakeResponse.BadAsync(config));

                var client = fake.Resolve <ElasticsearchClient>();

                var e = Assert.Throws <ElasticsearchServerException>(async() => await client.InfoAsync());
                e.Status.Should().Be(401);
                sniffCall.MustHaveHappened(Repeated.Exactly.Once);
                getCall.MustHaveHappened(Repeated.Exactly.Once);
            }
        }
示例#3
0
        public async void HostsReturnedBySniffAreVisited_Async()
        {
            using (var fake = new AutoFake())
            {
                var dateTimeProvider = fake.Resolve <IDateTimeProvider>();
                var nowCall          = A.CallTo(() => dateTimeProvider.Now());
                nowCall.Returns(DateTime.UtcNow);

                var connectionPool = new SniffingConnectionPool(new[]
                {
                    new Uri("http://localhost:9200"),
                    new Uri("http://localhost:9201")
                }, randomizeOnStartup: false);
                var config = new ConnectionConfiguration(connectionPool)
                             .SniffOnConnectionFault();
                fake.Provide <IConnectionConfigurationValues>(config);
                FakeCalls.ProvideDefaultTransport(fake);
                FakeCalls.PingAtConnectionLevelAsync(fake)
                .ReturnsLazily(() =>
                               FakeResponse.OkAsync(config)
                               );

                var sniffCall = FakeCalls.Sniff(fake, config, new List <Uri>()
                {
                    new Uri("http://localhost:9204"),
                    new Uri("http://localhost:9203"),
                    new Uri("http://localhost:9202"),
                    new Uri("http://localhost:9201")
                });

                var connection = fake.Resolve <IConnection>();
                var seenNodes  = new List <Uri>();
                //var getCall =  FakeResponse.GetSyncCall(fake);
                var getCall = A.CallTo(() => connection.Get(
                                           A <Uri> .That.Not.Matches(u => u.AbsolutePath.StartsWith("/_nodes")),
                                           A <IRequestConfiguration> ._));
                getCall.ReturnsNextFromSequence(
                    FakeResponse.OkAsync(config),                    //info 1
                    FakeResponse.BadAsync(config),                   //info 2
                    FakeResponse.OkAsync(config),                    //info 2 retry
                    FakeResponse.OkAsync(config),                    //info 3
                    FakeResponse.OkAsync(config),                    //info 4
                    FakeResponse.OkAsync(config),                    //info 5
                    FakeResponse.OkAsync(config),                    //info 6
                    FakeResponse.OkAsync(config),                    //info 7
                    FakeResponse.OkAsync(config),                    //info 8
                    FakeResponse.OkAsync(config)                     //info 9
                    );
                getCall.Invokes((Uri u, IRequestConfiguration o) => seenNodes.Add(u));

                var client1 = fake.Resolve <ElasticsearchClient>();
                await client1.InfoAsync();                 //info call 1

                await client1.InfoAsync();                 //info call 2

                await client1.InfoAsync();                 //info call 3

                await client1.InfoAsync();                 //info call 4

                await client1.InfoAsync();                 //info call 5

                await client1.InfoAsync();                 //info call 6

                await client1.InfoAsync();                 //info call 7

                await client1.InfoAsync();                 //info call 8

                await client1.InfoAsync();                 //info call 9

                sniffCall.MustHaveHappened(Repeated.Exactly.Once);
                seenNodes.Should().NotBeEmpty().And.HaveCount(10);
                seenNodes[0].Port.Should().Be(9200);
                seenNodes[1].Port.Should().Be(9201);
                //after sniff
                seenNodes[2].Port.Should().Be(9202, string.Join(",", seenNodes.Select(n => n.Port)));
                seenNodes[3].Port.Should().Be(9204);
                seenNodes[4].Port.Should().Be(9203);
                seenNodes[5].Port.Should().Be(9202);
                seenNodes[6].Port.Should().Be(9201);
            }
        }
        public async void DeadNodeIsSkipped_AndRevivedAfterTimeout_Async()
        {
            using (var fake = new AutoFake())
            {
                var dateTimeProvider = ProvideDateTimeProvider(fake);
                var config           = ProvideConfiguration(dateTimeProvider);
                var connection       = ProvideConnection(fake, config);

                var getCall = FakeCalls.GetCall(fake);
                var ok      = Task.FromResult(FakeResponse.Ok(config));
                var bad     = Task.FromResult(FakeResponse.Bad(config));
                getCall.ReturnsNextFromSequence(
                    ok,                      //info 1 - 9204
                    bad,                     //info 2 - 9203 DEAD
                    ok,                      //info 2 retry - 9202
                    ok,                      //info 3 - 9201
                    ok,                      //info 4 - 9204
                    ok,                      //info 5 - 9202
                    ok,                      //info 6 - 9201
                    ok,                      //info 7 - 9204
                    ok,                      //info 8 - 9203 (Now > Timeout)
                    ok                       //info 9 - 9202
                    );

                var seenNodes = new List <Uri>();
                getCall.Invokes((Uri u, IRequestConfiguration o) => seenNodes.Add(u));

                var pingCall = FakeCalls.PingAtConnectionLevelAsync(fake);
                pingCall.Returns(ok);

                var client1 = fake.Resolve <ElasticsearchClient>();
                var result  = await client1.InfoAsync();                //info call 1

                //first time node is used so a ping is sent first
                result.Metrics.Requests.Count.Should().Be(2);
                result.Metrics.Requests.First().RequestType.Should().Be(RequestType.Ping);
                result.Metrics.Requests.First().Node.Port.Should().Be(9204);
                result.Metrics.Requests.Last().RequestType.Should().Be(RequestType.ElasticsearchCall);
                result.Metrics.Requests.Last().Node.Port.Should().Be(9204);


                result = await client1.InfoAsync();                 //info call 2

                result.Metrics.Requests.Count.Should().Be(4);

                //using 9203 for the first time ping succeeds
                result.Metrics.Requests.First().RequestType.Should().Be(RequestType.Ping);
                result.Metrics.Requests.First().Node.Port.Should().Be(9203);
                result.Metrics.Requests.First().Success.Should().BeTrue();

                //call on 9203 fails
                result.Metrics.Requests[1].RequestType.Should().Be(RequestType.ElasticsearchCall);
                result.Metrics.Requests[1].Node.Port.Should().Be(9203);
                result.Metrics.Requests[1].Success.Should().BeFalse();
                result.Metrics.Requests[1].HttpStatusCode.Should().Be(503);

                //using 9202 for the first time ping succeeds
                result.Metrics.Requests[2].RequestType.Should().Be(RequestType.Ping);
                result.Metrics.Requests[2].Node.Port.Should().Be(9202);

                //call on 9203 fails
                result.Metrics.Requests[3].RequestType.Should().Be(RequestType.ElasticsearchCall);
                result.Metrics.Requests[3].Node.Port.Should().Be(9202);
                result.Metrics.Requests[3].Success.Should().BeTrue();
                result.Metrics.Requests[3].HttpStatusCode.Should().Be(200);

                result = await client1.InfoAsync();                 //info call 3

                result = await client1.InfoAsync();                 //info call 4

                result = await client1.InfoAsync();                 //info call 5

                result = await client1.InfoAsync();                 //info call 6

                result = await client1.InfoAsync();                 //info call 7

                result = await client1.InfoAsync();                 //info call 8

                result = await client1.InfoAsync();                 //info call 9

                AssertSeenNodesAreInExpectedOrder(seenNodes);

                //4 nodes first time usage + 1 time after the first time 9203 came back to live
                pingCall.MustHaveHappened(Repeated.Exactly.Times(5));
            }
        }