SteveYurongSu commented on a change in pull request #4460:
URL: https://github.com/apache/iotdb/pull/4460#discussion_r762679625



##########
File path: 
influxdb-protocol/src/main/java/org/apache/iotdb/influxdb/session/InfluxDBSession.java
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.influxdb.session;
+
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.protocol.influxdb.rpc.thrift.*;
+import org.apache.iotdb.rpc.IoTDBConnectionException;
+import org.apache.iotdb.rpc.RpcTransportFactory;
+import org.apache.iotdb.rpc.RpcUtils;
+import org.apache.iotdb.rpc.StatementExecutionException;
+import org.apache.iotdb.session.Config;
+
+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.influxdb.InfluxDBException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.time.ZoneId;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Random;
+
+import static org.apache.iotdb.session.SessionConnection.MSG_RECONNECTION_FAIL;
+
+public class InfluxDBSession {
+  private static final Logger logger = 
LoggerFactory.getLogger(InfluxDBSession.class);
+
+  private TTransport transport;
+  private InfluxDBService.Iface client;
+  private List<EndPoint> endPointList = new ArrayList<>();
+  private long sessionId;
+
+  protected String username;
+  protected String password;
+  protected int fetchSize;
+  protected ZoneId zoneId;
+  protected EndPoint defaultEndPoint;
+  protected int thriftDefaultBufferSize;
+  protected int thriftMaxFrameSize;
+
+  private boolean isClosed = true;
+  protected boolean enableRPCCompression;
+  protected int connectionTimeoutInMs;
+
+  public InfluxDBSession(String host, int rpcPort, String username, String 
password) {
+    this(
+        host,
+        rpcPort,
+        username,
+        password,
+        Config.DEFAULT_FETCH_SIZE,
+        ZoneId.systemDefault(),
+        Config.DEFAULT_INITIAL_BUFFER_CAPACITY,
+        Config.DEFAULT_MAX_FRAME_SIZE);
+  }
+
+  public InfluxDBSession(
+      String host,
+      int rpcPort,
+      String username,
+      String password,
+      int fetchSize,
+      ZoneId zoneId,
+      int thriftDefaultBufferSize,
+      int thriftMaxFrameSize) {
+    this.defaultEndPoint = new EndPoint(host, rpcPort);
+    this.username = username;
+    this.password = password;
+    this.fetchSize = fetchSize;
+    this.zoneId = zoneId;
+    this.thriftDefaultBufferSize = thriftDefaultBufferSize;
+    this.thriftMaxFrameSize = thriftMaxFrameSize;
+  }
+
+  public synchronized void open() throws IoTDBConnectionException {
+    open(false, Config.DEFAULT_CONNECTION_TIMEOUT_MS);
+  }
+
+  public synchronized void open(boolean enableRPCCompression, int 
connectionTimeoutInMs)
+      throws IoTDBConnectionException {
+    if (!isClosed) {
+      return;
+    }
+    this.enableRPCCompression = enableRPCCompression;
+    this.connectionTimeoutInMs = connectionTimeoutInMs;
+    this.endPointList.add(defaultEndPoint);
+
+    init();
+    isClosed = false;
+  }
+
+  public void init() throws IoTDBConnectionException {
+    RpcTransportFactory.setDefaultBufferCapacity(thriftDefaultBufferSize);
+    RpcTransportFactory.setThriftMaxFrameSize(thriftMaxFrameSize);
+    try {
+      transport =
+          RpcTransportFactory.INSTANCE.getTransport(
+              // as there is a try-catch already, we do not need to use 
TSocket.wrap
+              defaultEndPoint.getIp(), defaultEndPoint.getPort(), 
connectionTimeoutInMs);
+      transport.open();
+    } catch (TTransportException e) {
+      throw new IoTDBConnectionException(e);
+    }
+
+    if 
(IoTDBDescriptor.getInstance().getConfig().isRpcThriftCompressionEnable()) {
+      client = new InfluxDBService.Client(new TCompactProtocol(transport));
+    } else {
+      client = new InfluxDBService.Client(new TBinaryProtocol(transport));
+    }
+    client = RpcUtils.newSynchronizedClient(client);
+
+    TSOpenSessionReq openReq = new TSOpenSessionReq();
+    openReq.setUsername(username);
+    openReq.setPassword(password);
+    openReq.setZoneId(zoneId.toString());
+
+    try {
+      TSOpenSessionResp openResp = client.openSession(openReq);
+
+      sessionId = openResp.getSessionId();
+
+    } catch (Exception e) {
+      transport.close();
+      throw new IoTDBConnectionException(e);
+    }
+    System.out.println(sessionId);

Review comment:
       ```suggestion
   ```




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to