You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2021/07/02 01:16:18 UTC

[GitHub] [dubbo] yugj commented on issue #8172: 升级2.7.12 provider下线造成consumer线程池Terminated

yugj commented on issue #8172:
URL: https://github.com/apache/dubbo/issues/8172#issuecomment-872644669


   临时通过重写ExecutorRepository重新创建线程池兼容了下
   ```
   import lombok.extern.slf4j.Slf4j;
   import org.apache.dubbo.common.URL;
   import org.apache.dubbo.common.extension.ExtensionLoader;
   import org.apache.dubbo.common.threadpool.ThreadPool;
   import org.apache.dubbo.common.threadpool.manager.ExecutorRepository;
   import org.apache.dubbo.common.threadpool.manager.Ring;
   import org.apache.dubbo.common.utils.ExecutorUtil;
   import org.apache.dubbo.common.utils.NamedThreadFactory;
   
   import java.util.Map;
   import java.util.concurrent.*;
   
   import static org.apache.dubbo.common.constants.CommonConstants.*;
   
   /**
    * 升级dubbo版本到2.7.12 处理如下bug
    * https://github.com/apache/dubbo/pull/7109
    * https://github.com/apache/dubbo/pull/6959
    * <p>
    * 但是造成服务蓝绿发布下线服务时候,dubbo全局线程池 被关闭,导致节点请求失败 问题
    * https://github.com/apache/dubbo/issues/8172
    *
    * 临时解决方案:
    * 基于全局线程池;线程池关闭后重新创建线程池;
    *
    * <p>
    * copy from org.apache.dubbo.common.threadpool.manager.DefaultExecutorRepository
    * <p>
    * Consider implementing {@code Licycle} to enable executors shutdown when the process stops.
    */
   
   /**
    * Consider implementing {@code Licycle} to enable executors shutdown when the process stops.
    */
   @Slf4j
   public class SisyphusExecutorRepository implements ExecutorRepository {
   
       private int DEFAULT_SCHEDULER_SIZE = Runtime.getRuntime().availableProcessors();
   
       private final ExecutorService SHARED_EXECUTOR = Executors.newCachedThreadPool(new NamedThreadFactory("DubboSharedHandler", true));
   
       private Ring<ScheduledExecutorService> scheduledExecutors = new Ring<>();
   
       private ScheduledExecutorService serviceExporterExecutor;
   
   
       private ConcurrentMap<String, ConcurrentMap<String, ExecutorService>> data = new ConcurrentHashMap<>();
   
       public SisyphusExecutorRepository() {
           for (int i = 0; i < DEFAULT_SCHEDULER_SIZE; i++) {
               ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Dubbo-framework-scheduler"));
               scheduledExecutors.addItem(scheduler);
           }
           serviceExporterExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Dubbo-exporter-scheduler"));
       }
   
       /**
        * Get called when the server or client instance initiating.
        *
        * @param url
        * @return
        */
       @Override
       public synchronized ExecutorService createExecutorIfAbsent(URL url) {
           Map<String, ExecutorService> executors = data.computeIfAbsent(EXECUTOR_SERVICE_COMPONENT_KEY, k -> new ConcurrentHashMap<>());
           //issue-7054:Consumer's executor is sharing globally, key=Integer.MAX_VALUE. Provider's executor is sharing by protocol.
   
           String consumerKey = getConsumerExecutorKey(url);
   
           String portKey = CONSUMER_SIDE.equalsIgnoreCase(url.getParameter(SIDE_KEY)) ? consumerKey : String.valueOf(url.getPort());
           ExecutorService executor = executors.computeIfAbsent(portKey, k -> createExecutor(url));
           // If executor has been shut down, create a new one
           if (executor.isShutdown() || executor.isTerminated()) {
               executors.remove(portKey);
               executor = createExecutor(url);
               executors.put(portKey, executor);
           }
           return executor;
       }
   
       @Override
       public ExecutorService getExecutor(URL url) {
           Map<String, ExecutorService> executors = data.get(EXECUTOR_SERVICE_COMPONENT_KEY);
           /**
            * It's guaranteed that this method is called after {@link #createExecutorIfAbsent(URL)}, so data should already
            * have Executor instances generated and stored.
            */
           if (executors == null) {
               log.warn("No available executors, this is not expected, framework should call createExecutorIfAbsent first " +
                       "before coming to here.");
               return null;
           }
   
           String consumerKey = getConsumerExecutorKey(url);
           String portKey = CONSUMER_SIDE.equalsIgnoreCase(url.getParameter(SIDE_KEY)) ? consumerKey : String.valueOf(url.getPort());
   
           ExecutorService executor = executors.get(portKey);
           if (executor == null || executor.isShutdown() || executor.isTerminated()) {
   
               long createStart = System.currentTimeMillis();
               executor = createExecutorIfAbsent(url);
               long createEnd = System.currentTimeMillis();
   
               log.warn("Executor for " + url + " is shutdown. recreate global executor, cost :{}", createEnd - createStart);
           }
   
           return executor;
       }
   
       @Override
       public void updateThreadpool(URL url, ExecutorService executor) {
           try {
               if (url.hasParameter(THREADS_KEY)
                       && executor instanceof ThreadPoolExecutor && !executor.isShutdown()) {
                   ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
                   int threads = url.getParameter(THREADS_KEY, 0);
                   int max = threadPoolExecutor.getMaximumPoolSize();
                   int core = threadPoolExecutor.getCorePoolSize();
                   if (threads > 0 && (threads != max || threads != core)) {
                       if (threads < core) {
                           threadPoolExecutor.setCorePoolSize(threads);
                           if (core == max) {
                               threadPoolExecutor.setMaximumPoolSize(threads);
                           }
                       } else {
                           threadPoolExecutor.setMaximumPoolSize(threads);
                           if (core == max) {
                               threadPoolExecutor.setCorePoolSize(threads);
                           }
                       }
                   }
               }
           } catch (Throwable t) {
               log.error(t.getMessage(), t);
           }
       }
   
       @Override
       public ScheduledExecutorService nextScheduledExecutor() {
           return scheduledExecutors.pollItem();
       }
   
       @Override
       public ScheduledExecutorService getServiceExporterExecutor() {
           return serviceExporterExecutor;
       }
   
       @Override
       public ExecutorService getSharedExecutor() {
           log.info("using shared executor");
           return SHARED_EXECUTOR;
       }
   
       @Override
       public void destroyAll() {
   
           log.info("destroy all");
   
           long start = System.currentTimeMillis();
           data.values().forEach(executors -> {
               if (executors != null) {
                   executors.values().forEach(executor -> {
                       if (executor != null && !executor.isShutdown()) {
                           ExecutorUtil.shutdownNow(executor, 100);
                       }
                   });
               }
           });
   
   
           data.clear();
           long end = System.currentTimeMillis();
   
           log.info("destroy all cost :" + (end - start));
   
       }
   
       private ExecutorService createExecutor(URL url) {
           return (ExecutorService) ExtensionLoader.getExtensionLoader(ThreadPool.class).getAdaptiveExtension().getExecutor(url);
       }
   
       private String getConsumerExecutorKey(URL url) {
   //        return url.getIp() + "-" + url.getPort();
           return "global";
       }
   
   }
   ```java
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org