|
| 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.regionmigration.pass.daily.iotv1; |
| 21 | + |
| 22 | +import org.apache.iotdb.consensus.ConsensusFactory; |
| 23 | +import org.apache.iotdb.isession.SessionConfig; |
| 24 | +import org.apache.iotdb.it.env.EnvFactory; |
| 25 | +import org.apache.iotdb.it.env.cluster.node.DataNodeWrapper; |
| 26 | +import org.apache.iotdb.it.framework.IoTDBTestRunner; |
| 27 | +import org.apache.iotdb.itbase.category.ClusterIT; |
| 28 | +import org.apache.iotdb.itbase.env.BaseEnv; |
| 29 | + |
| 30 | +import org.apache.tsfile.utils.Pair; |
| 31 | +import org.awaitility.Awaitility; |
| 32 | +import org.junit.After; |
| 33 | +import org.junit.Assert; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.experimental.categories.Category; |
| 37 | +import org.junit.runner.RunWith; |
| 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 | + |
| 47 | +import static org.apache.iotdb.confignode.it.regionmigration.IoTDBRegionOperationReliabilityITFramework.getAllDataNodes; |
| 48 | +import static org.apache.iotdb.confignode.it.regionmigration.IoTDBRegionOperationReliabilityITFramework.getDataRegionMapWithLeader; |
| 49 | + |
| 50 | +@RunWith(IoTDBTestRunner.class) |
| 51 | +@Category({ClusterIT.class}) |
| 52 | +public class IoTDBRegionMigrateWithDeletionMultiDataDirIT { |
| 53 | + |
| 54 | + private static final String MULTI_DATA_DIRS = |
| 55 | + "data/datanode/data/disk0,data/datanode/data/disk1,data/datanode/data/disk2"; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setUp() throws Exception { |
| 59 | + EnvFactory.getEnv() |
| 60 | + .getConfig() |
| 61 | + .getCommonConfig() |
| 62 | + .setDataReplicationFactor(2) |
| 63 | + .setDataRegionConsensusProtocolClass(ConsensusFactory.IOT_CONSENSUS); |
| 64 | + EnvFactory.getEnv().getConfig().getDataNodeConfig().setDnDataDirs(MULTI_DATA_DIRS); |
| 65 | + EnvFactory.getEnv().initClusterEnvironment(1, 3); |
| 66 | + } |
| 67 | + |
| 68 | + @After |
| 69 | + public void tearDown() throws Exception { |
| 70 | + EnvFactory.getEnv().cleanClusterEnvironment(); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void testRegionMigratePreservesDeletionWithMultiDataDirs() throws Exception { |
| 75 | + try (Connection connection = EnvFactory.getEnv().getTableConnection(); |
| 76 | + Statement statement = connection.createStatement()) { |
| 77 | + statement.execute("CREATE DATABASE test"); |
| 78 | + statement.execute("USE test"); |
| 79 | + statement.execute("CREATE TABLE t1 (s1 INT64 FIELD)"); |
| 80 | + statement.execute("INSERT INTO t1 (time, s1) VALUES (100, 100), (200, 200), (300, 300)"); |
| 81 | + statement.execute("FLUSH"); |
| 82 | + statement.execute("DELETE FROM t1 WHERE time <= 200"); |
| 83 | + statement.execute("FLUSH"); |
| 84 | + |
| 85 | + Map<Integer, Pair<Integer, Set<Integer>>> dataRegionMapWithLeader = |
| 86 | + getDataRegionMapWithLeader(statement); |
| 87 | + int dataRegionIdForTest = |
| 88 | + dataRegionMapWithLeader.keySet().stream().max(Integer::compareTo).orElseThrow(); |
| 89 | + assertDeletionVisibleOnAllReplicas(statement, dataRegionIdForTest, 1); |
| 90 | + |
| 91 | + Pair<Integer, Set<Integer>> leaderAndNodes = dataRegionMapWithLeader.get(dataRegionIdForTest); |
| 92 | + Set<Integer> allDataNodes = getAllDataNodes(statement); |
| 93 | + int leaderId = leaderAndNodes.getLeft(); |
| 94 | + int followerId = |
| 95 | + leaderAndNodes.getRight().stream().filter(id -> id != leaderId).findFirst().orElseThrow(); |
| 96 | + int destDataNodeId = |
| 97 | + allDataNodes.stream() |
| 98 | + .filter(id -> id != leaderId && id != followerId) |
| 99 | + .findFirst() |
| 100 | + .orElseThrow(); |
| 101 | + |
| 102 | + statement.execute( |
| 103 | + String.format( |
| 104 | + "migrate region %d from %d to %d", dataRegionIdForTest, leaderId, destDataNodeId)); |
| 105 | + |
| 106 | + final int finalDestDataNodeId = destDataNodeId; |
| 107 | + Awaitility.await() |
| 108 | + .atMost(10, TimeUnit.MINUTES) |
| 109 | + .pollDelay(1, TimeUnit.SECONDS) |
| 110 | + .pollInterval(2, TimeUnit.SECONDS) |
| 111 | + .untilAsserted( |
| 112 | + () -> { |
| 113 | + try (ResultSet showRegions = statement.executeQuery("SHOW REGIONS")) { |
| 114 | + boolean migrated = false; |
| 115 | + while (showRegions.next()) { |
| 116 | + if (showRegions.getInt("RegionId") == dataRegionIdForTest |
| 117 | + && showRegions.getInt("DataNodeId") == finalDestDataNodeId) { |
| 118 | + migrated = true; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + Assert.assertTrue(migrated); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + assertDeletionVisibleOnAllReplicas(statement, dataRegionIdForTest, 1); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private void assertDeletionVisibleOnAllReplicas( |
| 131 | + Statement statement, int dataRegionId, int expectedCount) throws Exception { |
| 132 | + Set<Integer> replicaDataNodeIds = getReplicaDataNodeIds(statement, dataRegionId); |
| 133 | + for (int dataNodeId : replicaDataNodeIds) { |
| 134 | + DataNodeWrapper dataNodeWrapper = |
| 135 | + EnvFactory.getEnv().dataNodeIdToWrapper(dataNodeId).orElseThrow(); |
| 136 | + Awaitility.await() |
| 137 | + .atMost(2, TimeUnit.MINUTES) |
| 138 | + .pollDelay(500, TimeUnit.MILLISECONDS) |
| 139 | + .pollInterval(1, TimeUnit.SECONDS) |
| 140 | + .untilAsserted(() -> assertDeletionVisibleOnReplica(dataNodeWrapper, expectedCount)); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private void assertDeletionVisibleOnReplica(DataNodeWrapper dataNodeWrapper, int expectedCount) |
| 145 | + throws Exception { |
| 146 | + try (Connection connection = |
| 147 | + EnvFactory.getEnv() |
| 148 | + .getConnection( |
| 149 | + dataNodeWrapper, |
| 150 | + SessionConfig.DEFAULT_USER, |
| 151 | + SessionConfig.DEFAULT_PASSWORD, |
| 152 | + BaseEnv.TABLE_SQL_DIALECT); |
| 153 | + Statement dataNodeStatement = connection.createStatement()) { |
| 154 | + dataNodeStatement.execute("USE test"); |
| 155 | + try (ResultSet countResultSet = dataNodeStatement.executeQuery("SELECT COUNT(s1) FROM t1")) { |
| 156 | + Assert.assertTrue(countResultSet.next()); |
| 157 | + Assert.assertEquals(expectedCount, countResultSet.getLong(1)); |
| 158 | + } |
| 159 | + try (ResultSet deletedRangeResultSet = |
| 160 | + dataNodeStatement.executeQuery("SELECT s1 FROM t1 WHERE time <= 200")) { |
| 161 | + Assert.assertFalse(deletedRangeResultSet.next()); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + private Set<Integer> getReplicaDataNodeIds(Statement statement, int dataRegionId) |
| 167 | + throws Exception { |
| 168 | + Set<Integer> replicaDataNodeIds = new HashSet<>(); |
| 169 | + try (ResultSet showRegions = statement.executeQuery("SHOW REGIONS")) { |
| 170 | + while (showRegions.next()) { |
| 171 | + if ("DataRegion".equals(showRegions.getString("Type")) |
| 172 | + && showRegions.getInt("RegionId") == dataRegionId) { |
| 173 | + replicaDataNodeIds.add(showRegions.getInt("DataNodeId")); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + Assert.assertFalse(replicaDataNodeIds.isEmpty()); |
| 178 | + return replicaDataNodeIds; |
| 179 | + } |
| 180 | +} |
0 commit comments