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

[RESTEASY-3314] Use a global executor service to avoid creating too m… #3627

Merged
merged 1 commit into from
May 11, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class ContextualExecutorService implements ExecutorService {
@Override
public void shutdown() {
if (shutdown.compareAndSet(false, true)) {
if (managed) {
if (isManaged()) {
// Clear the delegate as we're done with it
delegate = null;
} else {
Expand All @@ -87,7 +87,7 @@ public void shutdown() {
@Override
public List<Runnable> shutdownNow() {
if (shutdown.compareAndSet(false, true)) {
if (managed) {
if (isManaged()) {
// Clear the delegate as we're done with it
delegate = null;
} else {
Expand All @@ -100,23 +100,23 @@ public List<Runnable> shutdownNow() {

@Override
public boolean isShutdown() {
if (managed) {
if (isManaged()) {
return shutdown.get();
}
return getDelegate().isShutdown();
}

@Override
public boolean isTerminated() {
if (managed) {
if (isManaged()) {
return false;
}
return getDelegate().isTerminated();
}

@Override
public boolean awaitTermination(final long timeout, final TimeUnit unit) throws InterruptedException {
if (managed) {
if (isManaged()) {
return false;
}
return getDelegate().awaitTermination(timeout, unit);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

package org.jboss.resteasy.concurrent;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -31,9 +29,7 @@
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import javax.naming.InitialContext;
Expand All @@ -43,7 +39,6 @@
import org.jboss.resteasy.spi.ResteasyProviderFactory;
import org.jboss.resteasy.spi.concurrent.ThreadContext;
import org.jboss.resteasy.spi.concurrent.ThreadContexts;
import org.jboss.resteasy.spi.config.ConfigurationFactory;

/**
* A utility to create and/or wrap {@linkplain ExecutorService executors} in a contextual executor.
Expand Down Expand Up @@ -87,12 +82,10 @@ public static Executor executor() {
*/
public static ContextualExecutorService threadPool() {
ExecutorService delegate = lookup(EXECUTOR_SERVICE_JNDI);
boolean managed = true;
if (delegate == null) {
delegate = Executors.newCachedThreadPool(new ContextualThreadFactory("contextual-pool"));
managed = false;
delegate = GlobalContextualExecutorService.INSTANCE;
}
return wrap(delegate, managed);
return wrap(delegate, true);
}

/**
Expand All @@ -115,8 +108,11 @@ public static ContextualExecutorService threadPool() {
* @return a new contextual executor
*/
public static ContextualScheduledExecutorService scheduledThreadPool() {
final int poolSize = getConfigValue("resteasy.async.timeout.scheduler.min.pool.size", Integer.class, () -> 1);
return scheduledThreadPool(poolSize, new ContextualThreadFactory("contextual-scheduled-pool"));
ScheduledExecutorService delegate = lookup(SCHEDULED_EXECUTOR_SERVICE_JNDI);
if (delegate == null) {
delegate = GlobalContextualScheduledExecutorService.INSTANCE;
}
return wrap(delegate, true);
}

/**
Expand Down Expand Up @@ -379,41 +375,4 @@ private static <T extends ExecutorService> T lookup(final String jndiName) {
return null;
}

@SuppressWarnings("SameParameterValue")
private static <T> T getConfigValue(final String name, final Class<T> type, final Supplier<T> dft) {
if (System.getSecurityManager() == null) {
return ConfigurationFactory.getInstance().getConfiguration()
.getOptionalValue(name, type)
.orElseGet(dft);
}
return AccessController.doPrivileged((PrivilegedAction<T>) () -> ConfigurationFactory.getInstance().getConfiguration()
.getOptionalValue(name, type)
.orElseGet(dft));
}

private static class ContextualThreadFactory implements ThreadFactory {
private static final AtomicInteger POOL_COUNTER = new AtomicInteger(0);
private final AtomicInteger threadCounter = new AtomicInteger(0);
private final String prefix;

private ContextualThreadFactory(final String prefix) {
this.prefix = prefix;
}

@Override
public Thread newThread(final Runnable r) {
final Thread thread = new Thread(r, String.format("%s-%d-thread-%d", prefix, POOL_COUNTER.incrementAndGet(),
threadCounter.incrementAndGet()));
if (System.getSecurityManager() == null) {
thread.setDaemon(true);
thread.setPriority(Thread.NORM_PRIORITY);
return thread;
}
return AccessController.doPrivileged((PrivilegedAction<Thread>) () -> {
thread.setDaemon(true);
thread.setPriority(Thread.NORM_PRIORITY);
return thread;
});
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 org.jboss.resteasy.concurrent;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Creates contextual threads that are always daemon threads.
*
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
class ContextualThreadFactory implements ThreadFactory {
private static final AtomicInteger POOL_COUNTER = new AtomicInteger(0);
private final AtomicInteger threadCounter = new AtomicInteger(0);
private final String prefix;

ContextualThreadFactory(final String prefix) {
this.prefix = String.format("%s-%d-thread", prefix, POOL_COUNTER.incrementAndGet());
}

@Override
public Thread newThread(final Runnable r) {
final Thread thread = new Thread(r, String.format("%s-%d", prefix, threadCounter.incrementAndGet()));
if (System.getSecurityManager() == null) {
thread.setDaemon(true);
if (thread.getPriority() != Thread.NORM_PRIORITY) {
thread.setPriority(Thread.NORM_PRIORITY);
}
return thread;
}
return AccessController.doPrivileged((PrivilegedAction<Thread>) () -> {
thread.setDaemon(true);
if (thread.getPriority() != Thread.NORM_PRIORITY) {
thread.setPriority(Thread.NORM_PRIORITY);
}
return thread;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 org.jboss.resteasy.concurrent;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
class GlobalContextualExecutorService extends ContextualExecutorService implements AutoCloseable {
static final GlobalContextualExecutorService INSTANCE = new GlobalContextualExecutorService();
private final Thread shutdownHook;

private volatile ExecutorService delegate;

private GlobalContextualExecutorService() {
super(null, true);
shutdownHook = new Thread("resteasy-shutdown") {
@Override
public void run() {
synchronized (GlobalContextualExecutorService.this) {
if (delegate != null) {
delegate.shutdown();
delegate = null;
}
}
}
};
}

@Override
public void shutdown() {
// Do nothing as we will shut it down later
}

@Override
public List<Runnable> shutdownNow() {
return Collections.emptyList();
}

@Override
public boolean isShutdown() {
return false;
}

@Override
public boolean isTerminated() {
return false;
}

@Override
ExecutorService getDelegate() {
if (delegate == null) {
synchronized (this) {
if (delegate == null) {
final int poolSize = SecurityActions.getCoreThreads("dev.resteasy.concurrent.core.pool.size");
delegate = new ThreadPoolExecutor(poolSize, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
new LinkedBlockingQueue<>(), new ContextualThreadFactory("contextual-pool"));
SecurityActions.registerShutdownHook(shutdownHook);
}
}
}
return delegate;
}

@Override
public void close() {
synchronized (this) {
SecurityActions.removeShutdownHook(shutdownHook);
if (delegate != null) {
delegate.shutdown();
delegate = null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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 org.jboss.resteasy.concurrent;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
class GlobalContextualScheduledExecutorService extends ContextualScheduledExecutorService implements AutoCloseable {
static final GlobalContextualScheduledExecutorService INSTANCE = new GlobalContextualScheduledExecutorService();
private final Thread shutdownHook;
private volatile ScheduledExecutorService delegate;

private GlobalContextualScheduledExecutorService() {
super(null, true);
shutdownHook = new Thread("resteasy-shutdown") {
@Override
public void run() {
synchronized (GlobalContextualScheduledExecutorService.this) {
if (delegate != null) {
delegate.shutdown();
delegate = null;
}
}
}
};
}

@Override
public void shutdown() {
// Do nothing as we will shut it down later
}

@Override
public List<Runnable> shutdownNow() {
return Collections.emptyList();
}

@Override
public boolean isShutdown() {
return false;
}

@Override
public boolean isTerminated() {
return false;
}

@Override
ScheduledExecutorService getDelegate() {
if (delegate == null) {
synchronized (this) {
if (delegate == null) {
final int poolSize = SecurityActions.getCoreThreads("resteasy.async.timeout.scheduler.min.pool.size");
delegate = Executors.newScheduledThreadPool(poolSize,
new ContextualThreadFactory("contextual-scheduled-pool"));
SecurityActions.registerShutdownHook(shutdownHook);
}
}
}
return delegate;
}

@Override
public void close() {
synchronized (this) {
SecurityActions.removeShutdownHook(shutdownHook);
if (delegate != null) {
delegate.shutdown();
delegate = null;
}
}
}
}