|
| 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.relational.it.schema; |
| 21 | + |
| 22 | +import org.apache.iotdb.commons.partition.executor.hash.BKDRHashExecutor; |
| 23 | +import org.apache.iotdb.db.it.utils.TestUtils; |
| 24 | +import org.apache.iotdb.it.env.EnvFactory; |
| 25 | +import org.apache.iotdb.it.framework.IoTDBTestRunner; |
| 26 | +import org.apache.iotdb.itbase.category.TableClusterIT; |
| 27 | +import org.apache.iotdb.itbase.category.TableLocalStandaloneIT; |
| 28 | +import org.apache.iotdb.itbase.env.BaseEnv; |
| 29 | + |
| 30 | +import org.apache.tsfile.file.metadata.IDeviceID; |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Assert; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.experimental.categories.Category; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | + |
| 38 | +import java.sql.Connection; |
| 39 | +import java.sql.ResultSet; |
| 40 | +import java.sql.SQLException; |
| 41 | +import java.sql.Statement; |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.HashSet; |
| 44 | +import java.util.Set; |
| 45 | + |
| 46 | +@RunWith(IoTDBTestRunner.class) |
| 47 | +@Category({TableLocalStandaloneIT.class, TableClusterIT.class}) |
| 48 | +public class IoTDBDatabaseMaxRegionGroupNumIT { |
| 49 | + |
| 50 | + private static final int SERIES_SLOT_NUM = 8; |
| 51 | + private static final int DEFAULT_SCHEMA_REGION_GROUP_NUM = 1; |
| 52 | + private static final int DEFAULT_DATA_REGION_GROUP_NUM = 2; |
| 53 | + private static final int MAX_SCHEMA_REGION_GROUP_NUM = 3; |
| 54 | + private static final int MAX_DATA_REGION_GROUP_NUM = 4; |
| 55 | + private static final String TABLE_NAME = "table1"; |
| 56 | + private static final BKDRHashExecutor PARTITION_EXECUTOR = new BKDRHashExecutor(SERIES_SLOT_NUM); |
| 57 | + |
| 58 | + @Before |
| 59 | + public void setUp() throws Exception { |
| 60 | + EnvFactory.getEnv() |
| 61 | + .getConfig() |
| 62 | + .getCommonConfig() |
| 63 | + .setSchemaRegionGroupExtensionPolicy("CUSTOM") |
| 64 | + .setDataRegionGroupExtensionPolicy("CUSTOM") |
| 65 | + .setDefaultSchemaRegionGroupNumPerDatabase(DEFAULT_SCHEMA_REGION_GROUP_NUM) |
| 66 | + .setDefaultDataRegionGroupNumPerDatabase(DEFAULT_DATA_REGION_GROUP_NUM) |
| 67 | + .setSeriesSlotNum(SERIES_SLOT_NUM) |
| 68 | + .setSeriesPartitionExecutorClass(BKDRHashExecutor.class.getName()) |
| 69 | + .setTimePartitionInterval(10); |
| 70 | + EnvFactory.getEnv().initClusterEnvironment(); |
| 71 | + } |
| 72 | + |
| 73 | + @After |
| 74 | + public void tearDown() throws Exception { |
| 75 | + EnvFactory.getEnv().cleanClusterEnvironment(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testCreateAndAlterMaxRegionGroupNum() throws SQLException { |
| 80 | + try (final Connection connection = |
| 81 | + EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT); |
| 82 | + final Statement statement = connection.createStatement()) { |
| 83 | + statement.execute( |
| 84 | + "create database test_create with(max_schema_region_group_num=3, max_data_region_group_num=4)"); |
| 85 | + TestUtils.assertResultSetEqual( |
| 86 | + statement.executeQuery( |
| 87 | + "select database, max_schema_region_group_num, max_data_region_group_num " |
| 88 | + + "from information_schema.databases where database = 'test_create'"), |
| 89 | + "database,max_schema_region_group_num,max_data_region_group_num,", |
| 90 | + Collections.singleton("test_create,3,4,")); |
| 91 | + |
| 92 | + statement.execute("create database test_alter"); |
| 93 | + statement.execute( |
| 94 | + "alter database test_alter set properties max_schema_region_group_num=3, max_data_region_group_num=4"); |
| 95 | + try (final ResultSet resultSet = statement.executeQuery("show databases details")) { |
| 96 | + boolean found = false; |
| 97 | + while (resultSet.next()) { |
| 98 | + if (!"test_alter".equals(resultSet.getString("Database"))) { |
| 99 | + continue; |
| 100 | + } |
| 101 | + found = true; |
| 102 | + org.junit.Assert.assertEquals(3, resultSet.getInt("MaxSchemaRegionGroupNum")); |
| 103 | + org.junit.Assert.assertEquals(4, resultSet.getInt("MaxDataRegionGroupNum")); |
| 104 | + } |
| 105 | + org.junit.Assert.assertTrue(found); |
| 106 | + } |
| 107 | + |
| 108 | + Assert.assertThrows( |
| 109 | + SQLException.class, |
| 110 | + () -> |
| 111 | + statement.execute( |
| 112 | + "create database test_deprecated with(schema_region_group_num=4, data_region_group_num=5)")); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testAllocatedRegionGroupNumEqualsQuotaAfterAlterAndWrite() throws SQLException { |
| 118 | + try (final Connection connection = |
| 119 | + EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT); |
| 120 | + final Statement statement = connection.createStatement()) { |
| 121 | + statement.execute("create database test_partition"); |
| 122 | + statement.execute("use test_partition"); |
| 123 | + statement.execute("create table " + TABLE_NAME + "(device string tag, s1 int32 field)"); |
| 124 | + |
| 125 | + final Set<Integer> usedSeriesSlots = new HashSet<>(); |
| 126 | + final String firstDevice = getDeviceInNewSeriesSlot(usedSeriesSlots); |
| 127 | + final String secondDevice = getDeviceInNewSeriesSlot(usedSeriesSlots); |
| 128 | + |
| 129 | + insertData(statement, firstDevice, 0, 0); |
| 130 | + assertRegionGroupNum( |
| 131 | + statement, |
| 132 | + "test_partition", |
| 133 | + DEFAULT_SCHEMA_REGION_GROUP_NUM, |
| 134 | + DEFAULT_SCHEMA_REGION_GROUP_NUM, |
| 135 | + DEFAULT_DATA_REGION_GROUP_NUM, |
| 136 | + DEFAULT_DATA_REGION_GROUP_NUM); |
| 137 | + |
| 138 | + statement.execute( |
| 139 | + "alter database test_partition set properties max_schema_region_group_num=" |
| 140 | + + MAX_SCHEMA_REGION_GROUP_NUM); |
| 141 | + insertData(statement, secondDevice, 0, 1); |
| 142 | + assertAllocatedRegionGroupNumEqualsQuota( |
| 143 | + statement, "test_partition", MAX_SCHEMA_REGION_GROUP_NUM, DEFAULT_DATA_REGION_GROUP_NUM); |
| 144 | + |
| 145 | + statement.execute( |
| 146 | + "alter database test_partition set properties max_data_region_group_num=" |
| 147 | + + MAX_DATA_REGION_GROUP_NUM); |
| 148 | + insertData(statement, secondDevice, 20, 2); |
| 149 | + assertAllocatedRegionGroupNumEqualsQuota( |
| 150 | + statement, "test_partition", MAX_SCHEMA_REGION_GROUP_NUM, MAX_DATA_REGION_GROUP_NUM); |
| 151 | + |
| 152 | + TestUtils.assertResultSetEqual( |
| 153 | + statement.executeQuery("select count(*) from " + TABLE_NAME), |
| 154 | + "_col0,", |
| 155 | + Collections.singleton("3,")); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static void insertData( |
| 160 | + final Statement statement, final String device, final int time, final int value) |
| 161 | + throws SQLException { |
| 162 | + statement.execute( |
| 163 | + "insert into " |
| 164 | + + TABLE_NAME |
| 165 | + + "(time, device, s1) values(" |
| 166 | + + time |
| 167 | + + ", '" |
| 168 | + + device |
| 169 | + + "', " |
| 170 | + + value |
| 171 | + + ")"); |
| 172 | + } |
| 173 | + |
| 174 | + private static String getDeviceInNewSeriesSlot(final Set<Integer> usedSeriesSlots) { |
| 175 | + for (int i = 0; i < 1_000; i++) { |
| 176 | + final String device = "d" + i; |
| 177 | + if (usedSeriesSlots.add(getSeriesSlot(device))) { |
| 178 | + return device; |
| 179 | + } |
| 180 | + } |
| 181 | + throw new AssertionError("Failed to find a device in a new series partition slot"); |
| 182 | + } |
| 183 | + |
| 184 | + private static int getSeriesSlot(final String device) { |
| 185 | + return PARTITION_EXECUTOR |
| 186 | + .getSeriesPartitionSlot( |
| 187 | + IDeviceID.Factory.DEFAULT_FACTORY.create(new String[] {TABLE_NAME, device})) |
| 188 | + .getSlotId(); |
| 189 | + } |
| 190 | + |
| 191 | + private static void assertAllocatedRegionGroupNumEqualsQuota( |
| 192 | + final Statement statement, |
| 193 | + final String database, |
| 194 | + final int schemaRegionGroupQuota, |
| 195 | + final int dataRegionGroupQuota) |
| 196 | + throws SQLException { |
| 197 | + assertRegionGroupNum( |
| 198 | + statement, |
| 199 | + database, |
| 200 | + schemaRegionGroupQuota, |
| 201 | + schemaRegionGroupQuota, |
| 202 | + dataRegionGroupQuota, |
| 203 | + dataRegionGroupQuota); |
| 204 | + } |
| 205 | + |
| 206 | + private static void assertRegionGroupNum( |
| 207 | + final Statement statement, |
| 208 | + final String database, |
| 209 | + final int schemaRegionGroupNum, |
| 210 | + final int maxSchemaRegionGroupNum, |
| 211 | + final int dataRegionGroupNum, |
| 212 | + final int maxDataRegionGroupNum) |
| 213 | + throws SQLException { |
| 214 | + try (final ResultSet resultSet = |
| 215 | + statement.executeQuery( |
| 216 | + "select schema_region_group_num, max_schema_region_group_num, " |
| 217 | + + "data_region_group_num, max_data_region_group_num " |
| 218 | + + "from information_schema.databases where database = '" |
| 219 | + + database |
| 220 | + + "'")) { |
| 221 | + Assert.assertTrue(resultSet.next()); |
| 222 | + Assert.assertEquals(schemaRegionGroupNum, resultSet.getInt("schema_region_group_num")); |
| 223 | + Assert.assertEquals(maxSchemaRegionGroupNum, resultSet.getInt("max_schema_region_group_num")); |
| 224 | + Assert.assertEquals(dataRegionGroupNum, resultSet.getInt("data_region_group_num")); |
| 225 | + Assert.assertEquals(maxDataRegionGroupNum, resultSet.getInt("max_data_region_group_num")); |
| 226 | + Assert.assertFalse(resultSet.next()); |
| 227 | + } |
| 228 | + } |
| 229 | +} |
0 commit comments