From d7c17f9a2cdc67fe5ab6b38944aaca0f1ebaf358 Mon Sep 17 00:00:00 2001 From: weihu Date: Mon, 28 Feb 2022 17:30:28 +0800 Subject: [PATCH 1/5] =?UTF-8?q?add=20jetty=E3=80=81undertow=20ThreadPool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hippo4j-spring-boot-starter/pom.xml | 14 +++++ .../DynamicThreadPoolAutoConfiguration.java | 30 ++++++++--- .../web/AbstractWebThreadPoolService.java | 18 ++++++- .../web/JettyWebThreadPoolHandler.java | 51 +++++++++++++++++++ .../web/TomcatWebThreadPoolHandler.java | 9 ++-- .../web/UndertowWebThreadPoolHandler.java | 44 ++++++++++++++-- 6 files changed, 147 insertions(+), 19 deletions(-) create mode 100644 hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml b/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml index 4aba794b..81e12dd3 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/pom.xml @@ -26,6 +26,20 @@ spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-jetty + compile + true + + + org.springframework.boot + spring-boot-starter-undertow + compile + true + + + com.squareup.okhttp3 okhttp diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java index a3381fe8..30ae05f0 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/config/DynamicThreadPoolAutoConfiguration.java @@ -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(); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java index 8a0560c0..c2297edd 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java @@ -1,5 +1,14 @@ 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; /** @@ -8,6 +17,7 @@ import java.util.concurrent.Executor; * @author chen.ma * @date 2022/1/19 21:20 */ +@Slf4j public abstract class AbstractWebThreadPoolService implements WebThreadPoolService { /** @@ -20,14 +30,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) { + ApplicationContext applicationContext = ApplicationContextHolder.getInstance(); + WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer(); if (executor == null) { - executor = getWebThreadPoolByServer(); + executor = getWebThreadPoolByServer(webServer); } } } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java new file mode 100644 index 00000000..e5b7ab9c --- /dev/null +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java @@ -0,0 +1,51 @@ +package cn.hippo4j.starter.handler.web; + +import cn.hippo4j.common.model.PoolParameterInfo; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.web.embedded.jetty.JettyWebServer; +import org.springframework.boot.web.server.WebServer; + +import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; + +/** + * @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 { + ThreadPoolExecutor jettyExecutor = (ThreadPoolExecutor) executor; + int originalCoreSize = jettyExecutor.getCorePoolSize(); + int originalMaximumPoolSize = jettyExecutor.getMaximumPoolSize(); + long originalKeepAliveTime = jettyExecutor.getKeepAliveTime(TimeUnit.SECONDS); + + jettyExecutor.setCorePoolSize(poolParameterInfo.getCoreSize()); + jettyExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); + jettyExecutor.setKeepAliveTime(poolParameterInfo.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 jetty thread pool parameter.", ex); + } + + } +} diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java index 8229d08e..b01e1cb7 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java @@ -4,7 +4,7 @@ import cn.hippo4j.common.model.PoolParameterInfo; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; 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; @@ -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(); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java index 9861f37d..4ac1f15f 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java @@ -1,10 +1,19 @@ package cn.hippo4j.starter.handler.web; import cn.hippo4j.common.model.PoolParameterInfo; +import io.undertow.Undertow; import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.web.embedded.undertow.UndertowWebServer; +import org.springframework.boot.web.server.WebServer; import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; +import org.springframework.util.ReflectionUtils; +import java.lang.reflect.Field; +import java.util.Objects; import java.util.concurrent.Executor; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; /** * Undertow web thread pool handler. @@ -12,18 +21,45 @@ 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 { + ThreadPoolExecutor undertowExecutor = (ThreadPoolExecutor) executor; + int originalCoreSize = undertowExecutor.getCorePoolSize(); + int originalMaximumPoolSize = undertowExecutor.getMaximumPoolSize(); + long originalKeepAliveTime = undertowExecutor.getKeepAliveTime(TimeUnit.SECONDS); + + undertowExecutor.setCorePoolSize(poolParameterInfo.getCoreSize()); + undertowExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); + undertowExecutor.setKeepAliveTime(poolParameterInfo.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 undertow thread pool parameter.", ex); + } } From d14a3ff8f89d767611f3c19ffcf9e16616303289 Mon Sep 17 00:00:00 2001 From: weihu Date: Mon, 28 Feb 2022 17:38:27 +0800 Subject: [PATCH 2/5] update getWebThreadPool --- .../starter/handler/web/AbstractWebThreadPoolService.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java index c2297edd..f29868fb 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java @@ -38,9 +38,9 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi public Executor getWebThreadPool() { if (executor == null) { synchronized (AbstractWebThreadPoolService.class) { - ApplicationContext applicationContext = ApplicationContextHolder.getInstance(); - WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer(); if (executor == null) { + ApplicationContext applicationContext = ApplicationContextHolder.getInstance(); + WebServer webServer = ((WebServerApplicationContext) applicationContext).getWebServer(); executor = getWebThreadPoolByServer(webServer); } } From 491cdacf7435fe7c617cbd0a8f713f4be06e529f Mon Sep 17 00:00:00 2001 From: weihu Date: Mon, 28 Feb 2022 17:44:11 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E5=B0=81=E8=A3=85log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/web/AbstractWebThreadPoolService.java | 15 +++++++++++++++ .../handler/web/JettyWebThreadPoolHandler.java | 11 +---------- .../handler/web/TomcatWebThreadPoolHandler.java | 12 ++---------- .../handler/web/UndertowWebThreadPoolHandler.java | 11 +---------- 4 files changed, 19 insertions(+), 30 deletions(-) diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java index f29868fb..8168610f 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java @@ -10,6 +10,8 @@ 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. @@ -49,4 +51,17 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi return executor; } + public void log(PoolParameterInfo poolParameterInfo, ThreadPoolExecutor threadPoolExecutor) { + int originalCoreSize = threadPoolExecutor.getCorePoolSize(); + int originalMaximumPoolSize = threadPoolExecutor.getMaximumPoolSize(); + long originalKeepAliveTime = threadPoolExecutor.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()) + ); + + } + } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java index e5b7ab9c..1fe1798e 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java @@ -29,20 +29,11 @@ public class JettyWebThreadPoolHandler extends AbstractWebThreadPoolService{ public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { try { ThreadPoolExecutor jettyExecutor = (ThreadPoolExecutor) executor; - int originalCoreSize = jettyExecutor.getCorePoolSize(); - int originalMaximumPoolSize = jettyExecutor.getMaximumPoolSize(); - long originalKeepAliveTime = jettyExecutor.getKeepAliveTime(TimeUnit.SECONDS); - jettyExecutor.setCorePoolSize(poolParameterInfo.getCoreSize()); jettyExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); jettyExecutor.setKeepAliveTime(poolParameterInfo.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()) - ); + super.log(poolParameterInfo, jettyExecutor); } catch (Exception ex) { log.error("Failed to modify the jetty thread pool parameter.", ex); } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java index b01e1cb7..263eb119 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java @@ -49,20 +49,12 @@ 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); - 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()) - ); + super.log(poolParameterInfo, tomcatExecutor); + } catch (Exception ex) { log.error("Failed to modify the Tomcat thread pool parameter.", ex); } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java index 4ac1f15f..ee432452 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java @@ -43,20 +43,11 @@ public class UndertowWebThreadPoolHandler extends AbstractWebThreadPoolService { public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { try { ThreadPoolExecutor undertowExecutor = (ThreadPoolExecutor) executor; - int originalCoreSize = undertowExecutor.getCorePoolSize(); - int originalMaximumPoolSize = undertowExecutor.getMaximumPoolSize(); - long originalKeepAliveTime = undertowExecutor.getKeepAliveTime(TimeUnit.SECONDS); - undertowExecutor.setCorePoolSize(poolParameterInfo.getCoreSize()); undertowExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); undertowExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS); + super.log(poolParameterInfo, undertowExecutor); - 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 undertow thread pool parameter.", ex); } From 4558594b4179d28306faedeb5edd033fb3841a18 Mon Sep 17 00:00:00 2001 From: weihu Date: Mon, 28 Feb 2022 18:02:06 +0800 Subject: [PATCH 4/5] =?UTF-8?q?update=20jetty=E3=80=81Undertow=20ThreadPoo?= =?UTF-8?q?l?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/AbstractWebThreadPoolService.java | 12 -------- .../web/JettyWebThreadPoolHandler.java | 22 +++++++++----- .../web/TomcatWebThreadPoolHandler.java | 10 ++++++- .../web/UndertowWebThreadPoolHandler.java | 30 +++++++++++++------ 4 files changed, 44 insertions(+), 30 deletions(-) diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java index 8168610f..71531a79 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/AbstractWebThreadPoolService.java @@ -51,17 +51,5 @@ public abstract class AbstractWebThreadPoolService implements WebThreadPoolServi return executor; } - public void log(PoolParameterInfo poolParameterInfo, ThreadPoolExecutor threadPoolExecutor) { - int originalCoreSize = threadPoolExecutor.getCorePoolSize(); - int originalMaximumPoolSize = threadPoolExecutor.getMaximumPoolSize(); - long originalKeepAliveTime = threadPoolExecutor.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()) - ); - - } } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java index 1fe1798e..0bf43750 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/JettyWebThreadPoolHandler.java @@ -2,12 +2,11 @@ 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; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; /** * @author : wh @@ -28,15 +27,22 @@ public class JettyWebThreadPoolHandler extends AbstractWebThreadPoolService{ @Override public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { try { - ThreadPoolExecutor jettyExecutor = (ThreadPoolExecutor) executor; - jettyExecutor.setCorePoolSize(poolParameterInfo.getCoreSize()); - jettyExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); - jettyExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS); - - super.log(poolParameterInfo, jettyExecutor); + 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); } } + + } diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java index 263eb119..178a14f3 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java @@ -52,8 +52,16 @@ public class TomcatWebThreadPoolHandler extends AbstractWebThreadPoolService{ 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()) + ); - super.log(poolParameterInfo, tomcatExecutor); } catch (Exception ex) { log.error("Failed to modify the Tomcat thread pool parameter.", ex); diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java index ee432452..b0b2262e 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/UndertowWebThreadPoolHandler.java @@ -2,18 +2,16 @@ package cn.hippo4j.starter.handler.web; import cn.hippo4j.common.model.PoolParameterInfo; import io.undertow.Undertow; -import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.boot.web.embedded.undertow.UndertowWebServer; import org.springframework.boot.web.server.WebServer; -import org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext; 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; -import java.util.concurrent.ThreadPoolExecutor; -import java.util.concurrent.TimeUnit; /** * Undertow web thread pool handler. @@ -42,11 +40,25 @@ public class UndertowWebThreadPoolHandler extends AbstractWebThreadPoolService { @Override public void updateWebThreadPool(PoolParameterInfo poolParameterInfo) { try { - ThreadPoolExecutor undertowExecutor = (ThreadPoolExecutor) executor; - undertowExecutor.setCorePoolSize(poolParameterInfo.getCoreSize()); - undertowExecutor.setMaximumPoolSize(poolParameterInfo.getMaxSize()); - undertowExecutor.setKeepAliveTime(poolParameterInfo.getKeepAliveTime(), TimeUnit.SECONDS); - super.log(poolParameterInfo, undertowExecutor); + 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); From 8c1afdb62f28746ee0a69d80de820ba8b1afc5b4 Mon Sep 17 00:00:00 2001 From: weihu Date: Mon, 28 Feb 2022 18:11:56 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dtomcat=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=B1=A0=E7=B1=BB=E5=9E=8B=E8=BD=AC=E6=8D=A2=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java index 178a14f3..d47ebb9c 100644 --- a/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java +++ b/hippo4j-spring-boot/hippo4j-spring-boot-starter/src/main/java/cn/hippo4j/starter/handler/web/TomcatWebThreadPoolHandler.java @@ -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.server.WebServer; import java.util.concurrent.Executor; -import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean;