|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.confignode.it.removeconfignode; |
| 21 | + |
| 22 | +import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient; |
| 23 | +import org.apache.iotdb.commons.schema.column.ColumnHeaderConstant; |
| 24 | +import org.apache.iotdb.confignode.it.removedatanode.SQLModel; |
| 25 | +import org.apache.iotdb.consensus.ConsensusFactory; |
| 26 | +import org.apache.iotdb.it.env.EnvFactory; |
| 27 | +import org.apache.iotdb.itbase.exception.InconsistentDataException; |
| 28 | +import org.apache.iotdb.jdbc.IoTDBSQLException; |
| 29 | +import org.apache.iotdb.relational.it.query.old.aligned.TableUtils; |
| 30 | + |
| 31 | +import org.awaitility.Awaitility; |
| 32 | +import org.awaitility.core.ConditionTimeoutException; |
| 33 | +import org.junit.After; |
| 34 | +import org.junit.Assert; |
| 35 | +import org.junit.Before; |
| 36 | +import org.slf4j.Logger; |
| 37 | +import org.slf4j.LoggerFactory; |
| 38 | + |
| 39 | +import java.sql.Connection; |
| 40 | +import java.sql.ResultSet; |
| 41 | +import java.sql.Statement; |
| 42 | +import java.util.HashSet; |
| 43 | +import java.util.Map; |
| 44 | +import java.util.Set; |
| 45 | +import java.util.concurrent.TimeUnit; |
| 46 | +import java.util.concurrent.atomic.AtomicReference; |
| 47 | + |
| 48 | +import static org.apache.iotdb.confignode.it.regionmigration.IoTDBRegionOperationReliabilityITFramework.getDataRegionMap; |
| 49 | +import static org.apache.iotdb.confignode.it.removedatanode.IoTDBRemoveDataNodeITFramework.getConnectionWithSQLType; |
| 50 | +import static org.apache.iotdb.util.MagicUtils.makeItCloseQuietly; |
| 51 | + |
| 52 | +public class IoTDBRemoveConfigNodeITFramework { |
| 53 | + private static final Logger LOGGER = |
| 54 | + LoggerFactory.getLogger(IoTDBRemoveConfigNodeITFramework.class); |
| 55 | + private static final String TREE_MODEL_INSERTION = |
| 56 | + "INSERT INTO root.sg.d1(timestamp,speed,temperature) values(100, 1, 2)"; |
| 57 | + |
| 58 | + private static final String SHOW_CONFIGNODES = "show confignodes"; |
| 59 | + |
| 60 | + private static final String defaultSchemaRegionGroupExtensionPolicy = "CUSTOM"; |
| 61 | + private static final String defaultDataRegionGroupExtensionPolicy = "CUSTOM"; |
| 62 | + |
| 63 | + @Before |
| 64 | + public void setUp() throws Exception { |
| 65 | + EnvFactory.getEnv() |
| 66 | + .getConfig() |
| 67 | + .getCommonConfig() |
| 68 | + .setConfigNodeConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS) |
| 69 | + .setSchemaRegionConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS) |
| 70 | + .setDataRegionConsensusProtocolClass(ConsensusFactory.IOT_CONSENSUS) |
| 71 | + .setSchemaRegionGroupExtensionPolicy(defaultSchemaRegionGroupExtensionPolicy) |
| 72 | + .setDataRegionGroupExtensionPolicy(defaultDataRegionGroupExtensionPolicy); |
| 73 | + } |
| 74 | + |
| 75 | + @After |
| 76 | + public void tearDown() throws InterruptedException { |
| 77 | + EnvFactory.getEnv().cleanClusterEnvironment(); |
| 78 | + } |
| 79 | + |
| 80 | + public void testRemoveConfigNode( |
| 81 | + final int dataReplicateFactor, |
| 82 | + final int schemaReplicationFactor, |
| 83 | + final int configNodeNum, |
| 84 | + final int dataNodeNum, |
| 85 | + final int dataRegionPerDataNode, |
| 86 | + final SQLModel model) |
| 87 | + throws Exception { |
| 88 | + |
| 89 | + // Set up the environment |
| 90 | + EnvFactory.getEnv() |
| 91 | + .getConfig() |
| 92 | + .getCommonConfig() |
| 93 | + .setSchemaReplicationFactor(schemaReplicationFactor) |
| 94 | + .setDataReplicationFactor(dataReplicateFactor) |
| 95 | + .setDefaultDataRegionGroupNumPerDatabase( |
| 96 | + dataRegionPerDataNode * dataNodeNum / dataReplicateFactor); |
| 97 | + EnvFactory.getEnv().initClusterEnvironment(configNodeNum, dataNodeNum); |
| 98 | + |
| 99 | + try (final Connection connection = makeItCloseQuietly(getConnectionWithSQLType(model)); |
| 100 | + final Statement statement = makeItCloseQuietly(connection.createStatement()); |
| 101 | + SyncConfigNodeIServiceClient client = |
| 102 | + (SyncConfigNodeIServiceClient) EnvFactory.getEnv().getLeaderConfigNodeConnection()) { |
| 103 | + |
| 104 | + if (SQLModel.TABLE_MODEL_SQL.equals(model)) { |
| 105 | + // Insert data in table model |
| 106 | + TableUtils.insertData(); |
| 107 | + } else { |
| 108 | + // Insert data in tree model |
| 109 | + statement.execute(TREE_MODEL_INSERTION); |
| 110 | + } |
| 111 | + |
| 112 | + Map<Integer, Set<Integer>> regionMap = getDataRegionMap(statement); |
| 113 | + regionMap.forEach( |
| 114 | + (key, valueSet) -> { |
| 115 | + LOGGER.info("Key: {}, Value: {}", key, valueSet); |
| 116 | + if (valueSet.size() != dataReplicateFactor) { |
| 117 | + Assert.fail(); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + // Get all config nodes |
| 122 | + ResultSet result = statement.executeQuery(SHOW_CONFIGNODES); |
| 123 | + Set<Integer> allConfigNodeId = new HashSet<>(); |
| 124 | + while (result.next()) { |
| 125 | + allConfigNodeId.add(result.getInt(ColumnHeaderConstant.NODE_ID)); |
| 126 | + } |
| 127 | + |
| 128 | + AtomicReference<SyncConfigNodeIServiceClient> clientRef = new AtomicReference<>(client); |
| 129 | + |
| 130 | + int removeConfigNodeId = allConfigNodeId.iterator().next(); |
| 131 | + String removeConfigNodeSQL = generateRemoveString(removeConfigNodeId); |
| 132 | + LOGGER.info("Remove ConfigNodes SQL: {}", removeConfigNodeSQL); |
| 133 | + try { |
| 134 | + statement.execute(removeConfigNodeSQL); |
| 135 | + } catch (IoTDBSQLException e) { |
| 136 | + LOGGER.error("Remove ConfigNodes SQL execute fail: {}", e.getMessage()); |
| 137 | + Assert.fail(); |
| 138 | + } |
| 139 | + LOGGER.info("Remove ConfigNodes SQL submit successfully."); |
| 140 | + |
| 141 | + // Wait until success |
| 142 | + try { |
| 143 | + awaitUntilSuccess(statement, removeConfigNodeId); |
| 144 | + } catch (ConditionTimeoutException e) { |
| 145 | + LOGGER.error("Remove ConfigNodes timeout in 2 minutes"); |
| 146 | + Assert.fail(); |
| 147 | + } |
| 148 | + |
| 149 | + LOGGER.info("Remove ConfigNodes success"); |
| 150 | + } catch (InconsistentDataException e) { |
| 151 | + LOGGER.error("Unexpected error:", e); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private static void awaitUntilSuccess(Statement statement, int removeConfigNodeId) { |
| 156 | + AtomicReference<Set<Integer>> lastTimeConfigNodes = new AtomicReference<>(); |
| 157 | + AtomicReference<Exception> lastException = new AtomicReference<>(); |
| 158 | + |
| 159 | + try { |
| 160 | + Awaitility.await() |
| 161 | + .atMost(2, TimeUnit.MINUTES) |
| 162 | + .pollDelay(2, TimeUnit.SECONDS) |
| 163 | + .until( |
| 164 | + () -> { |
| 165 | + try { |
| 166 | + // Get all config nodes |
| 167 | + ResultSet result = statement.executeQuery(SHOW_CONFIGNODES); |
| 168 | + Set<Integer> allConfigNodeId = new HashSet<>(); |
| 169 | + while (result.next()) { |
| 170 | + allConfigNodeId.add(result.getInt(ColumnHeaderConstant.NODE_ID)); |
| 171 | + } |
| 172 | + lastTimeConfigNodes.set(allConfigNodeId); |
| 173 | + return !allConfigNodeId.contains(removeConfigNodeId); |
| 174 | + } catch (Exception e) { |
| 175 | + // Any exception can be ignored |
| 176 | + lastException.set(e); |
| 177 | + return false; |
| 178 | + } |
| 179 | + }); |
| 180 | + } catch (ConditionTimeoutException e) { |
| 181 | + if (lastTimeConfigNodes.get() == null) { |
| 182 | + LOGGER.error( |
| 183 | + "Maybe show confignodes fail, lastTimeConfigNodes is null, last Exception:", |
| 184 | + lastException.get()); |
| 185 | + throw e; |
| 186 | + } |
| 187 | + String actualSetStr = lastTimeConfigNodes.get().toString(); |
| 188 | + lastTimeConfigNodes.get().remove(removeConfigNodeId); |
| 189 | + String expectedSetStr = lastTimeConfigNodes.get().toString(); |
| 190 | + LOGGER.error( |
| 191 | + "Remove ConfigNode timeout in 2 minutes, expected set: {}, actual set: {}", |
| 192 | + expectedSetStr, |
| 193 | + actualSetStr); |
| 194 | + if (lastException.get() == null) { |
| 195 | + LOGGER.info("No exception during awaiting"); |
| 196 | + } else { |
| 197 | + LOGGER.error("Last exception during awaiting:", lastException.get()); |
| 198 | + } |
| 199 | + throw e; |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | + public static String generateRemoveString(Integer configNodeId) { |
| 204 | + return "remove confignode " + configNodeId; |
| 205 | + } |
| 206 | +} |
0 commit comments