This is an automated email from the ASF dual-hosted git repository.
JackieTien97 pushed a commit to branch dev/1.3
in repository https://gitbox.apache.org/repos/asf/iotdb.git
The following commit(s) were added to refs/heads/dev/1.3 by this push:
new 752640331af [To dev/1.3] Fix query partition memory retention and
failure diagnostics
752640331af is described below
commit 752640331afa22a0bd43de5c46397c21e79b277c
Author: Jackie Tien <[email protected]>
AuthorDate: Sat Nov 14 13:04:00 2026 +0800
[To dev/1.3] Fix query partition memory retention and failure diagnostics
---
.github/workflows/sonar-codecov.yml | 5 +
.../org/apache/iotdb/rpc/AutoResizingBuffer.java | 23 +-
.../iotdb/rpc/AutoScalingBufferWriteTransport.java | 31 ++-
.../rpc/TCompressedElasticFramedTransport.java | 19 ++
.../apache/iotdb/rpc/TElasticFramedTransport.java | 42 ++-
.../apache/iotdb/rpc/AutoResizingBufferTest.java | 42 +++
.../iotdb/rpc/TElasticFramedTransportTest.java | 212 +++++++++++++++
.../iotdb/db/protocol/client/ConfigNodeClient.java | 6 +
.../db/queryengine/common/MPPQueryContext.java | 31 +++
.../iotdb/db/queryengine/plan/Coordinator.java | 8 +
.../queryengine/plan/analyze/AnalyzeVisitor.java | 24 +-
.../NotThreadSafeMemoryReservationManager.java | 15 +-
.../db/protocol/client/ConfigNodeClientTest.java | 101 +++++++
.../db/queryengine/common/MPPQueryContextTest.java | 37 +++
.../plan/analyze/QueryPartitionMemoryTest.java | 292 +++++++++++++++++++++
.../plan/analyze/QueryTimePartitionTest.java | 47 ++++
.../LocalExecutionPlannerOperatorsMemoryTest.java | 22 ++
17 files changed, 943 insertions(+), 14 deletions(-)
diff --git a/.github/workflows/sonar-codecov.yml
b/.github/workflows/sonar-codecov.yml
index e29ff35cf67..e633a5fbcdc 100644
--- a/.github/workflows/sonar-codecov.yml
+++ b/.github/workflows/sonar-codecov.yml
@@ -43,6 +43,11 @@ jobs:
steps:
- uses: actions/checkout@v4
+ - name: Set up JDK 21
+ uses: actions/setup-java@v4
+ with:
+ distribution: corretto
+ java-version: 21
- name: Cache Maven packages
uses: actions/cache@v4
with:
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..ee86294cced 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,29 @@ class AutoResizingBuffer {
}
void ensureCapacity(int size) {
+ ensureCapacity(size, MAX_ARRAY_SIZE);
+ }
+
+ void ensureCapacity(int size, int maxCapacity) {
+ int limit = Math.min(maxCapacity, MAX_ARRAY_SIZE);
+ if (size < 0 || size > limit) {
+ 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, Math.min(calculateNewCapacity(currentCapacity,
size), limit));
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..c193bd3ea35 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,14 +43,41 @@ 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 {
+ write(toWrite, off, len, AutoResizingBuffer.MAX_ARRAY_SIZE);
+ }
+
+ void write(byte[] toWrite, int off, int len, int maxCapacity) throws
TTransportException {
+ int limit = Math.min(maxCapacity, AutoResizingBuffer.MAX_ARRAY_SIZE);
+ long requiredCapacity = (long) pos + len;
+ if (requiredCapacity > limit) {
+ throw new TTransportException(
+ TTransportException.CORRUPTED_DATA,
+ "Write buffer size ("
+ + requiredCapacity
+ + ") larger than maximum buffer size ("
+ + limit
+ + ")!");
+ }
// 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, limit);
System.arraycopy(toWrite, off, buf.array(), pos, len);
pos += len;
}
+ // Framed callers keep capacity within their limit. A fitting write needs no
further limit
+ // checks; compare remaining space rather than adding pos and len, which
could overflow.
+ boolean tryWriteWithinCapacity(byte[] toWrite, int off, int len) {
+ byte[] array = buf.array();
+ if (len > array.length - pos) {
+ return false;
+ }
+ System.arraycopy(toWrite, off, array, pos, len);
+ pos += len;
+ return true;
+ }
+
public int getPos() {
return pos;
}
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..ff2037d81af 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);
@@ -49,6 +56,7 @@ public abstract class TCompressedElasticFramedTransport
extends TElasticFramedTr
RpcStat.readCompressedBytes.addAndGet(size);
try {
int uncompressedLength = uncompressedLength(readBuffer.getBuffer(), 0,
size);
+ checkBufferSize(uncompressedLength);
RpcStat.readBytes.addAndGet(uncompressedLength);
readCompressBuffer.resizeIfNecessary(uncompressedLength);
uncompress(readBuffer.getBuffer(), 0, size,
readCompressBuffer.getBuffer(), 0);
@@ -66,6 +74,7 @@ public abstract class TCompressedElasticFramedTransport
extends TElasticFramedTr
RpcStat.writeBytes.addAndGet(length);
try {
int maxCompressedLength = maxCompressedLength(length);
+ checkBufferSize(maxCompressedLength);
writeCompressBuffer.resizeIfNecessary(maxCompressedLength);
int compressedLength =
compress(writeBuffer.getBuffer(), 0, length,
writeCompressBuffer.getBuffer(), 0);
@@ -83,6 +92,16 @@ public abstract class TCompressedElasticFramedTransport
extends TElasticFramedTr
underlying.flush();
}
+ private void checkBufferSize(int size) throws TTransportException {
+ if (size < 0 || size > AutoResizingBuffer.MAX_ARRAY_SIZE) {
+ // Compression bounds can overflow int even when the input fits in a
Java array. Reject
+ // before resizing and invalidate the transport, just as for an
oversized wire frame.
+ close();
+ throw new TTransportException(
+ TTransportException.CORRUPTED_DATA, "Invalid compression buffer
size: " + size);
+ }
+ }
+
protected abstract int uncompressedLength(byte[] but, int off, int len)
throws IOException;
protected abstract int maxCompressedLength(int len);
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..beff263faee 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;
@@ -88,7 +87,11 @@ public class TElasticFramedTransport extends TTransport {
this.thriftMaxFrameSize = thriftMaxFrameSize;
this.copyBinary = copyBinary;
readBuffer = new AutoScalingBufferReadTransport(thriftDefaultBufferSize);
- writeBuffer = new AutoScalingBufferWriteTransport(thriftDefaultBufferSize);
+ writeBuffer =
+ new AutoScalingBufferWriteTransport(
+ Math.min(
+ thriftDefaultBufferSize,
+ Math.min(thriftMaxFrameSize,
AutoResizingBuffer.MAX_ARRAY_SIZE)));
}
protected final int thriftDefaultBufferSize;
@@ -288,8 +291,35 @@ public class TElasticFramedTransport extends TTransport {
}
@Override
- public void write(byte[] buf, int off, int len) {
- writeBuffer.write(buf, off, len);
+ public void write(byte[] buf, int off, int len) throws TTransportException {
+ if (writeBuffer.tryWriteWithinCapacity(buf, off, len)) {
+ return;
+ }
+ 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();
+ String limitDescription =
+ maxSize == AutoResizingBuffer.MAX_ARRAY_SIZE
+ ? "maximum Java array size"
+ : "configured Thrift max frame size";
+ throw new TTransportException(
+ TTransportException.CORRUPTED_DATA,
+ "Serialized frame size is at least "
+ + size
+ + " bytes, exceeding the "
+ + limitDescription
+ + " of "
+ + maxSize
+ + " bytes.");
+ }
+ writeBuffer.write(buf, off, len, maxSize);
+ }
+
+ protected int getMaxWriteBufferSize() {
+ return Math.min(thriftMaxFrameSize, AutoResizingBuffer.MAX_ARRAY_SIZE);
}
public TTransport getSocket() {
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..d1873ede91a 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,229 @@
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.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+import java.util.Random;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
+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(), e.getMessage().contains("at least 65
bytes"));
+ assertTrue(
+ e.getMessage(),
+ e.getMessage().contains("configured Thrift max frame size of 64
bytes"));
+ assertFalse(e.getMessage(),
e.getMessage().contains("dn_thrift_max_frame_size"));
+ assertSame(buffer, transport.writeBuffer.getBuffer());
+ assertEquals(prefixLength, transport.writeBuffer.getPos());
+ assertEquals(0, underlying.length());
+ assertTrue(closed[0]);
+ }
+ }
+ }
+
+ @Test
+ public void testIncrementalGrowthDoesNotExceedFrameLimit() throws Exception {
+ int limit = 1024 * 1024;
+ try (TElasticFramedTransport transport =
+ new TElasticFramedTransport(new TMemoryBuffer(0), 1024, limit, false))
{
+ byte[] chunk = new byte[8];
+ Arrays.fill(chunk, (byte) 42);
+ for (int written = 0; written < limit; written += chunk.length) {
+ transport.write(chunk);
+ assertTrue(
+ "Allocated " + transport.writeBuffer.getBuffer().length + " bytes
for limit " + limit,
+ transport.writeBuffer.getBuffer().length <= limit);
+ }
+ assertEquals(limit, transport.writeBuffer.getBuffer().length);
+ assertEquals(42, transport.writeBuffer.getBuffer()[0]);
+ assertEquals(42, transport.writeBuffer.getBuffer()[limit - 1]);
+ TTransportException failure =
+ assertThrows(TTransportException.class, () ->
transport.write(chunk));
+ assertTrue(failure.getMessage(), failure.getMessage().contains("at least
1048584 bytes"));
+ assertEquals(limit, transport.writeBuffer.getBuffer().length);
+ }
+ }
+
+ @Test
+ public void testInitialWriteBufferRespectsFrameLimit() throws Exception {
+ try (TElasticFramedTransport transport =
+ new TElasticFramedTransport(new TMemoryBuffer(0), 1024, 64, false)) {
+ assertEquals(64, transport.writeBuffer.getBuffer().length);
+ byte[] payload = new byte[64];
+ payload[63] = 42;
+ for (int frame = 0; frame < 2; frame++) {
+ transport.write(payload);
+ transport.flush();
+ byte[] actual = new byte[payload.length];
+ transport.readAll(actual, 0, actual.length);
+ assertArrayEquals(payload, actual);
+ assertEquals(64, transport.writeBuffer.getBuffer().length);
+ }
+ }
+ }
+
+ @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), 128, 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 testCompressedFrameLimitIsCheckedAfterBufferedWrites() throws
Exception {
+ boolean[] closed = {false};
+ TMemoryBuffer underlying =
+ new TMemoryBuffer(0) {
+ @Override
+ public void close() {
+ closed[0] = true;
+ super.close();
+ }
+ };
+ TSnappyElasticFramedTransport transport =
+ new TSnappyElasticFramedTransport(underlying, 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);
+ byte[] buffer = transport.writeBuffer.getBuffer();
+
+ new Random(0).nextBytes(payload);
+ transport.write(payload);
+ assertSame(buffer, transport.writeBuffer.getBuffer());
+ TTransportException failure = assertThrows(TTransportException.class,
transport::flush);
+ assertEquals(TTransportException.CORRUPTED_DATA, failure.getType());
+ assertTrue(closed[0]);
+ }
+
+ @Test
+ public void testBufferedWriteCannotBypassArrayLimitOnOverflow() throws
Exception {
+ for (boolean compressed : new boolean[] {false, true}) {
+ boolean[] closed = {false};
+ TMemoryBuffer underlying =
+ new TMemoryBuffer(0) {
+ @Override
+ public void close() {
+ closed[0] = true;
+ super.close();
+ }
+ };
+ TElasticFramedTransport transport =
+ compressed
+ ? new TSnappyElasticFramedTransport(underlying, 8, 64, false)
+ : new TElasticFramedTransport(underlying, 8, Integer.MAX_VALUE,
false);
+ Field position =
AutoScalingBufferWriteTransport.class.getDeclaredField("pos");
+ position.setAccessible(true);
+ int oldPosition = AutoResizingBuffer.MAX_ARRAY_SIZE - 4;
+ position.setInt(transport.writeBuffer, oldPosition);
+ final byte[] buffer = transport.writeBuffer.getBuffer();
+ TTransportException failure =
+ assertThrows(TTransportException.class, () -> transport.write(new
byte[16]));
+ assertEquals(TTransportException.CORRUPTED_DATA, failure.getType());
+ assertTrue(failure.getMessage(), failure.getMessage().contains("maximum
Java array size"));
+ assertFalse(failure.getMessage(),
failure.getMessage().contains("dn_thrift_max_frame_size"));
+ assertSame(buffer, transport.writeBuffer.getBuffer());
+ assertEquals(oldPosition, transport.writeBuffer.getPos());
+ assertTrue(closed[0]);
+ }
+ }
+
+ @Test
+ public void testInvalidCompressionBufferSizesCloseTransport() throws
Exception {
+ for (int size : new int[] {-1, Integer.MAX_VALUE}) {
+ for (boolean read : new boolean[] {false, true}) {
+ boolean[] closed = {false};
+ TMemoryBuffer underlying =
+ new TMemoryBuffer(0) {
+ @Override
+ public void close() {
+ closed[0] = true;
+ super.close();
+ }
+ };
+ TSnappyElasticFramedTransport transport =
+ new TSnappyElasticFramedTransport(underlying, 8, 64, false) {
+ @Override
+ protected int maxCompressedLength(int length) {
+ // Snappy's bound overflows int for large, otherwise valid
input lengths.
+ return size;
+ }
+
+ @Override
+ protected int uncompressedLength(byte[] bytes, int offset, int
length) {
+ return size;
+ }
+ };
+ TTransportException failure;
+ if (read) {
+ underlying.write(new byte[] {0, 0, 0, 1, 0});
+ failure =
+ assertThrows(TTransportException.class, () -> transport.read(new
byte[1], 0, 1));
+ } else {
+ transport.write(new byte[1]);
+ failure = assertThrows(TTransportException.class, transport::flush);
+ }
+ assertEquals(TTransportException.CORRUPTED_DATA, failure.getType());
+ assertTrue(closed[0]);
+ }
+ }
+ }
+
@Test
public void testSingularSize() {
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/client/ConfigNodeClient.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/client/ConfigNodeClient.java
index cf83a81cd48..07d12e5b607 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/client/ConfigNodeClient.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/client/ConfigNodeClient.java
@@ -386,6 +386,12 @@ public class ConfigNodeClient implements
IConfigNodeRPCService.Iface, ThriftClie
}
detectedNodeNum++;
} catch (TException e) {
+ if (e instanceof TTransportException transportException
+ && transportException.getType() ==
TTransportException.CORRUPTED_DATA) {
+ // Reconnecting cannot repair an invalid/oversized frame. Repeated
serialization would
+ // retain query memory and eventually hide the original error as a
connection failure.
+ throw e;
+ }
final String message =
String.format(
MSG_RECONNECTION_DATANODE_FAIL,
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/MPPQueryContext.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/MPPQueryContext.java
index 73d4093e6c7..7db3b7abe46 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/MPPQueryContext.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/common/MPPQueryContext.java
@@ -460,6 +460,37 @@ public class MPPQueryContext {
}
}
+ public void reserveMemoryForTimePartitionSlots(final long bytes) {
+ try {
+ this.memoryReservationManager.reserveMemoryCumulatively(bytes);
+ } catch (MemoryNotEnoughException e) {
+ long requestedBytes = bytes;
+ long freeBytes;
+ if (e instanceof OperatorMemoryNotEnoughException memoryFailure) {
+ requestedBytes = memoryFailure.getRequestedBytes();
+ freeBytes = memoryFailure.getFreeBytes();
+ } else {
+ freeBytes =
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators();
+ }
+ throw new MemoryNotEnoughException(
+ String.format(
+ Locale.ROOT,
+ "Not enough memory while analyzing time partitions. "
+ + "Narrow the query time range or increase query memory. "
+ + "Memory details: requested this time %s, current free
memory %s. "
+ + "Original error: %s",
+ formatBytes(requestedBytes),
+ formatBytes(freeBytes),
+ e.getMessage()));
+ }
+ }
+
+ public void finishResultSetColumnMemoryTracking() {
+ // Column expansion has finished. Later partition/planning reservations
must not be diagnosed
+ // as excessive result columns just because this context still contains
column statistics.
+ resultSetColumnMemoryTrackingEnabled = false;
+ }
+
public void releaseAllMemoryReservedForFrontEnd() {
this.memoryReservationManager.releaseAllReservedMemory();
}
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java
index 22951f71258..00fd3344119 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/Coordinator.java
@@ -124,6 +124,8 @@ public class Coordinator {
new ThreadPoolExecutor.CallerRunsPolicy());
}
+ // Record the SQL for Errors as well, preserving the original failure and
finally cleanup.
+ @SuppressWarnings("java:S1181")
private ExecutionResult execution(
long queryId,
SessionInfo session,
@@ -162,6 +164,12 @@ public class Coordinator {
result.status.setNeedRetry(true);
}
return result;
+ } catch (RuntimeException | Error e) {
+ // Analysis can fail before registration. Only link the query ID to its
SQL here; callers
+ // classify the failure and log its stack, avoiding duplicate stacks and
WARNs for user
+ // errors.
+ LOGGER.info("[QueryFailed] queryId: {}, sql: {}", globalQueryId, sql);
+ throw e;
} finally {
if (queryContext != null) {
queryContext.releaseAllMemoryReservedForFrontEnd();
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..cf7ccc884ad 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,8 @@ 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);
+ context.finishResultSetColumnMemoryTracking();
if (timeFilter == null) {
// (-oo, +oo)
return new Pair<>(Collections.emptyList(), new Pair<>(true, true));
@@ -2370,7 +2373,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,13 +2414,26 @@ 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) {
return;
}
long size = TimePartitionUtils.getEstimateTimePartitionSize(minTime,
maxTime);
- context.reserveMemoryForFrontEnd(estimateTimePartitionSlotMemory(size));
+
context.reserveMemoryForTimePartitionSlots(estimateTimePartitionSlotMemory(size));
}
private static List<TimeRange> normalizeTimeRanges(List<TimeRange>
timeRanges) {
diff --git
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/memory/NotThreadSafeMemoryReservationManager.java
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/memory/NotThreadSafeMemoryReservationManager.java
index 514a2935f6c..1140fec0793 100644
---
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/memory/NotThreadSafeMemoryReservationManager.java
+++
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/memory/NotThreadSafeMemoryReservationManager.java
@@ -77,7 +77,20 @@ public class NotThreadSafeMemoryReservationManager
implements MemoryReservationM
@Override
public void reserveMemoryCumulatively(final long size) {
- bytesToBeReserved += size;
+ try {
+ bytesToBeReserved = Math.addExact(bytesToBeReserved, size);
+ } catch (ArithmeticException e) {
+ // A saturated partition-size estimate can be Long.MAX_VALUE while an
earlier batch is
+ // still pending. Reject it without wrapping the counter or changing
that pending batch.
+ throw new OperatorMemoryNotEnoughException(
+ "Query "
+ + queryId.getId()
+ + " cannot reserve "
+ + size
+ + " bytes: pending front-end memory reservation exceeds
Long.MAX_VALUE bytes.",
+ Long.MAX_VALUE,
+ LOCAL_EXECUTION_PLANNER.getFreeMemoryForOperators());
+ }
if (bytesToBeReserved >= MEMORY_BATCH_THRESHOLD) {
try {
reserveMemoryImmediately();
diff --git
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/client/ConfigNodeClientTest.java
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/client/ConfigNodeClientTest.java
new file mode 100644
index 00000000000..edef1e7202f
--- /dev/null
+++
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/client/ConfigNodeClientTest.java
@@ -0,0 +1,101 @@
+/*
+ * 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.protocol.client;
+
+import org.apache.iotdb.common.rpc.thrift.TEndPoint;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.client.property.ThriftClientProperty;
+import org.apache.iotdb.confignode.rpc.thrift.IConfigNodeRPCService;
+import org.apache.iotdb.confignode.rpc.thrift.TDataPartitionReq;
+import org.apache.iotdb.confignode.rpc.thrift.TDataPartitionTableResp;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import org.apache.thrift.transport.TTransportException;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.util.Collections;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class ConfigNodeClientTest {
+
+ @Test
+ public void testInvalidFrameDoesNotReconnectOrHideOriginalFailure() throws
Exception {
+ IConfigNodeRPCService.Iface service =
mock(IConfigNodeRPCService.Iface.class);
+ TDataPartitionReq request = new TDataPartitionReq();
+ TTransportException failure =
+ new TTransportException(TTransportException.CORRUPTED_DATA, "Frame
size exceeded");
+ when(service.getDataPartitionTable(request)).thenThrow(failure);
+ AtomicInteger connections = new AtomicInteger();
+ ConfigNodeClient client = newClient(service, connections, 1);
+
+ assertSame(
+ failure,
+ assertThrows(TTransportException.class, () ->
client.getDataPartitionTable(request)));
+ assertEquals(1, connections.get());
+ verify(service).getDataPartitionTable(request);
+ }
+
+ @Test
+ public void testTransientTransportFailureStillRetries() throws Exception {
+ IConfigNodeRPCService.Iface service =
mock(IConfigNodeRPCService.Iface.class);
+ TDataPartitionReq request = new TDataPartitionReq();
+ TDataPartitionTableResp response =
+ new TDataPartitionTableResp()
+ .setStatus(new
TSStatus(TSStatusCode.SUCCESS_STATUS.getStatusCode()));
+ when(service.getDataPartitionTable(request))
+ .thenThrow(new TTransportException(TTransportException.TIMED_OUT,
"socket timeout"))
+ .thenReturn(response);
+ AtomicInteger connections = new AtomicInteger();
+ ConfigNodeClient client = newClient(service, connections, 2);
+
+ assertSame(response, client.getDataPartitionTable(request));
+ assertEquals(2, connections.get());
+ verify(service, times(2)).getDataPartitionTable(request);
+ }
+
+ private ConfigNodeClient newClient(
+ IConfigNodeRPCService.Iface service, AtomicInteger connections, int
allowedConnections)
+ throws Exception {
+ ConfigNodeClient client =
+ new ConfigNodeClient(
+ Collections.singletonList(new TEndPoint("127.0.0.1", 10710)),
+ new ThriftClientProperty.Builder().build(),
+ null) {
+ @Override
+ public void connect(TEndPoint endpoint, int timeoutMs) {
+ assertTrue("Unexpected reconnect", connections.incrementAndGet()
<= allowedConnections);
+ }
+ };
+ Field field = ConfigNodeClient.class.getDeclaredField("client");
+ field.setAccessible(true);
+ field.set(client, service);
+ return client;
+ }
+}
diff --git
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/common/MPPQueryContextTest.java
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/common/MPPQueryContextTest.java
index 6d5cd68e049..711e98dfff4 100644
---
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/common/MPPQueryContextTest.java
+++
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/common/MPPQueryContextTest.java
@@ -33,6 +33,43 @@ public class MPPQueryContextTest {
private static final long MEMORY_BATCH_THRESHOLD = 1024L * 1024L;
+ @Test
+ public void timePartitionMemoryFailureDoesNotUseColumnDiagnostics() {
+ MPPQueryContext context = new MPPQueryContext(new
QueryId("time_partition_oom_test"));
+ context.initResultSetColumnMemoryTracking(10, 2, false);
+ context.recordMatchedSourceColumnsForResultSet(1);
+ context.recordGeneratedResultSetColumn(128);
+ long requestedBytes = requestLargerThanFreeOperatorMemory();
+ MemoryNotEnoughException exception =
+ Assert.assertThrows(
+ MemoryNotEnoughException.class,
+ () -> context.reserveMemoryForTimePartitionSlots(requestedBytes));
+ assertContains(exception.getMessage(), "while analyzing time partitions");
+ assertContains(exception.getMessage(), "requested this time");
+ assertContains(exception.getMessage(), "current free memory");
+ Assert.assertFalse(exception.getMessage().contains("too many columns"));
+ Assert.assertFalse(exception.getMessage().contains("SLIMIT"));
+ Assert.assertEquals(
+ TSStatusCode.QUOTA_MEM_QUERY_NOT_ENOUGH.getStatusCode(),
+ ErrorHandlingUtils.onQueryException(exception, "query").getCode());
+ }
+
+ @Test
+ public void laterPlanningMemoryFailureDoesNotUseStaleColumnStatistics() {
+ MPPQueryContext context = new MPPQueryContext(new
QueryId("planning_oom_test"));
+ context.initResultSetColumnMemoryTracking(10, 2, false);
+ context.recordMatchedSourceColumnsForResultSet(1);
+ context.recordGeneratedResultSetColumn(128);
+ context.finishResultSetColumnMemoryTracking();
+ long requestedBytes = requestLargerThanFreeOperatorMemory();
+ MemoryNotEnoughException exception =
+ Assert.assertThrows(
+ MemoryNotEnoughException.class, () ->
context.reserveMemoryForFrontEnd(requestedBytes));
+ Assert.assertTrue(exception instanceof OperatorMemoryNotEnoughException);
+ Assert.assertFalse(exception.getMessage().contains("too many columns"));
+ Assert.assertFalse(exception.getMessage().contains("SLIMIT"));
+ }
+
@Test
public void
resultSetColumnMemoryNotEnoughExceptionContainsColumnDiagnostics() {
MPPQueryContext context = new MPPQueryContext(new
QueryId("result_column_oom_test"));
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..469bcb9e2c1
--- /dev/null
+++
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/analyze/QueryPartitionMemoryTest.java
@@ -0,0 +1,292 @@
+/*
+ * 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.path.PathPatternTree;
+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.query.QueryTimeoutRuntimeException;
+import org.apache.iotdb.db.exception.sql.SemanticException;
+import org.apache.iotdb.db.exception.sql.StatementAnalyzeException;
+import org.apache.iotdb.db.queryengine.common.MPPQueryContext;
+import org.apache.iotdb.db.queryengine.common.SessionInfo;
+import org.apache.iotdb.db.queryengine.common.schematree.ClusterSchemaTree;
+import org.apache.iotdb.db.queryengine.exception.MemoryNotEnoughException;
+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.db.queryengine.plan.statement.Statement;
+import org.apache.iotdb.db.utils.ErrorHandlingUtils;
+import org.apache.iotdb.rpc.TElasticFramedTransport;
+import org.apache.iotdb.rpc.TSStatusCode;
+
+import ch.qos.logback.classic.Level;
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.spi.ILoggingEvent;
+import ch.qos.logback.core.read.ListAppender;
+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 org.slf4j.LoggerFactory;
+
+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.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class QueryPartitionMemoryTest {
+
+ private int originalDataNodeId;
+ private Logger logger;
+ private Level originalLogLevel;
+ private ListAppender<ILoggingEvent> appender;
+
+ @Before
+ public void setUp() {
+ originalDataNodeId =
IoTDBDescriptor.getInstance().getConfig().getDataNodeId();
+ IoTDBDescriptor.getInstance().getConfig().setDataNodeId(0);
+ logger = (Logger) LoggerFactory.getLogger(Coordinator.class);
+ originalLogLevel = logger.getLevel();
+ logger.setLevel(Level.INFO);
+ appender = new ListAppender<>();
+ appender.start();
+ logger.addAppender(appender);
+ }
+
+ @After
+ public void tearDown() {
+
IoTDBDescriptor.getInstance().getConfig().setDataNodeId(originalDataNodeId);
+ logger.detachAppender(appender);
+ appender.stop();
+ logger.setLevel(originalLogLevel);
+ }
+
+ @Test
+ public void testPartitionMemoryFailureReportsItsSourceAndSql() {
+ long freeMemory =
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators();
+ String sql = "select s1 from root.sg.d1 where time >= 0 and time <= " +
(Long.MAX_VALUE - 1);
+ for (long queryId = 200; queryId < 203; queryId++) {
+ final long id = queryId;
+ FakeSchemaFetcherImpl schemaFetcher = reservingSchemaFetcher(false);
+ MemoryNotEnoughException failure =
+ assertThrows(MemoryNotEnoughException.class, () -> execute(sql, id,
schemaFetcher));
+ assertTrue(failure.getMessage(), failure.getMessage().contains("time
partitions"));
+ assertFalse(failure.getMessage(), failure.getMessage().contains("too
many columns"));
+ assertFalse(failure.getMessage(),
failure.getMessage().contains("SLIMIT"));
+ assertEquals(
+ TSStatusCode.QUOTA_MEM_QUERY_NOT_ENOUGH.getStatusCode(),
+ ErrorHandlingUtils.onQueryException(failure, "query").getCode());
+ assertEquals(freeMemory,
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators());
+ assertNull(Coordinator.getInstance().getQueryExecution(queryId));
+ }
+ assertFailureLogs(sql, 3);
+ }
+
+ @Test
+ public void testPartitionTimeoutLogsSqlAndReleasesEarlierReservations() {
+ long freeMemory =
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators();
+ String sql = "select s1 from root.sg.d1 where time >= 0 and time <= 100";
+ FakeSchemaFetcherImpl schemaFetcher = reservingSchemaFetcher(true);
+ assertThrows(QueryTimeoutRuntimeException.class, () -> execute(sql, 300,
schemaFetcher));
+ assertEquals(freeMemory,
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators());
+ assertNull(Coordinator.getInstance().getQueryExecution(300L));
+ assertFailureLogs(sql, 1);
+ }
+
+ @Test
+ public void testSemanticFailureOnlyLogsSqlContext() {
+ long freeMemory =
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators();
+ String sql = "select count(s1), s1 from root.sg.d1";
+ FakeSchemaFetcherImpl schemaFetcher = new FakeSchemaFetcherImpl();
+ SemanticException failure =
+ assertThrows(SemanticException.class, () -> execute(sql, 600,
schemaFetcher));
+ assertEquals(
+ TSStatusCode.SEMANTIC_ERROR.getStatusCode(),
+ ErrorHandlingUtils.onQueryException(failure, "query").getCode());
+ assertEquals(freeMemory,
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators());
+ assertNull(Coordinator.getInstance().getQueryExecution(600L));
+ assertFailureLogs(sql, 1);
+ }
+
+ @Test
+ public void testAnalysisErrorIsLoggedAndRethrownWithoutRetainingMemory() {
+ long freeMemory =
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators();
+ String sql = "select s1 from root.sg.d1 where time >= 0 and time <= 100";
+ OutOfMemoryError failure = new OutOfMemoryError("simulated analysis
allocation failure");
+ FakeSchemaFetcherImpl schemaFetcher =
+ new FakeSchemaFetcherImpl() {
+ @Override
+ public ClusterSchemaTree fetchSchema(
+ PathPatternTree patternTree, boolean withTemplate,
MPPQueryContext context) {
+ context.reserveMemoryForFrontEnd(2 * 1024 * 1024);
+ throw failure;
+ }
+ };
+ assertSame(
+ failure, assertThrows(OutOfMemoryError.class, () -> execute(sql, 500,
schemaFetcher)));
+ assertEquals(freeMemory,
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators());
+ assertNull(Coordinator.getInstance().getQueryExecution(500L));
+ assertFailureLogs(sql, 1);
+ }
+
+ @Test
+ public void testNormalQueryDoesNotLogFailure() {
+ String sql = "select s1 from root.sg.d1 where time > 10 and time < 5";
+ long freeMemory =
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators();
+ try {
+ assertEquals(
+ TSStatusCode.SUCCESS_STATUS.getStatusCode(),
+ execute(sql, 400, new FakeSchemaFetcherImpl()));
+ assertTrue(appender.list.isEmpty());
+ assertEquals(freeMemory,
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators());
+ } finally {
+ Coordinator.getInstance().cleanupQueryExecution(400L);
+ }
+ }
+
+ private int execute(String sql, long queryId, FakeSchemaFetcherImpl
schemaFetcher) {
+ return Coordinator.getInstance()
+ .executeForTreeModel(
+ StatementGenerator.createStatement(sql, ZoneOffset.UTC),
+ queryId,
+ new SessionInfo(0, "root", ZoneOffset.UTC, ""),
+ sql,
+ new FakePartitionFetcherImpl(),
+ schemaFetcher,
+ 60_000,
+ true,
+ false)
+ .status
+ .getCode();
+ }
+
+ private FakeSchemaFetcherImpl reservingSchemaFetcher(boolean expire) {
+ return new FakeSchemaFetcherImpl() {
+ @Override
+ public ClusterSchemaTree fetchSchema(
+ PathPatternTree patternTree, boolean withTemplate, MPPQueryContext
context) {
+ context.reserveMemoryForFrontEnd(2 * 1024 * 1024);
+ if (expire) {
+ context.setStartTime(1);
+ context.setTimeOut(1);
+ }
+ return super.fetchSchema(patternTree, withTemplate, context);
+ }
+ };
+ }
+
+ private void assertFailureLogs(String sql, int count) {
+ assertEquals(count, appender.list.size());
+ for (ILoggingEvent event : appender.list) {
+ assertEquals(Level.INFO, event.getLevel());
+ assertTrue(event.getFormattedMessage(),
event.getFormattedMessage().contains(sql));
+ assertTrue(event.getFormattedMessage(),
event.getFormattedMessage().contains("queryId:"));
+ assertNull(event.getThrowableProxy());
+ }
+ }
+
+ @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;
+ }
+ };
+ final long id = queryId;
+ Coordinator coordinator = Coordinator.getInstance();
+ Statement statement = StatementGenerator.createStatement(sql,
ZoneOffset.UTC);
+ SessionInfo session = new SessionInfo(0, "root", ZoneOffset.UTC, "");
+ FakeSchemaFetcherImpl schemaFetcher = new FakeSchemaFetcherImpl();
+ StatementAnalyzeException failure =
+ assertThrows(
+ StatementAnalyzeException.class,
+ () ->
+ coordinator.executeForTreeModel(
+ statement,
+ id,
+ session,
+ sql,
+ partitionFetcher,
+ schemaFetcher,
+ 60_000,
+ true,
+ false));
+ assertTrue(failure.getMessage(), failure.getMessage().contains("at
least"));
+ assertTrue(failure.getMessage(), failure.getMessage().contains("1024
bytes"));
+ assertTrue(
+ failure.getMessage(), failure.getMessage().contains("configured
Thrift max frame size"));
+ assertFalse(failure.getMessage(),
failure.getMessage().contains("dn_thrift_max_frame_size"));
+ assertTrue(serializedBytes[0] > 0);
+ assertTrue("Serialization must stop at the frame limit",
serializedBytes[0] <= 1024);
+ assertEquals(freeMemory, planner.getFreeMemoryForOperators());
+ assertNull(Coordinator.getInstance().getQueryExecution(queryId));
+ }
+ assertFailureLogs(sql, 3);
+ }
+}
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..bfd4556e09c 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;
@@ -38,10 +40,55 @@ import java.util.List;
import static
org.apache.iotdb.db.queryengine.plan.analyze.AnalyzeVisitor.getTimePartitionSlotList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
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);
+ Filter filter =
+ TimeFilterApi.between(0, TimePartitionUtils.getTimePartitionInterval()
* 50_000);
+ try {
+ assertThrows(
+ QueryTimeoutRuntimeException.class, () ->
getTimePartitionSlotList(filter, context));
+
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();
+ Filter filter =
+ TimeFilterApi.between(0, TimePartitionUtils.getTimePartitionInterval()
* 50_000);
+ try {
+ assertThrows(
+ QueryTimeoutRuntimeException.class, () ->
getTimePartitionSlotList(filter, context));
+ assertEquals(freeMemory,
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators());
+ } finally {
+ context.releaseAllMemoryReservedForFrontEnd();
+ }
+ assertEquals(freeMemory,
LocalExecutionPlanner.getInstance().getFreeMemoryForOperators());
+ }
+
@Test
public void testAndTimeFilter() {
Filter left = TimeFilterApi.gt(10);
diff --git
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlannerOperatorsMemoryTest.java
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlannerOperatorsMemoryTest.java
index cc763763d51..97264e25260 100644
---
a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlannerOperatorsMemoryTest.java
+++
b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/planner/LocalExecutionPlannerOperatorsMemoryTest.java
@@ -40,6 +40,28 @@ public class LocalExecutionPlannerOperatorsMemoryTest {
private long bytesHeldByTest = 0L;
+ @Test
+ public void testPendingReservationOverflowDoesNotCorruptAccounting() {
+ for (boolean highestPriority : new boolean[] {false, true}) {
+ long freeBefore = PLANNER.getFreeMemoryForOperators();
+ NotThreadSafeMemoryReservationManager manager =
+ new NotThreadSafeMemoryReservationManager(new
QueryId("overflow_query"), "test");
+ manager.setHighestPriority(highestPriority);
+ try {
+ manager.reserveMemoryCumulatively(128);
+ Assert.assertThrows(
+ MemoryNotEnoughException.class,
+ () -> manager.reserveMemoryCumulatively(Long.MAX_VALUE));
+ manager.reserveMemoryImmediately();
+ Assert.assertEquals(128, manager.getReservedBytesInTotalForTest());
+ Assert.assertEquals(freeBefore - 128,
PLANNER.getFreeMemoryForOperators());
+ } finally {
+ manager.releaseAllReservedMemory();
+ }
+ Assert.assertEquals(freeBefore, PLANNER.getFreeMemoryForOperators());
+ }
+ }
+
@After
public void tearDown() {
if (bytesHeldByTest > 0) {