Skip to content

Commit

Permalink
Verify connection && Refactor async connection framework (#12667)
Browse files Browse the repository at this point in the history
  • Loading branch information
liyuheng55555 authored Jun 14, 2024
1 parent 93211e1 commit 85971c6
Show file tree
Hide file tree
Showing 98 changed files with 3,774 additions and 1,186 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,10 @@ public final void addRequest(Callable<T> request) {
*/
public final T requestAllAndCompare() throws SQLException {
List<T> results = requestAll();
T data = results.get(0);
for (int i = 1; i < results.size(); i++) {
T anotherData = results.get(i);
if (!compare(data, anotherData)) {
throw new InconsistentDataException(results, endpoints);
}
if (!results.stream().allMatch(result -> compare(results.get(0), result))) {
throw new InconsistentDataException(results, endpoints);
}
return data;
return results.get(0);
}

private boolean compare(T data, T anotherData) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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.it;

import org.apache.iotdb.commons.service.ThriftService;
import org.apache.iotdb.consensus.ConsensusFactory;
import org.apache.iotdb.it.env.EnvFactory;
import org.apache.iotdb.it.framework.IoTDBTestRunner;
import org.apache.iotdb.itbase.category.ClusterIT;

import com.google.common.collect.ImmutableMap;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

@RunWith(IoTDBTestRunner.class)
@Category({ClusterIT.class})
public class IoTDBVerifyConnectionIT {
private static final String testConsensusProtocolClass = ConsensusFactory.RATIS_CONSENSUS;

@Before
public void setUp() throws Exception {
EnvFactory.getEnv()
.getConfig()
.getCommonConfig()
.setConfigNodeConsensusProtocolClass(testConsensusProtocolClass)
.setSchemaRegionConsensusProtocolClass(testConsensusProtocolClass)
.setDataRegionConsensusProtocolClass(testConsensusProtocolClass);
}

@After
public void tearDown() throws Exception {
EnvFactory.getEnv().cleanClusterEnvironment();
}

@Test
public void verifyConnectionAllUpTest() throws Exception {
EnvFactory.getEnv().initClusterEnvironment(3, 3);
try (Connection connection = EnvFactory.getEnv().getConnection();
Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery("verify connection");
Assert.assertEquals(
ImmutableMap.of(ThriftService.STATUS_UP, 12), collectConnectionResult(resultSet));
resultSet = statement.executeQuery("verify connection details");
Assert.assertEquals(
ImmutableMap.of(ThriftService.STATUS_UP, 54), collectConnectionResult(resultSet));
}
}

@Test
public void verifyConnectionWithNodeCorruptTest() throws Exception {
EnvFactory.getEnv().initClusterEnvironment(5, 1);
try (Connection connection = EnvFactory.getEnv().getConnection();
Statement statement = connection.createStatement()) {
ResultSet resultSet = statement.executeQuery("verify connection details");
Assert.assertEquals(
ImmutableMap.of(ThriftService.STATUS_UP, 38), collectConnectionResult(resultSet));
EnvFactory.getEnv().getConfigNodeWrapperList().get(0).stopForcibly();
resultSet = statement.executeQuery("verify connection");
Assert.assertEquals(
ImmutableMap.of(ThriftService.STATUS_UP, 2, ThriftService.STATUS_DOWN, 11),
collectConnectionResult(resultSet));
resultSet = statement.executeQuery("verify connection details");
Assert.assertEquals(
ImmutableMap.of(ThriftService.STATUS_UP, 27, ThriftService.STATUS_DOWN, 11),
collectConnectionResult(resultSet));
EnvFactory.getEnv().getConfigNodeWrapperList().get(1).stopForcibly();
Thread.sleep(10000);
resultSet = statement.executeQuery("verify connection");
Assert.assertEquals(
ImmutableMap.of(ThriftService.STATUS_UP, 2, ThriftService.STATUS_DOWN, 20),
collectConnectionResult(resultSet));
resultSet = statement.executeQuery("verify connection details");
Assert.assertEquals(
ImmutableMap.of(ThriftService.STATUS_UP, 18, ThriftService.STATUS_DOWN, 20),
collectConnectionResult(resultSet));
}
}

private static Map<String, Integer> collectConnectionResult(ResultSet resultSet)
throws Exception {
Map<String, Integer> map = new HashMap<>();
while (resultSet.next()) {
String result = resultSet.getString(3).split(" ")[0];
map.computeIfPresent(result, (key, value) -> value + 1);
map.putIfAbsent(result, 1);
}
return map;
}

private static void showResult(ResultSet resultSet) throws Exception {
while (resultSet.next()) {
System.out.printf(
"%s %s %s\n", resultSet.getString(1), resultSet.getString(2), resultSet.getString(3));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ public enum TSStatusCode {
CAN_NOT_CONNECT_DATANODE(1007),
TRANSFER_LEADER_ERROR(1008),
GET_CLUSTER_ID_ERROR(1009),
CAN_NOT_CONNECT_CONFIGNODE(1010),

// Sync, Load TsFile
LOAD_FILE_ERROR(1100),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ keyWords
| CONDITION
| CONFIGNODES
| CONFIGURATION
| CONNECTION
| CONNECTOR
| CONTAIN
| CONTAINS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ ddlStatement
| createContinuousQuery | dropContinuousQuery | showContinuousQueries
// Cluster
| showVariables | showCluster | showRegions | showDataNodes | showConfigNodes | showClusterId
| getRegionId | getTimeSlotList | countTimeSlotList | getSeriesSlotList | migrateRegion
| getRegionId | getTimeSlotList | countTimeSlotList | getSeriesSlotList | migrateRegion | verifyConnection
// Quota
| setSpaceQuota | showSpaceQuota | setThrottleQuota | showThrottleQuota
// View
Expand Down Expand Up @@ -524,6 +524,10 @@ migrateRegion
: MIGRATE REGION regionId=INTEGER_LITERAL FROM fromId=INTEGER_LITERAL TO toId=INTEGER_LITERAL
;

verifyConnection
: VERIFY CONNECTION (DETAILS)?
;

// Pipe Task =========================================================================================
createPipe
: CREATE PIPE pipeName=identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ CONFIGURATION
: C O N F I G U R A T I O N
;

CONNECTION
: C O N N E C T I O N
;

CONNECTOR
: C O N N E C T O R
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.apache.iotdb.confignode.client;

public enum ConfigNodeRequestType {
public enum CnToCnNodeRequestType {
ADD_CONSENSUS_GROUP,
NOTIFY_REGISTER_SUCCESS,
REGISTER_CONFIG_NODE,
Expand All @@ -30,4 +30,6 @@ public enum ConfigNodeRequestType {
STOP_CONFIG_NODE,
SET_CONFIGURATION,
SHOW_CONFIGURATION,
SUBMIT_TEST_CONNECTION_TASK,
TEST_CONNECTION,
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

package org.apache.iotdb.confignode.client;

public enum DataNodeRequestType {
public enum CnToDnRequestType {

// Node Maintenance
DISABLE_DATA_NODE,
Expand All @@ -35,6 +35,9 @@ public enum DataNodeRequestType {
SET_CONFIGURATION,
SHOW_CONFIGURATION,

SUBMIT_TEST_CONNECTION_TASK,
TEST_CONNECTION,

// Region Maintenance
CREATE_DATA_REGION,
CREATE_SCHEMA_REGION,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@
import org.apache.iotdb.common.rpc.thrift.TEndPoint;
import org.apache.iotdb.commons.client.ClientPoolFactory;
import org.apache.iotdb.commons.client.IClientManager;
import org.apache.iotdb.commons.client.async.AsyncConfigNodeIServiceClient;
import org.apache.iotdb.commons.client.async.AsyncConfigNodeInternalServiceClient;
import org.apache.iotdb.confignode.client.async.handlers.heartbeat.ConfigNodeHeartbeatHandler;
import org.apache.iotdb.confignode.rpc.thrift.TConfigNodeHeartbeatReq;

public class AsyncConfigNodeHeartbeatClientPool {

private final IClientManager<TEndPoint, AsyncConfigNodeIServiceClient> clientManager;
private final IClientManager<TEndPoint, AsyncConfigNodeInternalServiceClient> clientManager;

private AsyncConfigNodeHeartbeatClientPool() {
clientManager =
new IClientManager.Factory<TEndPoint, AsyncConfigNodeIServiceClient>()
new IClientManager.Factory<TEndPoint, AsyncConfigNodeInternalServiceClient>()
.createClientManager(
new ClientPoolFactory.AsyncConfigNodeHeartbeatServiceClientPoolFactory());
}
Expand All @@ -53,7 +53,6 @@ public void getConfigNodeHeartBeat(
}
}

// TODO: Is the ClientPool must be a singleton?
private static class AsyncConfigNodeHeartbeatClientPoolHolder {

private static final AsyncConfigNodeHeartbeatClientPool INSTANCE =
Expand Down
Loading

0 comments on commit 85971c6

Please sign in to comment.