Search in sources :

Example 1 with ElasticsearchHost

use of org.springframework.data.elasticsearch.client.ElasticsearchHost in project spring-data-elasticsearch by spring-projects.

the class SingleNodeHostProvider method lookupActiveHost.

/*
	 * (non-Javadoc)
	 * @see org.springframework.data.elasticsearch.client.reactive.HostProvider#lookupActiveHost(org.springframework.data.elasticsearch.client.reactive.HostProvider.Verification)
	 */
@Override
public Mono<InetSocketAddress> lookupActiveHost(Verification verification) {
    if (Verification.LAZY.equals(verification) && state.isOnline()) {
        return Mono.just(endpoint);
    }
    return clusterInfo().handle((information, sink) -> {
        ElasticsearchHost host = information.getNodes().iterator().next();
        if (host.isOnline()) {
            sink.next(host.getEndpoint());
            return;
        }
        sink.error(new NoReachableHostException(Collections.singleton(host)));
    });
}
Also used : ElasticsearchHost(org.springframework.data.elasticsearch.client.ElasticsearchHost) NoReachableHostException(org.springframework.data.elasticsearch.client.NoReachableHostException)

Example 2 with ElasticsearchHost

use of org.springframework.data.elasticsearch.client.ElasticsearchHost in project spring-data-elasticsearch by spring-projects.

the class MultiNodeHostProviderUnitTests method triesDeadHostsIfNoActiveFound.

// DATAES-488
@Test
public void triesDeadHostsIfNoActiveFound() {
    multiNodeDelegatingHostProvider.when(HOST_1).receive(Receive::error);
    multiNodeDelegatingHostProvider.when(HOST_2).get(requestHeadersUriSpec -> {
        ClientResponse response1 = mock(ClientResponse.class);
        when(response1.releaseBody()).thenReturn(Mono.empty());
        Receive.error(response1);
        ClientResponse response2 = mock(ClientResponse.class);
        when(response2.releaseBody()).thenReturn(Mono.empty());
        Receive.ok(response2);
        // 
        when(requestHeadersUriSpec.exchangeToMono(any())).thenAnswer(// 
        invocation -> getAnswer(invocation, response1)).thenAnswer(invocation -> getAnswer(invocation, response2));
    });
    multiNodeDelegatingHostProvider.when(HOST_3).receive(Receive::error);
    delegateHostProvider.clusterInfo().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    assertThat(delegateHostProvider.getCachedHostState()).extracting(ElasticsearchHost::getState).containsExactly(State.OFFLINE, State.OFFLINE, State.OFFLINE);
    delegateHostProvider.getActive().as(StepVerifier::create).expectNext(multiNodeDelegatingHostProvider.client(HOST_2)).verifyComplete();
    verify(multiNodeDelegatingHostProvider.client(HOST_2), times(2)).head();
}
Also used : ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) BeforeEach(org.junit.jupiter.api.BeforeEach) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) StepVerifier(reactor.test.StepVerifier) Mono(reactor.core.publisher.Mono) Verification(org.springframework.data.elasticsearch.client.reactive.HostProvider.Verification) Function(java.util.function.Function) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ElasticsearchHost(org.springframework.data.elasticsearch.client.ElasticsearchHost) State(org.springframework.data.elasticsearch.client.ElasticsearchHost.State) Assertions(org.assertj.core.api.Assertions) MockDelegatingElasticsearchHostProvider(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockDelegatingElasticsearchHostProvider) Receive(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive) Receive(org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 3 with ElasticsearchHost

use of org.springframework.data.elasticsearch.client.ElasticsearchHost in project spring-data-elasticsearch by spring-projects.

the class MultiNodeHostProvider method checkNodes.

private Flux<Tuple2<InetSocketAddress, State>> checkNodes(@Nullable State state) {
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace("checkNodes() with state " + state);
    }
    return // 
    Flux.fromIterable(hosts()).filter(// 
    entry -> state == null || entry.getState().equals(state)).map(// 
    ElasticsearchHost::getEndpoint).concatMap(host -> {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("checking host " + host);
        }
        Mono<ClientResponse> clientResponseMono = // 
        createWebClient(host).head().uri(// 
        "/").exchangeToMono(// 
        Mono::just).timeout(// 
        Duration.ofSeconds(1)).doOnError(throwable -> {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("error checking host " + host + ", " + throwable.getMessage());
            }
            hosts.put(host, new ElasticsearchHost(host, State.OFFLINE));
            clientProvider.getErrorListener().accept(throwable);
        });
        return // 
        Mono.just(host).zipWith(clientResponseMono.flatMap(it -> // 
        it.releaseBody().thenReturn(it.statusCode().isError() ? State.OFFLINE : State.ONLINE)));
    }).map(tuple -> {
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("check result " + tuple);
        }
        return tuple;
    }).onErrorContinue((throwable, o) -> clientProvider.getErrorListener().accept(throwable));
}
Also used : ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) ElasticsearchHost(org.springframework.data.elasticsearch.client.ElasticsearchHost) ClientResponse(org.springframework.web.reactive.function.client.ClientResponse) NoReachableHostException(org.springframework.data.elasticsearch.client.NoReachableHostException) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Tuple2(reactor.util.function.Tuple2) WebClient(org.springframework.web.reactive.function.client.WebClient) Mono(reactor.core.publisher.Mono) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) Flux(reactor.core.publisher.Flux) List(java.util.List) Duration(java.time.Duration) Map(java.util.Map) ElasticsearchHost(org.springframework.data.elasticsearch.client.ElasticsearchHost) State(org.springframework.data.elasticsearch.client.ElasticsearchHost.State) Log(org.apache.commons.logging.Log) Nullable(org.springframework.lang.Nullable) LogFactory(org.apache.commons.logging.LogFactory) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) Mono(reactor.core.publisher.Mono)

Example 4 with ElasticsearchHost

use of org.springframework.data.elasticsearch.client.ElasticsearchHost in project spring-data-elasticsearch by spring-projects.

the class MultiNodeHostProvider method updateNodeState.

private ElasticsearchHost updateNodeState(Tuple2<InetSocketAddress, State> tuple2) {
    State state = tuple2.getT2();
    ElasticsearchHost elasticsearchHost = new ElasticsearchHost(tuple2.getT1(), state);
    hosts.put(tuple2.getT1(), elasticsearchHost);
    return elasticsearchHost;
}
Also used : ElasticsearchHost(org.springframework.data.elasticsearch.client.ElasticsearchHost) State(org.springframework.data.elasticsearch.client.ElasticsearchHost.State)

Aggregations

ElasticsearchHost (org.springframework.data.elasticsearch.client.ElasticsearchHost)4 State (org.springframework.data.elasticsearch.client.ElasticsearchHost.State)3 NoReachableHostException (org.springframework.data.elasticsearch.client.NoReachableHostException)2 ClientResponse (org.springframework.web.reactive.function.client.ClientResponse)2 Mono (reactor.core.publisher.Mono)2 InetSocketAddress (java.net.InetSocketAddress)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 LinkedHashSet (java.util.LinkedHashSet)1 List (java.util.List)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Function (java.util.function.Function)1 Log (org.apache.commons.logging.Log)1 LogFactory (org.apache.commons.logging.LogFactory)1 Assertions (org.assertj.core.api.Assertions)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 Test (org.junit.jupiter.api.Test)1