Merge pull request #117 from weihubeats/develop

add jetty、undertow ThreadPool
pull/121/head
龙台 Long Tai 3 years ago committed by GitHub
commit d70689e5f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -26,6 +26,20 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-undertow</artifactId>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>

@ -17,9 +17,7 @@ import cn.hippo4j.starter.event.ApplicationContentPostProcessor;
import cn.hippo4j.starter.handler.BaseThreadDetailStateHandler;
import cn.hippo4j.starter.handler.DynamicThreadPoolBannerHandler;
import cn.hippo4j.starter.handler.ThreadPoolRunStateHandler;
import cn.hippo4j.starter.handler.web.TomcatWebThreadPoolHandler;
import cn.hippo4j.starter.handler.web.WebThreadPoolHandlerChoose;
import cn.hippo4j.starter.handler.web.WebThreadPoolRunStateHandler;
import cn.hippo4j.starter.handler.web.*;
import cn.hippo4j.starter.monitor.ReportingEventExecutor;
import cn.hippo4j.starter.monitor.collect.RunTimeInfoCollector;
import cn.hippo4j.starter.monitor.send.HttpConnectSender;
@ -28,13 +26,11 @@ import cn.hippo4j.starter.remote.HttpAgent;
import cn.hippo4j.starter.remote.HttpScheduledHealthCheck;
import cn.hippo4j.starter.remote.ServerHealthCheck;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
@ -55,6 +51,12 @@ import org.springframework.core.env.ConfigurableEnvironment;
@ImportAutoConfiguration({HttpClientConfiguration.class, DiscoveryConfiguration.class, MessageNotifyConfiguration.class, UtilAutoConfiguration.class})
public class DynamicThreadPoolAutoConfiguration {
private static final String TOMCAT_SERVLET_WEB_SERVER_FACTORY = "tomcatWebThreadPoolHandler";
private static final String JETTY_SERVLET_WEB_SERVER_FACTORY = "JettyServletWebServerFactory";
private static final String UNDERTOW_SERVLET_WEB_SERVER_FACTORY = "undertowServletWebServerFactory";
private final BootstrapProperties properties;
private final ConfigurableEnvironment environment;
@ -143,11 +145,23 @@ public class DynamicThreadPoolAutoConfiguration {
}
@Bean
@ConditionalOnBean(name = "tomcatServletWebServerFactory")
public TomcatWebThreadPoolHandler tomcatWebThreadPoolHandler(@Autowired(required = false) ServletWebServerApplicationContext applicationContext) {
return new TomcatWebThreadPoolHandler(applicationContext);
@ConditionalOnBean(name = TOMCAT_SERVLET_WEB_SERVER_FACTORY)
public TomcatWebThreadPoolHandler tomcatWebThreadPoolHandler() {
return new TomcatWebThreadPoolHandler();
}
@Bean
@ConditionalOnBean(name = JETTY_SERVLET_WEB_SERVER_FACTORY)
public JettyWebThreadPoolHandler jettyWebThreadPoolHandler() {return new JettyWebThreadPoolHandler();
}
@Bean
@ConditionalOnBean(name = UNDERTOW_SERVLET_WEB_SERVER_FACTORY)
public UndertowWebThreadPoolHandler undertowWebThreadPoolHandler() {
return new UndertowWebThreadPoolHandler();
}
@Bean
public WebThreadPoolHandlerChoose webThreadPoolServiceChoose() {
return new WebThreadPoolHandlerChoose();

@ -1,6 +1,17 @@
package cn.hippo4j.starter.handler.web;
import cn.hippo4j.common.config.ApplicationContextHolder;
import cn.hippo4j.common.model.PoolParameterInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.context.WebServerApplicationContext;
import org.springframework.boot.web.server.WebServer;
import org.springframework.boot.web.servlet.context.ServletWebServerInitializedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* Abstract web thread pool service.
@ -8,6 +19,7 @@ import java.util.concurrent.Executor;
* @author chen.ma
* @date 2022/1/19 21:20
*/
@Slf4j
public abstract class AbstractWebThreadPoolService implements WebThreadPoolService {
/**
@ -20,14 +32,18 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi
*
* @return
*/
protected abstract Executor getWebThreadPoolByServer();
protected abstract Executor getWebThreadPoolByServer(WebServer webServer);
@Override
public Executor getWebThreadPool() {
if (executor == null) {
synchronized (AbstractWebThreadPoolService.class) {
if (executor == null) {
executor = getWebThreadPoolByServer();
ApplicationContext applicationContext = ApplicationContextHolder.getInstance();
WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer();
executor = getWebThreadPoolByServer(webServer);
}
}
}
@ -35,4 +51,5 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi
return executor;
}
}

@ -0,0 +1,48 @@
package cn.hippo4j.starter.handler.web;
import cn.hippo4j.common.model.PoolParameterInfo;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.jetty.util.thread.ThreadPool;
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
import org.springframework.boot.web.server.WebServer;
import java.util.concurrent.Executor;
/**
* @author : wh
* @date : 2022/2/28 16:55
* @description:
*/
@Slf4j
public class JettyWebThreadPoolHandler extends AbstractWebThreadPoolService{
@Override
protected Executor getWebThreadPoolByServer(WebServer webServer) {
JettyWebServer jettyWebServer = (JettyWebServer) webServer;
return jettyWebServer.getServer().getThreadPool();
}
@Override
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try {
ThreadPool.SizedThreadPool jettyExecutor = (ThreadPool.SizedThreadPool) executor;
Integer coreSize = poolParameterInfo.getCoreSize();
Integer maxSize = poolParameterInfo.getMaxSize();
jettyExecutor.setMinThreads(coreSize);
jettyExecutor.setMaxThreads(maxSize);
log.info(
"🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}]",
String.format("%s => %s", jettyExecutor.getMinThreads(), coreSize),
String.format("%s => %s", jettyExecutor.getMaxThreads(), maxSize)
);
} catch (Exception ex) {
log.error("Failed to modify the jetty thread pool parameter.", ex);
}
}
}

@ -3,11 +3,11 @@ package cn.hippo4j.starter.handler.web;
import cn.hippo4j.common.model.PoolParameterInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.springframework.boot.web.embedded.tomcat.TomcatWebServer;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import org.springframework.boot.web.server.WebServer;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
@ -19,16 +19,15 @@ import java.util.concurrent.atomic.AtomicBoolean;
*/
@Slf4j
@AllArgsConstructor
public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService {
public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService{
private final ServletWebServerApplicationContext applicationContext;
private final AtomicBoolean cacheFlag = new AtomicBoolean(Boolean.FALSE);
private static String EXCEPTION_MESSAGE;
@Override
protected Executor getWebThreadPoolByServer() {
protected Executor getWebThreadPoolByServer(WebServer webServer) {
if (cacheFlag.get()) {
log.warn("Exception getting Tomcat thread pool. Exception message :: {}", EXCEPTION_MESSAGE);
return null;
@ -36,7 +35,7 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService {
Executor tomcatExecutor = null;
try {
tomcatExecutor = ((TomcatWebServer) applicationContext.getWebServer()).getTomcat().getConnector().getProtocolHandler().getExecutor();
tomcatExecutor = ((TomcatWebServer) webServer).getTomcat().getConnector().getProtocolHandler().getExecutor();
} catch (Exception ex) {
cacheFlag.set(Boolean.TRUE);
EXCEPTION_MESSAGE = ex.getMessage();
@ -50,20 +49,20 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService {
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try {
ThreadPoolExecutor tomcatExecutor = (ThreadPoolExecutor) executor;
int originalCoreSize = tomcatExecutor.getCorePoolSize();
int originalMaximumPoolSize = tomcatExecutor.getMaximumPoolSize();
long originalKeepAliveTime = tomcatExecutor.getKeepAliveTime(TimeUnit.SECONDS);
tomcatExecutor.setCorePoolSize(poolParameterInfo.getCoreSize());
tomcatExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize());
tomcatExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS);
int originalCoreSize = tomcatExecutor.getCorePoolSize();
int originalMaximumPoolSize = tomcatExecutor.getMaximumPoolSize();
long originalKeepAliveTime = tomcatExecutor.getKeepAliveTime(TimeUnit.SECONDS);
log.info(
"🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}], keepAliveTime :: [{}]",
String.format("%s => %s", originalCoreSize, poolParameterInfo.getCoreSize()),
String.format("%s => %s", originalMaximumPoolSize, poolParameterInfo.getMaxSize()),
String.format("%s => %s", originalKeepAliveTime, poolParameterInfo.getKeepAliveTime())
);
} catch (Exception ex) {
log.error("Failed to modify the Tomcat thread pool parameter.", ex);
}

@ -1,9 +1,16 @@
package cn.hippo4j.starter.handler.web;
import cn.hippo4j.common.model.PoolParameterInfo;
import lombok.AllArgsConstructor;
import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext;
import io.undertow.Undertow;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.embedded.undertow.UndertowWebServer;
import org.springframework.boot.web.server.WebServer;
import org.springframework.util.ReflectionUtils;
import org.xnio.Options;
import org.xnio.XnioWorker;
import java.lang.reflect.Field;
import java.util.Objects;
import java.util.concurrent.Executor;
/**
@ -12,18 +19,50 @@ import java.util.concurrent.Executor;
* @author chen.ma
* @date 2022/1/19 21:19
*/
@AllArgsConstructor
@Slf4j
public class UndertowWebThreadPoolHandler extends AbstractWebThreadPoolService {
private final ServletWebServerApplicationContext applicationContext;
private static final String UNDERTOW_NAME = "undertow";
@Override
protected Executor getWebThreadPoolByServer() {
return null;
protected Executor getWebThreadPoolByServer(WebServer webServer) {
// There is no need to consider reflection performance because the fetch is a singleton
UndertowWebServer undertowWebServer = (UndertowWebServer) webServer;
Field undertowField = ReflectionUtils.findField(UndertowWebServer.class, UNDERTOW_NAME);
assert undertowField != null;
ReflectionUtils.makeAccessible(undertowField);
Undertow undertow = (Undertow) ReflectionUtils.getField(undertowField, undertowWebServer);
return Objects.isNull(undertow) ? null : undertow.getWorker();
}
@Override
public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) {
try {
XnioWorker xnioWorker = (XnioWorker) executor;
Integer coreSize = poolParameterInfo.getCoreSize();
Integer maxSize = poolParameterInfo.getMaxSize();
Integer keepAliveTime = poolParameterInfo.getKeepAliveTime();
int originalCoreSize = xnioWorker.getOption(Options.WORKER_TASK_CORE_THREADS);
int originalMaximumPoolSize = xnioWorker.getOption(Options.WORKER_TASK_MAX_THREADS);
int originalKeepAliveTime = xnioWorker.getOption(Options.WORKER_TASK_KEEPALIVE);
xnioWorker.setOption(Options.WORKER_TASK_CORE_THREADS, coreSize);
xnioWorker.setOption(Options.WORKER_TASK_MAX_THREADS, maxSize);
xnioWorker.setOption(Options.WORKER_TASK_KEEPALIVE, keepAliveTime);
log.info(
"🔥 Changed web thread pool. coreSize :: [{}], maxSize :: [{}], keepAliveTime :: [{}]",
String.format("%s => %s", originalCoreSize, coreSize),
String.format("%s => %s", originalMaximumPoolSize, maxSize),
String.format("%s => %s", originalKeepAliveTime, keepAliveTime)
);
} catch (Exception ex) {
log.error("Failed to modify the undertow thread pool parameter.", ex);
}
}

Loading…
Cancel
Save