You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2020/04/01 14:39:20 UTC

[dubbo] branch master updated: [feature] publish ThreadPoolExhaustedEvent when thread pool exhausted (#5958)

This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new cfee3f8  [feature] publish ThreadPoolExhaustedEvent when thread pool exhausted (#5958)
cfee3f8 is described below

commit cfee3f835ab6634600ff8f23b8fd313a98cb05a9
Author: seasidesky <62...@users.noreply.github.com>
AuthorDate: Wed Apr 1 22:39:02 2020 +0800

    [feature] publish ThreadPoolExhaustedEvent when thread pool exhausted (#5958)
    
    fix #5957
---
 .../threadpool/event/ThreadPoolExhaustedEvent.java | 38 ++++++++++++
 .../threadpool/support/AbortPolicyWithReport.java  | 11 ++++
 .../ThreadPoolExhaustedEventListenerTest.java      | 69 ++++++++++++++++++++++
 .../event/ThreadPoolExhaustedEventTest.java        | 39 ++++++++++++
 4 files changed, 157 insertions(+)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedEvent.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedEvent.java
new file mode 100644
index 0000000..e29c8a3
--- /dev/null
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedEvent.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.dubbo.common.threadpool.event;
+
+import org.apache.dubbo.event.Event;
+
+/**
+ * An {@link Event Dubbo event} when the Dubbo thread pool is exhausted.
+ *
+ * @see Event
+ */
+public class ThreadPoolExhaustedEvent extends Event {
+
+    final String msg;
+
+    public ThreadPoolExhaustedEvent(Object source, String msg) {
+        super(source);
+        this.msg = msg;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+}
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 0872b5b..c6865eb 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
@@ -29,7 +29,9 @@ import java.util.concurrent.ThreadPoolExecutor;
 import org.apache.dubbo.common.URL;
 import org.apache.dubbo.common.logger.Logger;
 import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.threadpool.event.ThreadPoolExhaustedEvent;
 import org.apache.dubbo.common.utils.JVMUtil;
+import org.apache.dubbo.event.EventDispatcher;
 
 import static org.apache.dubbo.common.constants.CommonConstants.DUMP_DIRECTORY;
 
@@ -76,9 +78,18 @@ public class AbortPolicyWithReport extends ThreadPoolExecutor.AbortPolicy {
             url.getProtocol(), url.getIp(), url.getPort());
         logger.warn(msg);
         dumpJStack();
+        dispatchThreadPoolExhaustedEvent(msg);
         throw new RejectedExecutionException(msg);
     }
 
+    /**
+     * dispatch ThreadPoolExhaustedEvent
+     * @param msg
+     */
+    public void dispatchThreadPoolExhaustedEvent(String msg) {
+        EventDispatcher.getDefaultExtension().dispatch(new ThreadPoolExhaustedEvent(this, msg));
+    }
+
     private void dumpJStack() {
         long now = System.currentTimeMillis();
 
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedEventListenerTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedEventListenerTest.java
new file mode 100644
index 0000000..3d57269
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedEventListenerTest.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.dubbo.common.threadpool.event;
+
+import org.apache.dubbo.event.*;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * {@link ThreadPoolExhaustedEvent} Test
+ */
+public class ThreadPoolExhaustedEventListenerTest {
+
+    private EventDispatcher eventDispatcher;
+
+    private ThreadPoolExhaustedEventListenerTest.MyGenericEventListener listener;
+
+    @BeforeEach
+    public void init() {
+        this.listener = new ThreadPoolExhaustedEventListenerTest.MyGenericEventListener();
+        this.eventDispatcher = EventDispatcher.getDefaultExtension();
+        this.eventDispatcher.addEventListener(listener);
+    }
+
+    @AfterEach
+    public void destroy() {
+        this.eventDispatcher.removeAllEventListeners();
+    }
+
+    @Test
+    public void testOnEvent() {
+        String msg = "Thread pool is EXHAUSTED! Thread Name: DubboServerHandler-127.0.0.1:12345, Pool Size: 1 (active: 0, core: 1, max: 1, largest: 1), Task: 6 (completed: 6), Executor status:(isShutdown:false, isTerminated:false, isTerminating:false), in dubbo://127.0.0.1:12345!, dubbo version: 2.7.3, current host: 127.0.0.1";
+        ThreadPoolExhaustedEvent exhaustedEvent = new ThreadPoolExhaustedEvent(this, msg);
+        eventDispatcher.dispatch(exhaustedEvent);
+        assertEquals(exhaustedEvent, listener.getThreadPoolExhaustedEvent());
+        assertEquals(this, listener.getThreadPoolExhaustedEvent().getSource());
+    }
+
+    class MyGenericEventListener implements EventListener<ThreadPoolExhaustedEvent> {
+
+        private ThreadPoolExhaustedEvent threadPoolExhaustedEvent;
+
+        @Override
+        public void onEvent(ThreadPoolExhaustedEvent event) {
+            this.threadPoolExhaustedEvent = event;
+        }
+
+        public ThreadPoolExhaustedEvent getThreadPoolExhaustedEvent() {
+            return threadPoolExhaustedEvent;
+        }
+    }
+}
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedEventTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedEventTest.java
new file mode 100644
index 0000000..72a751d
--- /dev/null
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/threadpool/event/ThreadPoolExhaustedEventTest.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.dubbo.common.threadpool.event;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ *  {@link ThreadPoolExhaustedEvent} Test
+ */
+public class ThreadPoolExhaustedEventTest {
+
+    @Test
+    public void test() {
+        long timestamp = System.currentTimeMillis();
+        String msg = "Thread pool is EXHAUSTED! Thread Name: DubboServerHandler-127.0.0.1:12345, Pool Size: 1 (active: 0, core: 1, max: 1, largest: 1), Task: 6 (completed: 6), Executor status:(isShutdown:false, isTerminated:false, isTerminating:false), in dubbo://127.0.0.1:12345!, dubbo version: 2.7.3, current host: 127.0.0.1";
+        ThreadPoolExhaustedEvent event = new ThreadPoolExhaustedEvent(this, msg);
+
+        assertEquals(this, event.getSource());
+        assertEquals(msg, event.getMsg());
+        assertTrue(event.getTimestamp() >= timestamp);
+    }
+}