Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Add subdirectory-aware store module with recovery support ([#19132](https://github.com/opensearch-project/OpenSearch/pull/19132))

### Changed
- Refactor `if-else` chains to use `Java 17 pattern matching switch expressions`(([#18965](https://github.com/opensearch-project/OpenSearch/pull/18965))
- Add CompletionStage variants to methods in the Client Interface and default to ActionListener impl ([#18998](https://github.com/opensearch-project/OpenSearch/pull/18998))
- IllegalArgumentException when scroll ID references a node not found in Cluster ([#19031](https://github.com/opensearch-project/OpenSearch/pull/19031))
- Adding ScriptedAvg class to painless spi to allowlist usage from plugins ([#19006](https://github.com/opensearch-project/OpenSearch/pull/19006))
Expand Down
72 changes: 44 additions & 28 deletions libs/common/src/main/java/org/opensearch/common/Numbers.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,44 +83,60 @@ public static boolean isValidDouble(double value) {
* stored value cannot be converted to a long that stores the exact same
* value. */
public static long toLongExact(Number n) {
if (n instanceof Byte || n instanceof Short || n instanceof Integer || n instanceof Long) {
return n.longValue();
} else if (n instanceof Float || n instanceof Double) {
double d = n.doubleValue();
if (d != Math.round(d)) {
throw new IllegalArgumentException(n + " is not an integer value");
return switch (n) {
case Byte b -> b.longValue();
case Short s -> s.longValue();
case Integer i -> i.longValue();
case Long l -> l.longValue();
case Float f -> {
double d = f.doubleValue();
if (d != Math.round(d)) {
throw new IllegalArgumentException(f + " is not an integer value");
}
yield f.longValue();
}
return n.longValue();
} else if (n instanceof BigDecimal) {
return ((BigDecimal) n).toBigIntegerExact().longValueExact();
} else if (n instanceof BigInteger) {
return ((BigInteger) n).longValueExact();
} else {
throw new IllegalArgumentException(
case Double d -> {
if (d != Math.round(d)) {
throw new IllegalArgumentException(d + " is not an integer value");
}
yield d.longValue();
}
case BigDecimal bd -> bd.toBigIntegerExact().longValueExact();
case BigInteger bi -> bi.longValueExact();
default -> throw new IllegalArgumentException(
"Cannot check whether [" + n + "] of class [" + n.getClass().getName() + "] is actually a long"
);
}
};
}

/** Return the {@link BigInteger} that {@code n} stores, or throws an exception if the
* stored value cannot be converted to a {@link BigInteger} that stores the exact same
* value. */
public static BigInteger toBigIntegerExact(Number n) {
if (n instanceof Byte || n instanceof Short || n instanceof Integer || n instanceof Long) {
return BigInteger.valueOf(n.longValue());
} else if (n instanceof Float || n instanceof Double) {
double d = n.doubleValue();
if (d != Math.round(d)) {
throw new IllegalArgumentException(n + " is not an integer value");
return switch (n) {
case Byte b -> BigInteger.valueOf(b.longValue());
case Short s -> BigInteger.valueOf(s.longValue());
case Integer i -> BigInteger.valueOf(i.longValue());
case Long l -> BigInteger.valueOf(l.longValue());
case Float f -> {
double d = f.doubleValue();
if (d != Math.round(d)) {
throw new IllegalArgumentException(f + " is not an integer value");
}
yield BigInteger.valueOf(f.longValue());
}
return BigInteger.valueOf(n.longValue());
} else if (n instanceof BigDecimal) {
return ((BigDecimal) n).toBigIntegerExact();
} else if (n instanceof BigInteger) {
return ((BigInteger) n);
} else {
throw new IllegalArgumentException("Cannot convert [" + n + "] of class [" + n.getClass().getName() + "] to a BigInteger");
}
case Double d -> {
if (d != Math.round(d)) {
throw new IllegalArgumentException(d + " is not an integer value");
}
yield BigInteger.valueOf(d.longValue());
}
case BigDecimal bd -> bd.toBigIntegerExact();
case BigInteger bi -> bi;
default -> throw new IllegalArgumentException(
"Cannot convert [" + n + "] of class [" + n.getClass().getName() + "] to a BigInteger"
);
};
}

/** Return the unsigned long (as {@link BigInteger}) that {@code n} stores, or throws an exception if the
Expand Down
63 changes: 25 additions & 38 deletions libs/core/src/main/java/org/opensearch/ExceptionsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,53 +112,40 @@ private ErrorMessages() {
private ExceptionsHelper() {}

public static RuntimeException convertToRuntime(Exception e) {
if (e instanceof RuntimeException) {
return (RuntimeException) e;
}
return new OpenSearchException(e);
return switch (e) {
case RuntimeException re -> re;
default -> new OpenSearchException(e);
};
}

public static OpenSearchException convertToOpenSearchException(Exception e) {
if (e instanceof OpenSearchException) {
return (OpenSearchException) e;
}
return new OpenSearchException(e);
return switch (e) {
case OpenSearchException oe -> oe;
default -> new OpenSearchException(e);
};
}

public static RestStatus status(Throwable t) {
if (t != null) {
if (t instanceof OpenSearchException) {
return ((OpenSearchException) t).status();
} else if (t instanceof IllegalArgumentException) {
return RestStatus.BAD_REQUEST;
} else if (t instanceof InputCoercionException) {
return RestStatus.BAD_REQUEST;
} else if (t instanceof JsonParseException) {
return RestStatus.BAD_REQUEST;
} else if (t instanceof OpenSearchRejectedExecutionException) {
return RestStatus.TOO_MANY_REQUESTS;
} else if (t instanceof NotXContentException) {
return RestStatus.BAD_REQUEST;
}
}
return RestStatus.INTERNAL_SERVER_ERROR;
return switch (t) {
case OpenSearchException ose -> ose.status();
case IllegalArgumentException ignored -> RestStatus.BAD_REQUEST;
case InputCoercionException ignored -> RestStatus.BAD_REQUEST;
case JsonParseException ignored -> RestStatus.BAD_REQUEST;
case NotXContentException ignored -> RestStatus.BAD_REQUEST;
case OpenSearchRejectedExecutionException ignored -> RestStatus.TOO_MANY_REQUESTS;
case null, default -> RestStatus.INTERNAL_SERVER_ERROR;
};
}

public static String summaryMessage(Throwable t) {
if (t != null) {
if (t instanceof OpenSearchException) {
return getExceptionSimpleClassName(t) + "[" + t.getMessage() + "]";
} else if (t instanceof IllegalArgumentException) {
return ErrorMessages.INVALID_ARGUMENT;
} else if (t instanceof InputCoercionException) {
return ErrorMessages.JSON_COERCION_FAILED;
} else if (t instanceof JsonParseException) {
return ErrorMessages.JSON_PARSE_FAILED;
} else if (t instanceof OpenSearchRejectedExecutionException) {
return ErrorMessages.TOO_MANY_REQUESTS;
}
}
return ErrorMessages.INTERNAL_FAILURE;
return switch (t) {
case OpenSearchException ose -> getExceptionSimpleClassName(t) + "[" + ose.getMessage() + "]";
case IllegalArgumentException ignored -> ErrorMessages.INVALID_ARGUMENT;
case InputCoercionException ignored -> ErrorMessages.JSON_COERCION_FAILED;
case JsonParseException ignored -> ErrorMessages.JSON_PARSE_FAILED;
case OpenSearchRejectedExecutionException ignored -> ErrorMessages.TOO_MANY_REQUESTS;
case null, default -> "Internal failure";
};
}

public static Throwable unwrapCause(Throwable t) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ public interface BytesReference extends Comparable<BytesReference>, ToXContentFr
static BytesReference bytes(XContentBuilder xContentBuilder) {
xContentBuilder.close();
OutputStream stream = xContentBuilder.getOutputStream();
if (stream instanceof ByteArrayOutputStream) {
return new BytesArray(((ByteArrayOutputStream) stream).toByteArray());
} else {
return ((BytesStream) stream).bytes();
}
return switch (stream) {
case ByteArrayOutputStream baos -> new BytesArray(baos.toByteArray());
default -> ((BytesStream) stream).bytes();
};
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,16 @@ private static void deeplyAppendParameter(StringBuilder sbuf, Object o, Set<Obje
} else {
// check for primitive array types because they
// unfortunately cannot be cast to Object[]
if (o instanceof boolean[]) {
booleanArrayAppend(sbuf, (boolean[]) o);
} else if (o instanceof byte[]) {
byteArrayAppend(sbuf, (byte[]) o);
} else if (o instanceof char[]) {
charArrayAppend(sbuf, (char[]) o);
} else if (o instanceof short[]) {
shortArrayAppend(sbuf, (short[]) o);
} else if (o instanceof int[]) {
intArrayAppend(sbuf, (int[]) o);
} else if (o instanceof long[]) {
longArrayAppend(sbuf, (long[]) o);
} else if (o instanceof float[]) {
floatArrayAppend(sbuf, (float[]) o);
} else if (o instanceof double[]) {
doubleArrayAppend(sbuf, (double[]) o);
} else {
objectArrayAppend(sbuf, (Object[]) o, seen);
switch (o) {
case boolean[] boolArr -> booleanArrayAppend(sbuf, boolArr);
case byte[] byteArr -> byteArrayAppend(sbuf, byteArr);
case char[] charArr -> charArrayAppend(sbuf, charArr);
case short[] shortArr -> shortArrayAppend(sbuf, shortArr);
case int[] intArr -> intArrayAppend(sbuf, intArr);
case long[] longArr -> longArrayAppend(sbuf, longArr);
case float[] floatArr -> floatArrayAppend(sbuf, floatArr);
case double[] doubleArr -> doubleArrayAppend(sbuf, doubleArr);
default -> objectArrayAppend(sbuf, (Object[]) o, seen);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,9 @@ private static void startSelectors(Iterable<NioSelector> selectors, ThreadFactor
Thread.currentThread().interrupt();
throw new IllegalStateException("Interrupted while waiting for selector to start.", e);
} catch (ExecutionException e) {
if (e.getCause() instanceof RuntimeException) {
throw (RuntimeException) e.getCause();
} else {
throw new RuntimeException("Exception during selector start.", e);
switch (e.getCause()) {
case RuntimeException re -> throw re;
default -> throw new RuntimeException("Exception during selector start.", e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,10 @@ public boolean connect() throws IOException {
return true;
} else if (connectContext.isCompletedExceptionally()) {
Exception exception = connectException;
if (exception == null) {
throw new AssertionError("Should have received connection exception");
} else if (exception instanceof IOException) {
throw (IOException) exception;
} else {
throw (RuntimeException) exception;
switch (exception) {
case null -> throw new AssertionError("Should have received connection exception");
case IOException ioException -> throw ioException;
default -> throw (RuntimeException) exception;
}
}

Expand Down
50 changes: 28 additions & 22 deletions server/src/main/java/org/opensearch/action/DocWriteRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -263,33 +263,39 @@ static ActionRequestValidationException validateDocIdLength(String id, ActionReq

/** write a document write (index/delete/update) request*/
static void writeDocumentRequest(StreamOutput out, DocWriteRequest<?> request) throws IOException {
if (request instanceof IndexRequest) {
out.writeByte((byte) 0);
((IndexRequest) request).writeTo(out);
} else if (request instanceof DeleteRequest) {
out.writeByte((byte) 1);
((DeleteRequest) request).writeTo(out);
} else if (request instanceof UpdateRequest) {
out.writeByte((byte) 2);
((UpdateRequest) request).writeTo(out);
} else {
throw new IllegalStateException("invalid request [" + request.getClass().getSimpleName() + " ]");
switch (request) {
case IndexRequest indexRequest -> {
out.writeByte((byte) 0);
indexRequest.writeTo(out);
}
case DeleteRequest deleteRequest -> {
out.writeByte((byte) 1);
deleteRequest.writeTo(out);
}
case UpdateRequest updateRequest -> {
out.writeByte((byte) 2);
updateRequest.writeTo(out);
}
default -> throw new IllegalStateException("invalid request [" + request.getClass().getSimpleName() + " ]");
}
}

/** write a document write (index/delete/update) request without shard id*/
static void writeDocumentRequestThin(StreamOutput out, DocWriteRequest<?> request) throws IOException {
if (request instanceof IndexRequest) {
out.writeByte((byte) 0);
((IndexRequest) request).writeThin(out);
} else if (request instanceof DeleteRequest) {
out.writeByte((byte) 1);
((DeleteRequest) request).writeThin(out);
} else if (request instanceof UpdateRequest) {
out.writeByte((byte) 2);
((UpdateRequest) request).writeThin(out);
} else {
throw new IllegalStateException("invalid request [" + request.getClass().getSimpleName() + " ]");
switch (request) {
case IndexRequest indexRequest -> {
out.writeByte((byte) 0);
indexRequest.writeThin(out);
}
case DeleteRequest deleteRequest -> {
out.writeByte((byte) 1);
deleteRequest.writeThin(out);
}
case UpdateRequest updateRequest -> {
out.writeByte((byte) 2);
updateRequest.writeThin(out);
}
default -> throw new IllegalStateException("invalid request [" + request.getClass().getSimpleName() + " ]");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,11 @@ public void writeThin(StreamOutput out) throws IOException {
}

private void writeResponseType(StreamOutput out) throws IOException {
if (response instanceof IndexResponse) {
out.writeByte((byte) 0);
} else if (response instanceof DeleteResponse) {
out.writeByte((byte) 1);
} else if (response instanceof UpdateResponse) {
out.writeByte((byte) 3); // make 3 instead of 2, because 2 is already in use for 'no responses'
} else {
throw new IllegalStateException("Unexpected response type found [" + response.getClass() + "]");
switch (response) {
case IndexResponse ignored -> out.writeByte((byte) 0);
case DeleteResponse ignored -> out.writeByte((byte) 1);
case UpdateResponse ignored -> out.writeByte((byte) 3); // make 3 instead of 2, because 2 is already in use for 'no responses'
default -> throw new IllegalStateException("Unexpected response type found [" + response.getClass() + "]");
}
}
}
Loading
Loading