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 2023/02/02 07:26:57 UTC

[dubbo] branch 3.1 updated: Fix ThreadLocal Remove (#11443)

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

liujun 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 fe14026f68 Fix ThreadLocal Remove (#11443)
fe14026f68 is described below

commit fe14026f684feaa981e393da427e654732bac888
Author: Albumen Kevin <jh...@gmail.com>
AuthorDate: Thu Feb 2 15:26:14 2023 +0800

    Fix ThreadLocal Remove (#11443)
---
 .../threadpool/serial/SerializingExecutor.java     | 12 +--
 .../transport/dispatcher/ChannelEventRunnable.java | 91 +++++++++++-----------
 2 files changed, 53 insertions(+), 50 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/serial/SerializingExecutor.java b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/serial/SerializingExecutor.java
index 5c5327beff..5769c1296a 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/serial/SerializingExecutor.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/serial/SerializingExecutor.java
@@ -16,15 +16,15 @@
 
 package org.apache.dubbo.common.threadpool.serial;
 
-import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
-import org.apache.dubbo.common.logger.LoggerFactory;
-import org.apache.dubbo.common.threadlocal.InternalThreadLocal;
-
 import java.util.Queue;
 import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.concurrent.Executor;
 import java.util.concurrent.atomic.AtomicBoolean;
 
+import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
+import org.apache.dubbo.common.logger.LoggerFactory;
+import org.apache.dubbo.common.threadlocal.InternalThreadLocalMap;
+
 import static org.apache.dubbo.common.constants.LoggerCodeConstants.COMMON_ERROR_RUN_THREAD_TASK;
 
 /**
@@ -97,13 +97,13 @@ public final class SerializingExecutor implements Executor, Runnable {
         Runnable r;
         try {
             while ((r = runQueue.poll()) != null) {
+                InternalThreadLocalMap internalThreadLocalMap = InternalThreadLocalMap.getAndRemove();
                 try {
-                    InternalThreadLocal.removeAll();
                     r.run();
                 } catch (RuntimeException e) {
                     LOGGER.error(COMMON_ERROR_RUN_THREAD_TASK, "", "", "Exception while executing runnable " + r, e);
                 } finally {
-                    InternalThreadLocal.removeAll();
+                    InternalThreadLocalMap.set(internalThreadLocalMap);
                 }
             }
         } finally {
diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/ChannelEventRunnable.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/ChannelEventRunnable.java
index a250d05bf7..4bb558c4cd 100644
--- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/ChannelEventRunnable.java
+++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/ChannelEventRunnable.java
@@ -18,7 +18,7 @@ package org.apache.dubbo.remoting.transport.dispatcher;
 
 import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
 import org.apache.dubbo.common.logger.LoggerFactory;
-import org.apache.dubbo.common.threadlocal.InternalThreadLocal;
+import org.apache.dubbo.common.threadlocal.InternalThreadLocalMap;
 import org.apache.dubbo.remoting.Channel;
 import org.apache.dubbo.remoting.ChannelHandler;
 
@@ -55,51 +55,54 @@ public class ChannelEventRunnable implements Runnable {
 
     @Override
     public void run() {
-        InternalThreadLocal.removeAll();
-        if (state == ChannelState.RECEIVED) {
-            try {
-                handler.received(channel, message);
-            } catch (Exception e) {
-                logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "ChannelEventRunnable handle " + state + " operation error, channel is " + channel
-                    + ", message is " + message, e);
-            }
-        } else {
-            switch (state) {
-                case CONNECTED:
-                    try {
-                        handler.connected(channel);
-                    } catch (Exception e) {
-                        logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "ChannelEventRunnable handle " + state + " operation error, channel is " + channel, e);
-                    }
-                    break;
-                case DISCONNECTED:
-                    try {
-                        handler.disconnected(channel);
-                    } catch (Exception e) {
-                        logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "ChannelEventRunnable handle " + state + " operation error, channel is " + channel, e);
-                    }
-                    break;
-                case SENT:
-                    try {
-                        handler.sent(channel, message);
-                    } catch (Exception e) {
-                        logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "ChannelEventRunnable handle " + state + " operation error, channel is " + channel
-                            + ", message is " + message, e);
-                    }
-                    break;
-                case CAUGHT:
-                    try {
-                        handler.caught(channel, exception);
-                    } catch (Exception e) {
-                        logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "ChannelEventRunnable handle " + state + " operation error, channel is " + channel
-                            + ", message is: " + message + ", exception is " + exception, e);
-                    }
-                    break;
-                default:
-                    logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "unknown state: " + state + ", message is " + message);
+        InternalThreadLocalMap internalThreadLocalMap = InternalThreadLocalMap.getAndRemove();
+        try {
+            if (state == ChannelState.RECEIVED) {
+                try {
+                    handler.received(channel, message);
+                } catch (Exception e) {
+                    logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "ChannelEventRunnable handle " + state + " operation error, channel is " + channel
+                        + ", message is " + message, e);
+                }
+            } else {
+                switch (state) {
+                    case CONNECTED:
+                        try {
+                            handler.connected(channel);
+                        } catch (Exception e) {
+                            logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "ChannelEventRunnable handle " + state + " operation error, channel is " + channel, e);
+                        }
+                        break;
+                    case DISCONNECTED:
+                        try {
+                            handler.disconnected(channel);
+                        } catch (Exception e) {
+                            logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "ChannelEventRunnable handle " + state + " operation error, channel is " + channel, e);
+                        }
+                        break;
+                    case SENT:
+                        try {
+                            handler.sent(channel, message);
+                        } catch (Exception e) {
+                            logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "ChannelEventRunnable handle " + state + " operation error, channel is " + channel
+                                + ", message is " + message, e);
+                        }
+                        break;
+                    case CAUGHT:
+                        try {
+                            handler.caught(channel, exception);
+                        } catch (Exception e) {
+                            logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "ChannelEventRunnable handle " + state + " operation error, channel is " + channel
+                                + ", message is: " + message + ", exception is " + exception, e);
+                        }
+                        break;
+                    default:
+                        logger.warn(INTERNAL_ERROR, "unknown error in remoting module", "", "unknown state: " + state + ", message is " + message);
+                }
             }
+        } finally {
+            InternalThreadLocalMap.set(internalThreadLocalMap);
         }
-        InternalThreadLocal.removeAll();
     }
 
     /**