Skip to content
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

Using wrapped executor for Jersey async. #1647

Merged
merged 1 commit into from
Apr 16, 2020
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
2 changes: 1 addition & 1 deletion dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
<version.lib.jsonb-api>1.0.2</version.lib.jsonb-api>
<version.lib.jsonp-api>1.1.6</version.lib.jsonp-api>
<version.lib.jsonp-impl>1.1.6</version.lib.jsonp-impl>
<version.lib.junit>5.1.0</version.lib.junit>
<version.lib.junit>5.6.2</version.lib.junit>
<version.lib.maven-wagon>2.10</version.lib.maven-wagon>
<version.lib.microprofile-config>1.3</version.lib.microprofile-config>
<version.lib.microprofile-health>2.1</version.lib.microprofile-health>
Expand Down
50 changes: 50 additions & 0 deletions tests/integration/mp-gh-1538/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2020 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>helidon-tests-integration</artifactId>
<groupId>io.helidon.tests.integration</groupId>
<version>2.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>helidon-tests-integration-mp-gh-1538</artifactId>
<name>Helidon Tests Integration MP GH 1538</name>
<description>Reproducer for Github issue #1538 - control Jersey
Async executor size</description>

<dependencies>
<dependency>
<groupId>io.helidon.microprofile.bundles</groupId>
<artifactId>helidon-microprofile</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.tests.integration.gh1538;

import java.util.Set;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.core.Application;

@ApplicationScoped
public class JaxRsApplication extends Application {
@Override
public Set<Class<?>> getClasses() {
return Set.of(JaxRsResource.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.tests.integration.gh1538;

import javax.enterprise.context.RequestScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;

@RequestScoped
@Path("/test")
public class JaxRsResource {
@GET
@Path("/async")
public void asyncResponse(@Suspended AsyncResponse response) {
Thread thread = new Thread(() -> response.resume("result"));
thread.start();
}

@GET
@Path("/sync")
public String syncResponse() {
return Thread.currentThread().getName();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#
# Copyright (c) 2020 Oracle and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

server.port=0
server.executor-service.thread-name-prefix=gh-1538-
server.jersey.async-executor-service.thread-name-prefix=async-gh-1538-
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2020 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.tests.integration.gh1538;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;

import io.helidon.microprofile.server.Server;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;

@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class JaxRsResourceTest {
private static Server server;
private static Client client;
private static WebTarget target;

@BeforeAll
static void initClass() {
server = Server.create(JaxRsApplication.class).start();
client = ClientBuilder.newClient();
target = client.target("http://localhost:" + server.port() + "/test");
}

@AfterAll
static void destroyClass() {
if (null != server) {
server.stop();
}
if (null != client) {
client.close();
}
}

@Test
@Order(1)
void testSync() {
target.path("/sync")
.request()
.get(String.class);
}

@Test
@Order(2)
void testAsync() {
target.path("/async")
.request()
.get(String.class);
}

@Test
// this method must be after the async test, as the threads are not created before it
@Order(3)
void testThreads() {
int countOfJerseyServer = 0;
int countOfJerseyServerAsync = 0;
int countOfDefaultJersey = 0;

// now make sure the threads are as expected - array quite large, to fit all threads
Thread[] threads = new Thread[100];
Thread.enumerate(threads);
for (Thread thread : threads) {
if (null == thread) {
break;
}
String threadName = thread.getName();
// see microprofile-config.properties - this is an explicit prefix
if (threadName.startsWith("gh-1538-")) {
countOfJerseyServer++;
} else if (threadName.startsWith("async-gh-1538-")) {
countOfJerseyServerAsync++;
} else if (threadName.startsWith("jersey-server-managed-async-executor-")) {
countOfDefaultJersey++;
} else {
System.out.println(threadName);
}
}

assertThat("We should replace default async executor with a custom one", countOfDefaultJersey, is(0));
assertThat("We should use our configured server threads", countOfJerseyServer, greaterThan(0));
assertThat("We should use our configured server async threads", countOfJerseyServerAsync, greaterThan(0));
}
}
1 change: 1 addition & 0 deletions tests/integration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<module>mp-ws-services</module>
<module>webclient</module>
<module>security</module>
<module>mp-gh-1538</module>
</modules>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.helidon.webserver.jersey;

import java.util.concurrent.ExecutorService;
import java.util.function.Supplier;

import io.helidon.common.configurable.ThreadPoolSupplier;
import io.helidon.config.Config;
Expand All @@ -26,16 +27,24 @@

@ManagedAsyncExecutor
class AsyncExecutorProvider implements ExecutorServiceProvider {
private final ThreadPoolSupplier executorServiceSupplier;

AsyncExecutorProvider(Config config) {
this.executorServiceSupplier = ThreadPoolSupplier.builder()
.corePoolSize(1)
.maxPoolSize(10)
.prestart(false)
.threadNamePrefix("helidon-jersey-async")
.config(config)
.build();
private final Supplier<ExecutorService> executorServiceSupplier;

AsyncExecutorProvider(Supplier<ExecutorService> supplier) {
this.executorServiceSupplier = supplier;
}

static ExecutorServiceProvider create(Config config) {
return new AsyncExecutorProvider(ThreadPoolSupplier.builder()
.corePoolSize(1)
.maxPoolSize(10)
.prestart(false)
.threadNamePrefix("helidon-jersey-async")
.config(config.get("async-executor-service"))
.build());
}

static ExecutorServiceProvider create(ExecutorService executor) {
return new AsyncExecutorProvider(() -> executor);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@

import io.opentracing.Span;
import io.opentracing.SpanContext;
import org.eclipse.microprofile.config.ConfigProvider;
import org.glassfish.jersey.internal.PropertiesDelegate;
import org.glassfish.jersey.internal.util.collection.Ref;
import org.glassfish.jersey.server.ApplicationHandler;
Expand Down Expand Up @@ -129,10 +128,20 @@ public class JerseySupport implements Service {
* and Config
*/
private JerseySupport(Builder builder) {
ExecutorService executorService = (builder.executorService != null) ? builder.executorService : getDefaultThreadPool();
ExecutorService executorService = (builder.executorService != null)
? builder.executorService
: getDefaultThreadPool(builder.config);
this.service = Contexts.wrap(executorService);

builder.resourceConfig.register(new AsyncExecutorProvider(builder.config));
// make sure we have a wrapped async executor as well
if (builder.asyncExecutorService == null) {
// create a new one from configuration
builder.resourceConfig.register(AsyncExecutorProvider.create(builder.config));
} else {
// use the one provided
builder.resourceConfig.register(AsyncExecutorProvider.create(builder.asyncExecutorService));
}

this.appHandler = new ApplicationHandler(builder.resourceConfig, new ServerBinder(executorService));
this.container = new HelidonJerseyContainer(appHandler, builder.resourceConfig);
}
Expand All @@ -143,11 +152,9 @@ public void update(Routing.Rules routingRules) {
appHandler.onStartup(container);
}

private static ExecutorService getDefaultThreadPool() {
private static synchronized ExecutorService getDefaultThreadPool(Config config) {
if (DEFAULT_THREAD_POOL.get() == null) {
Config executorConfig = ((Config) ConfigProvider.getConfig())
.get("server.executor-service");

Config executorConfig = config.get("executor-service");
DEFAULT_THREAD_POOL.set(ServerThreadPoolSupplier.builder()
.name("server")
.config(executorConfig)
Expand Down Expand Up @@ -444,6 +451,7 @@ public static final class Builder implements Configurable<Builder>, io.helidon.c
private ResourceConfig resourceConfig;
private ExecutorService executorService;
private Config config = Config.empty();
private ExecutorService asyncExecutorService;

private Builder() {
this(null);
Expand Down Expand Up @@ -549,6 +557,18 @@ public Builder executorService(ExecutorService executorService) {
return this;
}

/**
* Sets the executor service to use for a handling of asynchronous requests
* with {@link javax.ws.rs.container.AsyncResponse}.
*
* @param executorService the executor service to use for a handling of asynchronous requests
* @return an updated instance
*/
public Builder asyncExecutorService(ExecutorService executorService) {
this.asyncExecutorService = executorService;
return this;
}

/**
* Update configuration from Config.
* Currently used to set up async executor service only.
Expand Down