You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ja...@apache.org on 2023/07/24 09:17:16 UTC
[iotdb] branch rel/1.1 updated: [To rel/1.1][IOTDB-6076] Add duplicate checking when upsert alias
This is an automated email from the ASF dual-hosted git repository.
jackietien pushed a commit to branch rel/1.1
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/rel/1.1 by this push:
new 2a5bac3e379 [To rel/1.1][IOTDB-6076] Add duplicate checking when upsert alias
2a5bac3e379 is described below
commit 2a5bac3e379792873682c4e63e0d577a59015b95
Author: Chen YZ <43...@users.noreply.github.com>
AuthorDate: Mon Jul 24 17:17:10 2023 +0800
[To rel/1.1][IOTDB-6076] Add duplicate checking when upsert alias
---
.../apache/iotdb/db/it/schema/IoTDBTagAlterIT.java | 29 +++++++++++++++
.../db/metadata/mtree/MTreeBelowSGCachedImpl.java | 27 ++++++++++++++
.../db/metadata/mtree/MTreeBelowSGMemoryImpl.java | 23 ++++++++++++
.../schemaregion/SchemaRegionMemoryImpl.java | 39 +++-----------------
.../schemaregion/SchemaRegionSchemaFileImpl.java | 43 +++-------------------
.../schemaRegion/SchemaRegionAliasAndTagTest.java | 19 ++++++++++
6 files changed, 110 insertions(+), 70 deletions(-)
diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBTagAlterIT.java b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBTagAlterIT.java
index d37c241df35..deae20bbee2 100644
--- a/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBTagAlterIT.java
+++ b/integration-test/src/test/java/org/apache/iotdb/db/it/schema/IoTDBTagAlterIT.java
@@ -601,4 +601,33 @@ public class IoTDBTagAlterIT extends AbstractSchemaIT {
fail();
}
}
+
+ @Test
+ public void alterDuplicateAliasTest() {
+ try (Connection connection = EnvFactory.getEnv().getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.execute(
+ "create timeseries root.turbine.d1.s1(a1) with datatype=FLOAT, encoding=RLE, compression=SNAPPY;");
+ statement.execute("create timeseries root.turbine.d1.s2 with datatype=INT32, encoding=RLE;");
+ try {
+ statement.execute("alter timeseries root.turbine.d1.s2 upsert alias=s1;");
+ fail();
+ } catch (Exception e) {
+ assertTrue(
+ e.getMessage()
+ .contains("The alias is duplicated with the name or alias of other measurement."));
+ }
+ try {
+ statement.execute("alter timeseries root.turbine.d1.s2 upsert alias=a1;");
+ fail();
+ } catch (Exception e) {
+ assertTrue(
+ e.getMessage()
+ .contains("The alias is duplicated with the name or alias of other measurement."));
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ fail();
+ }
+ }
}
diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java
index ae670621dc4..738f2ce2bb3 100644
--- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java
+++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java
@@ -416,6 +416,33 @@ public class MTreeBelowSGCachedImpl implements IMTreeBelowSG {
}
}
+ public boolean changeAlias(String alias, PartialPath fullPath) throws MetadataException {
+ IMeasurementMNode measurementMNode = getMeasurementMNode(fullPath);
+ try {
+ // upsert alias
+ if (alias != null && !alias.equals(measurementMNode.getAlias())) {
+ synchronized (this) {
+ IEntityMNode device = measurementMNode.getParent().getAsEntityMNode();
+ IMNode cachedMNode = store.getChild(device, alias);
+ if (cachedMNode != null) {
+ unPinMNode(cachedMNode);
+ throw new MetadataException(
+ "The alias is duplicated with the name or alias of other measurement.");
+ }
+ if (measurementMNode.getAlias() != null) {
+ device.deleteAliasChild(measurementMNode.getAlias());
+ }
+ device.addAlias(alias, measurementMNode);
+ setAlias(measurementMNode, alias);
+ }
+ return true;
+ }
+ return false;
+ } finally {
+ unPinMNode(measurementMNode);
+ }
+ }
+
@Override
public Map<Integer, MetadataException> checkMeasurementExistence(
PartialPath devicePath, List<String> measurementList, List<String> aliasList) {
diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java
index 26376b38f01..d33965bd4af 100644
--- a/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java
+++ b/server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java
@@ -432,6 +432,29 @@ public class MTreeBelowSGMemoryImpl implements IMTreeBelowSG {
}
return failingMeasurementMap;
}
+
+ public boolean changeAlias(String alias, PartialPath fullPath) throws MetadataException {
+ IMeasurementMNode measurementMNode = getMeasurementMNode(fullPath);
+ // upsert alias
+ if (alias != null && !alias.equals(measurementMNode.getAlias())) {
+ synchronized (this) {
+ IEntityMNode device = measurementMNode.getParent().getAsEntityMNode();
+ IMNode memMNode = store.getChild(device, alias);
+ if (memMNode != null) {
+ throw new MetadataException(
+ "The alias is duplicated with the name or alias of other measurement.");
+ }
+ if (measurementMNode.getAlias() != null) {
+ device.deleteAliasChild(measurementMNode.getAlias());
+ }
+ device.addAlias(alias, measurementMNode);
+ setAlias(measurementMNode, alias);
+ }
+ return true;
+ }
+ return false;
+ }
+
/**
* Delete path. The path should be a full path from root to leaf node
*
diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java
index 6c18a976a2d..ddf731a5672 100644
--- a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java
+++ b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java
@@ -841,21 +841,6 @@ public class SchemaRegionMemoryImpl implements ISchemaRegion {
}
}
- private void changeAlias(PartialPath path, String alias) throws MetadataException {
- IMeasurementMNode leafMNode = mtree.getMeasurementMNode(path);
- if (leafMNode.getAlias() != null) {
- leafMNode.getParent().deleteAliasChild(leafMNode.getAlias());
- }
- leafMNode.getParent().addAlias(alias, leafMNode);
- mtree.setAlias(leafMNode, alias);
-
- try {
- writeToMLog(SchemaRegionWritePlanFactory.getChangeAliasPlan(path, alias));
- } catch (IOException e) {
- throw new MetadataException(e);
- }
- }
-
/**
* Upsert tags and attributes key-value for the timeseries if the key has existed, just use the
* new value to update it.
@@ -873,11 +858,9 @@ public class SchemaRegionMemoryImpl implements ISchemaRegion {
Map<String, String> attributesMap,
PartialPath fullPath)
throws MetadataException, IOException {
- IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath);
-
// upsert alias
- upsertAlias(alias, fullPath, leafMNode);
-
+ upsertAlias(alias, fullPath);
+ IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath);
if (tagsMap == null && attributesMap == null) {
return;
}
@@ -897,19 +880,9 @@ public class SchemaRegionMemoryImpl implements ISchemaRegion {
tagManager.updateTagsAndAttributes(tagsMap, attributesMap, leafMNode);
}
- private void upsertAlias(String alias, PartialPath fullPath, IMeasurementMNode leafMNode)
+ private void upsertAlias(String alias, PartialPath fullPath)
throws MetadataException, IOException {
- // upsert alias
- if (alias != null && !alias.equals(leafMNode.getAlias())) {
- if (!leafMNode.getParent().addAlias(alias, leafMNode)) {
- throw new MetadataException("The alias already exists.");
- }
-
- if (leafMNode.getAlias() != null) {
- leafMNode.getParent().deleteAliasChild(leafMNode.getAlias());
- }
-
- mtree.setAlias(leafMNode, alias);
+ if (mtree.changeAlias(alias, fullPath)) {
// persist to WAL
writeToMLog(SchemaRegionWritePlanFactory.getChangeAliasPlan(fullPath, alias));
}
@@ -1203,9 +1176,9 @@ public class SchemaRegionMemoryImpl implements ISchemaRegion {
public RecoverOperationResult visitChangeAlias(
IChangeAliasPlan changeAliasPlan, SchemaRegionMemoryImpl context) {
try {
- changeAlias(changeAliasPlan.getPath(), changeAliasPlan.getAlias());
+ upsertAlias(changeAliasPlan.getAlias(), changeAliasPlan.getPath());
return RecoverOperationResult.SUCCESS;
- } catch (MetadataException e) {
+ } catch (MetadataException | IOException e) {
return new RecoverOperationResult(e);
}
}
diff --git a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java
index 95fdbac528f..22883da896c 100644
--- a/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java
+++ b/server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionSchemaFileImpl.java
@@ -958,25 +958,6 @@ public class SchemaRegionSchemaFileImpl implements ISchemaRegion {
}
}
- private void changeAlias(PartialPath path, String alias) throws MetadataException {
- IMeasurementMNode leafMNode = mtree.getMeasurementMNode(path);
- try {
- if (leafMNode.getAlias() != null) {
- leafMNode.getParent().deleteAliasChild(leafMNode.getAlias());
- }
- leafMNode.getParent().addAlias(alias, leafMNode);
- mtree.setAlias(leafMNode, alias);
- } finally {
- mtree.unPinMNode(leafMNode);
- }
-
- try {
- writeToMLog(SchemaRegionWritePlanFactory.getChangeAliasPlan(path, alias));
- } catch (IOException e) {
- throw new MetadataException(e);
- }
- }
-
/**
* Upsert tags and attributes key-value for the timeseries if the key has existed, just use the
* new value to update it.
@@ -994,15 +975,13 @@ public class SchemaRegionSchemaFileImpl implements ISchemaRegion {
Map<String, String> attributesMap,
PartialPath fullPath)
throws MetadataException, IOException {
+ // upsert alias
+ upsertAlias(alias, fullPath);
IMeasurementMNode leafMNode = mtree.getMeasurementMNode(fullPath);
try {
- // upsert alias
- upsertAlias(alias, fullPath, leafMNode);
-
if (tagsMap == null && attributesMap == null) {
return;
}
-
// no tag or attribute, we need to add a new record in log
if (leafMNode.getOffset() < 0) {
long offset = tagManager.writeTagFile(tagsMap, attributesMap);
@@ -1023,19 +1002,9 @@ public class SchemaRegionSchemaFileImpl implements ISchemaRegion {
}
}
- private void upsertAlias(String alias, PartialPath fullPath, IMeasurementMNode leafMNode)
+ private void upsertAlias(String alias, PartialPath fullPath)
throws MetadataException, IOException {
- // upsert alias
- if (alias != null && !alias.equals(leafMNode.getAlias())) {
- if (!leafMNode.getParent().addAlias(alias, leafMNode)) {
- throw new MetadataException("The alias already exists.");
- }
-
- if (leafMNode.getAlias() != null) {
- leafMNode.getParent().deleteAliasChild(leafMNode.getAlias());
- }
-
- mtree.setAlias(leafMNode, alias);
+ if (mtree.changeAlias(alias, fullPath)) {
// persist to WAL
writeToMLog(SchemaRegionWritePlanFactory.getChangeAliasPlan(fullPath, alias));
}
@@ -1357,9 +1326,9 @@ public class SchemaRegionSchemaFileImpl implements ISchemaRegion {
public RecoverOperationResult visitChangeAlias(
IChangeAliasPlan changeAliasPlan, SchemaRegionSchemaFileImpl context) {
try {
- changeAlias(changeAliasPlan.getPath(), changeAliasPlan.getAlias());
+ upsertAlias(changeAliasPlan.getAlias(), changeAliasPlan.getPath());
return RecoverOperationResult.SUCCESS;
- } catch (MetadataException e) {
+ } catch (MetadataException | IOException e) {
return new RecoverOperationResult(e);
}
}
diff --git a/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionAliasAndTagTest.java b/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionAliasAndTagTest.java
index d2f237408a3..cb994441693 100644
--- a/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionAliasAndTagTest.java
+++ b/server/src/test/java/org/apache/iotdb/db/metadata/schemaRegion/SchemaRegionAliasAndTagTest.java
@@ -245,6 +245,25 @@ public class SchemaRegionAliasAndTagTest extends AbstractSchemaRegionTest {
public void testUpsertAliasAndTagsAndAttributes() {
try {
prepareTimeseries();
+ try {
+ schemaRegion.upsertAliasAndTagsAndAttributes(
+ "s2", null, null, new PartialPath("root.sg.wf01.wt01.v1.s1"));
+ Assert.fail();
+ } catch (Exception e) {
+ Assert.assertTrue(
+ e.getMessage()
+ .contains("The alias is duplicated with the name or alias of other measurement."));
+ }
+ try {
+ schemaRegion.upsertAliasAndTagsAndAttributes(
+ "temp", null, null, new PartialPath("root.sg.wf01.wt01.v1.s1"));
+ Assert.fail();
+ } catch (Exception e) {
+ Assert.assertTrue(
+ e.getMessage()
+ .contains("The alias is duplicated with the name or alias of other measurement."));
+ }
+
Map<String, String> newTags =
new HashMap<String, String>() {
{