You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/05/06 15:53:54 UTC

[GitHub] [shardingsphere] terrymanu commented on a change in pull request #10260: Refactor ChannelThreadExecutorGroup to ConnectionThreadExecutorGroup

terrymanu commented on a change in pull request #10260:
URL: https://github.com/apache/shardingsphere/pull/10260#discussion_r627547543



##########
File path: shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-core/src/main/java/org/apache/shardingsphere/proxy/frontend/executor/ConnectionThreadExecutorGroup.java
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.shardingsphere.proxy.frontend.executor;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Connection thread executor group.
+ *
+ * <p>
+ * Manage the thread for each backend connection invoking.
+ * This ensure XA transaction framework processed by current thread id.
+ * </p>
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ConnectionThreadExecutorGroup {
+    
+    private static final ConnectionThreadExecutorGroup INSTANCE = new ConnectionThreadExecutorGroup();
+    
+    private final Map<Integer, ExecutorService> executorServices = new ConcurrentHashMap<>();
+    
+    /**
+     * Get connection thread executor group.
+     *
+     * @return channel thread executor group
+     */
+    public static ConnectionThreadExecutorGroup getInstance() {
+        return INSTANCE;
+    }
+    
+    /**
+     * Register connection.
+     *
+     * @param connectionId connection id
+     */
+    public void register(final int connectionId) {
+        executorServices.put(connectionId, Executors.newSingleThreadExecutor());
+    }
+    
+    /**
+     * Get executor service of connection.
+     *
+     * @param connectionId connection id
+     * @return executor service of current channel
+     */
+    public ExecutorService get(final int connectionId) {
+        return executorServices.get(connectionId);
+    }
+    
+    /**
+     * Unregister connection.
+     *
+     * @param connectionId connection id
+     */
+    public void unregisterAndAwaitTermination(final int connectionId) {
+        executorServices.computeIfPresent(connectionId, (unused, executorService) -> {
+            executorService.shutdown();
+            try {
+                executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
+            } catch (final InterruptedException ignored) {
+                Thread.currentThread().interrupt();
+            }
+            return null;
+        });

Review comment:
       We need to remove connectionId is unused.




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

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