You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by "luoluoyuyu (via GitHub)" <gi...@apache.org> on 2025/05/20 04:33:55 UTC

[PR] Pipe: Optimize Batch and WAl memory allocation algorithms [iotdb]

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

   ## Description
   As the title said
   
   <!--
   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


Re: [PR] Pipe: Optimize Batch and WAl memory allocation algorithms [iotdb]

Posted by "Caideyipi (via GitHub)" <gi...@apache.org>.
Caideyipi commented on code in PR #15534:
URL: https://github.com/apache/iotdb/pull/15534#discussion_r2103677023


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/resource/memory/PipeModelFixedMemoryBlock.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.pipe.resource.memory;
+
+import org.apache.iotdb.db.pipe.resource.memory.strategy.DynamicMemoryAllocationStrategy;
+
+import java.util.Collections;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Stream;
+
+public class PipeModelFixedMemoryBlock extends PipeFixedMemoryBlock {
+
+  private final Set<PipeDynamicMemoryBlock> memoryBlocks =
+      Collections.newSetFromMap(new ConcurrentHashMap<>());
+
+  private final DynamicMemoryAllocationStrategy allocationStrategy;
+
+  private volatile long memoryAllocatedInBytes;
+
+  public PipeModelFixedMemoryBlock(
+      final long memoryUsageInBytes, final DynamicMemoryAllocationStrategy allocationStrategy) {
+    super(memoryUsageInBytes);
+    this.memoryAllocatedInBytes = 0;
+    this.allocationStrategy = allocationStrategy;
+  }
+
+  public synchronized PipeDynamicMemoryBlock registerPipeBatchMemoryBlock(
+      final long memorySizeInBytes) {
+    final PipeDynamicMemoryBlock memoryBlock = new PipeDynamicMemoryBlock(this, 0);
+    memoryBlocks.add(memoryBlock);
+    if (memorySizeInBytes != 0) {
+      resetMemoryBlockSize(memoryBlock, memorySizeInBytes);
+      double e = (double) getMemoryUsageInBytes() / memorySizeInBytes;
+      memoryBlock.updateMemoryEfficiency(e, e);
+      return memoryBlock;
+    }
+
+    memoryBlock.updateMemoryEfficiency(0.0, 0.0);
+    return memoryBlock;
+  }
+
+  @Override
+  public synchronized boolean expand() {
+    // Ensure that the memory block that gets most of the memory is released first, which can reduce
+    // the jitter of memory allocationIf the memory block is not expanded, it will not be expanded
+    // again
+    memoryBlocks.stream()
+        .sorted((a, b) -> Long.compare(b.getMemoryUsageInBytes(), a.getMemoryUsageInBytes()))
+        .forEach(PipeDynamicMemoryBlock::doExpand);

Review Comment:
   If this is not purely "expand" then you may add some comments to explain this



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/connector/payload/evolvable/batch/PipeTabletEventBatch.java:
##########
@@ -131,8 +127,14 @@ protected abstract boolean constructBatch(final TabletInsertionEvent event)
       throws WALPipeException, IOException;
 
   public boolean shouldEmit() {
-    return totalBufferSize >= getMaxBatchSizeInBytes()
-        || System.currentTimeMillis() - firstEventProcessingTime >= maxDelayInMs;
+    final long currentTimeMillis = System.currentTimeMillis();
+    if (totalBufferSize >= getMaxBatchSizeInBytes()
+        || currentTimeMillis - firstEventProcessingTime >= maxDelayInMs) {
+      allocatedMemoryBlock.updateCurrentMemoryEfficiencyAdjustMem(
+          (double) firstEventProcessingTime / currentTimeMillis);

Review Comment:
   Why use this?



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


Re: [PR] Pipe: Optimize Batch and WAl memory allocation algorithms [iotdb]

Posted by "Caideyipi (via GitHub)" <gi...@apache.org>.
Caideyipi commented on code in PR #15534:
URL: https://github.com/apache/iotdb/pull/15534#discussion_r2103677023


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/resource/memory/PipeModelFixedMemoryBlock.java:
##########
@@ -0,0 +1,115 @@
+/*
+ * 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.pipe.resource.memory;
+
+import org.apache.iotdb.db.pipe.resource.memory.strategy.DynamicMemoryAllocationStrategy;
+
+import java.util.Collections;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.stream.Stream;
+
+public class PipeModelFixedMemoryBlock extends PipeFixedMemoryBlock {
+
+  private final Set<PipeDynamicMemoryBlock> memoryBlocks =
+      Collections.newSetFromMap(new ConcurrentHashMap<>());
+
+  private final DynamicMemoryAllocationStrategy allocationStrategy;
+
+  private volatile long memoryAllocatedInBytes;
+
+  public PipeModelFixedMemoryBlock(
+      final long memoryUsageInBytes, final DynamicMemoryAllocationStrategy allocationStrategy) {
+    super(memoryUsageInBytes);
+    this.memoryAllocatedInBytes = 0;
+    this.allocationStrategy = allocationStrategy;
+  }
+
+  public synchronized PipeDynamicMemoryBlock registerPipeBatchMemoryBlock(
+      final long memorySizeInBytes) {
+    final PipeDynamicMemoryBlock memoryBlock = new PipeDynamicMemoryBlock(this, 0);
+    memoryBlocks.add(memoryBlock);
+    if (memorySizeInBytes != 0) {
+      resetMemoryBlockSize(memoryBlock, memorySizeInBytes);
+      double e = (double) getMemoryUsageInBytes() / memorySizeInBytes;
+      memoryBlock.updateMemoryEfficiency(e, e);
+      return memoryBlock;
+    }
+
+    memoryBlock.updateMemoryEfficiency(0.0, 0.0);
+    return memoryBlock;
+  }
+
+  @Override
+  public synchronized boolean expand() {
+    // Ensure that the memory block that gets most of the memory is released first, which can reduce
+    // the jitter of memory allocationIf the memory block is not expanded, it will not be expanded
+    // again
+    memoryBlocks.stream()
+        .sorted((a, b) -> Long.compare(b.getMemoryUsageInBytes(), a.getMemoryUsageInBytes()))
+        .forEach(PipeDynamicMemoryBlock::doExpand);

Review Comment:
   If this is not pure "expand" then you may add some comments to explain this



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


Re: [PR] Pipe: Optimize Batch and WAl memory allocation algorithms [iotdb]

Posted by "Caideyipi (via GitHub)" <gi...@apache.org>.
Caideyipi commented on code in PR #15534:
URL: https://github.com/apache/iotdb/pull/15534#discussion_r2102018013


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/resource/memory/PipeMemoryManager.java:
##########
@@ -100,6 +101,18 @@ private double allowedMaxMemorySizeInBytesOfTsTiles() {
         * getTotalNonFloatingMemorySizeInBytes();
   }
 
+  public long getAllocatedMemorySizeInBytesOfWAL() {
+    return (long)
+        (PipeConfig.getInstance().getPipeDataStructureWalMemoryProportion()
+            * getTotalNonFloatingMemorySizeInBytes());
+  }
+
+  public long getAllocatedMemorySizeInBytesOfBatch() {
+    return (long)
+        (PipeConfig.getInstance().getPipeDataStructureBatchMemoryProportion()

Review Comment:
   Better use "PIPE_CONFIG" directly



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/resource/memory/PipeDynamicMemoryBlock.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.pipe.resource.memory;
+
+import org.apache.tsfile.utils.Pair;
+
+import javax.validation.constraints.NotNull;
+
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+
+public class PipeDynamicMemoryBlock {
+
+  private final PipeModelFixedMemoryBlock fixedMemoryBlock;
+
+  private boolean isExpandable = true;
+
+  private Consumer<PipeDynamicMemoryBlock> expand = null;
+
+  private volatile boolean released = false;
+
+  private volatile long memoryUsageInBytes;
+
+  private volatile double historyMemoryEfficiency;
+
+  private volatile double currentMemoryEfficiency;
+
+  PipeDynamicMemoryBlock(
+      final @NotNull PipeModelFixedMemoryBlock fixedMemoryBlock, final long memoryUsageInBytes) {
+    this.memoryUsageInBytes = Math.min(memoryUsageInBytes, 0);
+    this.fixedMemoryBlock = fixedMemoryBlock;
+  }
+
+  public long getMemoryUsageInBytes() {
+    return memoryUsageInBytes;
+  }
+
+  public void setMemoryUsageInBytes(final long memoryUsageInBytes) {
+    this.memoryUsageInBytes = memoryUsageInBytes;
+  }
+
+  public Pair<Double, Double> getMemoryEfficiency() {
+    synchronized (fixedMemoryBlock) {
+      return new Pair<>(historyMemoryEfficiency, currentMemoryEfficiency);
+    }
+  }
+
+  public void setExpandable(boolean expandable) {
+    isExpandable = expandable;
+  }
+
+  public void setExpand(Consumer<PipeDynamicMemoryBlock> expand) {
+    this.expand = expand;
+  }
+
+  public double getMemoryBlockUsageRatio() {
+    return (double) memoryUsageInBytes / fixedMemoryBlock.getMemoryAllocatedInBytes();
+  }
+
+  public double getFixedMemoryBlockUsageRatio() {
+    return (double) fixedMemoryBlock.getMemoryUsageInBytes()
+        / fixedMemoryBlock.getMemoryAllocatedInBytes();

Review Comment:
   Seemingly in the contrary?



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/resource/memory/PipeMemoryManager.java:
##########
@@ -100,6 +101,18 @@ private double allowedMaxMemorySizeInBytesOfTsTiles() {
         * getTotalNonFloatingMemorySizeInBytes();
   }
 
+  public long getAllocatedMemorySizeInBytesOfWAL() {
+    return (long)
+        (PipeConfig.getInstance().getPipeDataStructureWalMemoryProportion()

Review Comment:
   Better use "PIPE_CONFIG" directly



##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/resource/memory/PipeDynamicMemoryBlock.java:
##########
@@ -0,0 +1,143 @@
+/*
+ * 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.pipe.resource.memory;
+
+import org.apache.tsfile.utils.Pair;
+
+import javax.validation.constraints.NotNull;
+
+import java.util.function.Consumer;
+import java.util.stream.Stream;
+
+public class PipeDynamicMemoryBlock {
+
+  private final PipeModelFixedMemoryBlock fixedMemoryBlock;
+
+  private boolean isExpandable = true;
+
+  private Consumer<PipeDynamicMemoryBlock> expand = null;
+
+  private volatile boolean released = false;
+
+  private volatile long memoryUsageInBytes;
+
+  private volatile double historyMemoryEfficiency;
+
+  private volatile double currentMemoryEfficiency;
+
+  PipeDynamicMemoryBlock(
+      final @NotNull PipeModelFixedMemoryBlock fixedMemoryBlock, final long memoryUsageInBytes) {
+    this.memoryUsageInBytes = Math.min(memoryUsageInBytes, 0);
+    this.fixedMemoryBlock = fixedMemoryBlock;
+  }
+
+  public long getMemoryUsageInBytes() {
+    return memoryUsageInBytes;
+  }
+
+  public void setMemoryUsageInBytes(final long memoryUsageInBytes) {
+    this.memoryUsageInBytes = memoryUsageInBytes;
+  }
+
+  public Pair<Double, Double> getMemoryEfficiency() {
+    synchronized (fixedMemoryBlock) {
+      return new Pair<>(historyMemoryEfficiency, currentMemoryEfficiency);
+    }
+  }
+
+  public void setExpandable(boolean expandable) {
+    isExpandable = expandable;
+  }
+
+  public void setExpand(Consumer<PipeDynamicMemoryBlock> expand) {
+    this.expand = expand;
+  }
+
+  public double getMemoryBlockUsageRatio() {
+    return (double) memoryUsageInBytes / fixedMemoryBlock.getMemoryAllocatedInBytes();
+  }
+
+  public double getFixedMemoryBlockUsageRatio() {
+    return (double) fixedMemoryBlock.getMemoryUsageInBytes()
+        / fixedMemoryBlock.getMemoryAllocatedInBytes();
+  }
+
+  public long canAllocateMemorySize() {
+    return fixedMemoryBlock.getMemoryAllocatedInBytes() - fixedMemoryBlock.getMemoryUsageInBytes();

Review Comment:
   Seemingly in the contrary?



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


Re: [PR] Pipe: Optimize Batch and WAL memory allocation algorithms [iotdb]

Posted by "SteveYurongSu (via GitHub)" <gi...@apache.org>.
SteveYurongSu merged PR #15534:
URL: https://github.com/apache/iotdb/pull/15534


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