Skip to content

Bugfix: Catch RejectedExecutionException and skip validation #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@
import com.getyourguide.openapi.validation.api.model.ValidatorConfiguration;
import com.getyourguide.openapi.validation.core.validator.OpenApiInteractionValidatorWrapper;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.utils.URLEncodedUtils;

@Slf4j
public class OpenApiRequestValidator {
private final ThreadPoolExecutor threadPool = new ThreadPoolExecutor(2, 2, 1000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(10));

private final ThreadPoolExecutor threadPoolExecutor;
private final OpenApiInteractionValidatorWrapper validator;
private final ValidationReportHandler validationReportHandler;

public OpenApiRequestValidator(
ThreadPoolExecutor threadPoolExecutor,
ValidationReportHandler validationReportHandler,
MetricsReporter metricsReporter,
String specificationFilePath,
ValidatorConfiguration configuration
) {
this.threadPoolExecutor = threadPoolExecutor;
this.validator = new OpenApiInteractionValidatorFactory().build(specificationFilePath, configuration);
this.validationReportHandler = validationReportHandler;

Expand All @@ -42,11 +42,19 @@ public boolean isReady() {
}

public void validateRequestObjectAsync(final RequestMetaData request, String requestBody) {
threadPool.execute(() -> validateRequestObject(request, requestBody));
executeAsync(() -> validateRequestObject(request, requestBody));
}

public void validateResponseObjectAsync(final RequestMetaData request, ResponseMetaData response, final String responseBody) {
threadPool.execute(() -> validateResponseObject(request, response, responseBody));
executeAsync(() -> validateResponseObject(request, response, responseBody));
}

private void executeAsync(Runnable command) {
try {
threadPoolExecutor.execute(command);
} catch (RejectedExecutionException ignored) {
// ignored
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want to monitor this? It could point to a configuration issue (thread pool too small for the amount of requests handled). If you are not monitoring, you could simply set up the In ThreadPoolExecutor.DiscardPolicy handler instead, see https://stackoverflow.com/questions/8183205/what-could-be-the-cause-of-rejectedexecutionexception

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now not planning to monitor. Maybe later? Added DiscardPolicy now. Still leaving RejectedExecutionException just for safety :)

}
}

public ValidationResult validateRequestObject(final RequestMetaData request, String requestBody) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.getyourguide.openapi.validation.core;

import static org.mockito.Mockito.mock;

import com.getyourguide.openapi.validation.api.metrics.MetricsReporter;
import com.getyourguide.openapi.validation.api.model.ValidatorConfiguration;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

public class OpenApiRequestValidatorTest {

private ThreadPoolExecutor threadPoolExecutor;

private OpenApiRequestValidator openApiRequestValidator;

@BeforeEach
public void setup() {
threadPoolExecutor = mock();
ValidationReportHandler validationReportHandler = mock();
MetricsReporter metricsReporter = mock();

openApiRequestValidator = new OpenApiRequestValidator(
threadPoolExecutor,
validationReportHandler,
metricsReporter,
"",
new ValidatorConfiguration(null, null, null)
);
}

@Test
public void testWhenThreadPoolExecutorRejectsExecutionThenItShouldNotThrow() {
Mockito.doThrow(new RejectedExecutionException()).when(threadPoolExecutor).execute(Mockito.any());

openApiRequestValidator.validateRequestObjectAsync(mock(), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import com.getyourguide.openapi.validation.core.throttle.ValidationReportThrottler;
import com.getyourguide.openapi.validation.core.throttle.ValidationReportThrottlerNone;
import java.util.Optional;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import lombok.AllArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
Expand Down Expand Up @@ -96,7 +99,17 @@ public OpenApiRequestValidator openApiRequestValidator(
MetricsReporter metricsReporter,
ValidatorConfiguration validatorConfiguration
) {
var threadPoolExecutor = new ThreadPoolExecutor(
2,
2,
1000L,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(10),
new ThreadPoolExecutor.DiscardPolicy()
);

return new OpenApiRequestValidator(
threadPoolExecutor,
validationReportHandler,
metricsReporter,
properties.getSpecificationFilePath(),
Expand Down