This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.1
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.1 by this push:
new 2ff4274cc9 add thread pool exhausted listener config (#10724)
2ff4274cc9 is described below
commit 2ff4274cc914f61034e95931af0362e249247dc7
Author: huazhongming <[email protected]>
AuthorDate: Tue Oct 11 10:34:17 2022 +0800
add thread pool exhausted listener config (#10724)
Signed-off-by: crazyhzm <[email protected]>
Signed-off-by: crazyhzm <[email protected]>
---
.../dubbo/common/constants/CommonConstants.java | 2 ++
.../event/ThreadPoolExhaustedListener.java | 4 +++
.../threadpool/support/AbortPolicyWithReport.java | 37 ++++++++++++++++------
.../org/apache/dubbo/config/ProtocolConfig.java | 16 ++++++++++
.../src/main/resources/META-INF/compat/dubbo.xsd | 5 +++
.../src/main/resources/META-INF/dubbo.xsd | 5 +++
6 files changed, 60 insertions(+), 9 deletions(-)
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
index aad6f49ca0..3bbd55cc86 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java
@@ -122,6 +122,8 @@ public interface CommonConstants {
String CORE_THREADS_KEY = "corethreads";
+ String THREAD_POOL_EXHAUSTED_LISTENERS_KEY =
"thread-pool-exhausted-listeners";
+
String THREADS_KEY = "threads";
String QUEUES_KEY = "queues";
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedListener.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedListener.java
index 822b69fb9d..fd8d5b9f8b 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedListener.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedListener.java
@@ -16,6 +16,10 @@
*/
package org.apache.dubbo.common.threadpool.event;
+import org.apache.dubbo.common.extension.ExtensionScope;
+import org.apache.dubbo.common.extension.SPI;
+
+@SPI(scope = ExtensionScope.FRAMEWORK)
public interface ThreadPoolExhaustedListener {
/**
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java
index 43520f9fa7..19a1f967d9 100644
---
a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java
+++
b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/support/AbortPolicyWithReport.java
@@ -18,6 +18,7 @@
package org.apache.dubbo.common.threadpool.support;
import org.apache.dubbo.common.URL;
+import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.threadpool.event.ThreadPoolExhaustedEvent;
@@ -25,6 +26,7 @@ import
org.apache.dubbo.common.threadpool.event.ThreadPoolExhaustedListener;
import org.apache.dubbo.common.utils.ConcurrentHashSet;
import org.apache.dubbo.common.utils.JVMUtil;
import org.apache.dubbo.common.utils.StringUtils;
+import org.apache.dubbo.rpc.model.FrameworkModel;
import java.io.File;
import java.io.FileOutputStream;
@@ -38,9 +40,11 @@ import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import static java.lang.String.format;
+import static
org.apache.dubbo.common.constants.CommonConstants.COMMA_SEPARATOR_CHAR;
import static org.apache.dubbo.common.constants.CommonConstants.DUMP_DIRECTORY;
import static org.apache.dubbo.common.constants.CommonConstants.OS_NAME_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.OS_WIN_PREFIX;
+import static
org.apache.dubbo.common.constants.CommonConstants.THREAD_POOL_EXHAUSTED_LISTENERS_KEY;
import static
org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_THREAD_POOL_EXHAUSTED;
/**
@@ -72,18 +76,32 @@ public class AbortPolicyWithReport extends
ThreadPoolExecutor.AbortPolicy {
public AbortPolicyWithReport(String threadName, URL url) {
this.threadName = threadName;
this.url = url;
+
+ String threadPoolExhaustedListeners =
+ url.getParameter(THREAD_POOL_EXHAUSTED_LISTENERS_KEY, (String)
url.getAttribute(THREAD_POOL_EXHAUSTED_LISTENERS_KEY));
+
+ Set<String> listenerKeys =
StringUtils.splitToSet(threadPoolExhaustedListeners, COMMA_SEPARATOR_CHAR,
true);
+
+ FrameworkModel frameworkModel = url.getOrDefaultFrameworkModel();
+ ExtensionLoader<ThreadPoolExhaustedListener> extensionLoader =
frameworkModel.getExtensionLoader(ThreadPoolExhaustedListener.class);
+ listenerKeys.forEach(key -> {
+ if (extensionLoader.hasExtension(key)) {
+
addThreadPoolExhaustedEventListener(extensionLoader.getExtension(key));
+ }
+ });
+
}
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
String msg = String.format("Thread pool is EXHAUSTED!" +
- " Thread Name: %s, Pool Size: %d (active: %d, core:
%d, max: %d, largest: %d)," +
- " Task: %d (completed: %d)," +
- " Executor status:(isShutdown:%s, isTerminated:%s,
isTerminating:%s), in %s://%s:%d!",
- threadName, e.getPoolSize(), e.getActiveCount(),
e.getCorePoolSize(), e.getMaximumPoolSize(),
- e.getLargestPoolSize(),
- e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(),
e.isTerminated(), e.isTerminating(),
- url.getProtocol(), url.getIp(), url.getPort());
+ " Thread Name: %s, Pool Size: %d (active: %d, core: %d, max:
%d, largest: %d)," +
+ " Task: %d (completed: %d)," +
+ " Executor status:(isShutdown:%s, isTerminated:%s,
isTerminating:%s), in %s://%s:%d!",
+ threadName, e.getPoolSize(), e.getActiveCount(),
e.getCorePoolSize(), e.getMaximumPoolSize(),
+ e.getLargestPoolSize(),
+ e.getTaskCount(), e.getCompletedTaskCount(), e.isShutdown(),
e.isTerminated(), e.isTerminating(),
+ url.getProtocol(), url.getIp(), url.getPort());
// 0-1 - Thread pool is EXHAUSTED!
logger.warn(COMMON_THREAD_POOL_EXHAUSTED, "too much client requesting
provider", "", msg);
@@ -104,6 +122,7 @@ public class AbortPolicyWithReport extends
ThreadPoolExecutor.AbortPolicy {
/**
* dispatch ThreadPoolExhaustedEvent
+ *
* @param msg
*/
public void dispatchThreadPoolExhaustedEvent(String msg) {
@@ -140,7 +159,7 @@ public class AbortPolicyWithReport extends
ThreadPoolExecutor.AbortPolicy {
String dateStr = sdf.format(new Date());
//try-with-resources
try (FileOutputStream jStackStream = new FileOutputStream(
- new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr))) {
+ new File(dumpPath, "Dubbo_JStack.log" + "." + dateStr))) {
JVMUtil.jstack(jStackStream);
} catch (Throwable t) {
logger.error("dump jStack error", t);
@@ -165,7 +184,7 @@ public class AbortPolicyWithReport extends
ThreadPoolExecutor.AbortPolicy {
logger.info(format("Dubbo dump directory[%s] created",
dumpDirectory.getAbsolutePath()));
} else {
logger.warn(format("Dubbo dump directory[%s] can't be created,
use the 'user.home'[%s]",
- dumpDirectory.getAbsolutePath(), USER_HOME));
+ dumpDirectory.getAbsolutePath(), USER_HOME));
return USER_HOME;
}
}
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/config/ProtocolConfig.java
b/dubbo-common/src/main/java/org/apache/dubbo/config/ProtocolConfig.java
index 9ef6daf9bf..5821ac6442 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/ProtocolConfig.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/ProtocolConfig.java
@@ -24,6 +24,7 @@ import java.util.Map;
import static org.apache.dubbo.common.constants.CommonConstants.DUBBO_PROTOCOL;
import static
org.apache.dubbo.common.constants.CommonConstants.SSL_ENABLED_KEY;
+import static
org.apache.dubbo.common.constants.CommonConstants.THREAD_POOL_EXHAUSTED_LISTENERS_KEY;
/**
* ProtocolConfig
@@ -89,6 +90,12 @@ public class ProtocolConfig extends AbstractConfig {
*/
private Integer queues;
+
+ /**
+ * Thread pool exhausted listeners
+ */
+ private String threadPoolExhaustedListeners;
+
/**
* Max acceptable connections
*/
@@ -303,6 +310,15 @@ public class ProtocolConfig extends AbstractConfig {
this.threadname = threadname;
}
+ @Parameter(key = THREAD_POOL_EXHAUSTED_LISTENERS_KEY)
+ public String getThreadPoolExhaustedListeners() {
+ return threadPoolExhaustedListeners;
+ }
+
+ public void setThreadPoolExhaustedListeners(String
threadPoolExhaustedListeners) {
+ this.threadPoolExhaustedListeners = threadPoolExhaustedListeners;
+ }
+
public Integer getCorethreads() {
return corethreads;
}
diff --git
a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd
b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd
index eb92b4b11c..ca3a573d17 100644
---
a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd
+++
b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/compat/dubbo.xsd
@@ -1123,6 +1123,11 @@
<xsd:documentation><![CDATA[ The thread pool name.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
+ <xsd:attribute name="thread-pool-exhausted-listeners"
type="xsd:string">
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[ The thread pool exhausted
listeners. ]]></xsd:documentation>
+ </xsd:annotation>
+ </xsd:attribute>
<xsd:attribute name="threads" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The thread pool size.
]]></xsd:documentation>
diff --git
a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd
b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd
index b475c790f6..c840300cf8 100644
--- a/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd
+++ b/dubbo-config/dubbo-config-spring/src/main/resources/META-INF/dubbo.xsd
@@ -1299,6 +1299,11 @@
<xsd:documentation><![CDATA[ The thread pool name.
]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
+ <xsd:attribute name="thread-pool-exhausted-listeners"
type="xsd:string">
+ <xsd:annotation>
+ <xsd:documentation><![CDATA[ The thread pool exhausted
listeners. ]]></xsd:documentation>
+ </xsd:annotation>
+ </xsd:attribute>
<xsd:attribute name="threads" type="xsd:string">
<xsd:annotation>
<xsd:documentation><![CDATA[ The thread pool size.
]]></xsd:documentation>