You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/09/17 09:36:19 UTC

[GitHub] [iotdb] neuyilan commented on a change in pull request #3886: [cluster-refactor] refactor client pool

neuyilan commented on a change in pull request #3886:
URL: https://github.com/apache/iotdb/pull/3886#discussion_r710828756



##########
File path: cluster/src/main/java/org/apache/iotdb/cluster/client/ClientManager.java
##########
@@ -0,0 +1,199 @@
+/*
+ * 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.iotdb.cluster.client;
+
+import org.apache.iotdb.cluster.ClusterIoTDB;
+import org.apache.iotdb.cluster.rpc.thrift.Node;
+import org.apache.iotdb.cluster.rpc.thrift.RaftService;
+
+import com.google.common.collect.Maps;
+import com.sun.istack.Nullable;
+import org.apache.commons.pool2.KeyedObjectPool;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+
+/**
+ * One should borrow the reusable client from this class and return the client after use. The
+ * underlying client pool is powered by Apache Commons Pool. The class provided 3 default pool group
+ * according to current usage: ClusterClients, DataGroupClients, MetaGroupClients.
+ *
+ * <p>ClusterClients implement the data query and insert interfaces such as query and non-query
+ * request
+ *
+ * <p>DataGroupClient implement the data group raft rpc interfaces such as appendEntry,
+ * appendEntries, sendHeartbeat, etc.
+ *
+ * <p>MetaGroupClient implement the meta group raft rpc interfaces such as appendEntry,
+ * appendEntries, sendHeartbeat, etc.
+ *
+ * <p>TODO: We can refine the client structure by reorg the interfaces defined in cluster-thrift.
+ */
+public class ClientManager implements IClientManager {
+
+  private static final Logger logger = LoggerFactory.getLogger(ClusterIoTDB.class);

Review comment:
       ```suggestion
     private static final Logger logger = LoggerFactory.getLogger(ClientManager.class);
   ```

##########
File path: cluster/src/main/java/org/apache/iotdb/cluster/client/ClientManager.java
##########
@@ -0,0 +1,180 @@
+package org.apache.iotdb.cluster.client;
+
+import org.apache.iotdb.cluster.ClusterIoTDB;
+import org.apache.iotdb.cluster.rpc.thrift.Node;
+import org.apache.iotdb.cluster.rpc.thrift.RaftService;
+
+import com.google.common.collect.Maps;
+import org.apache.commons.pool2.KeyedObjectPool;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+
+/**
+ * One should borrow the reusable client from this class and return the client after use. The
+ * underlying client pool is powered by Apache Commons Pool. The class provided 3 default pool group
+ * according to current usage: ClusterClients, DataGroupClients, MetaGroupClients.
+ *
+ * <p>ClusterClients implement the data query and insert interfaces such as query and non-query
+ * request
+ *
+ * <p>DataGroupClient implement the data group raft rpc interfaces such as appendEntry,
+ * appendEntries, sendHeartbeat, etc.
+ *
+ * <p>MetaGroupClient implement the meta group raft rpc interfaces such as appendEntry,
+ * appendEntries, sendHeartbeat, etc.
+ *
+ * <p>TODO: We can refine the client structure by reorg the interfaces defined in cluster-thrift.
+ */
+public class ClientManager implements IClientManager {
+
+  private static final Logger logger = LoggerFactory.getLogger(ClusterIoTDB.class);
+
+  private Map<ClientCategory, KeyedObjectPool<Node, RaftService.AsyncClient>> asyncClientPoolMap;
+  private Map<ClientCategory, KeyedObjectPool<Node, RaftService.Client>> syncClientPoolMap;
+  private ClientPoolFactory clientPoolFactory;
+
+  public enum Type {
+    ClusterClient,
+    DataGroupClient,
+    MetaGroupClient
+  }
+
+  public ClientManager(boolean isAsyncMode, Type type) {
+    clientPoolFactory = new ClientPoolFactory();
+    clientPoolFactory.setClientManager(this);
+    if (isAsyncMode) {
+      asyncClientPoolMap = Maps.newHashMap();
+      constructAsyncClientMap(type);
+    } else {
+      syncClientPoolMap = Maps.newHashMap();
+      constructSyncClientMap(type);
+    }
+  }
+
+  private void constructAsyncClientMap(Type type) {
+    switch (type) {
+      case ClusterClient:
+        asyncClientPoolMap.put(
+            ClientCategory.DATA, clientPoolFactory.createAsyncDataPool(ClientCategory.DATA));
+        break;
+      case MetaGroupClient:
+        asyncClientPoolMap.put(
+            ClientCategory.META,
+            clientPoolFactory.createAsyncMetaPool(ClientCategory.META));
+        asyncClientPoolMap.put(
+            ClientCategory.META_HEARTBEAT,
+            clientPoolFactory.createAsyncMetaPool(ClientCategory.META_HEARTBEAT));
+        break;
+      case DataGroupClient:
+        asyncClientPoolMap.put(
+            ClientCategory.DATA, clientPoolFactory.createAsyncDataPool(ClientCategory.DATA));
+        asyncClientPoolMap.put(
+            ClientCategory.DATA_HEARTBEAT,
+            clientPoolFactory.createAsyncDataPool(ClientCategory.DATA_HEARTBEAT));
+        asyncClientPoolMap.put(
+            ClientCategory.SINGLE_MASTER, clientPoolFactory.createSingleManagerAsyncDataPool());
+        break;
+      default:
+        logger.warn("unsupported ClientManager type: {}", type);
+        break;
+    }
+  }
+
+  private void constructSyncClientMap(Type type) {
+    switch (type) {
+      case ClusterClient:
+        syncClientPoolMap.put(
+            ClientCategory.DATA, clientPoolFactory.createSyncDataPool(ClientCategory.DATA));

Review comment:
       +1,
   The same as `constructAsyncClientMap`




-- 
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: reviews-unsubscribe@iotdb.apache.org

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