Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.db.i18n;

public final class DataNodeSchemaMessages {

private DataNodeSchemaMessages() {}

public static final String
EXCEPTION_CANNOT_RECOVER_DUPLICATED_SCHEMAREGION_ARG_FOUND_IN_DATABASES_ARG_AND_ARG_D9F60E05 =
"Cannot recover duplicated SchemaRegion [%s] found in databases [%s] and [%s].";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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.db.i18n;

public final class DataNodeSchemaMessages {

private DataNodeSchemaMessages() {}

public static final String
EXCEPTION_CANNOT_RECOVER_DUPLICATED_SCHEMAREGION_ARG_FOUND_IN_DATABASES_ARG_AND_ARG_D9F60E05 =
"无法恢复重复的 SchemaRegion [%s],其同时存在于数据库 [%s] 和 [%s] 中。";
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.consensus.SchemaRegionConsensusImpl;
import org.apache.iotdb.db.i18n.DataNodeSchemaMessages;
import org.apache.iotdb.db.schemaengine.metric.ISchemaRegionMetric;
import org.apache.iotdb.db.schemaengine.metric.SchemaMetricManager;
import org.apache.iotdb.db.schemaengine.rescon.CachedSchemaEngineStatistics;
Expand Down Expand Up @@ -201,7 +202,8 @@ public static Map<String, List<SchemaRegionId>> getLocalSchemaRegionInfo() {
@SuppressWarnings("java:S2142")
private void initSchemaRegion() {
// recover SchemaRegion concurrently
Map<String, List<SchemaRegionId>> localSchemaRegionInfo = getLocalSchemaRegionInfo();
final Map<String, List<SchemaRegionId>> localSchemaRegionInfo = getLocalSchemaRegionInfo();
validateNoDuplicatedSchemaRegionId(localSchemaRegionInfo);
final ExecutorService schemaRegionRecoverPools =
IoTDBThreadPoolFactory.newFixedThreadPool(
Runtime.getRuntime().availableProcessors(),
Expand Down Expand Up @@ -232,6 +234,27 @@ private void initSchemaRegion() {
schemaRegionRecoverPools.shutdown();
}

static void validateNoDuplicatedSchemaRegionId(
final Map<String, List<SchemaRegionId>> localSchemaRegionInfo) {
final Map<SchemaRegionId, String> databaseBySchemaRegionId = new HashMap<>();
localSchemaRegionInfo.forEach(
(database, schemaRegionIds) -> {
for (final SchemaRegionId schemaRegionId : schemaRegionIds) {
final String existingDatabase =
databaseBySchemaRegionId.putIfAbsent(schemaRegionId, database);
if (existingDatabase != null) {
throw new IllegalStateException(
String.format(
DataNodeSchemaMessages
.EXCEPTION_CANNOT_RECOVER_DUPLICATED_SCHEMAREGION_ARG_FOUND_IN_DATABASES_ARG_AND_ARG_D9F60E05,
schemaRegionId,
existingDatabase,
database));
}
}
});
}

private void initSchemaEngineStatistics() {
if (CommonDescriptor.getInstance().getConfig().getSchemaEngineMode().equals("Memory")) {
schemaEngineStatistics = new MemSchemaEngineStatistics();
Expand Down Expand Up @@ -310,7 +333,6 @@ private Callable<ISchemaRegion> recoverSchemaRegionTask(
return () -> {
long timeRecord = System.currentTimeMillis();
try {
// TODO: handle duplicated regionId across different database
ISchemaRegion schemaRegion =
createSchemaRegionWithoutExistenceCheck(storageGroup, schemaRegionId);
timeRecord = System.currentTimeMillis() - timeRecord;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.db.schemaengine;

import org.apache.iotdb.commons.consensus.SchemaRegionId;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SchemaEngineTest {

@Test
public void uniqueSchemaRegionIdsPassRecoveryValidation() {
final Map<String, List<SchemaRegionId>> localSchemaRegionInfo = new HashMap<>();
localSchemaRegionInfo.put(
"root.db1", Arrays.asList(new SchemaRegionId(0), new SchemaRegionId(1)));
localSchemaRegionInfo.put("root.db2", Collections.singletonList(new SchemaRegionId(2)));

SchemaEngine.validateNoDuplicatedSchemaRegionId(localSchemaRegionInfo);
}

@Test
public void duplicatedSchemaRegionIdIsRejectedBeforeRecovery() {
final Map<String, List<SchemaRegionId>> localSchemaRegionInfo = new HashMap<>();
localSchemaRegionInfo.put("root.db1", Collections.singletonList(new SchemaRegionId(0)));
localSchemaRegionInfo.put("root.db2", Collections.singletonList(new SchemaRegionId(0)));

final IllegalStateException exception =
Assert.assertThrows(
IllegalStateException.class,
() -> SchemaEngine.validateNoDuplicatedSchemaRegionId(localSchemaRegionInfo));
Assert.assertTrue(exception.getMessage().contains("root.db1"));
Assert.assertTrue(exception.getMessage().contains("root.db2"));
}
}
Loading