Skip to content

Commit

Permalink
Cleanup code style on PR
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Nied <[email protected]>
  • Loading branch information
peternied committed Aug 7, 2024
1 parent d8f78a6 commit 5db78fa
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 84 deletions.
86 changes: 39 additions & 47 deletions RFS/src/main/java/com/rfs/common/InvalidResponse.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package com.rfs.common;

import java.util.Set;
import java.util.ArrayList;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.JsonNode;
import com.rfs.common.http.HttpResponse;

import com.rfs.common.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand All @@ -32,37 +32,32 @@ public Set<String> getIllegalArguments() {
var interimResults = new ArrayList<Map.Entry<String, String>>();
var bodyNode = OpenSearchClient.objectMapper.readTree(response.body);

var errorBody = Optional.ofNullable(bodyNode)
.map(node -> node.get("error"));
var errorBody = Optional.ofNullable(bodyNode).map(node -> node.get("error"));

// Check high level cause
errorBody
.map(InvalidResponse::getUnknownSetting)
.ifPresent(interimResults::add);
errorBody.map(InvalidResponse::getUnknownSetting).ifPresent(interimResults::add);

// Check root cause errors
errorBody
.map(node -> node.get("root_cause"))
.ifPresent(nodes -> {
nodes.forEach(node -> {
Optional.of(node)
.map(InvalidResponse::getUnknownSetting)
.ifPresent(interimResults::add);
});
});

errorBody.map(node -> node.get("root_cause")).ifPresent(nodes -> {
nodes.forEach(
node -> {
Optional.of(node).map(InvalidResponse::getUnknownSetting).ifPresent(interimResults::add);
}
);
});

// Check all suppressed errors
errorBody
.map(node -> node.get("suppressed"))
.ifPresent(nodes -> {
nodes.forEach(node -> {
Optional.of(node)
.map(InvalidResponse::getUnknownSetting)
.ifPresent(interimResults::add);
});
});
errorBody.map(node -> node.get("suppressed")).ifPresent(nodes -> {
nodes.forEach(
node -> {
Optional.of(node).map(InvalidResponse::getUnknownSetting).ifPresent(interimResults::add);
}
);
});

var onlyExpectedErrors = interimResults.stream().map(Entry::getKey).allMatch("illegal_argument_exception"::equals);
var onlyExpectedErrors = interimResults.stream()
.map(Entry::getKey)
.allMatch("illegal_argument_exception"::equals);
if (!onlyExpectedErrors) {
log.warn("Expecting only invalid argument errors, found additional error types " + interimResults);
return Set.of();
Expand All @@ -76,23 +71,20 @@ public Set<String> getIllegalArguments() {
}

private static Map.Entry<String, String> getUnknownSetting(JsonNode json) {
return Optional.ofNullable(json)
.map(node -> {
var typeNode = node.get("type");
var reasonNode = node.get("reason");
if (typeNode == null || reasonNode == null) {
return null;
}
return Map.entry(typeNode, reasonNode);
})
.map(entry -> {
var matcher = unknownSetting.matcher(entry.getValue().asText());
if (!matcher.matches()) {
return null;
}
return Optional.ofNullable(json).map(node -> {
var typeNode = node.get("type");
var reasonNode = node.get("reason");
if (typeNode == null || reasonNode == null) {
return null;
}
return Map.entry(typeNode, reasonNode);
}).map(entry -> {
var matcher = unknownSetting.matcher(entry.getValue().asText());
if (!matcher.matches()) {
return null;
}

return Map.entry(entry.getKey().asText(), matcher.group(1));
})
.orElse(null);
return Map.entry(entry.getKey().asText(), matcher.group(1));
}).orElse(null);
}
}
}
11 changes: 8 additions & 3 deletions RFS/src/main/java/com/rfs/common/OpenSearchClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,13 @@ private Optional<ObjectNode> createObjectIdempotent(
+ " should have been thrown.");
boolean objectDoesNotExist = getResponse.statusCode == HttpURLConnection.HTTP_NOT_FOUND;
if (objectDoesNotExist) {
logger.debug("Creating object " + objectPath + "\r\n" + settings.toPrettyString());
client.putAsync(objectPath, settings.toString(), context.createCheckRequestContext()).flatMap(resp -> {
if (resp.statusCode == HttpURLConnection.HTTP_OK) {
return Mono.just(resp);
} else if (resp.statusCode == HttpURLConnection.HTTP_BAD_REQUEST) {
return Mono.error(new InvalidResponse("Create object failed for " + objectPath + "\r\n" + resp.body, resp));
return Mono.error(
new InvalidResponse("Create object failed for " + objectPath + "\r\n" + resp.body, resp)
);
} else {
String errorMessage = ("Could not create object: "
+ objectPath
Expand All @@ -133,7 +134,11 @@ private Optional<ObjectNode> createObjectIdempotent(
}
})
.doOnError(e -> logger.error(e.getMessage()))
.retryWhen(Retry.backoff(3, Duration.ofSeconds(1)).maxBackoff(Duration.ofSeconds(10)).filter(OperationFailed.class::isInstance))
.retryWhen(
Retry.backoff(3, Duration.ofSeconds(1))
.maxBackoff(Duration.ofSeconds(10))
.filter(OperationFailed.class::isInstance)
)
.block();

return Optional.of(settings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.Optional;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

Expand All @@ -11,7 +10,6 @@
import com.rfs.common.InvalidResponse;
import com.rfs.common.OpenSearchClient;
import com.rfs.models.IndexMetadata;

import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand Down Expand Up @@ -61,7 +59,7 @@ public Optional<ObjectNode> create(
log.warn("Expecting all retryable errors to start with 'index.', instead saw " + illegalArgument);
return Optional.empty();
}

var shortenedIllegalArgument = illegalArgument.replaceFirst("index.", "");
removeFieldsByPath(settings, shortenedIllegalArgument);
}
Expand Down
91 changes: 60 additions & 31 deletions RFS/src/test/java/com/rfs/common/InvalidResponseTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.rfs.common;

import java.util.Map;

import org.junit.jupiter.api.Test;

import com.rfs.common.http.HttpResponse;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.MatcherAssert.assertThat;

import java.util.Map;
import com.rfs.common.http.HttpResponse;

class InvalidResponseTest {

Expand All @@ -29,9 +31,12 @@ void testGetIllegalArguments() {
var errorBody = "{\r\n" + //
" \"error\": {\r\n" + //
" \"type\": \"illegal_argument_exception\",\r\n" + //
" \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + //
" },\r\n" + //
" \"status\": 400\r\n" + //
" \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n"
+ //
" },\r\n"
+ //
" \"status\": 400\r\n"
+ //
"}";
var response = new HttpResponse(200, "statusText", Map.of(), errorBody);
var iar = new InvalidResponse("ignored", response);
Expand All @@ -48,13 +53,20 @@ void testGetIllegalArguments_inRootCause() {
" \"root_cause\": [\r\n" + //
" {\r\n" + //
" \"type\": \"illegal_argument_exception\",\r\n" + //
" \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + //
" }\r\n" + //
" ],\r\n" + //
" \"type\": \"illegal_argument_exception\",\r\n" + //
" \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + //
" },\r\n" + //
" \"status\": 400\r\n" + //
" \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n"
+ //
" }\r\n"
+ //
" ],\r\n"
+ //
" \"type\": \"illegal_argument_exception\",\r\n"
+ //
" \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n"
+ //
" },\r\n"
+ //
" \"status\": 400\r\n"
+ //
"}";
var response = new HttpResponse(200, "statusText", Map.of(), errorBody);
var iar = new InvalidResponse("ignored", response);
Expand All @@ -71,23 +83,40 @@ void testGetIllegalArguments_inRootCauseAndSuppressed() {
" \"root_cause\": [\r\n" + //
" {\r\n" + //
" \"type\": \"illegal_argument_exception\",\r\n" + //
" \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + //
" }\r\n" + //
" ],\r\n" + //
" \"type\": \"illegal_argument_exception\",\r\n" + //
" \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\",\r\n" + //
" \"suppressed\": [\r\n" + //
" {\r\n" + //
" \"type\": \"illegal_argument_exception\",\r\n" + //
" \"reason\": \"unknown setting [index.lifecycle.name] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + //
" },\r\n" + //
" {\r\n" + //
" \"type\": \"illegal_argument_exception\",\r\n" + //
" \"reason\": \"unknown setting [index.provided_name] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n" + //
" }\r\n" + //
" ]\r\n" + //
" },\r\n" + //
" \"status\": 400\r\n" + //
" \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n"
+ //
" }\r\n"
+ //
" ],\r\n"
+ //
" \"type\": \"illegal_argument_exception\",\r\n"
+ //
" \"reason\": \"unknown setting [index.creation_date] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\",\r\n"
+ //
" \"suppressed\": [\r\n"
+ //
" {\r\n"
+ //
" \"type\": \"illegal_argument_exception\",\r\n"
+ //
" \"reason\": \"unknown setting [index.lifecycle.name] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n"
+ //
" },\r\n"
+ //
" {\r\n"
+ //
" \"type\": \"illegal_argument_exception\",\r\n"
+ //
" \"reason\": \"unknown setting [index.provided_name] please check that any required plugins are installed, or check the breaking changes documentation for removed settings\"\r\n"
+ //
" }\r\n"
+ //
" ]\r\n"
+ //
" },\r\n"
+ //
" \"status\": 400\r\n"
+ //
"}";
var response = new HttpResponse(200, "statusText", Map.of(), errorBody);
var iar = new InvalidResponse("ignored", response);
Expand Down

0 comments on commit 5db78fa

Please sign in to comment.