You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2022/12/01 02:02:54 UTC

[GitHub] [iotdb] lancelly opened a new pull request, #8278: [IOTDB-5026] Improve last query on aligned timeseries

lancelly opened a new pull request, #8278:
URL: https://github.com/apache/iotdb/pull/8278

   Previously, we construct a PlanNode for every measurement of an AlignedPath which is unnecessary. This PR aims to reduce the cost of construcing unnecessary PlanNode for last query on aligned timeseries.


-- 
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


[GitHub] [iotdb] JackieTien97 commented on a diff in pull request #8278: [To rel/1.0] [IOTDB-5026] Improve last query on aligned timeseries

Posted by GitBox <gi...@apache.org>.
JackieTien97 commented on code in PR #8278:
URL: https://github.com/apache/iotdb/pull/8278#discussion_r1036635251


##########
server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/process/last/LastQueryOperator.java:
##########
@@ -109,7 +109,9 @@ public TsBlock next() {
         if (tsBlock == null) {
           return null;
         } else if (!tsBlock.isEmpty()) {
-          LastQueryUtil.appendLastValue(tsBlockBuilder, tsBlock, 0);
+          for (int i = 0; i < tsBlock.getPositionCount(); i++) {
+            LastQueryUtil.appendLastValue(tsBlockBuilder, tsBlock, i);
+          }

Review Comment:
   ```suggestion
             LastQueryUtil.appendLastValue(tsBlockBuilder, tsBlock);
   ```



##########
server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/process/last/UpdateLastCacheOperator.java:
##########
@@ -41,23 +41,23 @@ public class UpdateLastCacheOperator implements ProcessOperator {
       new TsBlockBuilder(ImmutableList.of(TSDataType.TEXT, TSDataType.TEXT, TSDataType.TEXT))
           .build();
 
-  private final OperatorContext operatorContext;
+  private OperatorContext operatorContext;
 
-  private final Operator child;
+  private Operator child;
 
   // fullPath for queried time series
   // It should be exact PartialPath, neither MeasurementPath nor AlignedPath, because lastCache only
   // accept PartialPath
-  private final MeasurementPath fullPath;
+  private MeasurementPath fullPath;
 
   // dataType for queried time series;
-  private final String dataType;
+  private String dataType;
 
-  private final DataNodeSchemaCache lastCache;
+  private DataNodeSchemaCache lastCache;
 
-  private final boolean needUpdateCache;
+  private boolean needUpdateCache;
 
-  private final TsBlockBuilder tsBlockBuilder;
+  private TsBlockBuilder tsBlockBuilder;

Review Comment:
   change it back, it should be final because once it's created, it won't changed any more



##########
server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/process/last/AlignedUpdateLastCacheOperator.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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.mpp.execution.operator.process.last;
+
+import org.apache.iotdb.commons.path.AlignedPath;
+import org.apache.iotdb.commons.path.MeasurementPath;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.db.metadata.cache.DataNodeSchemaCache;
+import org.apache.iotdb.db.mpp.execution.driver.DataDriverContext;
+import org.apache.iotdb.db.mpp.execution.operator.Operator;
+import org.apache.iotdb.db.mpp.execution.operator.OperatorContext;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.block.TsBlock;
+import org.apache.iotdb.tsfile.read.common.block.TsBlockBuilder;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.weakref.jmx.internal.guava.base.Preconditions.checkArgument;
+
+public class AlignedUpdateLastCacheOperator extends UpdateLastCacheOperator {

Review Comment:
   It seems that you've already override all the methods of `UpdateLastCacheOperator`, why still need to extend it.



##########
server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/process/last/AlignedUpdateLastCacheOperator.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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.mpp.execution.operator.process.last;
+
+import org.apache.iotdb.commons.path.AlignedPath;
+import org.apache.iotdb.commons.path.MeasurementPath;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.db.metadata.cache.DataNodeSchemaCache;
+import org.apache.iotdb.db.mpp.execution.driver.DataDriverContext;
+import org.apache.iotdb.db.mpp.execution.operator.Operator;
+import org.apache.iotdb.db.mpp.execution.operator.OperatorContext;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.block.TsBlock;
+import org.apache.iotdb.tsfile.read.common.block.TsBlockBuilder;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.weakref.jmx.internal.guava.base.Preconditions.checkArgument;
+
+public class AlignedUpdateLastCacheOperator extends UpdateLastCacheOperator {
+  private static final TsBlock LAST_QUERY_EMPTY_TSBLOCK =
+      new TsBlockBuilder(ImmutableList.of(TSDataType.TEXT, TSDataType.TEXT, TSDataType.TEXT))
+          .build();
+
+  private final OperatorContext operatorContext;
+
+  private final Operator child;
+
+  private final AlignedPath seriesPath;
+
+  private PartialPath devicePath;
+
+  private final DataNodeSchemaCache lastCache;
+
+  private final boolean needUpdateCache;
+
+  private final TsBlockBuilder tsBlockBuilder;
+
+  private String databaseName;
+
+  private static final Logger LOGGER =
+      LoggerFactory.getLogger(AlignedUpdateLastCacheOperator.class);

Review Comment:
   ```suggestion
   ```
   It's not used.



##########
server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/process/last/AlignedUpdateLastCacheOperator.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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.mpp.execution.operator.process.last;
+
+import org.apache.iotdb.commons.path.AlignedPath;
+import org.apache.iotdb.commons.path.MeasurementPath;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.db.metadata.cache.DataNodeSchemaCache;
+import org.apache.iotdb.db.mpp.execution.driver.DataDriverContext;
+import org.apache.iotdb.db.mpp.execution.operator.Operator;
+import org.apache.iotdb.db.mpp.execution.operator.OperatorContext;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.block.TsBlock;
+import org.apache.iotdb.tsfile.read.common.block.TsBlockBuilder;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.weakref.jmx.internal.guava.base.Preconditions.checkArgument;
+
+public class AlignedUpdateLastCacheOperator extends UpdateLastCacheOperator {
+  private static final TsBlock LAST_QUERY_EMPTY_TSBLOCK =
+      new TsBlockBuilder(ImmutableList.of(TSDataType.TEXT, TSDataType.TEXT, TSDataType.TEXT))
+          .build();
+
+  private final OperatorContext operatorContext;
+
+  private final Operator child;
+
+  private final AlignedPath seriesPath;
+
+  private PartialPath devicePath;
+
+  private final DataNodeSchemaCache lastCache;
+
+  private final boolean needUpdateCache;
+
+  private final TsBlockBuilder tsBlockBuilder;
+
+  private String databaseName;
+
+  private static final Logger LOGGER =
+      LoggerFactory.getLogger(AlignedUpdateLastCacheOperator.class);
+
+  public AlignedUpdateLastCacheOperator(
+      OperatorContext operatorContext,
+      Operator child,
+      AlignedPath seriesPath,
+      DataNodeSchemaCache dataNodeSchemaCache,
+      boolean needUpdateCache) {
+    this.operatorContext = operatorContext;
+    this.child = child;
+    this.seriesPath = seriesPath;
+    this.devicePath = seriesPath.getDevicePath();
+    this.lastCache = dataNodeSchemaCache;
+    this.needUpdateCache = needUpdateCache;
+    this.tsBlockBuilder = LastQueryUtil.createTsBlockBuilder(1);
+  }
+
+  @Override
+  public OperatorContext getOperatorContext() {
+    return operatorContext;
+  }
+
+  @Override
+  public ListenableFuture<?> isBlocked() {
+    return child.isBlocked();
+  }
+
+  @Override
+  public TsBlock next() {
+    TsBlock res = child.next();
+    if (res == null) {
+      return null;
+    }
+    if (res.isEmpty()) {
+      return LAST_QUERY_EMPTY_TSBLOCK;
+    }
+
+    checkArgument(res.getPositionCount() == 1, "last query result should only have one record");
+
+    tsBlockBuilder.reset();
+    boolean hasNonNullLastValue = false;
+    for (int i = 0; i + 1 < res.getValueColumnCount(); i += 2) {
+      if (!res.getColumn(i).isNull(0)) {
+        hasNonNullLastValue = true;
+        long lastTime = res.getColumn(i).getLong(0);
+        TsPrimitiveType lastValue = res.getColumn(i + 1).getTsPrimitiveType(0);
+        MeasurementPath measurementPath =
+            new MeasurementPath(
+                devicePath.concatNode(seriesPath.getMeasurementList().get(i / 2)),
+                seriesPath.getSchemaList().get(i / 2),
+                true);
+        if (needUpdateCache) {
+          TimeValuePair timeValuePair = new TimeValuePair(lastTime, lastValue);
+          lastCache.updateLastCache(
+              getDatabaseName(), measurementPath, timeValuePair, false, Long.MIN_VALUE);
+        }
+        LastQueryUtil.appendLastValue(
+            tsBlockBuilder,
+            lastTime,
+            measurementPath.getFullPath(),
+            lastValue.getStringValue(),
+            seriesPath.getSchemaList().get(i / 2).getType().name());
+      }
+    }
+
+    return hasNonNullLastValue ? tsBlockBuilder.build() : LAST_QUERY_EMPTY_TSBLOCK;

Review Comment:
   ```suggestion
           return !tsBlockBuilder.isEmpty() ? tsBlockBuilder.build() : LAST_QUERY_EMPTY_TSBLOCK;
   ```



##########
server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/process/last/AlignedUpdateLastCacheOperator.java:
##########
@@ -0,0 +1,171 @@
+/*
+ * 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.mpp.execution.operator.process.last;
+
+import org.apache.iotdb.commons.path.AlignedPath;
+import org.apache.iotdb.commons.path.MeasurementPath;
+import org.apache.iotdb.commons.path.PartialPath;
+import org.apache.iotdb.db.metadata.cache.DataNodeSchemaCache;
+import org.apache.iotdb.db.mpp.execution.driver.DataDriverContext;
+import org.apache.iotdb.db.mpp.execution.operator.Operator;
+import org.apache.iotdb.db.mpp.execution.operator.OperatorContext;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+import org.apache.iotdb.tsfile.read.TimeValuePair;
+import org.apache.iotdb.tsfile.read.common.block.TsBlock;
+import org.apache.iotdb.tsfile.read.common.block.TsBlockBuilder;
+import org.apache.iotdb.tsfile.utils.TsPrimitiveType;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.weakref.jmx.internal.guava.base.Preconditions.checkArgument;
+
+public class AlignedUpdateLastCacheOperator extends UpdateLastCacheOperator {
+  private static final TsBlock LAST_QUERY_EMPTY_TSBLOCK =
+      new TsBlockBuilder(ImmutableList.of(TSDataType.TEXT, TSDataType.TEXT, TSDataType.TEXT))
+          .build();
+
+  private final OperatorContext operatorContext;
+
+  private final Operator child;
+
+  private final AlignedPath seriesPath;
+
+  private PartialPath devicePath;
+
+  private final DataNodeSchemaCache lastCache;
+
+  private final boolean needUpdateCache;
+
+  private final TsBlockBuilder tsBlockBuilder;
+
+  private String databaseName;
+
+  private static final Logger LOGGER =
+      LoggerFactory.getLogger(AlignedUpdateLastCacheOperator.class);
+
+  public AlignedUpdateLastCacheOperator(
+      OperatorContext operatorContext,
+      Operator child,
+      AlignedPath seriesPath,
+      DataNodeSchemaCache dataNodeSchemaCache,
+      boolean needUpdateCache) {
+    this.operatorContext = operatorContext;
+    this.child = child;
+    this.seriesPath = seriesPath;
+    this.devicePath = seriesPath.getDevicePath();
+    this.lastCache = dataNodeSchemaCache;
+    this.needUpdateCache = needUpdateCache;
+    this.tsBlockBuilder = LastQueryUtil.createTsBlockBuilder(1);
+  }
+
+  @Override
+  public OperatorContext getOperatorContext() {
+    return operatorContext;
+  }
+
+  @Override
+  public ListenableFuture<?> isBlocked() {
+    return child.isBlocked();
+  }
+
+  @Override
+  public TsBlock next() {
+    TsBlock res = child.next();
+    if (res == null) {
+      return null;
+    }
+    if (res.isEmpty()) {
+      return LAST_QUERY_EMPTY_TSBLOCK;
+    }
+
+    checkArgument(res.getPositionCount() == 1, "last query result should only have one record");
+
+    tsBlockBuilder.reset();
+    boolean hasNonNullLastValue = false;

Review Comment:
   ```suggestion
   ```



##########
server/src/main/java/org/apache/iotdb/db/mpp/execution/operator/process/last/UpdateLastCacheOperator.java:
##########
@@ -77,6 +77,8 @@ public UpdateLastCacheOperator(
     this.tsBlockBuilder = LastQueryUtil.createTsBlockBuilder(1);
   }
 
+  public UpdateLastCacheOperator() {}

Review Comment:
   better to change your class hierarchy instead of add this empty contructor.



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/planner/OperatorTreeGenerator.java:
##########
@@ -208,6 +211,7 @@
 
 /** This Visitor is responsible for transferring PlanNode Tree to Operator Tree */
 public class OperatorTreeGenerator extends PlanVisitor<Operator, LocalExecutionPlanContext> {
+  private static final Logger LOGGER = LoggerFactory.getLogger(OperatorTreeGenerator.class);

Review Comment:
   ```suggestion
   ```
   It's not used.



##########
server/src/main/java/org/apache/iotdb/db/mpp/plan/planner/OperatorTreeGenerator.java:
##########
@@ -1704,34 +1708,46 @@ private SeriesAggregationScanOperator createLastQueryScanOperator(
   @Override
   public Operator visitAlignedLastQueryScan(
       AlignedLastQueryScanNode node, LocalExecutionPlanContext context) {
-    PartialPath seriesPath = node.getSeriesPath().transformToPartialPath();
-    TimeValuePair timeValuePair = DATA_NODE_SCHEMA_CACHE.getLastCache(seriesPath);
-    if (timeValuePair == null) { // last value is not cached
-      return createUpdateLastCacheOperator(
-          node, context, node.getSeriesPath().getMeasurementPath());
-    } else if (!LastQueryUtil.satisfyFilter(
-        updateFilterUsingTTL(context.getLastQueryTimeFilter(), context.getDataRegionTTL()),
-        timeValuePair)) { // cached last value is not satisfied
-
-      boolean isFilterGtOrGe =
-          (context.getLastQueryTimeFilter() instanceof Gt
-              || context.getLastQueryTimeFilter() instanceof GtEq);
-      // time filter is not > or >=, we still need to read from disk
-      if (!isFilterGtOrGe) {
-        return createUpdateLastCacheOperator(
-            node, context, node.getSeriesPath().getMeasurementPath());
-      } else { // otherwise, we just ignore it and return null
-        return null;
+    AlignedPath alignedPath = node.getSeriesPath();
+    PartialPath devicePath = alignedPath.getDevicePath();
+    // get series under aligned entity that has not been cached
+    List<Integer> unCachedMeasurementIndexes = new ArrayList<>();
+    List<String> measurementList = alignedPath.getMeasurementList();
+    for (int i = 0; i < measurementList.size(); i++) {
+      PartialPath measurementPath = devicePath.concatNode(measurementList.get(i));
+      TimeValuePair timeValuePair = DATA_NODE_SCHEMA_CACHE.getLastCache(measurementPath);
+      if (timeValuePair == null) { // last value is not cached
+        unCachedMeasurementIndexes.add(i);
+      } else if (!LastQueryUtil.satisfyFilter(
+          updateFilterUsingTTL(context.getLastQueryTimeFilter(), context.getDataRegionTTL()),
+          timeValuePair)) { // cached last value is not satisfied
+
+        boolean isFilterGtOrGe =
+            (context.getLastQueryTimeFilter() instanceof Gt
+                || context.getLastQueryTimeFilter() instanceof GtEq);
+        // time filter is not > or >=, we still need to read from disk
+        if (!isFilterGtOrGe) {
+          unCachedMeasurementIndexes.add(i);
+        }
+      } else { //  cached last value is satisfied, put it into LastCacheScanOperator
+        context.addCachedLastValue(timeValuePair, measurementPath.getFullPath());
       }
-    } else { //  cached last value is satisfied, put it into LastCacheScanOperator
-      context.addCachedLastValue(timeValuePair, seriesPath.getFullPath());
+    }
+    if (unCachedMeasurementIndexes.size() == 0) {

Review Comment:
   ```suggestion
       if (unCachedMeasurementIndexes.isEmpty() {
   ```



-- 
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


[GitHub] [iotdb] lancelly closed pull request #8278: [To rel/1.0] [IOTDB-5026] Improve last query on aligned timeseries

Posted by GitBox <gi...@apache.org>.
lancelly closed pull request #8278: [To rel/1.0] [IOTDB-5026] Improve last query on aligned timeseries
URL: https://github.com/apache/iotdb/pull/8278


-- 
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