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 2022/06/30 10:15:07 UTC

[GitHub] [dubbo] EarthChen commented on a diff in pull request #10210: [WIP] supports isolation thread pools

EarthChen commented on code in PR #10210:
URL: https://github.com/apache/dubbo/pull/10210#discussion_r910853600


##########
dubbo-common/src/main/java/org/apache/dubbo/common/threadpool/manager/IsolationExecutorRepository.java:
##########
@@ -0,0 +1,244 @@
+/*
+ * 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.manager;
+
+
+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.IsolationThreadPool;
+import org.apache.dubbo.common.utils.CollectionUtils;
+import org.apache.dubbo.common.utils.ExecutorUtil;
+import org.apache.dubbo.common.utils.MD5Utils;
+import org.apache.dubbo.rpc.model.ApplicationModel;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadPoolExecutor;
+
+import static org.apache.dubbo.common.constants.CommonConstants.THREADS_KEY;
+
+/**
+ * method level thread pools isolation
+ */
+public class IsolationExecutorRepository implements ExecutorRepository {
+
+    private static final Logger logger = LoggerFactory.getLogger(IsolationExecutorRepository.class);
+
+    public static final String NAME = "isolation";
+
+    private final ApplicationModel applicationModel;
+
+    private final FrameworkExecutorRepository frameworkExecutorRepository;
+
+    private final Map<String, ExecutorService> isolationThreadpool = new ConcurrentHashMap<>();
+
+    public IsolationExecutorRepository(URL url) {
+        this.applicationModel = url.getOrDefaultApplicationModel();
+        this.frameworkExecutorRepository = url.getOrDefaultApplicationModel().getFrameworkModel()
+            .getBeanFactory().getBean(FrameworkExecutorRepository.class);
+    }
+
+    /**
+     * init isolation thread pool
+     */
+    @Override
+    public ExecutorService createExecutorIfAbsent(URL url) {
+        String key = getIsolationThreadpoolKey(url);
+        ExecutorService executor = isolationThreadpool.computeIfAbsent(key, k -> createExecutor(url));
+        // if executor has been shut down, create a new one
+        if (executor.isShutdown() || executor.isTerminated()) {
+            isolationThreadpool.remove(key);
+            executor = createExecutor(url);
+            isolationThreadpool.put(key, executor);
+        }
+        return executor;
+    }
+
+    private ExecutorService createExecutor(URL url) {
+        return (ExecutorService) url.getOrDefaultFrameworkModel()
+            .getExtensionLoader(IsolationThreadPool.class)
+            .getAdaptiveExtension()
+            .getExecutor(url);
+    }
+
+    private String getIsolationThreadpoolKey(URL url) {
+        return new MD5Utils().getMd5(url.getProtocol() + url.getServiceKey() + url.getPort());
+    }
+
+    @Override
+    public ExecutorService getExecutor(URL url) {
+        String key = getIsolationThreadpoolKey(url);
+        ExecutorService executor = isolationThreadpool.get(key);
+
+        /*
+         * It's guaranteed that this method is called after {@link #createExecutorIfAbsent(URL)}, so isolationThreadpool should already
+         * have Executor instances generated and stored.
+         */
+        if (Objects.isNull(executor)) {
+            logger.warn("No available executors, this is not expected, framework should call createExecutorIfAbsent first " +
+                "before coming to here.");
+            return null;
+        }
+
+        if (executor.isShutdown() || executor.isTerminated()) {
+            isolationThreadpool.remove(key);
+            // Does not re-create a shutdown executor, use SHARED_EXECUTOR for downgrade.
+            executor = null;
+            logger.info("Executor for " + url + " is shutdown.");
+        }
+        if (Objects.isNull(executor)) {
+            return frameworkExecutorRepository.getSharedExecutor();
+        } else {
+            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) {
+            logger.error(t.getMessage(), t);
+        }
+    }
+
+    @Override
+    public ScheduledExecutorService getServiceExportExecutor() {
+        return null;
+    }
+
+    @Override
+    public void shutdownServiceExportExecutor() {
+
+    }
+
+    @Override
+    public ExecutorService getServiceReferExecutor() {

Review Comment:
   why null?



-- 
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