Skip to content
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

[remove confignode] Add Remove ConfigNode SQL #14813

Merged
merged 3 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* 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.confignode.it.removeconfignode;

import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient;
import org.apache.iotdb.commons.schema.column.ColumnHeaderConstant;
import org.apache.iotdb.confignode.it.removedatanode.SQLModel;
import org.apache.iotdb.consensus.ConsensusFactory;
import org.apache.iotdb.it.env.EnvFactory;
import org.apache.iotdb.itbase.exception.InconsistentDataException;
import org.apache.iotdb.jdbc.IoTDBSQLException;
import org.apache.iotdb.relational.it.query.old.aligned.TableUtils;

import org.awaitility.Awaitility;
import org.awaitility.core.ConditionTimeoutException;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

import static org.apache.iotdb.confignode.it.regionmigration.IoTDBRegionOperationReliabilityITFramework.getDataRegionMap;
import static org.apache.iotdb.confignode.it.removedatanode.IoTDBRemoveDataNodeITFramework.getConnectionWithSQLType;
import static org.apache.iotdb.util.MagicUtils.makeItCloseQuietly;

public class IoTDBRemoveConfigNodeITFramework {
private static final Logger LOGGER =
LoggerFactory.getLogger(IoTDBRemoveConfigNodeITFramework.class);
private static final String TREE_MODEL_INSERTION =
"INSERT INTO root.sg.d1(timestamp,speed,temperature) values(100, 1, 2)";

private static final String SHOW_CONFIGNODES = "show confignodes";

private static final String defaultSchemaRegionGroupExtensionPolicy = "CUSTOM";
private static final String defaultDataRegionGroupExtensionPolicy = "CUSTOM";

@Before
public void setUp() throws Exception {
EnvFactory.getEnv()
.getConfig()
.getCommonConfig()
.setConfigNodeConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS)
.setSchemaRegionConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS)
.setDataRegionConsensusProtocolClass(ConsensusFactory.IOT_CONSENSUS)
.setSchemaRegionGroupExtensionPolicy(defaultSchemaRegionGroupExtensionPolicy)
.setDataRegionGroupExtensionPolicy(defaultDataRegionGroupExtensionPolicy);
}

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

public void testRemoveConfigNode(
final int dataReplicateFactor,
final int schemaReplicationFactor,
final int configNodeNum,
final int dataNodeNum,
final int dataRegionPerDataNode,
final SQLModel model)
throws Exception {

// Set up the environment
EnvFactory.getEnv()
.getConfig()
.getCommonConfig()
.setSchemaReplicationFactor(schemaReplicationFactor)
.setDataReplicationFactor(dataReplicateFactor)
.setDefaultDataRegionGroupNumPerDatabase(
dataRegionPerDataNode * dataNodeNum / dataReplicateFactor);
EnvFactory.getEnv().initClusterEnvironment(configNodeNum, dataNodeNum);

try (final Connection connection = makeItCloseQuietly(getConnectionWithSQLType(model));
final Statement statement = makeItCloseQuietly(connection.createStatement());
SyncConfigNodeIServiceClient client =
(SyncConfigNodeIServiceClient) EnvFactory.getEnv().getLeaderConfigNodeConnection()) {

if (SQLModel.TABLE_MODEL_SQL.equals(model)) {
// Insert data in table model
TableUtils.insertData();
} else {
// Insert data in tree model
statement.execute(TREE_MODEL_INSERTION);
}

Map<Integer, Set<Integer>> regionMap = getDataRegionMap(statement);
regionMap.forEach(
(key, valueSet) -> {
LOGGER.info("Key: {}, Value: {}", key, valueSet);
if (valueSet.size() != dataReplicateFactor) {
Assert.fail();
}
});

// Get all config nodes
ResultSet result = statement.executeQuery(SHOW_CONFIGNODES);
Set<Integer> allConfigNodeId = new HashSet<>();
while (result.next()) {
allConfigNodeId.add(result.getInt(ColumnHeaderConstant.NODE_ID));
}

AtomicReference<SyncConfigNodeIServiceClient> clientRef = new AtomicReference<>(client);

int removeConfigNodeId = allConfigNodeId.iterator().next();
String removeConfigNodeSQL = generateRemoveString(removeConfigNodeId);
LOGGER.info("Remove ConfigNodes SQL: {}", removeConfigNodeSQL);
try {
statement.execute(removeConfigNodeSQL);
} catch (IoTDBSQLException e) {
LOGGER.error("Remove ConfigNodes SQL execute fail: {}", e.getMessage());
Assert.fail();
}
LOGGER.info("Remove ConfigNodes SQL submit successfully.");

// Wait until success
try {
awaitUntilSuccess(statement, removeConfigNodeId);
} catch (ConditionTimeoutException e) {
LOGGER.error("Remove ConfigNodes timeout in 2 minutes");
Assert.fail();
}

LOGGER.info("Remove ConfigNodes success");
} catch (InconsistentDataException e) {
LOGGER.error("Unexpected error:", e);
}
}

private static void awaitUntilSuccess(Statement statement, int removeConfigNodeId) {
AtomicReference<Set<Integer>> lastTimeConfigNodes = new AtomicReference<>();
AtomicReference<Exception> lastException = new AtomicReference<>();

try {
Awaitility.await()
.atMost(2, TimeUnit.MINUTES)
.pollDelay(2, TimeUnit.SECONDS)
.until(
() -> {
try {
// Get all config nodes
ResultSet result = statement.executeQuery(SHOW_CONFIGNODES);
Set<Integer> allConfigNodeId = new HashSet<>();
while (result.next()) {
allConfigNodeId.add(result.getInt(ColumnHeaderConstant.NODE_ID));
}
lastTimeConfigNodes.set(allConfigNodeId);
return !allConfigNodeId.contains(removeConfigNodeId);
} catch (Exception e) {
// Any exception can be ignored
lastException.set(e);
return false;
}
});
} catch (ConditionTimeoutException e) {
if (lastTimeConfigNodes.get() == null) {
LOGGER.error(
"Maybe show confignodes fail, lastTimeConfigNodes is null, last Exception:",
lastException.get());
throw e;
}
String actualSetStr = lastTimeConfigNodes.get().toString();
lastTimeConfigNodes.get().remove(removeConfigNodeId);
String expectedSetStr = lastTimeConfigNodes.get().toString();
LOGGER.error(
"Remove ConfigNode timeout in 2 minutes, expected set: {}, actual set: {}",
expectedSetStr,
actualSetStr);
if (lastException.get() == null) {
LOGGER.info("No exception during awaiting");
} else {
LOGGER.error("Last exception during awaiting:", lastException.get());
}
throw e;
}
}

public static String generateRemoveString(Integer configNodeId) {
return "remove confignode " + configNodeId;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.confignode.it.removeconfignode;

import org.apache.iotdb.confignode.it.removedatanode.SQLModel;
import org.apache.iotdb.it.framework.IoTDBTestRunner;
import org.apache.iotdb.itbase.category.ClusterIT;

import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

@Category({ClusterIT.class})
@RunWith(IoTDBTestRunner.class)
public class IoTDBRemoveConfigNodeNormalIT extends IoTDBRemoveConfigNodeITFramework {
@Test
public void test3C1DUseTreeSQL() throws Exception {
testRemoveConfigNode(1, 1, 3, 1, 2, SQLModel.TREE_MODEL_SQL);
}

@Test
public void test3C1DUseTableSQL() throws Exception {
testRemoveConfigNode(1, 1, 3, 1, 2, SQLModel.TABLE_MODEL_SQL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public static String generateRemoveString(Set<Integer> dataNodes) {
return sb.toString();
}

public Connection getConnectionWithSQLType(SQLModel model) throws SQLException {
public static Connection getConnectionWithSQLType(SQLModel model) throws SQLException {
if (SQLModel.TABLE_MODEL_SQL.equals(model)) {
return EnvFactory.getEnv().getTableConnection();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ keyWords
| CLUSTERID
| CONCAT
| CONDITION
| CONFIGNODE
| CONFIGNODES
| CONFIGURATION
| CONNECTION
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ ddlStatement
// Cluster
| showVariables | showCluster | showRegions | showDataNodes | showConfigNodes | showClusterId
| getRegionId | getTimeSlotList | countTimeSlotList | getSeriesSlotList
| migrateRegion | reconstructRegion | extendRegion | removeRegion | removeDataNode
| migrateRegion | reconstructRegion | extendRegion | removeRegion | removeDataNode | removeConfigNode
| verifyConnection
// AINode
| showAINodes | createModel | dropModel | showModels | callInference
Expand Down Expand Up @@ -555,6 +555,11 @@ removeDataNode
: REMOVE DATANODE dataNodeId=INTEGER_LITERAL (COMMA dataNodeId=INTEGER_LITERAL)*
;

// ---- Remove ConfigNode
removeConfigNode
: REMOVE CONFIGNODE configNodeId=INTEGER_LITERAL
;

// Pipe Task =========================================================================================
createPipe
: CREATE PIPE (IF NOT EXISTS)? pipeName=identifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ CONFIGNODES
: C O N F I G N O D E S
;

CONFIGNODE
: C O N F I G N O D E
;

CONFIGURATION
: C O N F I G U R A T I O N
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,8 @@ public TSStatus notifyRegisterSuccess() throws TException {

@Override
public TSStatus removeConfigNode(TConfigNodeLocation configNodeLocation) throws TException {
throw new TException("DataNode to ConfigNode client doesn't support removeConfigNode.");
return executeRemoteCallWithRetry(
() -> client.removeConfigNode(configNodeLocation), resp -> !updateConfigNodeLeader(resp));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.PipeStatement;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ReconstructRegion;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.RelationalAuthorStatement;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.RemoveConfigNode;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.RemoveDataNode;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.RemoveRegion;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.SetConfiguration;
Expand Down Expand Up @@ -419,6 +420,7 @@ private IQueryExecution createQueryExecutionForTableModel(
|| statement instanceof StopRepairData
|| statement instanceof PipeStatement
|| statement instanceof RemoveDataNode
|| statement instanceof RemoveConfigNode
|| statement instanceof SubscriptionStatement
|| statement instanceof ShowCurrentSqlDialect
|| statement instanceof ShowCurrentUser
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.CreatePipePluginTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.DropFunctionTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.DropPipePluginTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.RemoveConfigNodeTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.RemoveDataNodeTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.ShowClusterIdTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.ShowClusterTask;
Expand Down Expand Up @@ -134,6 +135,7 @@
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.QualifiedName;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.ReconstructRegion;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.RelationalAuthorStatement;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.RemoveConfigNode;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.RemoveDataNode;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.RemoveRegion;
import org.apache.iotdb.db.queryengine.plan.relational.sql.ast.RenameColumn;
Expand Down Expand Up @@ -168,6 +170,7 @@
import org.apache.iotdb.db.queryengine.plan.relational.sql.rewrite.StatementRewrite;
import org.apache.iotdb.db.queryengine.plan.relational.type.TypeNotFoundException;
import org.apache.iotdb.db.queryengine.plan.statement.metadata.DatabaseSchemaStatement;
import org.apache.iotdb.db.queryengine.plan.statement.metadata.RemoveConfigNodeStatement;
import org.apache.iotdb.db.queryengine.plan.statement.metadata.RemoveDataNodeStatement;
import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowClusterStatement;
import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowRegionStatement;
Expand Down Expand Up @@ -382,6 +385,18 @@ protected IConfigTask visitRemoveDataNode(
return new RemoveDataNodeTask(treeStatement);
}

@Override
protected IConfigTask visitRemoveConfigNode(
final RemoveConfigNode removeConfigNode, final MPPQueryContext context) {
context.setQueryType(QueryType.WRITE);
accessControl.checkUserHasMaintainPrivilege(context.getSession().getUserName());
// As the implementation is identical, we'll simply translate to the
// corresponding tree-model variant and execute that.
final RemoveConfigNodeStatement treeStatement =
new RemoveConfigNodeStatement(removeConfigNode.getNodeId());
return new RemoveConfigNodeTask(treeStatement);
}

@Override
protected IConfigTask visitShowDataNodes(
final ShowDataNodes showDataNodesStatement, final MPPQueryContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.GetRegionIdTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.GetSeriesSlotListTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.GetTimeSlotListTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.RemoveConfigNodeTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.RemoveDataNodeTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.SetTTLTask;
import org.apache.iotdb.db.queryengine.plan.execution.config.metadata.ShowAINodesTask;
Expand Down Expand Up @@ -118,6 +119,7 @@
import org.apache.iotdb.db.queryengine.plan.statement.metadata.GetRegionIdStatement;
import org.apache.iotdb.db.queryengine.plan.statement.metadata.GetSeriesSlotListStatement;
import org.apache.iotdb.db.queryengine.plan.statement.metadata.GetTimeSlotListStatement;
import org.apache.iotdb.db.queryengine.plan.statement.metadata.RemoveConfigNodeStatement;
import org.apache.iotdb.db.queryengine.plan.statement.metadata.RemoveDataNodeStatement;
import org.apache.iotdb.db.queryengine.plan.statement.metadata.SetTTLStatement;
import org.apache.iotdb.db.queryengine.plan.statement.metadata.ShowClusterIdStatement;
Expand Down Expand Up @@ -660,6 +662,12 @@ public IConfigTask visitRemoveDataNode(
return new RemoveDataNodeTask(removeDataNodeStatement);
}

@Override
public IConfigTask visitRemoveConfigNode(
RemoveConfigNodeStatement removeConfigNodeStatement, MPPQueryContext context) {
return new RemoveConfigNodeTask(removeConfigNodeStatement);
}

@Override
public IConfigTask visitCreateContinuousQuery(
CreateContinuousQueryStatement createContinuousQueryStatement, MPPQueryContext context) {
Expand Down
Loading
Loading