DATAES-767 - Fix ReactiveElasticsearch handling of 4xx HTTP responses.

Original PR: #445
This commit is contained in:
Peter-Josef Meisch 2020-05-02 20:08:25 +02:00 committed by GitHub
parent 07ee01f435
commit e605cad688
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 156 additions and 48 deletions

View File

@ -49,6 +49,7 @@ import java.util.function.Supplier;
import javax.net.ssl.SSLContext;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchStatusException;
import org.elasticsearch.action.ActionRequest;
import org.elasticsearch.action.admin.indices.close.CloseIndexRequest;
@ -115,6 +116,7 @@ import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.reactive.function.BodyExtractors;
import org.springframework.web.reactive.function.client.ClientRequest;
@ -764,6 +766,12 @@ public class DefaultReactiveElasticsearchClient implements ReactiveElasticsearch
return handleServerError(request, response);
}
if (response.statusCode().is4xxClientError()) {
ClientLogger.logRawResponse(logId, response.statusCode());
return handleClientError(logId, request, response, responseType);
}
return response.body(BodyExtractors.toMono(byte[].class)) //
.map(it -> new String(it, StandardCharsets.UTF_8)) //
.doOnNext(it -> ClientLogger.logResponse(logId, response.statusCode(), it)) //
@ -800,13 +808,68 @@ public class DefaultReactiveElasticsearchClient implements ReactiveElasticsearch
DeprecationHandler.THROW_UNSUPPORTED_OPERATION, content);
}
private static <T> Publisher<? extends T> handleServerError(Request request, ClientResponse response) {
private <T> Publisher<? extends T> handleServerError(Request request, ClientResponse response) {
return Mono.error(
new HttpServerErrorException(response.statusCode(), String.format("%s request to %s returned error code %s.",
request.getMethod(), request.getEndpoint(), response.statusCode().value())));
}
private <T> Publisher<? extends T> handleClientError(String logId, Request request, ClientResponse response,
Class<T> responseType) {
return response.body(BodyExtractors.toMono(byte[].class)) //
.map(bytes -> new String(bytes, StandardCharsets.UTF_8)) //
.flatMap(content -> {
String mediaType = response.headers().contentType().map(MediaType::toString)
.orElse(XContentType.JSON.mediaType());
try {
ElasticsearchException exception = getElasticsearchException(response, content, mediaType);
if (exception != null) {
StringBuilder sb = new StringBuilder();
buildExceptionMessages(sb, exception);
return Mono.error(new HttpClientErrorException(response.statusCode(), sb.toString()));
}
} catch (Exception e) {
return Mono
.error(new ElasticsearchStatusException(content, RestStatus.fromCode(response.statusCode().value())));
}
return Mono.just(content);
})
.doOnNext(it -> ClientLogger.logResponse(logId, response.statusCode(), it)) //
.flatMap(content -> doDecode(response, responseType, content));
}
// region ElasticsearchException helper
@Nullable
private ElasticsearchException getElasticsearchException(ClientResponse response, String content, String mediaType)
throws IOException {
XContentParser parser = createParser(mediaType, content);
// we have a JSON object with an error and a status field
XContentParser.Token token = parser.nextToken(); // Skip START_OBJECT
do {
token = parser.nextToken();
if (parser.currentName().equals("error")) {
return ElasticsearchException.failureFromXContent(parser);
}
} while (token == XContentParser.Token.FIELD_NAME);
return null;
}
private static void buildExceptionMessages(StringBuilder sb, Throwable t) {
sb.append(t.getMessage());
for (Throwable throwable : t.getSuppressed()) {
sb.append(", ");
buildExceptionMessages(sb, throwable);
}
}
// endregion
// region internal classes
/**
* Reactive client {@link ReactiveElasticsearchClient.Status} implementation.
*
@ -867,4 +930,5 @@ public class DefaultReactiveElasticsearchClient implements ReactiveElasticsearch
}
}
}
// endregion
}

View File

@ -34,6 +34,7 @@ import org.springframework.data.elasticsearch.UncategorizedElasticsearchExceptio
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
/**
* @author Christoph Strobl
@ -63,6 +64,15 @@ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTra
return new UncategorizedElasticsearchException(ex.getMessage(), ex);
}
if (ex instanceof HttpClientErrorException) {
HttpClientErrorException httpClientErrorException = (HttpClientErrorException) ex;
if (isSeqNoConflict(httpClientErrorException)) {
return new OptimisticLockingFailureException("Cannot index a document due to seq_no+primary_term conflict",
httpClientErrorException);
}
}
if (ex instanceof ValidationException) {
return new DataIntegrityViolationException(ex.getMessage(), ex);
}
@ -75,7 +85,7 @@ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTra
return null;
}
private boolean isSeqNoConflict(ElasticsearchException exception) {
private boolean isSeqNoConflict(Exception exception) {
if (exception instanceof ElasticsearchStatusException) {
ElasticsearchStatusException statusException = (ElasticsearchStatusException) exception;
@ -90,6 +100,13 @@ public class ElasticsearchExceptionTranslator implements PersistenceExceptionTra
&& versionConflictEngineException.getMessage().contains("version conflict, required seqNo");
}
if (exception instanceof HttpClientErrorException) {
HttpClientErrorException httpClientErrorException = (HttpClientErrorException) exception;
return httpClientErrorException.getMessage() != null
&& httpClientErrorException.getMessage().contains("version conflict, required seqNo");
}
return false;
}

View File

@ -18,6 +18,7 @@ package org.springframework.data.elasticsearch.client.reactive;
import static org.assertj.core.api.Assertions.*;
import lombok.SneakyThrows;
import org.springframework.web.client.HttpClientErrorException;
import reactor.test.StepVerifier;
import java.io.IOException;
@ -147,7 +148,7 @@ public class ReactiveElasticsearchClientTests {
client.get(new GetRequest(INDEX_I, "nonono")) //
.as(StepVerifier::create) //
.expectError(ElasticsearchStatusException.class) //
.expectError(HttpClientErrorException.class) //
.verify();
}
@ -304,7 +305,7 @@ public class ReactiveElasticsearchClientTests {
client.index(request) //
.as(StepVerifier::create) //
.verifyError(ElasticsearchStatusException.class);
.verifyError(HttpClientErrorException.class);
}
@Test // DATAES-488
@ -353,7 +354,7 @@ public class ReactiveElasticsearchClientTests {
client.update(request) //
.as(StepVerifier::create) //
.verifyError(ElasticsearchStatusException.class);
.verifyError(HttpClientErrorException.class);
}
@Test // DATAES-488
@ -514,7 +515,7 @@ public class ReactiveElasticsearchClientTests {
client.indices().createIndex(request -> request.index(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(ElasticsearchStatusException.class);
.verifyError(HttpClientErrorException.class);
}
@Test // DATAES-569
@ -529,12 +530,12 @@ public class ReactiveElasticsearchClientTests {
assertThat(syncClient.indices().exists(new GetIndexRequest(INDEX_I), RequestOptions.DEFAULT)).isFalse();
}
@Test // DATAES-569
@Test // DATAES-569, DATAES-767
public void deleteNonExistingIndexErrors() {
client.indices().deleteIndex(request -> request.indices(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(ElasticsearchStatusException.class);
.verifyError(HttpClientErrorException.class);
}
@Test // DATAES-569
@ -547,12 +548,12 @@ public class ReactiveElasticsearchClientTests {
.verifyComplete();
}
@Test // DATAES-569
@Test // DATAES-569, DATAES-767
public void openNonExistingIndex() {
client.indices().openIndex(request -> request.indices(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(ElasticsearchStatusException.class);
.verifyError(HttpClientErrorException.class);
}
@Test // DATAES-569
@ -565,12 +566,12 @@ public class ReactiveElasticsearchClientTests {
.verifyComplete();
}
@Test // DATAES-569
@Test // DATAES-569, DATAES-767
public void closeNonExistingIndex() {
client.indices().closeIndex(request -> request.indices(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(ElasticsearchStatusException.class);
.verifyError(HttpClientErrorException.class);
}
@Test // DATAES-569
@ -583,12 +584,12 @@ public class ReactiveElasticsearchClientTests {
.verifyComplete();
}
@Test // DATAES-569
@Test // DATAES-569, DATAES-767
public void refreshNonExistingIndex() {
client.indices().refreshIndex(request -> request.indices(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(ElasticsearchStatusException.class);
.verifyError(HttpClientErrorException.class);
}
@Test // DATAES-569
@ -604,7 +605,7 @@ public class ReactiveElasticsearchClientTests {
.verifyComplete();
}
@Test // DATAES-569
@Test // DATAES-569, DATAES-767
public void updateMappingNonExistingIndex() {
Map<String, Object> jsonMap = Collections.singletonMap("properties",
@ -612,7 +613,7 @@ public class ReactiveElasticsearchClientTests {
client.indices().updateMapping(request -> request.indices(INDEX_I).type(TYPE_I).source(jsonMap)) //
.as(StepVerifier::create) //
.verifyError(ElasticsearchStatusException.class);
.verifyError(HttpClientErrorException.class);
}
@Test // DATAES-569
@ -625,12 +626,12 @@ public class ReactiveElasticsearchClientTests {
.verifyComplete();
}
@Test // DATAES-569
@Test // DATAES-569, DATAES-767
public void flushNonExistingIndex() {
client.indices().flushIndex(request -> request.indices(INDEX_I)) //
.as(StepVerifier::create) //
.verifyError(ElasticsearchStatusException.class);
.verifyError(HttpClientErrorException.class);
}
@Test // DATAES-684

View File

@ -19,6 +19,7 @@ import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.elasticsearch.client.reactive.ReactiveMockClientTestsUtils.MockWebClientProvider.Receive.*;
import org.springframework.web.client.HttpClientErrorException;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@ -459,7 +460,7 @@ public class ReactiveElasticsearchClientUnitTests {
.verifyComplete();
}
@Test // DATAES-488
@Test // DATAES-488, DATAES-767
public void updateShouldEmitErrorWhenNotFound() {
hostProvider.when(HOST) //
@ -467,7 +468,7 @@ public class ReactiveElasticsearchClientUnitTests {
client.update(new UpdateRequest("twitter", "doc", "1").doc(Collections.singletonMap("user", "cstrobl")))
.as(StepVerifier::create) //
.expectError(ElasticsearchStatusException.class) //
.expectError(HttpClientErrorException.class) //
.verify();
}

View File

@ -24,9 +24,12 @@ import org.elasticsearch.rest.RestStatus;
import org.junit.jupiter.api.Test;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpClientErrorException;
/**
* @author Roman Puchkovskiy
* @author Peter-Josef Meisch
*/
class ElasticsearchExceptionTranslatorTests {
private final ElasticsearchExceptionTranslator translator = new ElasticsearchExceptionTranslator();
@ -56,4 +59,16 @@ class ElasticsearchExceptionTranslatorTests {
assertThat(translated.getMessage()).startsWith("Cannot index a document due to seq_no+primary_term conflict");
assertThat(translated.getCause()).isSameAs(ex);
}
@Test // DATAES-767
void shouldConvertHttpClientErrorExceptionWithSeqNoConflictToOptimisticLockingFailureException() {
HttpClientErrorException ex = new HttpClientErrorException(HttpStatus.BAD_REQUEST,
"Elasticsearch exception [type=version_conflict_engine_exception, reason=[WPUUsXEB6uuA6j8_A7AB]: version conflict, required seqNo [34], primary term [16]. current document has seqNo [35] and primary term [16]]");
DataAccessException translated = translator.translateExceptionIfPossible(ex);
assertThat(translated).isInstanceOf(OptimisticLockingFailureException.class);
assertThat(translated.getMessage()).startsWith("Cannot index a document due to seq_no+primary_term conflict");
assertThat(translated.getCause()).isSameAs(ex);
}
}

View File

@ -25,6 +25,7 @@ import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.springframework.web.client.HttpClientErrorException;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
@ -213,12 +214,12 @@ public class ReactiveElasticsearchTemplateTests {
}).isInstanceOf(IllegalArgumentException.class);
}
@Test // DATAES-519
public void getByIdShouldCompleteWhenIndexDoesNotExist() {
@Test // DATAES-519, DATAES-767
public void getByIdShouldErrorWhenIndexDoesNotExist() {
template.get("foo", SampleEntity.class, IndexCoordinates.of("no-such-index").withTypes("test-type")) //
.as(StepVerifier::create) //
.verifyComplete();
.expectError(HttpClientErrorException.class);
}
@Test // DATAES-504
@ -326,14 +327,14 @@ public class ReactiveElasticsearchTemplateTests {
.verifyComplete();
}
@Test // DATAES-519
@Test // DATAES-519, DATAES-767
public void searchShouldCompleteWhenIndexDoesNotExist() {
template
.search(new CriteriaQuery(Criteria.where("message").is("some message")), SampleEntity.class,
IndexCoordinates.of("no-such-index")) //
.as(StepVerifier::create) //
.verifyComplete();
.expectError(HttpClientErrorException.class);
}
@Test // DATAES-504
@ -430,7 +431,7 @@ public class ReactiveElasticsearchTemplateTests {
.verifyComplete();
}
@Test // DATAES-595
@Test // DATAES-595, DATAES-767
public void shouldThrowElasticsearchStatusExceptionWhenInvalidPreferenceForGivenCriteria() {
SampleEntity sampleEntity1 = randomEntity("test message");
@ -445,7 +446,7 @@ public class ReactiveElasticsearchTemplateTests {
template.search(queryWithInvalidPreference, SampleEntity.class) //
.as(StepVerifier::create) //
.expectError(UncategorizedElasticsearchException.class).verify();
.expectError(HttpClientErrorException.class).verify();
}
@Test // DATAES-504
@ -520,22 +521,20 @@ public class ReactiveElasticsearchTemplateTests {
}).verifyComplete();
}
@Test // DATAES-567
public void aggregateShouldReturnEmptyWhenIndexDoesNotExist() {
template
.aggregate(new CriteriaQuery(Criteria.where("message").is("some message")), SampleEntity.class,
@Test // DATAES-567, DATAES-767
public void aggregateShouldErrorWhenIndexDoesNotExist() {
template.aggregate(new CriteriaQuery(Criteria.where("message").is("some message")), SampleEntity.class,
IndexCoordinates.of("no-such-index")) //
.as(StepVerifier::create) //
.verifyComplete();
.expectError(HttpClientErrorException.class);
}
@Test // DATAES-519
@Test // DATAES-519, DATAES-767
public void countShouldReturnZeroWhenIndexDoesNotExist() {
template.count(SampleEntity.class) //
.as(StepVerifier::create) //
.expectNext(0L) //
.verifyComplete();
.expectError(HttpClientErrorException.class);
}
@Test // DATAES-504
@ -562,12 +561,12 @@ public class ReactiveElasticsearchTemplateTests {
.verifyComplete();
}
@Test // DATAES-519
public void deleteShouldCompleteWhenIndexDoesNotExist() {
@Test // DATAES-519, DATAES-767
public void deleteShouldErrorWhenIndexDoesNotExist() {
template.delete("does-not-exists", IndexCoordinates.of("no-such-index")) //
.as(StepVerifier::create)//
.verifyComplete();
.expectError(HttpClientErrorException.class);
}
@Test // DATAES-504
@ -926,7 +925,10 @@ public class ReactiveElasticsearchTemplateTests {
template.save(forEdit1).block();
forEdit2.setMessage("It'll be great");
template.save(forEdit2).as(StepVerifier::create).expectError(OptimisticLockingFailureException.class).verify();
template.save(forEdit2) //
.as(StepVerifier::create) //
.expectError(OptimisticLockingFailureException.class) //
.verify();
}
@Test // DATAES-799

View File

@ -72,6 +72,7 @@ import org.springframework.data.elasticsearch.repository.config.EnableReactiveEl
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.util.StringUtils;
import org.springframework.web.client.HttpClientErrorException;
/**
* @author Christoph Strobl
@ -129,9 +130,11 @@ public class SimpleReactiveElasticsearchRepositoryTests {
.verifyComplete();
}
@Test // DATAES-519
public void findByIdShouldCompleteIfIndexDoesNotExist() {
repository.findById("id-two").as(StepVerifier::create).verifyComplete();
@Test // DATAES-519, DATAES-767
public void findByIdShouldErrorIfIndexDoesNotExist() {
repository.findById("id-two") //
.as(StepVerifier::create) //
.expectError(HttpClientErrorException.class);
}
@Test // DATAES-519
@ -264,9 +267,11 @@ public class SimpleReactiveElasticsearchRepositoryTests {
.verifyComplete();
}
@Test // DATAES-519
public void countShouldReturnZeroWhenIndexDoesNotExist() {
repository.count().as(StepVerifier::create).expectNext(0L).verifyComplete();
@Test // DATAES-519, DATAES-767
public void countShouldErrorWhenIndexDoesNotExist() {
repository.count() //
.as(StepVerifier::create) //
.expectError(HttpClientErrorException.class);
}
@Test // DATAES-519
@ -352,9 +357,12 @@ public class SimpleReactiveElasticsearchRepositoryTests {
repository.deleteById("does-not-exist").as(StepVerifier::create).verifyComplete();
}
@Test // DATAES-519
public void deleteByIdShouldCompleteWhenIndexDoesNotExist() {
repository.deleteById("does-not-exist").as(StepVerifier::create).verifyComplete();
@Test // DATAES-519, DATAES-767
public void deleteByIdShouldErrorWhenIndexDoesNotExist() {
repository.deleteById("does-not-exist") //
.as(StepVerifier::create) //
.verifyError(HttpClientErrorException.class);
;
}
@Test // DATAES-519