Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public static void setUp() throws Exception {
EnvFactory.getEnv()
.getConfig()
.getCommonConfig()
.setSchemaRegionGroupExtensionPolicy("CUSTOM")
.setDataRegionGroupExtensionPolicy("CUSTOM")
.setDefaultSchemaRegionGroupNumPerDatabase(testDefaultSchemaRegionGroupNumPerDatabase)
.setDefaultDataRegionGroupNumPerDatabase(testDefaultDataRegionGroupNumPerDatabase);

Expand Down Expand Up @@ -132,7 +134,7 @@ public void testRegionGroupNumControlThroughCreation()
final int testDataRegionGroupNum = 3;
String createDatabaseSQL =
String.format(
"CREATE DATABASE %s WITH SCHEMA_REGION_GROUP_NUM=%d, DATA_REGION_GROUP_NUM=%d;",
"CREATE DATABASE %s WITH MAX_SCHEMA_REGION_GROUP_NUM=%d, MAX_DATA_REGION_GROUP_NUM=%d;",
database, testSchemaRegionGroupNum, testDataRegionGroupNum);
statement.execute(createDatabaseSQL);
insertBatchData(statement, database, 0);
Expand Down Expand Up @@ -204,7 +206,7 @@ public void testRegionGroupNumControlThroughAlter()
final int testDataRegionGroupNum = 3;
String alterDatabaseSQL =
String.format(
"ALTER DATABASE %s WITH SCHEMA_REGION_GROUP_NUM=%d, DATA_REGION_GROUP_NUM=%d;",
"ALTER DATABASE %s WITH MAX_SCHEMA_REGION_GROUP_NUM=%d, MAX_DATA_REGION_GROUP_NUM=%d;",
database, testSchemaRegionGroupNum, testDataRegionGroupNum);
statement.execute(alterDatabaseSQL);
insertBatchData(statement, database, batchSize);
Expand Down Expand Up @@ -233,4 +235,16 @@ public void testRegionGroupNumControlThroughAlter()
Assert.assertEquals(testDataRegionGroupNum, dataRegionGroupNum.get());
}
}

@Test
public void testDeprecatedRegionGroupNumSqlRejected() throws SQLException {
try (final Connection connection = EnvFactory.getEnv().getConnection();
final Statement statement = connection.createStatement()) {
Assert.assertThrows(
SQLException.class,
() ->
statement.execute(
"CREATE DATABASE root.paradise3 WITH SCHEMA_REGION_GROUP_NUM=3, DATA_REGION_GROUP_NUM=4;"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ public void testNoTree() throws Exception {
senderEnv,
Arrays.asList(
"create database root.test",
"alter database root.test with schema_region_group_num=2, data_region_group_num=3",
"create timeSeries root.test.d1.s1 int32",
"insert into root.test.d1 (s1) values (1)"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public void setUp() {
// Limit the schemaRegion number to 1 to guarantee the after sql executed on the same region
// of the tested idempotent sql.
.setDefaultSchemaRegionGroupNumPerDatabase(1)
.setSchemaRegionGroupExtensionPolicy("CUSTOM")
.setDataRegionGroupExtensionPolicy("CUSTOM")
.setConfigNodeConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS)
.setSchemaRegionConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS)
.setEnforceStrongPassword(false)
Expand All @@ -75,6 +77,8 @@ public void setUp() {
.getConfig()
.getCommonConfig()
.setAutoCreateSchemaEnabled(true)
.setSchemaRegionGroupExtensionPolicy("CUSTOM")
.setDataRegionGroupExtensionPolicy("CUSTOM")
.setConfigNodeConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS)
.setSchemaRegionConsensusProtocolClass(ConsensusFactory.RATIS_CONSENSUS)
.setEnforceStrongPassword(false)
Expand Down Expand Up @@ -289,7 +293,7 @@ public void testCreateDatabaseIdempotent() throws Exception {
public void testAlterDatabaseIdempotent() throws Exception {
testIdempotent(
Collections.singletonList("create database root.sg1"),
"ALTER DATABASE root.sg1 WITH SCHEMA_REGION_GROUP_NUM=2, DATA_REGION_GROUP_NUM=3;",
"ALTER DATABASE root.sg1 WITH MAX_SCHEMA_REGION_GROUP_NUM=2, MAX_DATA_REGION_GROUP_NUM=3;",
"create database root.sg2",
"count databases",
"count,",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.iotdb.itbase.category.TableClusterIT;
import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
import org.apache.iotdb.itbase.env.BaseEnv;
import org.apache.iotdb.jdbc.IoTDBSQLException;

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

// Test create database with properties
statement.execute(
"create database test_prop with (ttl=300, schema_region_group_num=DEFAULT, time_partition_interval=100000)");
statement.execute("create database test_prop with (ttl=300, time_partition_interval=100000)");
databaseNames = new String[] {"test_prop"};
TTLs = new String[] {"300"};
timePartitionInterval = new int[] {100000};
Expand Down Expand Up @@ -811,8 +811,11 @@ public void testMixedDatabase() throws SQLException {
try (final Connection connection = EnvFactory.getEnv().getConnection();
final Statement statement = connection.createStatement()) {
statement.execute("create database root.test");
statement.execute(
"alter database root.test WITH SCHEMA_REGION_GROUP_NUM=2, DATA_REGION_GROUP_NUM=3");
Assert.assertThrows(
IoTDBSQLException.class,
() ->
statement.execute(
"alter database root.test WITH MAX_SCHEMA_REGION_GROUP_NUM=2, MAX_DATA_REGION_GROUP_NUM=3"));
statement.execute("insert into root.test.d1 (s1) values(1)");
statement.execute("drop database root.test");
}
Expand Down Expand Up @@ -882,6 +885,34 @@ public void testCountDatabases() throws SQLException {
}
}

@Test
public void testMaxRegionGroupNumRejectedInAutoPolicy() throws SQLException {
try (final Connection connection =
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
final Statement statement = connection.createStatement()) {
try {
statement.execute("create database test_max with(max_data_region_group_num=4)");
fail("max_data_region_group_num should be rejected under AUTO policy");
} catch (final SQLException e) {
assertTrue(
e.getMessage()
.contains(
"max_data_region_group_num can only be set when data_region_group_extension_policy is CUSTOM"));
}

statement.execute("create database test_max");
try {
statement.execute("alter database test_max set properties max_data_region_group_num=4");
fail("max_data_region_group_num should be rejected under AUTO policy");
} catch (final SQLException e) {
assertTrue(
e.getMessage()
.contains(
"max_data_region_group_num can only be set when data_region_group_extension_policy is CUSTOM"));
}
}
}

@Test
public void testDBAuth() throws SQLException {
try (final Connection adminCon = EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.iotdb.relational.it.schema;

import org.apache.iotdb.commons.partition.executor.hash.BKDRHashExecutor;
import org.apache.iotdb.db.it.utils.TestUtils;
import org.apache.iotdb.it.env.EnvFactory;
import org.apache.iotdb.it.framework.IoTDBTestRunner;
import org.apache.iotdb.itbase.category.TableClusterIT;
import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
import org.apache.iotdb.itbase.env.BaseEnv;

import org.apache.tsfile.file.metadata.IDeviceID;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

@RunWith(IoTDBTestRunner.class)
@Category({TableLocalStandaloneIT.class, TableClusterIT.class})
public class IoTDBDatabaseMaxRegionGroupNumIT {

private static final int SERIES_SLOT_NUM = 8;
private static final int DEFAULT_SCHEMA_REGION_GROUP_NUM = 1;
private static final int DEFAULT_DATA_REGION_GROUP_NUM = 2;
private static final int MAX_SCHEMA_REGION_GROUP_NUM = 3;
private static final int MAX_DATA_REGION_GROUP_NUM = 4;
private static final String TABLE_NAME = "table1";
private static final BKDRHashExecutor PARTITION_EXECUTOR = new BKDRHashExecutor(SERIES_SLOT_NUM);

@Before
public void setUp() throws Exception {
EnvFactory.getEnv()
.getConfig()
.getCommonConfig()
.setSchemaRegionGroupExtensionPolicy("CUSTOM")
.setDataRegionGroupExtensionPolicy("CUSTOM")
.setDefaultSchemaRegionGroupNumPerDatabase(DEFAULT_SCHEMA_REGION_GROUP_NUM)
.setDefaultDataRegionGroupNumPerDatabase(DEFAULT_DATA_REGION_GROUP_NUM)
.setSeriesSlotNum(SERIES_SLOT_NUM)
.setSeriesPartitionExecutorClass(BKDRHashExecutor.class.getName())
.setTimePartitionInterval(10);
EnvFactory.getEnv().initClusterEnvironment();
}

@After
public void tearDown() throws Exception {
EnvFactory.getEnv().cleanClusterEnvironment();
}

@Test
public void testCreateAndAlterMaxRegionGroupNum() throws SQLException {
try (final Connection connection =
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
final Statement statement = connection.createStatement()) {
statement.execute(
"create database test_create with(max_schema_region_group_num=3, max_data_region_group_num=4)");
TestUtils.assertResultSetEqual(
statement.executeQuery(
"select database, max_schema_region_group_num, max_data_region_group_num "
+ "from information_schema.databases where database = 'test_create'"),
"database,max_schema_region_group_num,max_data_region_group_num,",
Collections.singleton("test_create,3,4,"));
Comment thread
CRZbulabula marked this conversation as resolved.

statement.execute("create database test_alter");
statement.execute(
"alter database test_alter set properties max_schema_region_group_num=3, max_data_region_group_num=4");
try (final ResultSet resultSet = statement.executeQuery("show databases details")) {
boolean found = false;
while (resultSet.next()) {
if (!"test_alter".equals(resultSet.getString("Database"))) {
continue;
}
found = true;
org.junit.Assert.assertEquals(3, resultSet.getInt("MaxSchemaRegionGroupNum"));
org.junit.Assert.assertEquals(4, resultSet.getInt("MaxDataRegionGroupNum"));
}
org.junit.Assert.assertTrue(found);
}

Assert.assertThrows(
SQLException.class,
() ->
statement.execute(
"create database test_deprecated with(schema_region_group_num=4, data_region_group_num=5)"));
}
}

@Test
public void testAllocatedRegionGroupNumEqualsQuotaAfterAlterAndWrite() throws SQLException {
try (final Connection connection =
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
final Statement statement = connection.createStatement()) {
statement.execute("create database test_partition");
statement.execute("use test_partition");
statement.execute("create table " + TABLE_NAME + "(device string tag, s1 int32 field)");

final Set<Integer> usedSeriesSlots = new HashSet<>();
final String firstDevice = getDeviceInNewSeriesSlot(usedSeriesSlots);
final String secondDevice = getDeviceInNewSeriesSlot(usedSeriesSlots);

insertData(statement, firstDevice, 0, 0);
assertRegionGroupNum(
statement,
"test_partition",
DEFAULT_SCHEMA_REGION_GROUP_NUM,
DEFAULT_SCHEMA_REGION_GROUP_NUM,
DEFAULT_DATA_REGION_GROUP_NUM,
DEFAULT_DATA_REGION_GROUP_NUM);

statement.execute(
"alter database test_partition set properties max_schema_region_group_num="
+ MAX_SCHEMA_REGION_GROUP_NUM);
insertData(statement, secondDevice, 0, 1);
assertAllocatedRegionGroupNumEqualsQuota(
statement, "test_partition", MAX_SCHEMA_REGION_GROUP_NUM, DEFAULT_DATA_REGION_GROUP_NUM);

statement.execute(
"alter database test_partition set properties max_data_region_group_num="
+ MAX_DATA_REGION_GROUP_NUM);
insertData(statement, secondDevice, 20, 2);
assertAllocatedRegionGroupNumEqualsQuota(
statement, "test_partition", MAX_SCHEMA_REGION_GROUP_NUM, MAX_DATA_REGION_GROUP_NUM);

TestUtils.assertResultSetEqual(
statement.executeQuery("select count(*) from " + TABLE_NAME),
"_col0,",
Collections.singleton("3,"));
}
}

private static void insertData(
final Statement statement, final String device, final int time, final int value)
throws SQLException {
statement.execute(
"insert into "
+ TABLE_NAME
+ "(time, device, s1) values("
+ time
+ ", '"
+ device
+ "', "
+ value
+ ")");
}

private static String getDeviceInNewSeriesSlot(final Set<Integer> usedSeriesSlots) {
for (int i = 0; i < 1_000; i++) {
final String device = "d" + i;
if (usedSeriesSlots.add(getSeriesSlot(device))) {
return device;
}
}
throw new AssertionError("Failed to find a device in a new series partition slot");
}

private static int getSeriesSlot(final String device) {
return PARTITION_EXECUTOR
.getSeriesPartitionSlot(
IDeviceID.Factory.DEFAULT_FACTORY.create(new String[] {TABLE_NAME, device}))
.getSlotId();
}

private static void assertAllocatedRegionGroupNumEqualsQuota(
final Statement statement,
final String database,
final int schemaRegionGroupQuota,
final int dataRegionGroupQuota)
throws SQLException {
assertRegionGroupNum(
statement,
database,
schemaRegionGroupQuota,
schemaRegionGroupQuota,
dataRegionGroupQuota,
dataRegionGroupQuota);
}

private static void assertRegionGroupNum(
final Statement statement,
final String database,
final int schemaRegionGroupNum,
final int maxSchemaRegionGroupNum,
final int dataRegionGroupNum,
final int maxDataRegionGroupNum)
throws SQLException {
try (final ResultSet resultSet =
statement.executeQuery(
"select schema_region_group_num, max_schema_region_group_num, "
+ "data_region_group_num, max_data_region_group_num "
+ "from information_schema.databases where database = '"
+ database
+ "'")) {
Assert.assertTrue(resultSet.next());
Assert.assertEquals(schemaRegionGroupNum, resultSet.getInt("schema_region_group_num"));
Assert.assertEquals(maxSchemaRegionGroupNum, resultSet.getInt("max_schema_region_group_num"));
Assert.assertEquals(dataRegionGroupNum, resultSet.getInt("data_region_group_num"));
Assert.assertEquals(maxDataRegionGroupNum, resultSet.getInt("max_data_region_group_num"));
Assert.assertFalse(resultSet.next());
}
}
}
Loading