Skip to content

Commit a98bd56

Browse files
authored
Supply the max_schema/data_region_group_num param to modify schema when create or alter database (#17988)
1 parent c1843f2 commit a98bd56

18 files changed

Lines changed: 619 additions & 156 deletions

File tree

integration-test/src/test/java/org/apache/iotdb/confignode/it/database/IoTDBDatabaseRegionControlIT.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ public static void setUp() throws Exception {
5858
EnvFactory.getEnv()
5959
.getConfig()
6060
.getCommonConfig()
61+
.setSchemaRegionGroupExtensionPolicy("CUSTOM")
62+
.setDataRegionGroupExtensionPolicy("CUSTOM")
6163
.setDefaultSchemaRegionGroupNumPerDatabase(testDefaultSchemaRegionGroupNumPerDatabase)
6264
.setDefaultDataRegionGroupNumPerDatabase(testDefaultDataRegionGroupNumPerDatabase);
6365

@@ -132,7 +134,7 @@ public void testRegionGroupNumControlThroughCreation()
132134
final int testDataRegionGroupNum = 3;
133135
String createDatabaseSQL =
134136
String.format(
135-
"CREATE DATABASE %s WITH SCHEMA_REGION_GROUP_NUM=%d, DATA_REGION_GROUP_NUM=%d;",
137+
"CREATE DATABASE %s WITH MAX_SCHEMA_REGION_GROUP_NUM=%d, MAX_DATA_REGION_GROUP_NUM=%d;",
136138
database, testSchemaRegionGroupNum, testDataRegionGroupNum);
137139
statement.execute(createDatabaseSQL);
138140
insertBatchData(statement, database, 0);
@@ -204,7 +206,7 @@ public void testRegionGroupNumControlThroughAlter()
204206
final int testDataRegionGroupNum = 3;
205207
String alterDatabaseSQL =
206208
String.format(
207-
"ALTER DATABASE %s WITH SCHEMA_REGION_GROUP_NUM=%d, DATA_REGION_GROUP_NUM=%d;",
209+
"ALTER DATABASE %s WITH MAX_SCHEMA_REGION_GROUP_NUM=%d, MAX_DATA_REGION_GROUP_NUM=%d;",
208210
database, testSchemaRegionGroupNum, testDataRegionGroupNum);
209211
statement.execute(alterDatabaseSQL);
210212
insertBatchData(statement, database, batchSize);
@@ -233,4 +235,16 @@ public void testRegionGroupNumControlThroughAlter()
233235
Assert.assertEquals(testDataRegionGroupNum, dataRegionGroupNum.get());
234236
}
235237
}
238+
239+
@Test
240+
public void testDeprecatedRegionGroupNumSqlRejected() throws SQLException {
241+
try (final Connection connection = EnvFactory.getEnv().getConnection();
242+
final Statement statement = connection.createStatement()) {
243+
Assert.assertThrows(
244+
SQLException.class,
245+
() ->
246+
statement.execute(
247+
"CREATE DATABASE root.paradise3 WITH SCHEMA_REGION_GROUP_NUM=3, DATA_REGION_GROUP_NUM=4;"));
248+
}
249+
}
236250
}

integration-test/src/test/java/org/apache/iotdb/pipe/it/dual/tablemodel/manual/enhanced/IoTDBPipeMetaIT.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,6 @@ public void testNoTree() throws Exception {
230230
senderEnv,
231231
Arrays.asList(
232232
"create database root.test",
233-
"alter database root.test with schema_region_group_num=2, data_region_group_num=3",
234233
"create timeSeries root.test.d1.s1 int32",
235234
"insert into root.test.d1 (s1) values (1)"));
236235

integration-test/src/test/java/org/apache/iotdb/pipe/it/dual/treemodel/auto/enhanced/IoTDBPipeIdempotentIT.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public void setUp() {
6565
// Limit the schemaRegion number to 1 to guarantee the after sql executed on the same region
6666
// of the tested idempotent sql.
6767
.setDefaultSchemaRegionGroupNumPerDatabase(1)
68+
.setSchemaRegionGroupExtensionPolicy("CUSTOM")
69+
.setDataRegionGroupExtensionPolicy("CUSTOM")
6870
.setConfigNodeConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS)
6971
.setSchemaRegionConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS)
7072
.setEnforceStrongPassword(false)
@@ -75,6 +77,8 @@ public void setUp() {
7577
.getConfig()
7678
.getCommonConfig()
7779
.setAutoCreateSchemaEnabled(true)
80+
.setSchemaRegionGroupExtensionPolicy("CUSTOM")
81+
.setDataRegionGroupExtensionPolicy("CUSTOM")
7882
.setConfigNodeConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS)
7983
.setSchemaRegionConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS)
8084
.setEnforceStrongPassword(false)
@@ -289,7 +293,7 @@ public void testCreateDatabaseIdempotent() throws Exception {
289293
public void testAlterDatabaseIdempotent() throws Exception {
290294
testIdempotent(
291295
Collections.singletonList("create database root.sg1"),
292-
"ALTER DATABASE root.sg1 WITH SCHEMA_REGION_GROUP_NUM=2, DATA_REGION_GROUP_NUM=3;",
296+
"ALTER DATABASE root.sg1 WITH MAX_SCHEMA_REGION_GROUP_NUM=2, MAX_DATA_REGION_GROUP_NUM=3;",
293297
"create database root.sg2",
294298
"count databases",
295299
"count,",

integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBDatabaseIT.java

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.iotdb.itbase.category.TableClusterIT;
2828
import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
2929
import org.apache.iotdb.itbase.env.BaseEnv;
30+
import org.apache.iotdb.jdbc.IoTDBSQLException;
3031

3132
import org.junit.After;
3233
import org.junit.Assert;
@@ -203,8 +204,7 @@ public void testManageDatabase() {
203204
statement.execute("drop database IF EXISTS test");
204205

205206
// Test create database with properties
206-
statement.execute(
207-
"create database test_prop with (ttl=300, schema_region_group_num=DEFAULT, time_partition_interval=100000)");
207+
statement.execute("create database test_prop with (ttl=300, time_partition_interval=100000)");
208208
databaseNames = new String[] {"test_prop"};
209209
TTLs = new String[] {"300"};
210210
timePartitionInterval = new int[] {100000};
@@ -811,8 +811,11 @@ public void testMixedDatabase() throws SQLException {
811811
try (final Connection connection = EnvFactory.getEnv().getConnection();
812812
final Statement statement = connection.createStatement()) {
813813
statement.execute("create database root.test");
814-
statement.execute(
815-
"alter database root.test WITH SCHEMA_REGION_GROUP_NUM=2, DATA_REGION_GROUP_NUM=3");
814+
Assert.assertThrows(
815+
IoTDBSQLException.class,
816+
() ->
817+
statement.execute(
818+
"alter database root.test WITH MAX_SCHEMA_REGION_GROUP_NUM=2, MAX_DATA_REGION_GROUP_NUM=3"));
816819
statement.execute("insert into root.test.d1 (s1) values(1)");
817820
statement.execute("drop database root.test");
818821
}
@@ -882,6 +885,34 @@ public void testCountDatabases() throws SQLException {
882885
}
883886
}
884887

888+
@Test
889+
public void testMaxRegionGroupNumRejectedInAutoPolicy() throws SQLException {
890+
try (final Connection connection =
891+
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
892+
final Statement statement = connection.createStatement()) {
893+
try {
894+
statement.execute("create database test_max with(max_data_region_group_num=4)");
895+
fail("max_data_region_group_num should be rejected under AUTO policy");
896+
} catch (final SQLException e) {
897+
assertTrue(
898+
e.getMessage()
899+
.contains(
900+
"max_data_region_group_num can only be set when data_region_group_extension_policy is CUSTOM"));
901+
}
902+
903+
statement.execute("create database test_max");
904+
try {
905+
statement.execute("alter database test_max set properties max_data_region_group_num=4");
906+
fail("max_data_region_group_num should be rejected under AUTO policy");
907+
} catch (final SQLException e) {
908+
assertTrue(
909+
e.getMessage()
910+
.contains(
911+
"max_data_region_group_num can only be set when data_region_group_extension_policy is CUSTOM"));
912+
}
913+
}
914+
}
915+
885916
@Test
886917
public void testDBAuth() throws SQLException {
887918
try (final Connection adminCon = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
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

Comments
 (0)