|
| 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.removedatanode; |
| 21 | + |
| 22 | +import org.apache.iotdb.commons.schema.column.ColumnHeaderConstant; |
| 23 | +import org.apache.iotdb.consensus.ConsensusFactory; |
| 24 | +import org.apache.iotdb.it.env.EnvFactory; |
| 25 | +import org.apache.iotdb.it.framework.IoTDBTestRunner; |
| 26 | +import org.apache.iotdb.itbase.category.LocalStandaloneIT; |
| 27 | +import org.apache.iotdb.jdbc.IoTDBSQLException; |
| 28 | + |
| 29 | +import org.junit.After; |
| 30 | +import org.junit.Assert; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | +import org.junit.experimental.categories.Category; |
| 34 | +import org.junit.runner.RunWith; |
| 35 | + |
| 36 | +import java.sql.Connection; |
| 37 | +import java.sql.ResultSet; |
| 38 | +import java.sql.Statement; |
| 39 | +import java.util.HashSet; |
| 40 | +import java.util.Set; |
| 41 | + |
| 42 | +import static org.apache.iotdb.confignode.it.removedatanode.IoTDBRemoveDataNodeUtils.generateRemoveString; |
| 43 | +import static org.apache.iotdb.confignode.it.removedatanode.IoTDBRemoveDataNodeUtils.selectRemoveDataNodes; |
| 44 | +import static org.apache.iotdb.util.MagicUtils.makeItCloseQuietly; |
| 45 | + |
| 46 | +/** |
| 47 | + * Removing the last DataNode of a single-replica cluster must be rejected. This only needs a 1C1D |
| 48 | + * cluster, so it lives in the 1C1D (LocalStandaloneIT) suite, separate from the multi-DataNode |
| 49 | + * removal tests in {@link IoTDBRemoveDataNodeNormalIT}. |
| 50 | + */ |
| 51 | +@Category({LocalStandaloneIT.class}) |
| 52 | +@RunWith(IoTDBTestRunner.class) |
| 53 | +public class IoTDBRemoveLastDataNodeIT { |
| 54 | + |
| 55 | + private static final String SHOW_DATANODES = "show datanodes"; |
| 56 | + |
| 57 | + @Before |
| 58 | + public void setUp() throws Exception { |
| 59 | + EnvFactory.getEnv() |
| 60 | + .getConfig() |
| 61 | + .getCommonConfig() |
| 62 | + .setConfigNodeConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS) |
| 63 | + .setSchemaRegionConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS); |
| 64 | + } |
| 65 | + |
| 66 | + @After |
| 67 | + public void tearDown() throws InterruptedException { |
| 68 | + EnvFactory.getEnv().cleanClusterEnvironment(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void failWhenRemovingLastSingleReplicaDataNodeUseSQL() throws Exception { |
| 73 | + // With a single replica (schema_replication_factor and data_replication_factor are both 1), |
| 74 | + // removing DataNodes is still supported as long as more than one DataNode remains, but the last |
| 75 | + // remaining DataNode cannot be removed because there is nowhere to migrate its regions to. |
| 76 | + // Here we set up 1C1D with single replica and try to remove the only DataNode, which must fail |
| 77 | + // because removing it would leave the cluster with no DataNode. |
| 78 | + EnvFactory.getEnv() |
| 79 | + .getConfig() |
| 80 | + .getCommonConfig() |
| 81 | + .setDataRegionConsensusProtocolClass(ConsensusFactory.IOT_CONSENSUS) |
| 82 | + .setSchemaReplicationFactor(1) |
| 83 | + .setDataReplicationFactor(1) |
| 84 | + .setDefaultDataRegionGroupNumPerDatabase(1); |
| 85 | + EnvFactory.getEnv().initClusterEnvironment(1, 1); |
| 86 | + |
| 87 | + try (final Connection connection = makeItCloseQuietly(EnvFactory.getEnv().getConnection()); |
| 88 | + final Statement statement = makeItCloseQuietly(connection.createStatement()); |
| 89 | + final ResultSet resultSet = statement.executeQuery(SHOW_DATANODES)) { |
| 90 | + final Set<Integer> allDataNodeId = new HashSet<>(); |
| 91 | + while (resultSet.next()) { |
| 92 | + allDataNodeId.add(resultSet.getInt(ColumnHeaderConstant.NODE_ID)); |
| 93 | + } |
| 94 | + |
| 95 | + final String removeDataNodeSQL = |
| 96 | + generateRemoveString(selectRemoveDataNodes(allDataNodeId, 1)); |
| 97 | + try { |
| 98 | + statement.execute(removeDataNodeSQL); |
| 99 | + Assert.fail( |
| 100 | + "Remove DataNode should fail when it would leave no DataNode under single replica"); |
| 101 | + } catch (final IoTDBSQLException e) { |
| 102 | + // The unified rejection message reports the gap and, for a single replica, appends the |
| 103 | + // "at least one DataNode must always remain" hint. |
| 104 | + Assert.assertTrue(e.getMessage(), e.getMessage().contains("Cannot remove")); |
| 105 | + Assert.assertTrue(e.getMessage(), e.getMessage().contains("single replica")); |
| 106 | + Assert.assertFalse( |
| 107 | + e.getMessage(), e.getMessage().contains("Failed to remove all requested data nodes")); |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments