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)));
});
}
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();
}
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));
}
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;
}
Aggregations