Skip to content

Commit 797319b

Browse files
authored
The new value cannot be less than the current maximum quota. The quota uses the default value if unspecified in the ALTER DATABASE statement (#18154)
1 parent 012026f commit 797319b

5 files changed

Lines changed: 219 additions & 7 deletions

File tree

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

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
import java.io.IOException;
4141
import java.sql.Connection;
42+
import java.sql.ResultSet;
4243
import java.sql.SQLException;
4344
import java.sql.Statement;
4445
import java.util.Collections;
@@ -164,6 +165,53 @@ public void testRegionGroupNumControlThroughCreation()
164165
}
165166
}
166167

168+
@Test
169+
public void testRecreateWithPartialMaxRegionGroupNumUsesDefaultCounterpart() throws SQLException {
170+
try (final Connection connection = EnvFactory.getEnv().getConnection();
171+
final Statement statement = connection.createStatement()) {
172+
final String database = "root.rg_tree_create_schema";
173+
statement.execute(String.format("CREATE DATABASE %s;", database));
174+
175+
try (final ResultSet resultSet =
176+
statement.executeQuery("SHOW DATABASES DETAILS " + database)) {
177+
Assert.assertTrue(resultSet.next());
178+
Assert.assertEquals(
179+
testDefaultSchemaRegionGroupNumPerDatabase,
180+
resultSet.getInt("MaxSchemaRegionGroupNum"));
181+
Assert.assertEquals(
182+
testDefaultDataRegionGroupNumPerDatabase, resultSet.getInt("MaxDataRegionGroupNum"));
183+
Assert.assertFalse(resultSet.next());
184+
}
185+
186+
statement.execute(String.format("DROP DATABASE %s;", database));
187+
statement.execute(
188+
String.format("CREATE DATABASE %s WITH MAX_SCHEMA_REGION_GROUP_NUM=3;", database));
189+
190+
try (final ResultSet resultSet =
191+
statement.executeQuery("SHOW DATABASES DETAILS " + database)) {
192+
Assert.assertTrue(resultSet.next());
193+
Assert.assertEquals(3, resultSet.getInt("MaxSchemaRegionGroupNum"));
194+
Assert.assertEquals(
195+
testDefaultDataRegionGroupNumPerDatabase, resultSet.getInt("MaxDataRegionGroupNum"));
196+
Assert.assertFalse(resultSet.next());
197+
}
198+
199+
final String dataDatabase = "root.rg_tree_create_data";
200+
statement.execute(
201+
String.format("CREATE DATABASE %s WITH MAX_DATA_REGION_GROUP_NUM=4;", dataDatabase));
202+
203+
try (final ResultSet resultSet =
204+
statement.executeQuery("SHOW DATABASES DETAILS " + dataDatabase)) {
205+
Assert.assertTrue(resultSet.next());
206+
Assert.assertEquals(
207+
testDefaultSchemaRegionGroupNumPerDatabase,
208+
resultSet.getInt("MaxSchemaRegionGroupNum"));
209+
Assert.assertEquals(4, resultSet.getInt("MaxDataRegionGroupNum"));
210+
Assert.assertFalse(resultSet.next());
211+
}
212+
}
213+
}
214+
167215
@Test
168216
public void testRegionGroupNumControlThroughAlter()
169217
throws SQLException, ClientManagerException, IOException, InterruptedException, TException {
@@ -236,6 +284,69 @@ public void testRegionGroupNumControlThroughAlter()
236284
}
237285
}
238286

287+
@Test
288+
public void testAlterMaxDataRegionGroupNumCannotDecrease() throws SQLException {
289+
try (final Connection connection = EnvFactory.getEnv().getConnection();
290+
final Statement statement = connection.createStatement()) {
291+
final String database = "root.rg_tree_decrease";
292+
statement.execute(
293+
String.format("CREATE DATABASE %s WITH MAX_DATA_REGION_GROUP_NUM=8;", database));
294+
statement.execute(
295+
String.format("ALTER DATABASE %s WITH MAX_DATA_REGION_GROUP_NUM=16;", database));
296+
297+
final SQLException exception =
298+
Assert.assertThrows(
299+
SQLException.class,
300+
() ->
301+
statement.execute(
302+
String.format(
303+
"ALTER DATABASE %s WITH MAX_DATA_REGION_GROUP_NUM=12;", database)));
304+
Assert.assertTrue(
305+
exception.getMessage(),
306+
exception
307+
.getMessage()
308+
.contains(
309+
"MaxDataRegionGroupNum should be greater than or equal to current max "
310+
+ "DataRegionGroupNum: 16."));
311+
312+
try (final ResultSet resultSet =
313+
statement.executeQuery("SHOW DATABASES DETAILS " + database)) {
314+
Assert.assertTrue(resultSet.next());
315+
Assert.assertEquals(16, resultSet.getInt("MaxDataRegionGroupNum"));
316+
Assert.assertFalse(resultSet.next());
317+
}
318+
319+
final String schemaDatabase = "root.rg_tree_decrease_schema";
320+
statement.execute(
321+
String.format("CREATE DATABASE %s WITH MAX_SCHEMA_REGION_GROUP_NUM=4;", schemaDatabase));
322+
statement.execute(
323+
String.format("ALTER DATABASE %s WITH MAX_SCHEMA_REGION_GROUP_NUM=5;", schemaDatabase));
324+
325+
final SQLException schemaException =
326+
Assert.assertThrows(
327+
SQLException.class,
328+
() ->
329+
statement.execute(
330+
String.format(
331+
"ALTER DATABASE %s WITH MAX_SCHEMA_REGION_GROUP_NUM=3;",
332+
schemaDatabase)));
333+
Assert.assertTrue(
334+
schemaException.getMessage(),
335+
schemaException
336+
.getMessage()
337+
.contains(
338+
"MaxSchemaRegionGroupNum should be greater than or equal to current max "
339+
+ "SchemaRegionGroupNum: 5."));
340+
341+
try (final ResultSet resultSet =
342+
statement.executeQuery("SHOW DATABASES DETAILS " + schemaDatabase)) {
343+
Assert.assertTrue(resultSet.next());
344+
Assert.assertEquals(5, resultSet.getInt("MaxSchemaRegionGroupNum"));
345+
Assert.assertFalse(resultSet.next());
346+
}
347+
}
348+
}
349+
239350
@Test
240351
public void testDeprecatedRegionGroupNumSqlRejected() throws SQLException {
241352
try (final Connection connection = EnvFactory.getEnv().getConnection();

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

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ public void testCreateAndAlterMaxRegionGroupNum() throws SQLException {
8989
"database,max_schema_region_group_num,max_data_region_group_num,",
9090
Collections.singleton("test_create,3,4,"));
9191

92+
statement.execute("create database test_create_schema with(max_schema_region_group_num=3)");
93+
TestUtils.assertResultSetEqual(
94+
statement.executeQuery(
95+
"select database, max_schema_region_group_num, max_data_region_group_num "
96+
+ "from information_schema.databases where database = 'test_create_schema'"),
97+
"database,max_schema_region_group_num,max_data_region_group_num,",
98+
Collections.singleton("test_create_schema,3," + DEFAULT_DATA_REGION_GROUP_NUM + ","));
99+
100+
statement.execute("create database test_create_data with(max_data_region_group_num=4)");
101+
TestUtils.assertResultSetEqual(
102+
statement.executeQuery(
103+
"select database, max_schema_region_group_num, max_data_region_group_num "
104+
+ "from information_schema.databases where database = 'test_create_data'"),
105+
"database,max_schema_region_group_num,max_data_region_group_num,",
106+
Collections.singleton("test_create_data," + DEFAULT_SCHEMA_REGION_GROUP_NUM + ",4,"));
107+
92108
statement.execute("create database test_alter");
93109
statement.execute(
94110
"alter database test_alter set properties max_schema_region_group_num=3, max_data_region_group_num=4");
@@ -113,6 +129,63 @@ public void testCreateAndAlterMaxRegionGroupNum() throws SQLException {
113129
}
114130
}
115131

132+
@Test
133+
public void testAlterMaxRegionGroupNumCannotDecrease() throws SQLException {
134+
try (final Connection connection =
135+
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
136+
final Statement statement = connection.createStatement()) {
137+
statement.execute("create database test_decrease_data with(max_data_region_group_num=8)");
138+
statement.execute(
139+
"alter database test_decrease_data set properties max_data_region_group_num=16");
140+
141+
final SQLException exception =
142+
Assert.assertThrows(
143+
SQLException.class,
144+
() ->
145+
statement.execute(
146+
"alter database test_decrease_data set properties max_data_region_group_num=12"));
147+
Assert.assertTrue(
148+
exception.getMessage(),
149+
exception
150+
.getMessage()
151+
.contains(
152+
"MaxDataRegionGroupNum should be greater than or equal to current max "
153+
+ "DataRegionGroupNum: 16."));
154+
155+
TestUtils.assertResultSetEqual(
156+
statement.executeQuery(
157+
"select database, max_data_region_group_num from information_schema.databases "
158+
+ "where database = 'test_decrease_data'"),
159+
"database,max_data_region_group_num,",
160+
Collections.singleton("test_decrease_data,16,"));
161+
162+
statement.execute("create database test_decrease_schema with(max_schema_region_group_num=4)");
163+
statement.execute(
164+
"alter database test_decrease_schema set properties max_schema_region_group_num=5");
165+
166+
final SQLException schemaException =
167+
Assert.assertThrows(
168+
SQLException.class,
169+
() ->
170+
statement.execute(
171+
"alter database test_decrease_schema set properties max_schema_region_group_num=3"));
172+
Assert.assertTrue(
173+
schemaException.getMessage(),
174+
schemaException
175+
.getMessage()
176+
.contains(
177+
"MaxSchemaRegionGroupNum should be greater than or equal to current max "
178+
+ "SchemaRegionGroupNum: 5."));
179+
180+
TestUtils.assertResultSetEqual(
181+
statement.executeQuery(
182+
"select database, max_schema_region_group_num from information_schema.databases "
183+
+ "where database = 'test_decrease_schema'"),
184+
"database,max_schema_region_group_num,",
185+
Collections.singleton("test_decrease_schema,5,"));
186+
}
187+
}
188+
116189
@Test
117190
public void testAllocatedRegionGroupNumEqualsQuotaAfterAlterAndWrite() throws SQLException {
118191
try (final Connection connection =

‎iotdb-core/confignode/src/main/i18n/en/org/apache/iotdb/confignode/i18n/ManagerMessages.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,9 @@ private ManagerMessages() {}
598598
public static final String MESSAGE_CURRENT_SCHEMAREGIONGROUPNUM_ARG_ALTER_SCHEMAREGIONGROUPNUM_ARG_F7495BC2 = "Current SchemaRegionGroupNum: %d, Alter SchemaRegionGroupNum: %d";
599599
public static final String MESSAGE_FAILED_ALTER_DATABASE_DATAREGIONGROUPNUM_COULD_ONLY_INCREASED_84283EB5 = "Failed to alter database. The DataRegionGroupNum could only be increased. ";
600600
public static final String MESSAGE_CURRENT_DATAREGIONGROUPNUM_ARG_ALTER_DATAREGIONGROUPNUM_ARG_61C6E978 = "Current DataRegionGroupNum: %d, Alter DataRegionGroupNum: %d";
601+
public static final String MESSAGE_ARG_SHOULD_BE_GREATER_THAN_OR_EQUAL_TO_CURRENT_MIN_ARG_REGIONGROUPNUM_ARG_B81D93DF = "%s should be greater than or equal to current min %sRegionGroupNum: %d.";
602+
public static final String MESSAGE_ARG_SHOULD_BE_GREATER_THAN_OR_EQUAL_TO_CURRENT_MAX_ARG_REGIONGROUPNUM_ARG_3D170323 = "%s should be greater than or equal to current max %sRegionGroupNum: %d.";
603+
public static final String MESSAGE_ARG_SHOULD_BE_GREATER_THAN_OR_EQUAL_TO_ALLOCATED_ARG_REGIONGROUPNUM_ARG_994394A1 = "%s should be greater than or equal to allocated %sRegionGroupNum: %d.";
601604
public static final String MESSAGE_FAILED_CREATE_DATABASE_SCHEMAREPLICATIONFACTOR_SHOULD_POSITIVE_8847F33C = "Failed to create database. The schemaReplicationFactor should be positive.";
602605
public static final String MESSAGE_FAILED_CREATE_DATABASE_DATAREPLICATIONFACTOR_SHOULD_POSITIVE_C2565B7E = "Failed to create database. The dataReplicationFactor should be positive.";
603606
public static final String MESSAGE_FAILED_CREATE_DATABASE_TIMEPARTITIONORIGIN_SHOULD_NON_NEGATIVE_BD0595C9 = "Failed to create database. The timePartitionOrigin should be non-negative.";

‎iotdb-core/confignode/src/main/i18n/zh/org/apache/iotdb/confignode/i18n/ManagerMessages.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,9 @@ private ManagerMessages() {}
590590
public static final String MESSAGE_CURRENT_SCHEMAREGIONGROUPNUM_ARG_ALTER_SCHEMAREGIONGROUPNUM_ARG_F7495BC2 = "当前 SchemaRegionGroupNum:%d,待修改的 SchemaRegionGroupNum:%d";
591591
public static final String MESSAGE_FAILED_ALTER_DATABASE_DATAREGIONGROUPNUM_COULD_ONLY_INCREASED_84283EB5 = "修改数据库失败。DataRegionGroupNum 只能增加。";
592592
public static final String MESSAGE_CURRENT_DATAREGIONGROUPNUM_ARG_ALTER_DATAREGIONGROUPNUM_ARG_61C6E978 = "当前 DataRegionGroupNum:%d,待修改的 DataRegionGroupNum:%d";
593+
public static final String MESSAGE_ARG_SHOULD_BE_GREATER_THAN_OR_EQUAL_TO_CURRENT_MIN_ARG_REGIONGROUPNUM_ARG_B81D93DF = "%s 应大于等于当前最小 %sRegionGroupNum:%d。";
594+
public static final String MESSAGE_ARG_SHOULD_BE_GREATER_THAN_OR_EQUAL_TO_CURRENT_MAX_ARG_REGIONGROUPNUM_ARG_3D170323 = "%s 应大于等于当前最大 %sRegionGroupNum:%d。";
595+
public static final String MESSAGE_ARG_SHOULD_BE_GREATER_THAN_OR_EQUAL_TO_ALLOCATED_ARG_REGIONGROUPNUM_ARG_994394A1 = "%s 应大于等于已分配的 %sRegionGroupNum:%d。";
593596
public static final String MESSAGE_FAILED_CREATE_DATABASE_SCHEMAREPLICATIONFACTOR_SHOULD_POSITIVE_8847F33C = "创建数据库失败。schemaReplicationFactor 应为正数。";
594597
public static final String MESSAGE_FAILED_CREATE_DATABASE_DATAREPLICATIONFACTOR_SHOULD_POSITIVE_C2565B7E = "创建数据库失败。dataReplicationFactor 应为正数。";
595598
public static final String MESSAGE_FAILED_CREATE_DATABASE_TIMEPARTITIONORIGIN_SHOULD_NON_NEGATIVE_BD0595C9 = "创建数据库失败。timePartitionOrigin 应为非负数。";

‎iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/schema/ClusterSchemaManager.java‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -934,16 +934,16 @@ public static TSStatus enrichDatabaseSchemaWithDefaultProperties(
934934
.FAILED_TO_CREATE_DATABASE_THE_DATAREGIONGROUPNUM_SHOULD_BE_POSITIVE);
935935
}
936936

937-
if (databaseSchema.isSetMaxSchemaRegionGroupNum()) {
937+
if (!isErrorStatus(errorResp) && databaseSchema.isSetMaxSchemaRegionGroupNum()) {
938938
errorResp =
939939
validateMaxRegionGroupNumOnCreation(databaseSchema, TConsensusGroupType.SchemaRegion);
940940
}
941-
if (databaseSchema.isSetMaxDataRegionGroupNum()) {
941+
if (!isErrorStatus(errorResp) && databaseSchema.isSetMaxDataRegionGroupNum()) {
942942
errorResp =
943943
validateMaxRegionGroupNumOnCreation(databaseSchema, TConsensusGroupType.DataRegion);
944944
}
945945

946-
if (errorResp != null) {
946+
if (isErrorStatus(errorResp)) {
947947
LOGGER.warn(ConfigNodeMessages.EXECUTE_SETDATABASE_WITH_RESULT, databaseSchema, errorResp);
948948
return errorResp;
949949
}
@@ -958,6 +958,10 @@ public static TSStatus enrichDatabaseSchemaWithDefaultProperties(
958958
return StatusUtils.OK;
959959
}
960960

961+
private static boolean isErrorStatus(final TSStatus status) {
962+
return status != null && status.getCode() != TSStatusCode.SUCCESS_STATUS.getStatusCode();
963+
}
964+
961965
private static TSStatus validateMaxRegionGroupNumOnCreation(
962966
final TDatabaseSchema databaseSchema, final TConsensusGroupType consensusGroupType) {
963967
return validateMaxRegionGroupNum(
@@ -1021,8 +1025,23 @@ private TSStatus validateMaxRegionGroupNumOnAlter(
10211025
return new TSStatus(TSStatusCode.DATABASE_CONFIG_ERROR.getStatusCode())
10221026
.setMessage(
10231027
String.format(
1024-
"%s should be greater than or equal to current min %sRegionGroupNum: %d.",
1025-
fieldName, isSchemaRegion ? "Schema" : "Data", minRegionGroupNum));
1028+
ManagerMessages
1029+
.MESSAGE_ARG_SHOULD_BE_GREATER_THAN_OR_EQUAL_TO_CURRENT_MIN_ARG_REGIONGROUPNUM_ARG_B81D93DF,
1030+
fieldName,
1031+
isSchemaRegion ? "Schema" : "Data",
1032+
minRegionGroupNum));
1033+
}
1034+
1035+
final int currentMaxRegionGroupNum = getMaxRegionGroupNum(database, consensusGroupType);
1036+
if (maxRegionGroupNum < currentMaxRegionGroupNum) {
1037+
return new TSStatus(TSStatusCode.DATABASE_CONFIG_ERROR.getStatusCode())
1038+
.setMessage(
1039+
String.format(
1040+
ManagerMessages
1041+
.MESSAGE_ARG_SHOULD_BE_GREATER_THAN_OR_EQUAL_TO_CURRENT_MAX_ARG_REGIONGROUPNUM_ARG_3D170323,
1042+
fieldName,
1043+
isSchemaRegion ? "Schema" : "Data",
1044+
currentMaxRegionGroupNum));
10261045
}
10271046

10281047
final int allocatedRegionGroupCount;
@@ -1037,8 +1056,11 @@ private TSStatus validateMaxRegionGroupNumOnAlter(
10371056
return new TSStatus(TSStatusCode.DATABASE_CONFIG_ERROR.getStatusCode())
10381057
.setMessage(
10391058
String.format(
1040-
"%s should be greater than or equal to allocated %sRegionGroupNum: %d.",
1041-
fieldName, isSchemaRegion ? "Schema" : "Data", allocatedRegionGroupCount));
1059+
ManagerMessages
1060+
.MESSAGE_ARG_SHOULD_BE_GREATER_THAN_OR_EQUAL_TO_ALLOCATED_ARG_REGIONGROUPNUM_ARG_994394A1,
1061+
fieldName,
1062+
isSchemaRegion ? "Schema" : "Data",
1063+
allocatedRegionGroupCount));
10421064
}
10431065

10441066
return StatusUtils.OK;

0 commit comments

Comments
 (0)