Skip to content

Commit f54d915

Browse files
authored
Implement the ability to notify changes to the web container thread pool under the Config mode. (#1133)
* Add notification logic after modifying web thread pool parameters. * Refactor the notification module to enable notification capability for the web container. * Add application profile enum. * Revert the bugfix changes and split them into issue #1134. * Revert @order annotation in AbstractWebThreadPoolService. * fix profile enum name with test environment. * Fix: remove unused imports. * Delete ProfileEnum class. * Modify the way of obtaining the ID of the web thread pool. * Move the IExecutorProperties class to the common module. * Narrow the scope of @SuppressWarnings annotation usage.
1 parent ffaa4fb commit f54d915

File tree

29 files changed

+373
-95
lines changed

29 files changed

+373
-95
lines changed

hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/jetty/DefaultJettyWebThreadPoolHandler.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,10 @@
1818
package cn.hippo4j.adapter.web.jetty;
1919

2020
import cn.hippo4j.adapter.web.DefaultAbstractWebThreadPoolService;
21-
import cn.hippo4j.adapter.web.IWebThreadPoolHandlerSupport;
22-
import cn.hippo4j.common.constant.ChangeThreadPoolConstants;
23-
import cn.hippo4j.common.enums.WebContainerEnum;
24-
import cn.hippo4j.common.model.ThreadPoolBaseInfo;
25-
import cn.hippo4j.common.model.ThreadPoolParameter;
26-
import cn.hippo4j.common.model.ThreadPoolParameterInfo;
27-
import cn.hippo4j.common.model.ThreadPoolRunStateInfo;
28-
import cn.hippo4j.common.toolkit.ReflectUtil;
2921
import lombok.extern.slf4j.Slf4j;
30-
import org.eclipse.jetty.util.thread.QueuedThreadPool;
3122
import org.springframework.boot.web.embedded.jetty.JettyWebServer;
3223
import org.springframework.boot.web.server.WebServer;
3324

34-
import java.util.concurrent.BlockingQueue;
3525
import java.util.concurrent.Executor;
3626

3727
/**

hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/tomcat/TomcatWebThreadPoolHandlerSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public ThreadPoolParameter getWebThreadPoolParameter() {
9191
long keepAliveTime = tomcatThreadPoolExecutor.getKeepAliveTime(TimeUnit.SECONDS);
9292
parameterInfo.setCoreSize(minThreads);
9393
parameterInfo.setMaxSize(maxThreads);
94-
parameterInfo.setKeepAliveTime((int) keepAliveTime);
94+
parameterInfo.setKeepAliveTime(keepAliveTime);
9595
} catch (Exception ex) {
9696
log.error("Failed to get the tomcat thread pool parameter.", ex);
9797
}

hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/undertow/DefaultUndertowWebThreadPoolHandler.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@
2222
import java.util.concurrent.Executor;
2323

2424
import cn.hippo4j.adapter.web.DefaultAbstractWebThreadPoolService;
25-
import cn.hippo4j.adapter.web.IWebThreadPoolHandlerSupport;
26-
import cn.hippo4j.common.enums.WebContainerEnum;
27-
import cn.hippo4j.common.model.ThreadPoolBaseInfo;
28-
import cn.hippo4j.common.model.ThreadPoolParameter;
29-
import cn.hippo4j.common.model.ThreadPoolParameterInfo;
30-
import cn.hippo4j.common.model.ThreadPoolRunStateInfo;
3125
import io.undertow.Undertow;
3226
import lombok.extern.slf4j.Slf4j;
3327

hippo4j-adapter/hippo4j-adapter-web/src/main/java/cn/hippo4j/adapter/web/undertow/UndertowWebThreadPoolHandlerSupport.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public ThreadPoolParameter getWebThreadPoolParameter() {
8282
XnioWorker xnioWorker = (XnioWorker) executor;
8383
int minThreads = xnioWorker.getOption(Options.WORKER_TASK_CORE_THREADS);
8484
int maxThreads = xnioWorker.getOption(Options.WORKER_TASK_MAX_THREADS);
85-
int keepAliveTime = xnioWorker.getOption(Options.WORKER_TASK_KEEPALIVE);
85+
long keepAliveTime = xnioWorker.getOption(Options.WORKER_TASK_KEEPALIVE);
8686
parameterInfo.setCoreSize(minThreads);
8787
parameterInfo.setMaxSize(maxThreads);
8888
parameterInfo.setKeepAliveTime(keepAliveTime);
@@ -135,7 +135,7 @@ public void updateWebThreadPool(ThreadPoolParameterInfo threadPoolParameterInfo)
135135
XnioWorker xnioWorker = (XnioWorker) executor;
136136
Integer coreSize = threadPoolParameterInfo.corePoolSizeAdapt();
137137
Integer maxSize = threadPoolParameterInfo.maximumPoolSizeAdapt();
138-
Integer keepAliveTime = threadPoolParameterInfo.getKeepAliveTime();
138+
int keepAliveTime = threadPoolParameterInfo.getKeepAliveTime().intValue();
139139
int originalCoreSize = xnioWorker.getOption(Options.WORKER_TASK_CORE_THREADS);
140140
int originalMaximumPoolSize = xnioWorker.getOption(Options.WORKER_TASK_MAX_THREADS);
141141
int originalKeepAliveTime = xnioWorker.getOption(Options.WORKER_TASK_KEEPALIVE);
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
package cn.hippo4j.config.springboot.starter.config;
18+
package cn.hippo4j.common.api;
1919

2020
import lombok.AllArgsConstructor;
2121
import lombok.Data;
@@ -27,7 +27,7 @@
2727
@Data
2828
@NoArgsConstructor
2929
@AllArgsConstructor
30-
public class DynamicThreadPoolNotifyProperties {
30+
public class ExecutorNotifyProperties {
3131

3232
/**
3333
* Thread pool run alarm interval. unit: s
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package cn.hippo4j.common.api;
19+
20+
/**
21+
* Interface for thread pool configuration.
22+
*/
23+
public interface IExecutorProperties {
24+
25+
/**
26+
* Thread pool id
27+
*/
28+
String getThreadPoolId();
29+
30+
/**
31+
* Core pool size
32+
*/
33+
Integer getCorePoolSize();
34+
35+
/**
36+
* Maximum pool size
37+
*/
38+
Integer getMaximumPoolSize();
39+
40+
/**
41+
* Keep alive time
42+
*/
43+
Long getKeepAliveTime();
44+
45+
/**
46+
* Notify configs
47+
*/
48+
ExecutorNotifyProperties getNotify();
49+
}

hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolParameter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public interface ThreadPoolParameter {
7676
*
7777
* @return
7878
*/
79-
Integer getKeepAliveTime();
79+
Long getKeepAliveTime();
8080

8181
/**
8282
* Get execute time out

hippo4j-common/src/main/java/cn/hippo4j/common/model/ThreadPoolParameterInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public class ThreadPoolParameterInfo implements ThreadPoolParameter, Serializabl
9494
/**
9595
* Keep alive time
9696
*/
97-
private Integer keepAliveTime;
97+
private Long keepAliveTime;
9898

9999
/**
100100
* Execute time out

hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/ContentUtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void assertGetPoolContent() {
2929
":1,\"capacityAlarm\":80,\"livenessAlarm\":80,\"allowCoreThreadTimeOut\":1}";
3030
ThreadPoolParameterInfo threadPoolParameterInfo = ThreadPoolParameterInfo.builder().tenantId("prescription")
3131
.itemId("dynamic-threadpool-example").tpId("message-consume").content("描述信息").corePoolSize(1)
32-
.maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513).executeTimeOut(null).rejectedType(4)
32+
.maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513L).executeTimeOut(null).rejectedType(4)
3333
.isAlarm(1).capacityAlarm(80).livenessAlarm(80).allowCoreThreadTimeOut(1).build();
3434
Assert.isTrue(testText.equals(ContentUtil.getPoolContent(threadPoolParameterInfo)));
3535
}

hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/Md5UtilTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public void assetGetTpContentMd5() {
5353
String md5Result = "ef5ea7cb47377fb9fb85a7125e76715d";
5454
ThreadPoolParameterInfo threadPoolParameterInfo = ThreadPoolParameterInfo.builder().tenantId("prescription")
5555
.itemId("dynamic-threadpool-example").tpId("message-consume").content("描述信息").corePoolSize(1)
56-
.maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513).executeTimeOut(null).rejectedType(4)
56+
.maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513L).executeTimeOut(null).rejectedType(4)
5757
.isAlarm(1).capacityAlarm(80).livenessAlarm(80).allowCoreThreadTimeOut(1).build();
5858
Assert.isTrue(md5Result.equals(Md5Util.getTpContentMd5(threadPoolParameterInfo)));
5959
}

hippo4j-common/src/test/java/cn/hippo4j/common/toolkit/SingletonTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public void assertSingletonGet() {
3030
Assert.assertEquals("hippo4j", Singleton.get("userName"));
3131
ThreadPoolParameterInfo threadPoolParameterInfo = ThreadPoolParameterInfo.builder().tenantId("prescription")
3232
.itemId("dynamic-threadpool-example").tpId("message-consume").content("描述信息").corePoolSize(1)
33-
.maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513).executeTimeOut(null).rejectedType(4)
33+
.maximumPoolSize(2).queueType(1).capacity(4).keepAliveTime(513L).executeTimeOut(null).rejectedType(4)
3434
.isAlarm(1).capacityAlarm(80).livenessAlarm(80).allowCoreThreadTimeOut(1).build();
3535
Singleton.put(threadPoolParameterInfo);
3636
Assert.assertEquals(threadPoolParameterInfo, Singleton.get(ThreadPoolParameterInfo.class.getName()));

hippo4j-message/src/main/java/cn/hippo4j/message/platform/LarkSendMessageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
*/
4545
@Slf4j
4646
@RequiredArgsConstructor
47-
public class LarkSendMessageHandler implements SendMessageHandler<AlarmNotifyRequest, ChangeParameterNotifyRequest> {
47+
public class LarkSendMessageHandler implements SendMessageHandler {
4848

4949
@Override
5050
public String getType() {

hippo4j-message/src/main/java/cn/hippo4j/message/platform/base/AbstractRobotSendMessageHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
/**
3333
* Abstract robot send message handler.
3434
*/
35-
public abstract class AbstractRobotSendMessageHandler implements SendMessageHandler<AlarmNotifyRequest, ChangeParameterNotifyRequest> {
35+
public abstract class AbstractRobotSendMessageHandler implements SendMessageHandler {
3636

3737
/**
3838
* Build message actual content.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package cn.hippo4j.message.request;
19+
20+
import cn.hippo4j.message.request.base.BaseNotifyRequest;
21+
import lombok.AllArgsConstructor;
22+
import lombok.Builder;
23+
import lombok.Data;
24+
import lombok.NoArgsConstructor;
25+
26+
/**
27+
* Change parameter notify request for web thread pool.
28+
*/
29+
@Data
30+
@Builder
31+
@NoArgsConstructor
32+
@AllArgsConstructor
33+
public class WebChangeParameterNotifyRequest extends BaseNotifyRequest {
34+
35+
private String active;
36+
37+
private String appName;
38+
39+
private String identify;
40+
41+
private Integer beforeCorePoolSize;
42+
43+
private Integer nowCorePoolSize;
44+
45+
private Integer beforeMaximumPoolSize;
46+
47+
private Integer nowMaximumPoolSize;
48+
49+
private Long beforeKeepAliveTime;
50+
51+
private Long nowKeepAliveTime;
52+
53+
}

hippo4j-message/src/main/java/cn/hippo4j/message/request/base/BaseNotifyRequest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package cn.hippo4j.message.request.base;
1919

2020
import cn.hippo4j.common.api.NotifyRequest;
21+
import cn.hippo4j.message.enums.NotifyTypeEnum;
2122
import lombok.Data;
2223

2324
/**

hippo4j-message/src/main/java/cn/hippo4j/message/service/Hippo4jBaseSendMessageService.java

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import cn.hippo4j.message.enums.NotifyTypeEnum;
2626
import cn.hippo4j.message.request.AlarmNotifyRequest;
2727
import cn.hippo4j.message.request.ChangeParameterNotifyRequest;
28+
import cn.hippo4j.message.request.WebChangeParameterNotifyRequest;
2829
import lombok.Getter;
2930
import lombok.RequiredArgsConstructor;
3031
import lombok.extern.slf4j.Slf4j;
@@ -53,11 +54,7 @@ public class Hippo4jBaseSendMessageService implements Hippo4jSendMessageService,
5354
@Override
5455
public void sendAlarmMessage(NotifyTypeEnum typeEnum, AlarmNotifyRequest alarmNotifyRequest) {
5556
String threadPoolId = alarmNotifyRequest.getThreadPoolId();
56-
String buildKey = new StringBuilder()
57-
.append(threadPoolId)
58-
.append("+")
59-
.append("ALARM")
60-
.toString();
57+
String buildKey = generateAlarmKey(threadPoolId);
6158
List<NotifyConfigDTO> notifyList = notifyConfigs.get(buildKey);
6259
if (CollectionUtil.isEmpty(notifyList)) {
6360
return;
@@ -82,11 +79,7 @@ public void sendAlarmMessage(NotifyTypeEnum typeEnum, AlarmNotifyRequest alarmNo
8279
@Override
8380
public void sendChangeMessage(ChangeParameterNotifyRequest changeParameterNotifyRequest) {
8481
String threadPoolId = changeParameterNotifyRequest.getThreadPoolId();
85-
String buildKey = new StringBuilder()
86-
.append(threadPoolId)
87-
.append("+")
88-
.append("CONFIG")
89-
.toString();
82+
String buildKey = generateConfigKey(threadPoolId);
9083
List<NotifyConfigDTO> notifyList = notifyConfigs.get(buildKey);
9184
if (CollectionUtil.isEmpty(notifyList)) {
9285
log.warn("[{}] Please configure alarm notification on the server.", threadPoolId);
@@ -106,6 +99,45 @@ public void sendChangeMessage(ChangeParameterNotifyRequest changeParameterNotify
10699
});
107100
}
108101

102+
@Override
103+
public void sendChangeMessage(WebChangeParameterNotifyRequest webChangeParameterNotifyRequest) {
104+
String threadPoolId = webChangeParameterNotifyRequest.getThreadPoolId();
105+
String buildKey = generateConfigKey(threadPoolId);
106+
List<NotifyConfigDTO> notifyList = notifyConfigs.get(buildKey);
107+
if (CollectionUtil.isEmpty(notifyList)) {
108+
log.warn("[{}] Please configure alarm notification on the server.", threadPoolId);
109+
return;
110+
}
111+
notifyList.forEach(each -> {
112+
try {
113+
SendMessageHandler messageHandler = sendMessageHandlers.get(each.getPlatform());
114+
if (messageHandler == null) {
115+
log.warn("[{}] Please configure alarm notification on the server.", threadPoolId);
116+
return;
117+
}
118+
messageHandler.sendWebChangeMessage(each, webChangeParameterNotifyRequest);
119+
} catch (Exception ex) {
120+
log.warn("Failed to send thread pool change notification. key: [{}]", threadPoolId, ex);
121+
}
122+
});
123+
}
124+
125+
private String generateConfigKey(String threadPoolId) {
126+
return new StringBuilder()
127+
.append(threadPoolId)
128+
.append("+")
129+
.append("CONFIG")
130+
.toString();
131+
}
132+
133+
private String generateAlarmKey(String threadPoolId) {
134+
return new StringBuilder()
135+
.append(threadPoolId)
136+
.append("+")
137+
.append("ALARM")
138+
.toString();
139+
}
140+
109141
/**
110142
* Is send alarm.
111143
*

hippo4j-message/src/main/java/cn/hippo4j/message/service/Hippo4jSendMessageService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import cn.hippo4j.message.enums.NotifyTypeEnum;
2121
import cn.hippo4j.message.request.AlarmNotifyRequest;
2222
import cn.hippo4j.message.request.ChangeParameterNotifyRequest;
23+
import cn.hippo4j.message.request.WebChangeParameterNotifyRequest;
2324

2425
/**
2526
* Hippo-4j send message service.
@@ -40,4 +41,11 @@ public interface Hippo4jSendMessageService {
4041
* @param changeParameterNotifyRequest change parameter notify request
4142
*/
4243
void sendChangeMessage(ChangeParameterNotifyRequest changeParameterNotifyRequest);
44+
45+
/**
46+
* Send web thread pool parameter change notification.
47+
*
48+
* @param webChangeParameterNotifyRequest change parameter notify request
49+
*/
50+
void sendChangeMessage(WebChangeParameterNotifyRequest webChangeParameterNotifyRequest);
4351
}

hippo4j-message/src/main/java/cn/hippo4j/message/service/SendMessageHandler.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919

2020
import cn.hippo4j.message.dto.NotifyConfigDTO;
2121
import cn.hippo4j.common.api.NotifyRequest;
22+
import cn.hippo4j.message.request.AlarmNotifyRequest;
23+
import cn.hippo4j.message.request.ChangeParameterNotifyRequest;
24+
import cn.hippo4j.message.request.WebChangeParameterNotifyRequest;
2225

2326
/**
2427
* Send message handler.
2528
*/
26-
public interface SendMessageHandler<T extends NotifyRequest, R extends NotifyRequest> {
29+
public interface SendMessageHandler {
2730

2831
/**
2932
* Get the message send type.
@@ -38,13 +41,23 @@ public interface SendMessageHandler<T extends NotifyRequest, R extends NotifyReq
3841
* @param notifyConfig notify config
3942
* @param alarmNotifyRequest alarm notify request
4043
*/
41-
void sendAlarmMessage(NotifyConfigDTO notifyConfig, T alarmNotifyRequest);
44+
void sendAlarmMessage(NotifyConfigDTO notifyConfig, AlarmNotifyRequest alarmNotifyRequest);
4245

4346
/**
4447
* Send change message.
4548
*
4649
* @param notifyConfig notify config
4750
* @param changeParameterNotifyRequest change parameter notify request
4851
*/
49-
void sendChangeMessage(NotifyConfigDTO notifyConfig, R changeParameterNotifyRequest);
52+
void sendChangeMessage(NotifyConfigDTO notifyConfig, ChangeParameterNotifyRequest changeParameterNotifyRequest);
53+
54+
/**
55+
* Send web change message.
56+
*
57+
* @param notifyConfig notify config
58+
* @param changeParameterNotifyRequest parameter notify request
59+
*/
60+
default void sendWebChangeMessage(NotifyConfigDTO notifyConfig, WebChangeParameterNotifyRequest changeParameterNotifyRequest) throws IllegalAccessException {
61+
throw new IllegalAccessException("Please implement this method before using it.");
62+
}
5063
}

0 commit comments

Comments
 (0)