You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by "MarcosZyk (via GitHub)" <gi...@apache.org> on 2023/05/25 14:34:46 UTC

[GitHub] [iotdb] MarcosZyk commented on a diff in pull request #9937: Draft: Support insertion in logical view

MarcosZyk commented on code in PR #9937:
URL: https://github.com/apache/iotdb/pull/9937#discussion_r1205580092


##########
server/src/main/java/org/apache/iotdb/db/metadata/cache/DeviceUsingTemplateSchemaCache.java:
##########
@@ -181,6 +181,15 @@ public MeasurementSchema getSchemaAsMeasurementSchema() {
                   schema.getCompressor());
             }
 
+            @Override
+            public LogicalViewSchema getSchemaAsLogicalViewSchema() {
+              if (isLogicalView()) {
+                return new LogicalViewSchema(
+                    schema.getMeasurementId(), ((LogicalViewSchema) schema).getExpression());
+              }
+              return null;

Review Comment:
   throw UnsupportedException



##########
server/src/main/java/org/apache/iotdb/db/metadata/cache/TimeSeriesSchemaCache.java:
##########
@@ -159,6 +161,66 @@ public void computeValue(int index, SchemaCacheEntry value) {
     return indexOfMissingMeasurements;
   }
 
+  public Pair<List<Integer>, List<String>> computeSourceOfLogicalViewInCache(
+      ISchemaComputation schemaComputation) {
+    List<Integer> indexOfMissingMeasurements = new ArrayList<>();
+    List<String> missedPathStringList = new ArrayList<>();
+    final AtomicBoolean isFirstMeasurement = new AtomicBoolean(true);
+    Pair<Integer, Integer> beginToEnd = schemaComputation.getSizeOfLogicalViewSchemaListRecorded();
+    List<LogicalViewSchema> logicalViewSchemaList = schemaComputation.getLogicalViewSchemaList();
+    List<Integer> indexListOfLogicalViewPaths = schemaComputation.getIndexListOfLogicalViewPaths();
+    for (int i = beginToEnd.left; i < beginToEnd.right; i++) {
+      LogicalViewSchema logicalViewSchema = logicalViewSchemaList.get(i);
+      final int realIndex = indexListOfLogicalViewPaths.get(i);
+      final int recordMissingIndex = i;
+      if (!logicalViewSchema.isWritable()) {
+        throw new RuntimeException(
+            new UnsupportedOperationException(
+                "You can not insert into a logical view which is not alias series!"));
+      }
+      PartialPath fullPath = logicalViewSchema.getSourcePathIfWritable();
+      dualKeyCache.compute(
+          new IDualKeyCacheComputation<PartialPath, String, SchemaCacheEntry>() {
+            @Override
+            public PartialPath getFirstKey() {
+              return fullPath.getDevicePath();
+            }
+
+            @Override
+            public String[] getSecondKeyList() {
+              return new String[] {fullPath.getMeasurement()};
+            }
+
+            @Override
+            public void computeValue(int index, SchemaCacheEntry value) {
+              index = realIndex;
+              if (value == null) {
+                indexOfMissingMeasurements.add(recordMissingIndex);
+              } else {
+                if (isFirstMeasurement.get()) {
+                  schemaComputation.computeDevice(value.isAligned());

Review Comment:
   We need record the alignment of source device and use it to split new insert statement. When inserting to a logical view with aligned source device, the insert statement shall be parsed as insertion with aligned values.



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/statement/crud/InsertRowsStatement.java:
##########
@@ -136,4 +136,16 @@ public long getMinTime() {
   public Object getFirstValueOfIndex(int index) {
     throw new NotImplementedException();
   }
+
+  @Override
+  public InsertBaseStatement split() {
+    List<InsertRowStatement> mergedList = new ArrayList<>();
+    for (InsertRowStatement child : this.insertRowStatementList) {
+      List<InsertRowStatement> childSplitResult = child.getSplitList();
+      mergedList.addAll(childSplitResult);
+    }
+    InsertRowsStatement splitResult = new InsertRowsStatement();
+    splitResult.setInsertRowStatementList(mergedList);
+    return splitResult;

Review Comment:
   Maybe add a should split check is better.



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/statement/crud/InsertRowsOfOneDeviceStatement.java:
##########
@@ -136,4 +136,25 @@ public long getMinTime() {
   public Object getFirstValueOfIndex(int index) {
     throw new NotImplementedException();
   }
+
+  @Override
+  public InsertBaseStatement split() {
+    boolean needSplit = false;
+    for (InsertRowStatement child : this.insertRowStatementList) {
+      if (child.isNeedSplit()) {
+        needSplit = true;
+      }
+    }

Review Comment:
   We can record the index and start split from the recorded index.



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/statement/crud/InsertTabletStatement.java:
##########
@@ -186,6 +194,113 @@ public void markFailedMeasurement(int index, Exception cause) {
     columns[index] = null;
   }
 
+  protected Map<PartialPath, List<Pair<String, Integer>>> getMapFromDeviceToMeasurementAndIndex() {

Review Comment:
   Add comments about usage.



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/statement/crud/InsertTabletStatement.java:
##########
@@ -186,6 +194,113 @@ public void markFailedMeasurement(int index, Exception cause) {
     columns[index] = null;
   }
 
+  protected Map<PartialPath, List<Pair<String, Integer>>> getMapFromDeviceToMeasurementAndIndex() {
+    boolean[] isLogicalView = new boolean[this.measurements.length];
+    int[] indexMapToLogicalViewList = new int[this.measurements.length];
+    for (int i = 0; i < this.measurements.length; i++) {
+      isLogicalView[i] = false;
+    }
+    for (int i = 0; i < this.indexListOfLogicalViewPaths.size(); i++) {
+      int realIndex = this.indexListOfLogicalViewPaths.get(i);
+      isLogicalView[realIndex] = true;
+      indexMapToLogicalViewList[realIndex] = i;
+    }
+    // construct map from device to measurements and record the index of its measurement schema
+    Map<PartialPath, List<Pair<String, Integer>>> mapFromDeviceToMeasurementAndIndex =
+        new HashMap<>();
+    for (int i = 0; i < this.measurements.length; i++) {
+      PartialPath devicePath;
+      String measurementName;
+      if (isLogicalView[i]) {
+        int viewIndex = indexMapToLogicalViewList[i];
+        devicePath =
+            this.logicalViewSchemaList.get(viewIndex).getSourcePathIfWritable().getDevicePath();
+        measurementName =
+            this.logicalViewSchemaList.get(viewIndex).getSourcePathIfWritable().getMeasurement();
+      } else {
+        devicePath = this.devicePath;
+        measurementName = this.measurements[i];
+      }
+      int index = i;
+      final String finalMeasurementName = measurementName;
+      mapFromDeviceToMeasurementAndIndex.compute(
+          devicePath,
+          (k, v) -> {
+            if (v == null) {
+              List<Pair<String, Integer>> valueList = new ArrayList<>();
+              valueList.add(new Pair<>(finalMeasurementName, index));
+              return valueList;
+            } else {
+              v.add(new Pair<>(finalMeasurementName, index));
+              return v;
+            }
+          });
+    }
+    return mapFromDeviceToMeasurementAndIndex;

Review Comment:
   Maybe we can iterate ```indexListOfLogicalViewPaths``` to get desired result directly.



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/schema/NormalSchemaFetcher.java:
##########
@@ -122,6 +180,19 @@ void processNormalTimeSeries(
                 .collect(Collectors.toList()),
             indexOfDevicesWithMissingMeasurements,
             indexOfMissingMeasurementsList);
+    if (hasUnFetchedLogicalView) {
+      List<String> fullPathsNeedRefetch = new ArrayList<>();
+      for (Pair<List<Integer>, List<String>> pair : missedIndexAndPathStringOfViewList) {
+        fullPathsNeedRefetch.addAll(pair.right);
+      }
+      reFetchFullPathsAndMerge(remoteSchemaTree, fullPathsNeedRefetch);
+    }

Review Comment:
   Merge these two fetch operations to make sure only fetch once.



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/statement/crud/InsertBaseStatement.java:
##########
@@ -52,6 +53,14 @@ public abstract class InsertBaseStatement extends Statement {
   /** index of failed measurements -> info including measurement, data type and value */
   protected Map<Integer, FailedMeasurementInfo> failedMeasurementIndex2Info;
 
+  List<LogicalViewSchema> logicalViewSchemaList;
+
+  List<Integer> indexListOfLogicalViewPaths;
+
+  int recordedBeginOfLogicalViewSchemaList = 0;
+
+  int recordedEndOfLogicalViewSchemaList = 0;

Review Comment:
   It's better two save these two parameters somewhere else. They are the runtime temporary parameters.



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/analyze/schema/NormalSchemaFetcher.java:
##########
@@ -47,30 +50,72 @@ class NormalSchemaFetcher {
     this.clusterSchemaFetchExecutor = clusterSchemaFetchExecutor;
   }
 
+  private ClusterSchemaTree reFetchFullPathsAndMerge(
+      ClusterSchemaTree originTree, List<String> fullPaths) {
+    if (fullPaths.size() <= 0) {
+      return originTree;
+    }
+    ClusterSchemaTree viewSchemaTree =
+        clusterSchemaFetchExecutor.fetchSchemaWithFullPaths(fullPaths);
+    originTree.mergeSchemaTree(viewSchemaTree);
+    Set<String> databaseSet = new HashSet<>();
+    databaseSet.addAll(originTree.getDatabases());
+    databaseSet.addAll(viewSchemaTree.getDatabases());
+    originTree.setDatabases(databaseSet);
+    return originTree;
+  }
+
   List<Integer> processNormalTimeSeries(
       ISchemaComputationWithAutoCreation schemaComputationWithAutoCreation) {
+    // [Step 1] Cache 1. compute measurements and record logical views.
     List<Integer> indexOfMissingMeasurements =
         schemaCache.computeWithoutTemplate(schemaComputationWithAutoCreation);
+    // [Step 2] Cache 2. process recorded logical views.
+    Pair<List<Integer>, List<String>> missedIndexAndPathString =
+        schemaCache.computeLogicalViewWithoutTemplate(schemaComputationWithAutoCreation);
+    List<Integer> indexOfMissingLogicalView = missedIndexAndPathString.left;
+    List<String> missedPathStringOfLogicalView = missedIndexAndPathString.right;
     // all schema can be taken from cache
-    if (indexOfMissingMeasurements.isEmpty()) {
+    if (indexOfMissingMeasurements.isEmpty() && indexOfMissingLogicalView.isEmpty()) {
       return indexOfMissingMeasurements;
     }
-
-    // try fetch the missing schema from remote and cache fetched schema
+    // [Step 3] Fetch 1. fetch schema from remote. Process logical view first; then process
+    // measurements.
+    // try fetch the missing raw schema from remote and cache fetched schema
     ClusterSchemaTree remoteSchemaTree =
         clusterSchemaFetchExecutor.fetchSchemaOfOneDevice(
             schemaComputationWithAutoCreation.getDevicePath(),
             schemaComputationWithAutoCreation.getMeasurements(),
             indexOfMissingMeasurements);
+    if (!missedPathStringOfLogicalView.isEmpty()) {
+      reFetchFullPathsAndMerge(remoteSchemaTree, missedPathStringOfLogicalView);
+    }

Review Comment:
   Merge this two fetch operation to make sure only fetch once in this step.



##########
server/src/main/java/org/apache/iotdb/db/metadata/cache/DataNodeSchemaCache.java:
##########
@@ -127,7 +127,14 @@ public ClusterSchemaTree getMatchedSchemaWithTemplate(PartialPath path) {
   }
 
   public List<Integer> computeWithoutTemplate(ISchemaComputation schemaComputation) {
-    return timeSeriesSchemaCache.compute(schemaComputation);
+    List<Integer> result = timeSeriesSchemaCache.computeAndRecordLogicalView(schemaComputation);
+    schemaComputation.recordSizeOfLogicalViewSchemaListNow();
+    return result;
+  }
+
+  public Pair<List<Integer>, List<String>> computeLogicalViewWithoutTemplate(

Review Comment:
   Add javadoc about input and output.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org