-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[IOTDB-1399]Add a session interface to connect multiple nodes #3434
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b18e41c
7b3040e
1ac28f0
571550f
f044347
3ab3bb1
ec7602b
6c4ab8f
a752ef9
908c0e6
da0e440
9b89424
9f5a66e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,9 @@ | |
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.time.ZoneId; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
|
|
||
| public class SessionConnection { | ||
|
|
||
|
|
@@ -74,6 +76,7 @@ public class SessionConnection { | |
| private long statementId; | ||
| private ZoneId zoneId; | ||
| private EndPoint endPoint; | ||
| private List<EndPoint> endPointList = new ArrayList<>(); | ||
| private boolean enableRedirect = false; | ||
|
|
||
| // TestOnly | ||
|
|
@@ -83,10 +86,18 @@ public SessionConnection(Session session, EndPoint endPoint, ZoneId zoneId) | |
| throws IoTDBConnectionException { | ||
| this.session = session; | ||
| this.endPoint = endPoint; | ||
| endPointList.add(endPoint); | ||
| this.zoneId = zoneId == null ? ZoneId.systemDefault() : zoneId; | ||
| init(endPoint); | ||
| } | ||
|
|
||
| public SessionConnection(Session session, ZoneId zoneId) throws IoTDBConnectionException { | ||
| this.session = session; | ||
| this.zoneId = zoneId == null ? ZoneId.systemDefault() : zoneId; | ||
| this.endPointList = SessionUtils.parseSeedNodeUrls(session.nodeUrls); | ||
| initClusterConn(); | ||
| } | ||
|
|
||
| private void init(EndPoint endPoint) throws IoTDBConnectionException { | ||
| RpcTransportFactory.setDefaultBufferCapacity(session.thriftDefaultBufferSize); | ||
| RpcTransportFactory.setThriftMaxFrameSize(session.thriftMaxFrameSize); | ||
|
|
@@ -145,6 +156,21 @@ private void init(EndPoint endPoint) throws IoTDBConnectionException { | |
| } | ||
| } | ||
|
|
||
| private void initClusterConn() throws IoTDBConnectionException { | ||
| for (EndPoint endPoint : endPointList) { | ||
| try { | ||
| session.defaultEndPoint = endPoint; | ||
| init(endPoint); | ||
| } catch (IoTDBConnectionException e) { | ||
| if (!reconnect()) { | ||
| logger.error("Cluster has no nodes to connect"); | ||
| throw new IoTDBConnectionException(e); | ||
| } | ||
| } | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| public void close() throws IoTDBConnectionException { | ||
| TSCloseSessionReq req = new TSCloseSessionReq(sessionId); | ||
| try { | ||
|
|
@@ -720,24 +746,38 @@ protected void testInsertTablets(TSInsertTabletsReq request) | |
| } | ||
|
|
||
| private boolean reconnect() { | ||
| boolean flag = false; | ||
| boolean connectedSuccess = false; | ||
| Random random = new Random(); | ||
| for (int i = 1; i <= Config.RETRY_NUM; i++) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always try the first RETRY_NUM clients, it's not good. Maybe you can record a nextHostIndex to try next hostId.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in new commit,Instead of trying the first node in the list every time, try the current node again, and then try the next node in the list
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should connect nodes randomly, so that we can reduce the pressure on one node.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, in new commit,connect nodes randomly |
||
| try { | ||
| if (transport != null) { | ||
| close(); | ||
| init(endPoint); | ||
| flag = true; | ||
| } | ||
| } catch (Exception e) { | ||
| try { | ||
| Thread.sleep(Config.RETRY_INTERVAL_MS); | ||
| } catch (InterruptedException e1) { | ||
| logger.error("reconnect is interrupted.", e1); | ||
| Thread.currentThread().interrupt(); | ||
| if (transport != null) { | ||
| transport.close(); | ||
| int currHostIndex = random.nextInt(endPointList.size()); | ||
| int tryHostNum = 0; | ||
| for (int j = currHostIndex; j < endPointList.size(); j++) { | ||
HTHou marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (tryHostNum == endPointList.size()) { | ||
| break; | ||
| } | ||
| session.defaultEndPoint = endPointList.get(j); | ||
| this.endPoint = endPointList.get(j); | ||
| if (j == endPointList.size() - 1) { | ||
| j = -1; | ||
| } | ||
| tryHostNum++; | ||
| try { | ||
| init(endPoint); | ||
| connectedSuccess = true; | ||
| } catch (IoTDBConnectionException e) { | ||
| logger.error("The current node may have been down {},try next node", endPoint); | ||
| continue; | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| if (connectedSuccess) { | ||
| break; | ||
| } | ||
| } | ||
| return flag; | ||
| return connectedSuccess; | ||
| } | ||
|
|
||
| protected void createSchemaTemplate(TSCreateSchemaTemplateReq request) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |
| */ | ||
| package org.apache.iotdb.session; | ||
|
|
||
| import org.apache.iotdb.service.rpc.thrift.EndPoint; | ||
| import org.apache.iotdb.tsfile.exception.write.UnSupportedDataTypeException; | ||
| import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; | ||
| import org.apache.iotdb.tsfile.utils.Binary; | ||
|
|
@@ -27,10 +28,17 @@ | |
| import org.apache.iotdb.tsfile.write.schema.IMeasurementSchema; | ||
| import org.apache.iotdb.tsfile.write.schema.MeasurementSchema; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class SessionUtils { | ||
|
|
||
| private static final Logger logger = LoggerFactory.getLogger(SessionUtils.class); | ||
|
|
||
| public static ByteBuffer getTimeBuffer(Tablet tablet) { | ||
| ByteBuffer timeBuffer = ByteBuffer.allocate(tablet.getTimeBytesSize()); | ||
| for (int i = 0; i < tablet.rowSize; i++) { | ||
|
|
@@ -149,4 +157,31 @@ private static void getValueBufferOfDataType( | |
| String.format("Data type %s is not supported.", dataType)); | ||
| } | ||
| } | ||
|
|
||
| public static List<EndPoint> parseSeedNodeUrls(List<String> nodeUrls) { | ||
| if (nodeUrls == null) { | ||
| throw new NumberFormatException("nodeUrls is null"); | ||
| } | ||
| List<EndPoint> endPointsList = new ArrayList<>(); | ||
| for (String nodeUrl : nodeUrls) { | ||
| EndPoint endPoint = parseNodeUrl(nodeUrl); | ||
| endPointsList.add(endPoint); | ||
|
Comment on lines
+167
to
+168
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| return endPointsList; | ||
| } | ||
|
|
||
| private static EndPoint parseNodeUrl(String nodeUrl) { | ||
| EndPoint endPoint = new EndPoint(); | ||
| String[] split = nodeUrl.split(":"); | ||
| if (split.length != 2) { | ||
| throw new NumberFormatException("NodeUrl Incorrect format"); | ||
| } | ||
| String ip = split[0]; | ||
| try { | ||
| int rpcPort = Integer.parseInt(split[1]); | ||
| return endPoint.setIp(ip).setPort(rpcPort); | ||
| } catch (Exception e) { | ||
| throw new NumberFormatException("NodeUrl Incorrect format"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /* | ||
| * 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.sql; | ||
|
|
||
| import org.apache.iotdb.rpc.IoTDBConnectionException; | ||
| import org.apache.iotdb.rpc.StatementExecutionException; | ||
| import org.apache.iotdb.session.Session; | ||
| import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType; | ||
| import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType; | ||
| import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding; | ||
|
|
||
| import org.junit.Rule; | ||
| import org.junit.Test; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.testcontainers.containers.DockerComposeContainer; | ||
| import org.testcontainers.containers.NoProjectNameDockerComposeContainer; | ||
| import org.testcontainers.containers.output.Slf4jLogConsumer; | ||
| import org.testcontainers.containers.wait.strategy.Wait; | ||
|
|
||
| import java.io.File; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class ClusterSessionSimpleIT { | ||
|
|
||
| private static Logger node1Logger = LoggerFactory.getLogger("iotdb-server_1"); | ||
| private static Logger node2Logger = LoggerFactory.getLogger("iotdb-server_2"); | ||
| private static Logger node3Logger = LoggerFactory.getLogger("iotdb-server_3"); | ||
|
|
||
| private Session session; | ||
|
|
||
| @Rule | ||
| public DockerComposeContainer environment = | ||
| new NoProjectNameDockerComposeContainer( | ||
| "3nodes", new File("src/test/resources/3nodes/docker-compose.yaml")) | ||
| .withExposedService("iotdb-server_1", 6667, Wait.forListeningPort()) | ||
| .withLogConsumer("iotdb-server_1", new Slf4jLogConsumer(node1Logger)) | ||
| .withExposedService("iotdb-server_2", 6667, Wait.forListeningPort()) | ||
| .withLogConsumer("iotdb-server_2", new Slf4jLogConsumer(node2Logger)) | ||
| .withExposedService("iotdb-server_3", 6667, Wait.forListeningPort()) | ||
| .withLogConsumer("iotdb-server_3", new Slf4jLogConsumer(node3Logger)) | ||
| .withLocalCompose(true); | ||
|
|
||
| protected DockerComposeContainer getContainer() { | ||
| return environment; | ||
| } | ||
|
|
||
| @Test | ||
| public void testSessionCluster() throws IoTDBConnectionException, StatementExecutionException { | ||
| List<String> stringList = new ArrayList<>(); | ||
| Integer service1Port = getContainer().getServicePort("iotdb-server_1", 6667); | ||
| Integer service2Port = getContainer().getServicePort("iotdb-server_2", 6667); | ||
| Integer service3Port = getContainer().getServicePort("iotdb-server_3", 6667); | ||
| stringList.add("localhost:" + service1Port); | ||
| stringList.add("localhost:" + service2Port); | ||
| stringList.add("localhost:" + service3Port); | ||
| session = new Session(stringList, "root", "root"); | ||
| session.open(); | ||
| session.setStorageGroup("root.sg1"); | ||
| session.createTimeseries( | ||
| "root.sg1.d1.s1", TSDataType.INT64, TSEncoding.RLE, CompressionType.SNAPPY); | ||
|
|
||
| session.createTimeseries( | ||
| "root.sg1.d2.s1", TSDataType.INT64, TSEncoding.RLE, CompressionType.SNAPPY); | ||
| session.close(); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's better using java doc to specify the URL format clearly