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 2023/01/05 15:47:00 UTC

[GitHub] [iotdb] Cpaulyz opened a new pull request, #8765: [IOTDB-4982] Replace recursion algorithm in Traverser with iteration algorithm

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

   ## Description
   
   WIP
   
   ### Content1 ...
   
   ### Content2 ...
   
   ### Content3 ...
   
   <!--
   In each section, please describe design decisions made, including:
    - Choice of algorithms
    - Behavioral aspects. What configuration values are acceptable? How are corner cases and error 
       conditions handled, such as when there are insufficient resources?
    - Class organization and design (how the logic is split between classes, inheritance, composition, 
       design patterns)
    - Method organization and design (how the logic is split between methods, parameters and return types)
    - Naming (class, method, API, configuration, HTTP endpoint, names of emitted metrics)
   -->
   
   
   <!-- It's good to describe an alternative design (or mention an alternative name) for every design 
   (or naming) decision point and compare the alternatives with the designs that you've implemented 
   (or the names you've chosen) to highlight the advantages of the chosen designs and names. -->
   
   <!-- If there was a discussion of the design of the feature implemented in this PR elsewhere 
   (e. g. a "Proposal" issue, any other issue, or a thread in the development mailing list), 
   link to that discussion from this PR description and explain what have changed in your final design 
   compared to your original proposal or the consensus version in the end of the discussion. 
   If something hasn't changed since the original discussion, you can omit a detailed discussion of 
   those aspects of the design here, perhaps apart from brief mentioning for the sake of readability 
   of this PR description. -->
   
   <!-- Some of the aspects mentioned above may be omitted for simple and small changes. -->
   
   <hr>
   
   This PR has:
   - [ ] been self-reviewed.
       - [ ] concurrent read
       - [ ] concurrent write
       - [ ] concurrent read and write 
   - [ ] added documentation for new or modified features or behaviors.
   - [ ] added Javadocs for most classes and all non-trivial methods. 
   - [ ] added or updated version, __license__, or notice information
   - [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious 
     for an unfamiliar reader.
   - [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold 
     for code coverage.
   - [ ] added integration tests.
   - [ ] been tested in a test IoTDB cluster.
   
   <!-- Check the items by putting "x" in the brackets for the done things. Not all of these items 
   apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items 
   from the checklist above are strictly necessary, but it would be very helpful if you at least 
   self-review the PR. -->
   
   <hr>
   
   ##### Key changed/added classes (or packages if there are too many classes) in this PR
   


-- 
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] Cpaulyz commented on a diff in pull request #8765: [IOTDB-4982] Replace recursion algorithm in Traverser with iteration algorithm

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


##########
node-commons/src/main/java/org/apache/iotdb/commons/schema/tree/AbstractTreeVisitor.java:
##########
@@ -284,10 +293,41 @@ protected final String[] generateFullPathNodes() {
         nodeNames.add(iterator.next().node.getName());
       }
     }
-    nodeNames.add(nextMatchedNode.getName());
+    return new PartialPath(nodeNames.toArray(new String[0]));
+  }
+
+  /**
+   * Get partial path from root to node.
+   *
+   * @param node node must be concluded in ancestorStack or nextMatchedNode
+   * @return partial path from traverse start node to the specified node
+   */
+  protected final PartialPath getPartialPathFromRootToNode(N node) {
+    return new PartialPath(getFullPathFromRootToNode(node));
+  }
+
+  /**
+   * Get full path from root to node.
+   *
+   * @param node node must be concluded in ancestorStack or nextMatchedNode
+   * @return full path from traverse start node to the specified node
+   */
+  protected final String[] getFullPathFromRootToNode(N node) {

Review Comment:
   The word currentNode is so vague that it can be difficult for users to understand. 
   
   In fact, the interface returns the path from root to the node in the parameter, which could be nextMatchedNode or any of the ancestor nodes. So I don't think currentNode is a better representation.



##########
node-commons/src/main/java/org/apache/iotdb/commons/schema/tree/AbstractTreeVisitor.java:
##########
@@ -284,10 +293,41 @@ protected final String[] generateFullPathNodes() {
         nodeNames.add(iterator.next().node.getName());
       }
     }
-    nodeNames.add(nextMatchedNode.getName());
+    return new PartialPath(nodeNames.toArray(new String[0]));
+  }
+
+  /**
+   * Get partial path from root to node.
+   *
+   * @param node node must be concluded in ancestorStack or nextMatchedNode
+   * @return partial path from traverse start node to the specified node
+   */
+  protected final PartialPath getPartialPathFromRootToNode(N node) {

Review Comment:
   The word currentNode is so vague that it can be difficult for users to understand.
   
   In fact, the interface returns the path from root to the node in the parameter, which could be nextMatchedNode or any of the ancestor nodes. So I don't think currentNode is a better representation.



-- 
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] Cpaulyz commented on a diff in pull request #8765: [IOTDB-4982] Replace recursion algorithm in Traverser with iteration algorithm

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


##########
server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java:
##########
@@ -697,7 +754,7 @@ public IMeasurementMNode getMeasurementMNode(PartialPath path) throws MetadataEx
   }
 
   @Override
-  public List<IMeasurementMNode> getAllMeasurementMNode() throws MetadataException {
+  public List<IMeasurementMNode> getAllMeasurementMNode() {

Review Comment:
   still a public method used by SchemaRegion..



-- 
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] Cpaulyz commented on a diff in pull request #8765: [IOTDB-4982] Replace recursion algorithm in Traverser with iteration algorithm

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


##########
server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/AbstractTraverserIterator.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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.metadata.mnode.iterator;
+
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.db.metadata.mnode.IMNode;
+import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore;
+import org.apache.iotdb.db.metadata.template.Template;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+
+import static org.apache.iotdb.db.metadata.MetadataConstant.NON_TEMPLATE;
+
+/**
+ * ChildrenIterator for traverser. The iterator returns the filtered child nodes in the mtree and
+ * the child nodes in the template.
+ */
+public abstract class AbstractTraverserIterator implements IMNodeIterator {
+  private final IMNodeIterator directChildrenIterator;
+  private Iterator<IMNode> templateChildrenIterator;
+  protected IMNode nextMatchedNode;
+  protected boolean usingDirectChildrenIterator = true;
+  // if true, the pre deleted measurement or pre deactivated template won't be processed
+  private boolean skipPreDeletedSchema = false;

Review Comment:
   done



-- 
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] Cpaulyz commented on a diff in pull request #8765: [IOTDB-4982] Replace recursion algorithm in Traverser with iteration algorithm

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


##########
server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/CachedMTreeStore.java:
##########
@@ -609,4 +621,19 @@ public void close() {
       }
     }
   }
+
+  private class CachedTraverserIterator extends AbstractTraverserIterator {
+    CachedTraverserIterator(IMTreeStore store, IMNode parent, Map<Integer, Template> templateMap)
+        throws MetadataException {
+      super(store, parent, templateMap);
+    }
+
+    @Override
+    public void close() {
+      if (nextMatchedNode != null && usingDirectChildrenIterator) {
+        unPin(nextMatchedNode);
+      }
+      super.close();
+    }

Review Comment:
   done



-- 
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] Cpaulyz commented on a diff in pull request #8765: [IOTDB-4982] Replace recursion algorithm in Traverser with iteration algorithm

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


##########
server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java:
##########
@@ -578,39 +592,78 @@ public boolean isEmptyInternalMNode(IMNode node) throws MetadataException {
     }
   }
 
+  @Override
+  public List<PartialPath> constructSchemaBlackList(PartialPath pathPattern)
+      throws MetadataException {
+    List<PartialPath> result = new ArrayList<>();
+    try (MeasurementUpdater updater =
+        new MeasurementUpdater(rootNode, pathPattern, store, false) {
+          @Override
+          protected void updateMeasurement(IMeasurementMNode node) throws MetadataException {
+            node.setPreDeleted(true);
+            updateMNode(node);

Review Comment:
   Sure. I added it to the Updater's TODO in case I forgot.



-- 
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] Cpaulyz commented on a diff in pull request #8765: [IOTDB-4982] Replace recursion algorithm in Traverser with iteration algorithm

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


##########
server/src/main/java/org/apache/iotdb/db/metadata/mtree/ConfigMTree.java:
##########
@@ -460,19 +458,19 @@ protected void transferToResult(IMNode node) {
    */
   public Pair<Set<String>, Set<PartialPath>> getChildNodeNameInNextLevel(PartialPath pathPattern)

Review Comment:
   It seems deprecated. I have deleted this method and related test.



-- 
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] Cpaulyz commented on a diff in pull request #8765: [IOTDB-4982] Replace recursion algorithm in Traverser with iteration algorithm

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


##########
server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java:
##########
@@ -1180,72 +1174,37 @@ private void recoverActivatingSchemaTemplate(IActivateTemplateInClusterPlan plan
   @Override
   public long constructSchemaBlackListWithTemplate(IPreDeactivateTemplatePlan plan)
       throws MetadataException {
-    long preDeactivateNum = 0;
-    Map<PartialPath, List<Integer>> templateSetInfo = plan.getTemplateSetInfo();
-    for (Map.Entry<PartialPath, List<Integer>> entry : templateSetInfo.entrySet()) {
-      for (IEntityMNode entityMNode :
-          mtree.getDeviceMNodeUsingTargetTemplate(entry.getKey(), entry.getValue())) {
-        Map<PartialPath, List<Integer>> subTemplateSetInfo = new HashMap<>();
-        subTemplateSetInfo.put(
-            entityMNode.getPartialPath(),
-            Collections.singletonList(entityMNode.getSchemaTemplateId()));
-        entityMNode.preDeactivateTemplate();
-        preDeactivateNum++;
-        try {
-          writeToMLog(
-              SchemaRegionWritePlanFactory.getPreDeactivateTemplatePlan(subTemplateSetInfo));
-        } catch (IOException e) {
-          throw new MetadataException(e);
-        }
-      }
+    Map<PartialPath, List<Integer>> resultTemplateSetInfo =
+        mtree.constructSchemaBlackListWithTemplate(plan.getTemplateSetInfo());
+    try {
+      writeToMLog(SchemaRegionWritePlanFactory.getPreDeactivateTemplatePlan(resultTemplateSetInfo));
+    } catch (IOException e) {
+      throw new MetadataException(e);
     }
-    return preDeactivateNum;
+    return resultTemplateSetInfo.size();
   }
 
   @Override
   public void rollbackSchemaBlackListWithTemplate(IRollbackPreDeactivateTemplatePlan plan)
       throws MetadataException {
-    Map<PartialPath, List<Integer>> templateSetInfo = plan.getTemplateSetInfo();
-    for (Map.Entry<PartialPath, List<Integer>> entry : templateSetInfo.entrySet()) {
-      for (IEntityMNode entityMNode :
-          mtree.getPreDeactivatedDeviceMNode(entry.getKey(), entry.getValue())) {
-        if (!entityMNode.isPreDeactivateTemplate()) {
-          continue;
-        }
-        Map<PartialPath, List<Integer>> subTemplateSetInfo = new HashMap<>();
-        subTemplateSetInfo.put(
-            entityMNode.getPartialPath(),
-            Collections.singletonList(entityMNode.getSchemaTemplateId()));
-        entityMNode.rollbackPreDeactivateTemplate();
-        try {
-          writeToMLog(
-              SchemaRegionWritePlanFactory.getRollbackPreDeactivateTemplatePlan(
-                  subTemplateSetInfo));
-        } catch (IOException e) {
-          throw new MetadataException(e);
-        }
-      }
+    Map<PartialPath, List<Integer>> resultTemplateSetInfo =
+        mtree.rollbackSchemaBlackListWithTemplate(plan.getTemplateSetInfo());
+    try {
+      writeToMLog(
+          SchemaRegionWritePlanFactory.getRollbackPreDeactivateTemplatePlan(resultTemplateSetInfo));
+    } catch (IOException e) {
+      throw new MetadataException(e);
     }
   }
 
   @Override
   public void deactivateTemplateInBlackList(IDeactivateTemplatePlan plan) throws MetadataException {
-    Map<PartialPath, List<Integer>> templateSetInfo = plan.getTemplateSetInfo();
-    for (Map.Entry<PartialPath, List<Integer>> entry : templateSetInfo.entrySet()) {
-      for (IEntityMNode entityMNode :
-          mtree.getPreDeactivatedDeviceMNode(entry.getKey(), entry.getValue())) {
-        Map<PartialPath, List<Integer>> subTemplateSetInfo = new HashMap<>();
-        subTemplateSetInfo.put(
-            entityMNode.getPartialPath(),
-            Collections.singletonList(entityMNode.getSchemaTemplateId()));
-        entityMNode.deactivateTemplate();
-        mtree.deleteEmptyInternalMNodeAndReturnEmptyStorageGroup(entityMNode);
-        try {
-          writeToMLog(SchemaRegionWritePlanFactory.getDeactivateTemplatePlan(subTemplateSetInfo));
-        } catch (IOException e) {
-          throw new MetadataException(e);
-        }
-      }
+    Map<PartialPath, List<Integer>> resultTemplateSetInfo =
+        mtree.deactivateTemplateInBlackList(plan.getTemplateSetInfo());
+    try {
+      writeToMLog(SchemaRegionWritePlanFactory.getDeactivateTemplatePlan(resultTemplateSetInfo));

Review Comment:
   add todo



-- 
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] MarcosZyk merged pull request #8765: [IOTDB-4982] Replace recursion algorithm in Traverser with iteration algorithm

Posted by GitBox <gi...@apache.org>.
MarcosZyk merged PR #8765:
URL: https://github.com/apache/iotdb/pull/8765


-- 
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] MarcosZyk commented on a diff in pull request #8765: [IOTDB-4982] Replace recursion algorithm in Traverser with iteration algorithm

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


##########
node-commons/src/main/java/org/apache/iotdb/commons/schema/tree/AbstractTreeVisitor.java:
##########
@@ -284,10 +293,41 @@ protected final String[] generateFullPathNodes() {
         nodeNames.add(iterator.next().node.getName());
       }
     }
-    nodeNames.add(nextMatchedNode.getName());
+    return new PartialPath(nodeNames.toArray(new String[0]));
+  }
+
+  /**
+   * Get partial path from root to node.
+   *
+   * @param node node must be concluded in ancestorStack or nextMatchedNode
+   * @return partial path from traverse start node to the specified node
+   */
+  protected final PartialPath getPartialPathFromRootToNode(N node) {

Review Comment:
   Maybe ```getCurrentNodePartialPath``` is better.



##########
node-commons/src/main/java/org/apache/iotdb/commons/schema/tree/AbstractTreeVisitor.java:
##########
@@ -284,10 +293,41 @@ protected final String[] generateFullPathNodes() {
         nodeNames.add(iterator.next().node.getName());
       }
     }
-    nodeNames.add(nextMatchedNode.getName());
+    return new PartialPath(nodeNames.toArray(new String[0]));
+  }
+
+  /**
+   * Get partial path from root to node.
+   *
+   * @param node node must be concluded in ancestorStack or nextMatchedNode
+   * @return partial path from traverse start node to the specified node
+   */
+  protected final PartialPath getPartialPathFromRootToNode(N node) {
+    return new PartialPath(getFullPathFromRootToNode(node));
+  }
+
+  /**
+   * Get full path from root to node.
+   *
+   * @param node node must be concluded in ancestorStack or nextMatchedNode
+   * @return full path from traverse start node to the specified node
+   */
+  protected final String[] getFullPathFromRootToNode(N node) {

Review Comment:
   Maybe ```getPathNodesFromRootToCurrentNodes``` is better.



##########
server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/AbstractTraverserIterator.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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.metadata.mnode.iterator;
+
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.db.metadata.mnode.IMNode;
+import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore;
+import org.apache.iotdb.db.metadata.template.Template;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+
+import static org.apache.iotdb.db.metadata.MetadataConstant.NON_TEMPLATE;
+
+/**
+ * ChildrenIterator for traverser. The iterator returns the filtered child nodes in the mtree and
+ * the child nodes in the template.
+ */
+public abstract class AbstractTraverserIterator implements IMNodeIterator {
+  private final IMNodeIterator directChildrenIterator;
+  private Iterator<IMNode> templateChildrenIterator;
+  protected IMNode nextMatchedNode;
+  protected boolean usingDirectChildrenIterator = true;
+  // if true, the pre deleted measurement or pre deactivated template won't be processed
+  private boolean skipPreDeletedSchema = false;
+
+  protected AbstractTraverserIterator(
+      IMTreeStore store, IMNode parent, Map<Integer, Template> templateMap)
+      throws MetadataException {
+    this.directChildrenIterator = store.getChildrenIterator(parent);
+    if (templateMap != null && parent.isEntity() && parent.isUseTemplate()) {
+      Template template = getActivatedSchemaTemplate(parent, templateMap);
+      if (template != null) {
+        templateChildrenIterator = template.getDirectNodes().iterator();
+      }
+    }
+  }
+
+  public void setSkipPreDeletedSchema(boolean skipPreDeletedSchema) {
+    this.skipPreDeletedSchema = skipPreDeletedSchema;
+  }
+
+  private Template getActivatedSchemaTemplate(IMNode node, Map<Integer, Template> templateMap) {

Review Comment:
   Directly set the parameter type to ```IEntityMNode```.



##########
server/src/main/java/org/apache/iotdb/db/metadata/mtree/ConfigMTree.java:
##########
@@ -460,19 +458,19 @@ protected void transferToResult(IMNode node) {
    */
   public Pair<Set<String>, Set<PartialPath>> getChildNodeNameInNextLevel(PartialPath pathPattern)

Review Comment:
   Is this method still in use in business? If TestOnly, eliminate it.



##########
server/src/main/java/org/apache/iotdb/db/metadata/mtree/store/CachedMTreeStore.java:
##########
@@ -609,4 +621,19 @@ public void close() {
       }
     }
   }
+
+  private class CachedTraverserIterator extends AbstractTraverserIterator {
+    CachedTraverserIterator(IMTreeStore store, IMNode parent, Map<Integer, Template> templateMap)
+        throws MetadataException {
+      super(store, parent, templateMap);
+    }
+
+    @Override
+    public void close() {
+      if (nextMatchedNode != null && usingDirectChildrenIterator) {
+        unPin(nextMatchedNode);
+      }
+      super.close();
+    }

Review Comment:
   Seems not strongly related to MTreeStore and invokes no private method of MTreeStore. Use an independent factory.



##########
server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGMemoryImpl.java:
##########
@@ -697,7 +754,7 @@ public IMeasurementMNode getMeasurementMNode(PartialPath path) throws MetadataEx
   }
 
   @Override
-  public List<IMeasurementMNode> getAllMeasurementMNode() throws MetadataException {
+  public List<IMeasurementMNode> getAllMeasurementMNode() {

Review Comment:
   Is this still a public method used by SchemaRegion? If not, make it private.



##########
node-commons/src/main/java/org/apache/iotdb/commons/schema/tree/AbstractTreeVisitor.java:
##########
@@ -274,7 +274,16 @@ private void popStack() {
     }
   }
 
-  protected final String[] generateFullPathNodes() {
+  /**
+   * Get full path of parent of current node. This method should be used in {@linkplain
+   * AbstractTreeVisitor#acceptInternalMatchedNode}, {@linkplain
+   * AbstractTreeVisitor#acceptFullMatchedNode},{@linkplain
+   * AbstractTreeVisitor#shouldVisitSubtreeOfInternalMatchedNode} or {@linkplain
+   * AbstractTreeVisitor#shouldVisitSubtreeOfFullMatchedNode}.
+   *
+   * @return full path from traverse start node to the parent of current node
+   */
+  protected PartialPath getAncestorPartialPath() {

Review Comment:
   Maybe ```getParentPartialPath``` is better.



##########
node-commons/src/main/java/org/apache/iotdb/commons/schema/tree/AbstractTreeVisitor.java:
##########
@@ -296,10 +336,32 @@ protected final N getParentOfNextMatchedNode() {
     }
   }
 
+  /**
+   * Get level from root to current node. Level of root is 0. For example, root.sg.d1.s1,
+   * currentNode is s1, then return 3.
+   *
+   * @return level from root to current node
+   */
+  protected final int getLevelOfNextMatchedNode() {

Review Comment:
   Anyway, the interface name and description and the parameter name provided for subclasses shall keep consistence, no matter ```NextMatchedNode``` or ```CurrentNode```.  



##########
server/src/main/java/org/apache/iotdb/db/metadata/mtree/MTreeBelowSGCachedImpl.java:
##########
@@ -578,39 +592,78 @@ public boolean isEmptyInternalMNode(IMNode node) throws MetadataException {
     }
   }
 
+  @Override
+  public List<PartialPath> constructSchemaBlackList(PartialPath pathPattern)
+      throws MetadataException {
+    List<PartialPath> result = new ArrayList<>();
+    try (MeasurementUpdater updater =
+        new MeasurementUpdater(rootNode, pathPattern, store, false) {
+          @Override
+          protected void updateMeasurement(IMeasurementMNode node) throws MetadataException {
+            node.setPreDeleted(true);
+            updateMNode(node);

Review Comment:
   Since the updated node won't be cache evicted until it been flushed to disk and currently the flush won't happen during traversing which takes the read lock, we need to consider the concurrency of flush and traverse for better memory control in future work.



##########
server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/DatabaseCounter.java:
##########
@@ -22,19 +22,33 @@
 import org.apache.iotdb.commons.path.PartialPath;
 import org.apache.iotdb.db.metadata.mnode.IMNode;
 import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore;
-import org.apache.iotdb.db.metadata.mtree.traverser.Traverser;
+import org.apache.iotdb.db.metadata.mtree.traverser.basic.DatabaseTraverser;
 
-// This class define the count as traversal result.
-public abstract class CounterTraverser extends Traverser {
+// This class implement database counter.
+public class DatabaseCounter extends DatabaseTraverser<Void> implements Counter {
 
-  protected long count;
+  private int count;
 
-  protected CounterTraverser(IMNode startNode, PartialPath path, IMTreeStore store)
+  public DatabaseCounter(
+      IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch)
       throws MetadataException {
-    super(startNode, path, store);
+    super(startNode, path, store, isPrefixMatch);
   }
 
-  public long getCount() {
+  @Override
+  protected Void generateResult(IMNode nextMatchedNode) {
+    count++;
+    return null;
+  }
+
+  @Override
+  public long count() throws MetadataException {
+    while (hasNext()) {
+      next();
+    }
+    if (getFailure() != null) {
+      throw new MetadataException(getFailure());

Review Comment:
   Set the failure message to thrown MetadataException.



##########
server/src/main/java/org/apache/iotdb/db/metadata/mnode/iterator/AbstractTraverserIterator.java:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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.metadata.mnode.iterator;
+
+import org.apache.iotdb.commons.exception.MetadataException;
+import org.apache.iotdb.db.metadata.mnode.IMNode;
+import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore;
+import org.apache.iotdb.db.metadata.template.Template;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.NoSuchElementException;
+
+import static org.apache.iotdb.db.metadata.MetadataConstant.NON_TEMPLATE;
+
+/**
+ * ChildrenIterator for traverser. The iterator returns the filtered child nodes in the mtree and
+ * the child nodes in the template.
+ */
+public abstract class AbstractTraverserIterator implements IMNodeIterator {
+  private final IMNodeIterator directChildrenIterator;
+  private Iterator<IMNode> templateChildrenIterator;
+  protected IMNode nextMatchedNode;
+  protected boolean usingDirectChildrenIterator = true;
+  // if true, the pre deleted measurement or pre deactivated template won't be processed
+  private boolean skipPreDeletedSchema = false;

Review Comment:
   It seems the ```templateChildrenIterator```, ```usingDirectChildrenIterator``` and ```skipPreDeletedSchema``` are all parameters used for measurement traverse when the parent is an entity. Should we encapsulate these into a subclass like ```MeasurementTraverserIterator``` ?



##########
server/src/main/java/org/apache/iotdb/db/metadata/schemaregion/SchemaRegionMemoryImpl.java:
##########
@@ -1180,72 +1174,37 @@ private void recoverActivatingSchemaTemplate(IActivateTemplateInClusterPlan plan
   @Override
   public long constructSchemaBlackListWithTemplate(IPreDeactivateTemplatePlan plan)
       throws MetadataException {
-    long preDeactivateNum = 0;
-    Map<PartialPath, List<Integer>> templateSetInfo = plan.getTemplateSetInfo();
-    for (Map.Entry<PartialPath, List<Integer>> entry : templateSetInfo.entrySet()) {
-      for (IEntityMNode entityMNode :
-          mtree.getDeviceMNodeUsingTargetTemplate(entry.getKey(), entry.getValue())) {
-        Map<PartialPath, List<Integer>> subTemplateSetInfo = new HashMap<>();
-        subTemplateSetInfo.put(
-            entityMNode.getPartialPath(),
-            Collections.singletonList(entityMNode.getSchemaTemplateId()));
-        entityMNode.preDeactivateTemplate();
-        preDeactivateNum++;
-        try {
-          writeToMLog(
-              SchemaRegionWritePlanFactory.getPreDeactivateTemplatePlan(subTemplateSetInfo));
-        } catch (IOException e) {
-          throw new MetadataException(e);
-        }
-      }
+    Map<PartialPath, List<Integer>> resultTemplateSetInfo =
+        mtree.constructSchemaBlackListWithTemplate(plan.getTemplateSetInfo());
+    try {
+      writeToMLog(SchemaRegionWritePlanFactory.getPreDeactivateTemplatePlan(resultTemplateSetInfo));
+    } catch (IOException e) {
+      throw new MetadataException(e);
     }
-    return preDeactivateNum;
+    return resultTemplateSetInfo.size();
   }
 
   @Override
   public void rollbackSchemaBlackListWithTemplate(IRollbackPreDeactivateTemplatePlan plan)
       throws MetadataException {
-    Map<PartialPath, List<Integer>> templateSetInfo = plan.getTemplateSetInfo();
-    for (Map.Entry<PartialPath, List<Integer>> entry : templateSetInfo.entrySet()) {
-      for (IEntityMNode entityMNode :
-          mtree.getPreDeactivatedDeviceMNode(entry.getKey(), entry.getValue())) {
-        if (!entityMNode.isPreDeactivateTemplate()) {
-          continue;
-        }
-        Map<PartialPath, List<Integer>> subTemplateSetInfo = new HashMap<>();
-        subTemplateSetInfo.put(
-            entityMNode.getPartialPath(),
-            Collections.singletonList(entityMNode.getSchemaTemplateId()));
-        entityMNode.rollbackPreDeactivateTemplate();
-        try {
-          writeToMLog(
-              SchemaRegionWritePlanFactory.getRollbackPreDeactivateTemplatePlan(
-                  subTemplateSetInfo));
-        } catch (IOException e) {
-          throw new MetadataException(e);
-        }
-      }
+    Map<PartialPath, List<Integer>> resultTemplateSetInfo =
+        mtree.rollbackSchemaBlackListWithTemplate(plan.getTemplateSetInfo());
+    try {
+      writeToMLog(
+          SchemaRegionWritePlanFactory.getRollbackPreDeactivateTemplatePlan(resultTemplateSetInfo));
+    } catch (IOException e) {
+      throw new MetadataException(e);
     }
   }
 
   @Override
   public void deactivateTemplateInBlackList(IDeactivateTemplatePlan plan) throws MetadataException {
-    Map<PartialPath, List<Integer>> templateSetInfo = plan.getTemplateSetInfo();
-    for (Map.Entry<PartialPath, List<Integer>> entry : templateSetInfo.entrySet()) {
-      for (IEntityMNode entityMNode :
-          mtree.getPreDeactivatedDeviceMNode(entry.getKey(), entry.getValue())) {
-        Map<PartialPath, List<Integer>> subTemplateSetInfo = new HashMap<>();
-        subTemplateSetInfo.put(
-            entityMNode.getPartialPath(),
-            Collections.singletonList(entityMNode.getSchemaTemplateId()));
-        entityMNode.deactivateTemplate();
-        mtree.deleteEmptyInternalMNodeAndReturnEmptyStorageGroup(entityMNode);
-        try {
-          writeToMLog(SchemaRegionWritePlanFactory.getDeactivateTemplatePlan(subTemplateSetInfo));
-        } catch (IOException e) {
-          throw new MetadataException(e);
-        }
-      }
+    Map<PartialPath, List<Integer>> resultTemplateSetInfo =
+        mtree.deactivateTemplateInBlackList(plan.getTemplateSetInfo());
+    try {
+      writeToMLog(SchemaRegionWritePlanFactory.getDeactivateTemplatePlan(resultTemplateSetInfo));

Review Comment:
   We can consider implement this as a consumer passed to MTree which takes responsibility of operating tree structure and concurrency control in future work.



-- 
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] Cpaulyz commented on a diff in pull request #8765: [IOTDB-4982] Replace recursion algorithm in Traverser with iteration algorithm

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


##########
server/src/main/java/org/apache/iotdb/db/metadata/mtree/traverser/counter/DatabaseCounter.java:
##########
@@ -22,19 +22,33 @@
 import org.apache.iotdb.commons.path.PartialPath;
 import org.apache.iotdb.db.metadata.mnode.IMNode;
 import org.apache.iotdb.db.metadata.mtree.store.IMTreeStore;
-import org.apache.iotdb.db.metadata.mtree.traverser.Traverser;
+import org.apache.iotdb.db.metadata.mtree.traverser.basic.DatabaseTraverser;
 
-// This class define the count as traversal result.
-public abstract class CounterTraverser extends Traverser {
+// This class implement database counter.
+public class DatabaseCounter extends DatabaseTraverser<Void> implements Counter {
 
-  protected long count;
+  private int count;
 
-  protected CounterTraverser(IMNode startNode, PartialPath path, IMTreeStore store)
+  public DatabaseCounter(
+      IMNode startNode, PartialPath path, IMTreeStore store, boolean isPrefixMatch)
       throws MetadataException {
-    super(startNode, path, store);
+    super(startNode, path, store, isPrefixMatch);
   }
 
-  public long getCount() {
+  @Override
+  protected Void generateResult(IMNode nextMatchedNode) {
+    count++;
+    return null;
+  }
+
+  @Override
+  public long count() throws MetadataException {
+    while (hasNext()) {
+      next();
+    }
+    if (getFailure() != null) {
+      throw new MetadataException(getFailure());

Review Comment:
   done



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