This is an automated email from the ASF dual-hosted git repository.

JackieTien97 pushed a commit to branch ty/fix-query-partition-memory
in repository https://gitbox.apache.org/repos/asf/iotdb.git

commit 33277a6cf5c064d31810c519f5e1f7d4c4c1e24b
Author: JackieTien97 <[email protected]>
AuthorDate: Fri Nov 13 21:23:19 2026 +0800

    Fix query memory retention during oversized partition RPC serialization
---
 .../org/apache/iotdb/rpc/AutoResizingBuffer.java   |  17 ++-
 .../iotdb/rpc/AutoScalingBufferWriteTransport.java |  14 ++-
 .../rpc/TCompressedElasticFramedTransport.java     |   7 ++
 .../apache/iotdb/rpc/TElasticFramedTransport.java  |  21 +++-
 .../apache/iotdb/rpc/AutoResizingBufferTest.java   |  42 +++++++
 .../iotdb/rpc/TElasticFramedTransportTest.java     |  60 ++++++++++
 .../queryengine/plan/analyze/AnalyzeVisitor.java   |  21 ++++
 .../plan/analyze/QueryPartitionMemoryTest.java     | 129 +++++++++++++++++++++
 .../plan/analyze/QueryTimePartitionTest.java       |  49 ++++++++
 9 files changed, 350 insertions(+), 10 deletions(-)

diff --git 
a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java
 
b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java
index ca47d80acae..d933ecb1b40 100644
--- 
a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java
+++ 
b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java
@@ -34,6 +34,8 @@ import java.util.Arrays;
  */
 class AutoResizingBuffer {
 
+  static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
+
   private byte[] array;
   private int bufTooLargeCounter = RpcUtils.MAX_BUFFER_OVERSIZE_TIME;
   private final int initialCapacity;
@@ -59,16 +61,23 @@ class AutoResizingBuffer {
   }
 
   void ensureCapacity(int size) {
+    if (size < 0 || size > MAX_ARRAY_SIZE) {
+      throw new IllegalArgumentException("Invalid buffer size: " + size);
+    }
     final int currentCapacity = this.array.length;
     if (currentCapacity < size) {
-      // Increase by a factor of 1.5x
-      int growCapacity = currentCapacity + (currentCapacity >> 1);
-      int newCapacity = Math.max(growCapacity, size);
-      this.array = Arrays.copyOf(array, newCapacity);
+      this.array = Arrays.copyOf(array, calculateNewCapacity(currentCapacity, 
size));
       bufTooLargeCounter = RpcUtils.MAX_BUFFER_OVERSIZE_TIME;
     }
   }
 
+  static int calculateNewCapacity(int currentCapacity, int requiredCapacity) {
+    // Keep geometric growth even above 1.33 GiB. An int overflow here would 
otherwise copy the
+    // entire buffer for every small write during Thrift serialization.
+    long growCapacity = currentCapacity + (long) (currentCapacity >> 1);
+    return (int) Math.min(MAX_ARRAY_SIZE, Math.max(growCapacity, 
requiredCapacity));
+  }
+
   public byte[] array() {
     return this.array;
   }
diff --git 
a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java
 
b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java
index 9e57e53768f..ccb70b6c602 100644
--- 
a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java
+++ 
b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java
@@ -43,10 +43,20 @@ public class AutoScalingBufferWriteTransport extends 
NonOpenTransport {
   }
 
   @Override
-  public void write(byte[] toWrite, int off, int len) {
+  public void write(byte[] toWrite, int off, int len) throws 
TTransportException {
+    long requiredCapacity = (long) pos + len;
+    if (requiredCapacity > AutoResizingBuffer.MAX_ARRAY_SIZE) {
+      throw new TTransportException(
+          TTransportException.CORRUPTED_DATA,
+          "Write buffer size ("
+              + requiredCapacity
+              + ") larger than maximum array size ("
+              + AutoResizingBuffer.MAX_ARRAY_SIZE
+              + ")!");
+    }
     // A small prefix during serialization does not indicate a small frame. 
Only the complete
     // frame size, supplied after flushing, may trigger shrinking.
-    buf.ensureCapacity(pos + len);
+    buf.ensureCapacity((int) requiredCapacity);
     System.arraycopy(toWrite, off, buf.array(), pos, len);
     pos += len;
   }
diff --git 
a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransport.java
 
b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransport.java
index ab00ca2ac63..2c0e74b37d0 100644
--- 
a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransport.java
+++ 
b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransport.java
@@ -40,6 +40,13 @@ public abstract class TCompressedElasticFramedTransport 
extends TElasticFramedTr
     readCompressBuffer = new 
AutoScalingBufferReadTransport(thriftDefaultBufferSize);
   }
 
+  @Override
+  protected int getMaxWriteBufferSize() {
+    // Preserve the wire-size limit: a compressible payload may exceed 
thriftMaxFrameSize before
+    // compression. Incremental writes still must fit in a Java array.
+    return AutoResizingBuffer.MAX_ARRAY_SIZE;
+  }
+
   @Override
   protected void readFrame() throws TTransportException {
     underlying.readAll(i32buf, 0, 4);
diff --git 
a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TElasticFramedTransport.java
 
b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TElasticFramedTransport.java
index cbc74e22f42..f8a6f5af58e 100644
--- 
a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TElasticFramedTransport.java
+++ 
b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TElasticFramedTransport.java
@@ -40,9 +40,8 @@ public class TElasticFramedTransport extends TTransport {
   public static class Factory extends TTransportFactory {
 
     /**
-     * It is used to prevent the size of the parsing package from being too 
large and allocating the
-     * buffer will cause oom. Therefore, the maximum length of the requested 
memory is limited when
-     * reading.
+     * Maximum size of a frame on the wire. Uncompressed writes are checked 
before buffering;
+     * compressed writes are checked after compression.
      */
     protected final int thriftMaxFrameSize;
 
@@ -288,10 +287,24 @@ public class TElasticFramedTransport extends TTransport {
   }
 
   @Override
-  public void write(byte[] buf, int off, int len) {
+  public void write(byte[] buf, int off, int len) throws TTransportException {
+    long size = (long) writeBuffer.getPos() + len;
+    int maxSize = getMaxWriteBufferSize();
+    if (size > maxSize) {
+      // Thrift serializes the whole request before flush. Reject it before 
growing the buffer so
+      // an oversized request cannot keep allocating and retain the caller's 
query memory.
+      close();
+      throw new TTransportException(
+          TTransportException.CORRUPTED_DATA,
+          "Frame size (" + size + ") larger than protect max size (" + maxSize 
+ ")!");
+    }
     writeBuffer.write(buf, off, len);
   }
 
+  protected int getMaxWriteBufferSize() {
+    return Math.min(thriftMaxFrameSize, AutoResizingBuffer.MAX_ARRAY_SIZE);
+  }
+
   public TTransport getSocket() {
     return underlying;
   }
diff --git 
a/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/AutoResizingBufferTest.java
 
b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/AutoResizingBufferTest.java
index d4ddaa00313..4933a0e0e96 100644
--- 
a/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/AutoResizingBufferTest.java
+++ 
b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/AutoResizingBufferTest.java
@@ -20,6 +20,7 @@
 package org.apache.iotdb.rpc;
 
 import org.apache.thrift.transport.TMemoryBuffer;
+import org.apache.thrift.transport.TTransportException;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -28,6 +29,47 @@ import java.util.Random;
 
 public class AutoResizingBufferTest {
 
+  @Test
+  public void testGrowthNearIntegerLimitKeepsHeadroom() {
+    Assert.assertEquals(
+        AutoResizingBuffer.MAX_ARRAY_SIZE,
+        AutoResizingBuffer.calculateNewCapacity(1_500_000_000, 1_500_000_008));
+    Assert.assertEquals(
+        AutoResizingBuffer.MAX_ARRAY_SIZE,
+        AutoResizingBuffer.calculateNewCapacity(
+            AutoResizingBuffer.MAX_ARRAY_SIZE - 8, 
AutoResizingBuffer.MAX_ARRAY_SIZE));
+  }
+
+  @Test
+  public void testInvalidBufferSizeDoesNotResize() {
+    AutoResizingBuffer buffer = new AutoResizingBuffer(8);
+    for (int size : new int[] {-1, Integer.MAX_VALUE}) {
+      try {
+        buffer.ensureCapacity(size);
+        Assert.fail("The requested capacity must fit in a Java array");
+      } catch (IllegalArgumentException expected) {
+        Assert.assertEquals(8, buffer.array().length);
+      }
+    }
+  }
+
+  @Test
+  public void testWritePositionOverflowIsRejectedBeforeCopying() throws 
Exception {
+    AutoScalingBufferWriteTransport transport = new 
AutoScalingBufferWriteTransport(8);
+    Field position = 
AutoScalingBufferWriteTransport.class.getDeclaredField("pos");
+    position.setAccessible(true);
+    position.setInt(transport, Integer.MAX_VALUE - 4);
+    byte[] buffer = transport.getBuffer();
+    try {
+      transport.write(new byte[8]);
+      Assert.fail("The write position must not overflow");
+    } catch (TTransportException e) {
+      Assert.assertEquals(TTransportException.CORRUPTED_DATA, e.getType());
+      Assert.assertSame(buffer, transport.getBuffer());
+      Assert.assertEquals(Integer.MAX_VALUE - 4, transport.getPos());
+    }
+  }
+
   @Test
   public void testGrowthPreservesDataAndHeadroom() {
     AutoResizingBuffer buffer = new AutoResizingBuffer(100);
diff --git 
a/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/TElasticFramedTransportTest.java
 
b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/TElasticFramedTransportTest.java
index 086dc338250..55b3f1418cb 100644
--- 
a/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/TElasticFramedTransportTest.java
+++ 
b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/TElasticFramedTransportTest.java
@@ -20,17 +20,77 @@
 package org.apache.iotdb.rpc;
 
 import org.apache.thrift.transport.TByteBuffer;
+import org.apache.thrift.transport.TMemoryBuffer;
 import org.apache.thrift.transport.TTransportException;
 import org.junit.Test;
 
 import java.nio.ByteBuffer;
 import java.nio.charset.StandardCharsets;
 
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 public class TElasticFramedTransportTest {
 
+  @Test
+  public void testRejectOversizedFrameBeforeGrowingBuffer() throws Exception {
+    for (int prefixLength : new int[] {0, 64}) {
+      boolean[] closed = {false};
+      TMemoryBuffer underlying =
+          new TMemoryBuffer(0) {
+            @Override
+            public void close() {
+              closed[0] = true;
+              super.close();
+            }
+          };
+      TElasticFramedTransport transport = new 
TElasticFramedTransport(underlying, 8, 64, false);
+      transport.write(new byte[prefixLength]);
+      byte[] buffer = transport.writeBuffer.getBuffer();
+      try {
+        transport.write(new byte[65 - prefixLength]);
+        fail("An oversized frame must fail during serialization, before 
flush");
+      } catch (TTransportException e) {
+        assertEquals(TTransportException.CORRUPTED_DATA, e.getType());
+        assertTrue(e.getMessage().contains("Frame size (65)"));
+        assertSame(buffer, transport.writeBuffer.getBuffer());
+        assertEquals(prefixLength, transport.writeBuffer.getPos());
+        assertEquals(0, underlying.length());
+        assertTrue(closed[0]);
+      }
+    }
+  }
+
+  @Test
+  public void testFrameAtSizeLimit() throws Exception {
+    TElasticFramedTransport transport =
+        new TElasticFramedTransport(new TMemoryBuffer(0), 8, 64, false);
+    byte[] payload = new byte[64];
+    payload[63] = 42;
+    for (int i = 0; i < payload.length; i += 8) {
+      transport.write(payload, i, 8);
+    }
+    transport.flush();
+    byte[] actual = new byte[payload.length];
+    transport.readAll(actual, 0, actual.length);
+    assertArrayEquals(payload, actual);
+  }
+
+  @Test
+  public void testCompressedFrameLimitAppliesAfterCompression() throws 
Exception {
+    TElasticFramedTransport transport =
+        new TSnappyElasticFramedTransport(new TMemoryBuffer(0), 8, 64, false);
+    byte[] payload = new byte[256];
+    transport.write(payload);
+    transport.flush();
+    byte[] actual = new byte[payload.length];
+    transport.readAll(actual, 0, actual.length);
+    assertArrayEquals(payload, actual);
+  }
+
   @Test
   public void testSingularSize() {
 
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java
index c393c275dd5..b9ab605f5a7 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/analyze/AnalyzeVisitor.java
@@ -47,6 +47,7 @@ import org.apache.iotdb.db.conf.IoTDBDescriptor;
 import org.apache.iotdb.db.exception.ainode.GetModelInfoException;
 import 
org.apache.iotdb.db.exception.metadata.template.TemplateIncompatibleException;
 import org.apache.iotdb.db.exception.metadata.view.UnsupportedViewException;
+import org.apache.iotdb.db.exception.query.QueryTimeoutRuntimeException;
 import org.apache.iotdb.db.exception.sql.SemanticException;
 import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
 import org.apache.iotdb.db.i18n.DataNodeQueryMessages;
@@ -2327,6 +2328,7 @@ public class AnalyzeVisitor extends 
StatementVisitor<Analysis, MPPQueryContext>
   @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity 
warning
   public static Pair<List<TTimePartitionSlot>, Pair<Boolean, Boolean>> 
getTimePartitionSlotList(
       Filter timeFilter, MPPQueryContext context) {
+    checkTimePartitionQueryTimeout(context);
     if (timeFilter == null) {
       // (-oo, +oo)
       return new Pair<>(Collections.emptyList(), new Pair<>(true, true));
@@ -2370,7 +2372,13 @@ public class AnalyzeVisitor extends 
StatementVisitor<Analysis, MPPQueryContext>
     TimeRange currentTimeRange = timeRangeList.get(index);
     reserveMemoryForTimePartitionSlot(
         currentTimeRange.getMax(), currentTimeRange.getMin(), context);
+    int iterations = 0;
     while (index < size) {
+      // Large finite ranges can keep the front-end reservation alive for a 
long time. Check in
+      // batches so timeout handling does not add a clock read for every 
partition.
+      if (iterations++ % 1024 == 0) {
+        checkTimePartitionQueryTimeout(context);
+      }
       long curLeft = timeRangeList.get(index).getMin();
       long curRight = timeRangeList.get(index).getMax();
       if (curLeft >= endTime) {
@@ -2405,6 +2413,19 @@ public class AnalyzeVisitor extends 
StatementVisitor<Analysis, MPPQueryContext>
     return new Pair<>(result, new Pair<>(needLeftAll, needRightAll));
   }
 
+  private static void checkTimePartitionQueryTimeout(MPPQueryContext context) {
+    long startTime = context.getStartTime();
+    // Standalone analysis contexts may not have an execution deadline.
+    if (startTime == 0) {
+      return;
+    }
+    long currentTime = System.currentTimeMillis();
+    long timeout = context.getTimeOut();
+    if (currentTime - startTime >= timeout) {
+      throw new QueryTimeoutRuntimeException(startTime, currentTime, timeout);
+    }
+  }
+
   private static void reserveMemoryForTimePartitionSlot(
       long maxTime, long minTime, MPPQueryContext context) {
     if (maxTime == Long.MAX_VALUE || minTime == Long.MIN_VALUE) {
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/QueryPartitionMemoryTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/QueryPartitionMemoryTest.java
new file mode 100644
index 00000000000..78dec77e804
--- /dev/null
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/QueryPartitionMemoryTest.java
@@ -0,0 +1,129 @@
+/*
+ * 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.queryengine.plan.analyze;
+
+import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot;
+import org.apache.iotdb.commons.partition.DataPartition;
+import org.apache.iotdb.commons.partition.DataPartitionQueryParam;
+import org.apache.iotdb.commons.utils.TimePartitionUtils;
+import org.apache.iotdb.confignode.rpc.thrift.IConfigNodeRPCService;
+import org.apache.iotdb.confignode.rpc.thrift.TDataPartitionReq;
+import org.apache.iotdb.confignode.rpc.thrift.TTimeSlotList;
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.queryengine.common.SessionInfo;
+import org.apache.iotdb.db.queryengine.plan.Coordinator;
+import org.apache.iotdb.db.queryengine.plan.parser.StatementGenerator;
+import org.apache.iotdb.db.queryengine.plan.planner.LocalExecutionPlanner;
+import org.apache.iotdb.rpc.TElasticFramedTransport;
+
+import org.apache.thrift.TException;
+import org.apache.thrift.protocol.TBinaryProtocol;
+import org.apache.thrift.transport.TMemoryBuffer;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.time.ZoneOffset;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class QueryPartitionMemoryTest {
+
+  private int originalDataNodeId;
+
+  @Before
+  public void setUp() {
+    originalDataNodeId = 
IoTDBDescriptor.getInstance().getConfig().getDataNodeId();
+    IoTDBDescriptor.getInstance().getConfig().setDataNodeId(0);
+  }
+
+  @After
+  public void tearDown() {
+    
IoTDBDescriptor.getInstance().getConfig().setDataNodeId(originalDataNodeId);
+  }
+
+  @Test
+  public void testOversizedPartitionRequestReleasesFrontEndMemory() {
+    LocalExecutionPlanner planner = LocalExecutionPlanner.getInstance();
+    long freeMemory = planner.getFreeMemoryForOperators();
+    String sql =
+        "select s1 from root.sg.d1 where time >= 0 and time <= "
+            + TimePartitionUtils.getTimePartitionInterval() * 50_000;
+    for (long queryId = 100; queryId < 103; queryId++) {
+      final int[] serializedBytes = {0};
+      IPartitionFetcher partitionFetcher =
+          new FakePartitionFetcherImpl() {
+            @Override
+            public DataPartition getDataPartition(
+                Map<String, List<DataPartitionQueryParam>> params) {
+              assertTrue(planner.getFreeMemoryForOperators() < freeMemory);
+              DataPartitionQueryParam param = params.get("root.sg").get(0);
+              TTimeSlotList slots =
+                  new TTimeSlotList(param.getTimePartitionSlotList(), false, 
false);
+              TDataPartitionReq request =
+                  new TDataPartitionReq(
+                      Collections.singletonMap(
+                          "root.sg", Collections.singletonMap(new 
TSeriesPartitionSlot(0), slots)));
+              try (TElasticFramedTransport transport =
+                  new TElasticFramedTransport(new TMemoryBuffer(0), 16, 1024, 
false) {
+                    @Override
+                    public void close() {
+                      serializedBytes[0] = writeBuffer.getPos();
+                      super.close();
+                    }
+                  }) {
+                new IConfigNodeRPCService.Client(new 
TBinaryProtocol(transport))
+                    .send_getDataPartitionTable(request);
+                fail("The partition request must exceed the frame size limit");
+              } catch (TException e) {
+                throw new StatementAnalyzeException(e.getMessage());
+              }
+              return null;
+            }
+          };
+      try {
+        Coordinator.getInstance()
+            .executeForTreeModel(
+                StatementGenerator.createStatement(sql, ZoneOffset.UTC),
+                queryId,
+                new SessionInfo(0, "root", ZoneOffset.UTC, ""),
+                sql,
+                partitionFetcher,
+                new FakeSchemaFetcherImpl(),
+                60_000,
+                true,
+                false);
+        fail("The oversized partition request must fail query analysis");
+      } catch (StatementAnalyzeException e) {
+        assertTrue(e.getMessage(), e.getMessage().contains("larger than 
protect max size (1024)"));
+      }
+      assertTrue(serializedBytes[0] > 0);
+      assertTrue("Serialization must stop at the frame limit", 
serializedBytes[0] <= 1024);
+      assertEquals(freeMemory, planner.getFreeMemoryForOperators());
+      assertNull(Coordinator.getInstance().getQueryExecution(queryId));
+    }
+  }
+}
diff --git 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/QueryTimePartitionTest.java
 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/QueryTimePartitionTest.java
index aa31e0ed376..5293445498b 100644
--- 
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/QueryTimePartitionTest.java
+++ 
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/QueryTimePartitionTest.java
@@ -21,8 +21,10 @@ package org.apache.iotdb.db.queryengine.plan.analyze;
 import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
 import org.apache.iotdb.commons.conf.CommonDescriptor;
 import org.apache.iotdb.commons.utils.TimePartitionUtils;
+import org.apache.iotdb.db.exception.query.QueryTimeoutRuntimeException;
 import org.apache.iotdb.db.queryengine.common.MPPQueryContext;
 import org.apache.iotdb.db.queryengine.common.QueryId;
+import org.apache.iotdb.db.queryengine.plan.planner.LocalExecutionPlanner;
 
 import org.apache.tsfile.read.common.TimeRange;
 import org.apache.tsfile.read.filter.basic.Filter;
@@ -39,9 +41,56 @@ import static 
org.apache.iotdb.db.queryengine.plan.analyze.AnalyzeVisitor.getTim
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 public class QueryTimePartitionTest {
 
+  @Test
+  public void testTimeoutDuringTimePartitionEnumerationReleasesMemory() {
+    long freeMemory = 
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators();
+    MPPQueryContext context =
+        new MPPQueryContext(new QueryId("time_partition_enumeration_timeout")) 
{
+          private int timeoutChecks;
+
+          @Override
+          public long getTimeOut() {
+            // Expire after the initial check, once the partition list has 
reserved memory.
+            return timeoutChecks++ == 0 ? Long.MAX_VALUE : 1;
+          }
+        };
+    context.setStartTime(1);
+    try {
+      getTimePartitionSlotList(
+          TimeFilterApi.between(0, 
TimePartitionUtils.getTimePartitionInterval() * 50_000),
+          context);
+      fail("Time partition enumeration must periodically check the query 
timeout");
+    } catch (QueryTimeoutRuntimeException expected) {
+      
assertTrue(LocalExecutionPlanner.getInstance().getFreeMemoryForOperators() < 
freeMemory);
+    } finally {
+      context.releaseAllMemoryReservedForFrontEnd();
+    }
+    assertEquals(freeMemory, 
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators());
+  }
+
+  @Test
+  public void testExpiredQueryDoesNotEnumerateTimePartitions() {
+    MPPQueryContext context = new MPPQueryContext(new 
QueryId("expired_time_partitions"));
+    context.setStartTime(System.currentTimeMillis() - 10_000);
+    context.setTimeOut(1);
+    long freeMemory = 
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators();
+    try {
+      getTimePartitionSlotList(
+          TimeFilterApi.between(0, 
TimePartitionUtils.getTimePartitionInterval() * 50_000),
+          context);
+      fail("An expired query must stop before enumerating time partitions");
+    } catch (QueryTimeoutRuntimeException expected) {
+      assertEquals(freeMemory, 
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators());
+    } finally {
+      context.releaseAllMemoryReservedForFrontEnd();
+    }
+    assertEquals(freeMemory, 
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators());
+  }
+
   @Test
   public void testAndTimeFilter() {
     Filter left = TimeFilterApi.gt(10);

Reply via email to