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 2022/04/12 12:43:43 UTC

[GitHub] [iotdb] CRZbulabula commented on a diff in pull request #5488: [IOTDB-2871] Data node client to connect with config node

CRZbulabula commented on code in PR #5488:
URL: https://github.com/apache/iotdb/pull/5488#discussion_r848384990


##########
server/src/main/java/org/apache/iotdb/db/client/ConfigNodeClient.java:
##########
@@ -0,0 +1,315 @@
+/*
+ * 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.db.client;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.cluster.Endpoint;
+import org.apache.iotdb.commons.exception.BadNodeUrlException;
+import org.apache.iotdb.commons.utils.CommonUtils;
+import org.apache.iotdb.confignode.rpc.thrift.*;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.RpcTransportFactory;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.service.rpc.thrift.*;
+
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TCompactProtocol;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.TTransportException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Random;
+
+public class ConfigNodeClient {
+  private static final Logger logger = LoggerFactory.getLogger(ConfigNodeClient.class);
+
+  private static final int TIMEOUT_MS = 2000;
+
+  private static final int RETRY_NUM = 5;
+
+  public static final String MSG_RECONNECTION_FAIL =
+      "Fail to connect to any config node. Please check server it";
+
+  private ConfigIService.Iface client;
+
+  private TTransport transport;
+
+  private Endpoint configLeader;
+
+  private List<Endpoint> configNodes;
+
+  public ConfigNodeClient() throws BadNodeUrlException, IoTDBConnectionException {
+    // Read config nodes from configuration
+    configNodes =
+        CommonUtils.parseNodeUrls(IoTDBDescriptor.getInstance().getConfig().getConfigNodeUrls());
+    init();
+  }
+
+  public ConfigNodeClient(List<Endpoint> configNodes) throws IoTDBConnectionException {
+    this.configNodes = configNodes;
+    init();
+  }
+
+  public ConfigNodeClient(List<Endpoint> configNodes, Endpoint configLeader)
+      throws IoTDBConnectionException {
+    this.configNodes = configNodes;
+    this.configLeader = configLeader;
+    init();
+  }
+
+  public void init() throws IoTDBConnectionException {
+    reconnect();
+  }
+
+  public void connect(Endpoint endpoint) throws IoTDBConnectionException {
+    try {
+      transport =
+          RpcTransportFactory.INSTANCE.getTransport(
+              // as there is a try-catch already, we do not need to use TSocket.wrap
+              endpoint.getIp(), endpoint.getPort(), TIMEOUT_MS);
+      transport.open();
+    } catch (TTransportException e) {
+      throw new IoTDBConnectionException(e);
+    }
+
+    if (IoTDBDescriptor.getInstance().getConfig().isRpcThriftCompressionEnable()) {
+      client = new ConfigIService.Client(new TCompactProtocol(transport));
+    } else {
+      client = new ConfigIService.Client(new TBinaryProtocol(transport));
+    }
+  }
+
+  private void reconnect() throws IoTDBConnectionException {
+    if (configLeader != null) {
+      try {
+        connect(configLeader);
+        return;
+      } catch (IoTDBConnectionException e) {
+        logger.error("The current node may have been down {},try next node", configLeader);

Review Comment:
   logger.error might confuse user. How about logger.warn?



##########
server/src/main/java/org/apache/iotdb/db/client/ConfigNodeClient.java:
##########
@@ -0,0 +1,315 @@
+/*
+ * 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.db.client;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.cluster.Endpoint;
+import org.apache.iotdb.commons.exception.BadNodeUrlException;
+import org.apache.iotdb.commons.utils.CommonUtils;
+import org.apache.iotdb.confignode.rpc.thrift.*;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.RpcTransportFactory;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.service.rpc.thrift.*;
+
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TCompactProtocol;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.TTransportException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Random;
+
+public class ConfigNodeClient {
+  private static final Logger logger = LoggerFactory.getLogger(ConfigNodeClient.class);
+
+  private static final int TIMEOUT_MS = 2000;
+
+  private static final int RETRY_NUM = 5;
+
+  public static final String MSG_RECONNECTION_FAIL =
+      "Fail to connect to any config node. Please check server it";
+
+  private ConfigIService.Iface client;
+
+  private TTransport transport;
+
+  private Endpoint configLeader;
+
+  private List<Endpoint> configNodes;
+
+  public ConfigNodeClient() throws BadNodeUrlException, IoTDBConnectionException {
+    // Read config nodes from configuration
+    configNodes =
+        CommonUtils.parseNodeUrls(IoTDBDescriptor.getInstance().getConfig().getConfigNodeUrls());
+    init();
+  }
+
+  public ConfigNodeClient(List<Endpoint> configNodes) throws IoTDBConnectionException {
+    this.configNodes = configNodes;
+    init();
+  }
+
+  public ConfigNodeClient(List<Endpoint> configNodes, Endpoint configLeader)
+      throws IoTDBConnectionException {
+    this.configNodes = configNodes;
+    this.configLeader = configLeader;
+    init();
+  }
+
+  public void init() throws IoTDBConnectionException {
+    reconnect();
+  }
+
+  public void connect(Endpoint endpoint) throws IoTDBConnectionException {
+    try {
+      transport =
+          RpcTransportFactory.INSTANCE.getTransport(
+              // as there is a try-catch already, we do not need to use TSocket.wrap
+              endpoint.getIp(), endpoint.getPort(), TIMEOUT_MS);
+      transport.open();
+    } catch (TTransportException e) {
+      throw new IoTDBConnectionException(e);
+    }
+
+    if (IoTDBDescriptor.getInstance().getConfig().isRpcThriftCompressionEnable()) {
+      client = new ConfigIService.Client(new TCompactProtocol(transport));
+    } else {
+      client = new ConfigIService.Client(new TBinaryProtocol(transport));
+    }
+  }
+
+  private void reconnect() throws IoTDBConnectionException {
+    if (configLeader != null) {
+      try {
+        connect(configLeader);
+        return;
+      } catch (IoTDBConnectionException e) {
+        logger.error("The current node may have been down {},try next node", configLeader);
+        configLeader = null;
+      }
+    }
+
+    Random random = new Random();
+    if (transport != null) {
+      transport.close();
+    }
+    int currHostIndex = random.nextInt(configNodes.size());
+    int tryHostNum = 0;
+    for (int j = currHostIndex; j < configNodes.size(); j++) {
+      if (tryHostNum == configNodes.size()) {
+        break;
+      }
+      Endpoint tryEndpoint = configNodes.get(j);
+      if (j == configNodes.size() - 1) {
+        j = -1;
+      }
+      tryHostNum++;
+      try {
+        connect(tryEndpoint);
+        return;
+      } catch (IoTDBConnectionException e) {
+        logger.error("The current node may have been down {},try next node", tryEndpoint);
+      }
+    }
+    throw new IoTDBConnectionException(MSG_RECONNECTION_FAIL);
+  }
+
+  public void close() {
+    transport.close();
+  }
+
+  private boolean verifyNeedRedirect(TSStatus status) throws StatementExecutionException {
+    if (status.getCode() == TSStatusCode.NEED_REDIRECTION.getStatusCode()) {
+      configLeader =
+          new Endpoint(status.getRedirectNode().getIp(), status.getRedirectNode().getPort());

Review Comment:
   The leader of ConfigNodeGroup is null when election. PTAL~



##########
server/src/main/java/org/apache/iotdb/db/client/ConfigNodeClient.java:
##########
@@ -0,0 +1,315 @@
+/*
+ * 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.db.client;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.cluster.Endpoint;
+import org.apache.iotdb.commons.exception.BadNodeUrlException;
+import org.apache.iotdb.commons.utils.CommonUtils;
+import org.apache.iotdb.confignode.rpc.thrift.*;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.RpcTransportFactory;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.service.rpc.thrift.*;
+
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TCompactProtocol;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.TTransportException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Random;
+
+public class ConfigNodeClient {
+  private static final Logger logger = LoggerFactory.getLogger(ConfigNodeClient.class);
+
+  private static final int TIMEOUT_MS = 2000;
+
+  private static final int RETRY_NUM = 5;
+
+  public static final String MSG_RECONNECTION_FAIL =
+      "Fail to connect to any config node. Please check server it";
+
+  private ConfigIService.Iface client;
+
+  private TTransport transport;
+
+  private Endpoint configLeader;
+
+  private List<Endpoint> configNodes;
+
+  public ConfigNodeClient() throws BadNodeUrlException, IoTDBConnectionException {
+    // Read config nodes from configuration
+    configNodes =
+        CommonUtils.parseNodeUrls(IoTDBDescriptor.getInstance().getConfig().getConfigNodeUrls());
+    init();
+  }
+
+  public ConfigNodeClient(List<Endpoint> configNodes) throws IoTDBConnectionException {
+    this.configNodes = configNodes;
+    init();
+  }
+
+  public ConfigNodeClient(List<Endpoint> configNodes, Endpoint configLeader)
+      throws IoTDBConnectionException {
+    this.configNodes = configNodes;
+    this.configLeader = configLeader;
+    init();
+  }
+
+  public void init() throws IoTDBConnectionException {
+    reconnect();
+  }
+
+  public void connect(Endpoint endpoint) throws IoTDBConnectionException {
+    try {
+      transport =
+          RpcTransportFactory.INSTANCE.getTransport(
+              // as there is a try-catch already, we do not need to use TSocket.wrap
+              endpoint.getIp(), endpoint.getPort(), TIMEOUT_MS);
+      transport.open();
+    } catch (TTransportException e) {
+      throw new IoTDBConnectionException(e);
+    }
+
+    if (IoTDBDescriptor.getInstance().getConfig().isRpcThriftCompressionEnable()) {
+      client = new ConfigIService.Client(new TCompactProtocol(transport));
+    } else {
+      client = new ConfigIService.Client(new TBinaryProtocol(transport));
+    }
+  }
+
+  private void reconnect() throws IoTDBConnectionException {
+    if (configLeader != null) {
+      try {
+        connect(configLeader);
+        return;
+      } catch (IoTDBConnectionException e) {
+        logger.error("The current node may have been down {},try next node", configLeader);
+        configLeader = null;
+      }
+    }
+
+    Random random = new Random();
+    if (transport != null) {
+      transport.close();
+    }
+    int currHostIndex = random.nextInt(configNodes.size());
+    int tryHostNum = 0;
+    for (int j = currHostIndex; j < configNodes.size(); j++) {
+      if (tryHostNum == configNodes.size()) {
+        break;
+      }
+      Endpoint tryEndpoint = configNodes.get(j);
+      if (j == configNodes.size() - 1) {
+        j = -1;
+      }
+      tryHostNum++;
+      try {
+        connect(tryEndpoint);
+        return;
+      } catch (IoTDBConnectionException e) {
+        logger.error("The current node may have been down {},try next node", tryEndpoint);

Review Comment:
   logger.error might confuse user. How about logger.warn?



##########
server/src/main/java/org/apache/iotdb/db/client/ConfigNodeClient.java:
##########
@@ -0,0 +1,315 @@
+/*
+ * 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.db.client;
+
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.cluster.Endpoint;
+import org.apache.iotdb.commons.exception.BadNodeUrlException;
+import org.apache.iotdb.commons.utils.CommonUtils;
+import org.apache.iotdb.confignode.rpc.thrift.*;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.RpcTransportFactory;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.rpc.TSStatusCode;
+import org.apache.iotdb.service.rpc.thrift.*;
+
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.protocol.TCompactProtocol;
+import org.apache.thrift.transport.TTransport;
+import org.apache.thrift.transport.TTransportException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.List;
+import java.util.Random;
+
+public class ConfigNodeClient {
+  private static final Logger logger = LoggerFactory.getLogger(ConfigNodeClient.class);
+
+  private static final int TIMEOUT_MS = 2000;
+
+  private static final int RETRY_NUM = 5;
+
+  public static final String MSG_RECONNECTION_FAIL =
+      "Fail to connect to any config node. Please check server it";
+
+  private ConfigIService.Iface client;
+
+  private TTransport transport;
+
+  private Endpoint configLeader;
+
+  private List<Endpoint> configNodes;
+
+  public ConfigNodeClient() throws BadNodeUrlException, IoTDBConnectionException {
+    // Read config nodes from configuration
+    configNodes =
+        CommonUtils.parseNodeUrls(IoTDBDescriptor.getInstance().getConfig().getConfigNodeUrls());
+    init();
+  }
+
+  public ConfigNodeClient(List<Endpoint> configNodes) throws IoTDBConnectionException {
+    this.configNodes = configNodes;
+    init();
+  }
+
+  public ConfigNodeClient(List<Endpoint> configNodes, Endpoint configLeader)
+      throws IoTDBConnectionException {
+    this.configNodes = configNodes;
+    this.configLeader = configLeader;
+    init();
+  }
+
+  public void init() throws IoTDBConnectionException {
+    reconnect();
+  }
+
+  public void connect(Endpoint endpoint) throws IoTDBConnectionException {
+    try {
+      transport =
+          RpcTransportFactory.INSTANCE.getTransport(
+              // as there is a try-catch already, we do not need to use TSocket.wrap
+              endpoint.getIp(), endpoint.getPort(), TIMEOUT_MS);
+      transport.open();
+    } catch (TTransportException e) {
+      throw new IoTDBConnectionException(e);
+    }
+
+    if (IoTDBDescriptor.getInstance().getConfig().isRpcThriftCompressionEnable()) {
+      client = new ConfigIService.Client(new TCompactProtocol(transport));
+    } else {
+      client = new ConfigIService.Client(new TBinaryProtocol(transport));
+    }
+  }
+
+  private void reconnect() throws IoTDBConnectionException {
+    if (configLeader != null) {
+      try {
+        connect(configLeader);
+        return;
+      } catch (IoTDBConnectionException e) {
+        logger.error("The current node may have been down {},try next node", configLeader);
+        configLeader = null;
+      }
+    }
+
+    Random random = new Random();
+    if (transport != null) {
+      transport.close();
+    }
+    int currHostIndex = random.nextInt(configNodes.size());
+    int tryHostNum = 0;
+    for (int j = currHostIndex; j < configNodes.size(); j++) {
+      if (tryHostNum == configNodes.size()) {
+        break;
+      }
+      Endpoint tryEndpoint = configNodes.get(j);
+      if (j == configNodes.size() - 1) {
+        j = -1;
+      }
+      tryHostNum++;
+      try {
+        connect(tryEndpoint);
+        return;
+      } catch (IoTDBConnectionException e) {
+        logger.error("The current node may have been down {},try next node", tryEndpoint);
+      }
+    }
+    throw new IoTDBConnectionException(MSG_RECONNECTION_FAIL);
+  }
+
+  public void close() {
+    transport.close();
+  }
+
+  private boolean verifyNeedRedirect(TSStatus status) throws StatementExecutionException {
+    if (status.getCode() == TSStatusCode.NEED_REDIRECTION.getStatusCode()) {

Review Comment:
   I set TSStatus.message when its TSStatusCode is NEED_REDIRECTION,  which you can use in logger



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