forked from opensearch-project/data-prepper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Centralize exception handling and fix behavior for RequestTimeoutExce…
…ption (opensearch-project#3063) * Centralize exception handling and fix behavior for RequestTimeoutException Signed-off-by: Chase Engelbrecht <engechas@amazon.com> * Fix existing tests Signed-off-by: Chase Engelbrecht <engechas@amazon.com> * Add unit tests for exception handlers Signed-off-by: Chase Engelbrecht <engechas@amazon.com> * Add copyright headers Signed-off-by: Chase Engelbrecht <engechas@amazon.com> * Add better default messages Signed-off-by: Chase Engelbrecht <engechas@amazon.com> --------- Signed-off-by: Chase Engelbrecht <engechas@amazon.com>
- Loading branch information
Showing
23 changed files
with
615 additions
and
361 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
.../armeria-common/src/main/java/org/opensearch/dataprepper/GrpcRequestExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper; | ||
|
||
import com.linecorp.armeria.common.RequestContext; | ||
import com.linecorp.armeria.common.annotation.Nullable; | ||
import com.linecorp.armeria.common.grpc.GrpcStatusFunction; | ||
import com.linecorp.armeria.server.RequestTimeoutException; | ||
import io.grpc.Metadata; | ||
import io.grpc.Status; | ||
import io.micrometer.core.instrument.Counter; | ||
import org.opensearch.dataprepper.exceptions.BadRequestException; | ||
import org.opensearch.dataprepper.exceptions.BufferWriteException; | ||
import org.opensearch.dataprepper.exceptions.RequestCancelledException; | ||
import org.opensearch.dataprepper.metrics.PluginMetrics; | ||
import org.opensearch.dataprepper.model.buffer.SizeOverflowException; | ||
|
||
import java.util.concurrent.TimeoutException; | ||
|
||
public class GrpcRequestExceptionHandler implements GrpcStatusFunction { | ||
static final String ARMERIA_REQUEST_TIMEOUT_MESSAGE = "Timeout waiting for request to be served. This is usually due to the buffer being full."; | ||
|
||
public static final String REQUEST_TIMEOUTS = "requestTimeouts"; | ||
public static final String BAD_REQUESTS = "badRequests"; | ||
public static final String REQUESTS_TOO_LARGE = "requestsTooLarge"; | ||
public static final String INTERNAL_SERVER_ERROR = "internalServerError"; | ||
|
||
private final Counter requestTimeoutsCounter; | ||
private final Counter badRequestsCounter; | ||
private final Counter requestsTooLargeCounter; | ||
private final Counter internalServerErrorCounter; | ||
|
||
public GrpcRequestExceptionHandler(final PluginMetrics pluginMetrics) { | ||
requestTimeoutsCounter = pluginMetrics.counter(REQUEST_TIMEOUTS); | ||
badRequestsCounter = pluginMetrics.counter(BAD_REQUESTS); | ||
requestsTooLargeCounter = pluginMetrics.counter(REQUESTS_TOO_LARGE); | ||
internalServerErrorCounter = pluginMetrics.counter(INTERNAL_SERVER_ERROR); | ||
} | ||
|
||
@Override | ||
public @Nullable Status apply(final RequestContext context, final Throwable exception, final Metadata metadata) { | ||
final Throwable exceptionCause = exception instanceof BufferWriteException ? exception.getCause() : exception; | ||
|
||
return handleExceptions(exceptionCause); | ||
} | ||
|
||
private Status handleExceptions(final Throwable e) { | ||
if (e instanceof RequestTimeoutException || e instanceof TimeoutException) { | ||
requestTimeoutsCounter.increment(); | ||
return createStatus(e, Status.RESOURCE_EXHAUSTED); | ||
} else if (e instanceof SizeOverflowException) { | ||
requestsTooLargeCounter.increment(); | ||
return createStatus(e, Status.RESOURCE_EXHAUSTED); | ||
} else if (e instanceof BadRequestException) { | ||
badRequestsCounter.increment(); | ||
return createStatus(e, Status.INVALID_ARGUMENT); | ||
} else if (e instanceof RequestCancelledException) { | ||
requestTimeoutsCounter.increment(); | ||
return createStatus(e, Status.CANCELLED); | ||
} | ||
|
||
internalServerErrorCounter.increment(); | ||
return createStatus(e, Status.INTERNAL); | ||
} | ||
|
||
private Status createStatus(final Throwable e, final Status status) { | ||
final String message; | ||
if (e instanceof RequestTimeoutException) { | ||
message = ARMERIA_REQUEST_TIMEOUT_MESSAGE; | ||
} else { | ||
message = e.getMessage() == null ? status.getCode().name() : e.getMessage(); | ||
} | ||
|
||
return status.withDescription(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
...meria-common/src/main/java/org/opensearch/dataprepper/exceptions/BadRequestException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.exceptions; | ||
|
||
public class BadRequestException extends RuntimeException { | ||
public BadRequestException(final String message, final Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...eria-common/src/main/java/org/opensearch/dataprepper/exceptions/BufferWriteException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.exceptions; | ||
|
||
public class BufferWriteException extends RuntimeException { | ||
public BufferWriteException(final String message, final Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
...common/src/main/java/org/opensearch/dataprepper/exceptions/RequestCancelledException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.exceptions; | ||
|
||
public class RequestCancelledException extends RuntimeException { | ||
public RequestCancelledException(final String message) { | ||
super(message); | ||
} | ||
} |
165 changes: 165 additions & 0 deletions
165
...eria-common/src/test/java/org/opensearch/dataprepper/GrpcRequestExceptionHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper; | ||
|
||
import com.linecorp.armeria.common.RequestContext; | ||
import com.linecorp.armeria.server.RequestTimeoutException; | ||
import io.grpc.Metadata; | ||
import io.grpc.Status; | ||
import io.micrometer.core.instrument.Counter; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.opensearch.dataprepper.exceptions.BadRequestException; | ||
import org.opensearch.dataprepper.exceptions.BufferWriteException; | ||
import org.opensearch.dataprepper.exceptions.RequestCancelledException; | ||
import org.opensearch.dataprepper.metrics.PluginMetrics; | ||
import org.opensearch.dataprepper.model.buffer.SizeOverflowException; | ||
|
||
import java.io.IOException; | ||
import java.util.UUID; | ||
import java.util.concurrent.TimeoutException; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
import static org.opensearch.dataprepper.GrpcRequestExceptionHandler.ARMERIA_REQUEST_TIMEOUT_MESSAGE; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
public class GrpcRequestExceptionHandlerTest { | ||
@Mock | ||
private PluginMetrics pluginMetrics; | ||
|
||
@Mock | ||
private Counter requestTimeoutsCounter; | ||
|
||
@Mock | ||
private Counter badRequestsCounter; | ||
|
||
@Mock | ||
private Counter requestsTooLargeCounter; | ||
|
||
@Mock | ||
private Counter internalServerErrorCounter; | ||
|
||
@Mock | ||
private RequestContext requestContext; | ||
|
||
@Mock | ||
private Metadata metadata; | ||
|
||
private GrpcRequestExceptionHandler grpcRequestExceptionHandler; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
when(pluginMetrics.counter(HttpRequestExceptionHandler.REQUEST_TIMEOUTS)).thenReturn(requestTimeoutsCounter); | ||
when(pluginMetrics.counter(HttpRequestExceptionHandler.BAD_REQUESTS)).thenReturn(badRequestsCounter); | ||
when(pluginMetrics.counter(HttpRequestExceptionHandler.REQUESTS_TOO_LARGE)).thenReturn(requestsTooLargeCounter); | ||
when(pluginMetrics.counter(HttpRequestExceptionHandler.INTERNAL_SERVER_ERROR)).thenReturn(internalServerErrorCounter); | ||
|
||
grpcRequestExceptionHandler = new GrpcRequestExceptionHandler(pluginMetrics); | ||
} | ||
|
||
@Test | ||
public void testHandleBadRequestException() { | ||
final BadRequestException badRequestExceptionNoMessage = new BadRequestException(null, new IOException()); | ||
final String exceptionMessage = UUID.randomUUID().toString(); | ||
final BadRequestException badRequestExceptionWithMessage = new BadRequestException(exceptionMessage, new IOException()); | ||
|
||
final Status noMessageStatus = grpcRequestExceptionHandler.apply(requestContext, badRequestExceptionNoMessage, metadata); | ||
assertThat(noMessageStatus.getCode(), equalTo(Status.Code.INVALID_ARGUMENT)); | ||
assertThat(noMessageStatus.getDescription(), equalTo(Status.Code.INVALID_ARGUMENT.name())); | ||
|
||
final Status messageStatus = grpcRequestExceptionHandler.apply(requestContext, badRequestExceptionWithMessage, metadata); | ||
assertThat(messageStatus.getCode(), equalTo(Status.Code.INVALID_ARGUMENT)); | ||
assertThat(messageStatus.getDescription(), equalTo(exceptionMessage)); | ||
|
||
verify(badRequestsCounter, times(2)).increment(); | ||
} | ||
|
||
@Test | ||
public void testHandleTimeoutException() { | ||
final BufferWriteException timeoutExceptionNoMessage = new BufferWriteException(null, new TimeoutException()); | ||
final String exceptionMessage = UUID.randomUUID().toString(); | ||
final BufferWriteException timeoutExceptionWithMessage = new BufferWriteException(exceptionMessage, new TimeoutException(exceptionMessage)); | ||
|
||
final Status noMessageStatus = grpcRequestExceptionHandler.apply(requestContext, timeoutExceptionNoMessage, metadata); | ||
assertThat(noMessageStatus.getCode(), equalTo(Status.Code.RESOURCE_EXHAUSTED)); | ||
assertThat(noMessageStatus.getDescription(), equalTo(Status.Code.RESOURCE_EXHAUSTED.name())); | ||
|
||
final Status messageStatus = grpcRequestExceptionHandler.apply(requestContext, timeoutExceptionWithMessage, metadata); | ||
assertThat(messageStatus.getCode(), equalTo(Status.Code.RESOURCE_EXHAUSTED)); | ||
assertThat(messageStatus.getDescription(), equalTo(exceptionMessage)); | ||
|
||
verify(requestTimeoutsCounter, times(2)).increment(); | ||
} | ||
|
||
@Test | ||
public void testHandleArmeriaTimeoutException() { | ||
final RequestTimeoutException timeoutExceptionNoMessage = RequestTimeoutException.get(); | ||
|
||
final Status noMessageStatus = grpcRequestExceptionHandler.apply(requestContext, timeoutExceptionNoMessage, metadata); | ||
assertThat(noMessageStatus.getCode(), equalTo(Status.Code.RESOURCE_EXHAUSTED)); | ||
assertThat(noMessageStatus.getDescription(), equalTo(ARMERIA_REQUEST_TIMEOUT_MESSAGE)); | ||
|
||
verify(requestTimeoutsCounter, times(1)).increment(); | ||
} | ||
|
||
@Test | ||
public void testHandleSizeOverflowException() { | ||
final BufferWriteException sizeOverflowExceptionNoMessage = new BufferWriteException(null, new SizeOverflowException(null)); | ||
final String exceptionMessage = UUID.randomUUID().toString(); | ||
final BufferWriteException sizeOverflowExceptionWithMessage = new BufferWriteException(exceptionMessage, new SizeOverflowException(exceptionMessage)); | ||
|
||
final Status noMessageStatus = grpcRequestExceptionHandler.apply(requestContext, sizeOverflowExceptionNoMessage, metadata); | ||
assertThat(noMessageStatus.getCode(), equalTo(Status.Code.RESOURCE_EXHAUSTED)); | ||
assertThat(noMessageStatus.getDescription(), equalTo(Status.Code.RESOURCE_EXHAUSTED.name())); | ||
|
||
final Status messageStatus = grpcRequestExceptionHandler.apply(requestContext, sizeOverflowExceptionWithMessage, metadata); | ||
assertThat(messageStatus.getCode(), equalTo(Status.Code.RESOURCE_EXHAUSTED)); | ||
assertThat(messageStatus.getDescription(), equalTo(exceptionMessage)); | ||
|
||
verify(requestsTooLargeCounter, times(2)).increment(); | ||
} | ||
|
||
@Test | ||
public void testHandleRequestCancelledException() { | ||
final RequestCancelledException requestCancelledExceptionNoMessage = new RequestCancelledException(null); | ||
final String exceptionMessage = UUID.randomUUID().toString(); | ||
final RequestCancelledException requestCancelledExceptionWithMessage = new RequestCancelledException(exceptionMessage); | ||
|
||
final Status noMessageStatus = grpcRequestExceptionHandler.apply(requestContext, requestCancelledExceptionNoMessage, metadata); | ||
assertThat(noMessageStatus.getCode(), equalTo(Status.Code.CANCELLED)); | ||
assertThat(noMessageStatus.getDescription(), equalTo(Status.Code.CANCELLED.name())); | ||
|
||
final Status messageStatus = grpcRequestExceptionHandler.apply(requestContext, requestCancelledExceptionWithMessage, metadata); | ||
assertThat(messageStatus.getCode(), equalTo(Status.Code.CANCELLED)); | ||
assertThat(messageStatus.getDescription(), equalTo(exceptionMessage)); | ||
|
||
verify(requestTimeoutsCounter, times(2)).increment(); | ||
} | ||
|
||
@Test | ||
public void testHandleInternalServerException() { | ||
final RuntimeException runtimeExceptionNoMessage = new RuntimeException(); | ||
final String exceptionMessage = UUID.randomUUID().toString(); | ||
final RuntimeException runtimeExceptionWithMessage = new RuntimeException(exceptionMessage); | ||
|
||
final Status noMessageStatus = grpcRequestExceptionHandler.apply(requestContext, runtimeExceptionNoMessage, metadata); | ||
assertThat(noMessageStatus.getCode(), equalTo(Status.Code.INTERNAL)); | ||
assertThat(noMessageStatus.getDescription(), equalTo(Status.Code.INTERNAL.name())); | ||
|
||
final Status messageStatus = grpcRequestExceptionHandler.apply(requestContext, runtimeExceptionWithMessage, metadata); | ||
assertThat(messageStatus.getCode(), equalTo(Status.Code.INTERNAL)); | ||
assertThat(messageStatus.getDescription(), equalTo(exceptionMessage)); | ||
|
||
verify(internalServerErrorCounter, times(2)).increment(); | ||
} | ||
} |
Oops, something went wrong.