From a5fa2a56d794007743041617aaa0f6b124823232 Mon Sep 17 00:00:00 2001 From: Weihao Li <18110526956@163.com> Date: Thu, 12 Nov 2026 16:46:35 +0800 Subject: [PATCH 01/11] draft Signed-off-by: Weihao Li <18110526956@163.com> --- .../v1/handler/QueryDataSetHandlerTest.java | 7 + .../apache/iotdb/isession/SessionDataSet.java | 100 +++++++++- .../java/org/apache/iotdb/jdbc/Config.java | 2 + .../apache/iotdb/jdbc/IoTDBConnection.java | 2 + .../apache/iotdb/jdbc/IoTDBJDBCResultSet.java | 100 +++++++++- .../org/apache/iotdb/jdbc/IoTDBStatement.java | 10 +- .../jdbc/IoTDBTablePreparedStatement.java | 5 +- iotdb-client/service-rpc/pom.xml | 5 + .../apache/iotdb/rpc/i18n/RpcMessages.java | 11 ++ .../apache/iotdb/rpc/i18n/RpcMessages.java | 11 ++ .../org/apache/iotdb/rpc/IoTDBRpcDataSet.java | 172 +++++++++++++++++- .../iotdb/session/SessionConnection.java | 32 +++- .../iotdb/session/ThriftConnection.java | 7 +- .../iotdb/db/i18n/DataNodeQueryMessages.java | 7 + .../iotdb/db/i18n/DataNodeQueryMessages.java | 7 + .../db/protocol/session/IClientSession.java | 11 ++ .../thrift/impl/ClientRPCServiceImpl.java | 120 ++++++++++-- .../plan/execution/IQueryExecution.java | 6 + .../plan/execution/QueryExecution.java | 8 + .../execution/config/ConfigExecution.java | 9 + .../iotdb/db/utils/QueryDataSetUtils.java | 155 ++++++++++++++++ .../operator/MergeTreeSortOperatorTest.java | 7 + .../iotdb/db/utils/QueryDataSetUtilsTest.java | 154 ++++++++++++++++ .../conf/iotdb-system.properties.template | 3 + .../src/main/thrift/client.thrift | 12 ++ 25 files changed, 920 insertions(+), 43 deletions(-) diff --git a/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java index 36a2ec1292e17..f8aa9c95a2b72 100644 --- a/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java +++ b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java @@ -26,6 +26,7 @@ import org.apache.iotdb.db.queryengine.plan.analyze.QueryType; import org.apache.iotdb.db.queryengine.plan.execution.ExecutionResult; import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution; +import org.apache.iotdb.db.queryengine.plan.execution.PendingResultFragment; import org.apache.iotdb.rest.protocol.model.ExecutionStatus; import org.apache.iotdb.rest.protocol.v1.model.QueryDataSet; import org.apache.iotdb.rpc.TSStatusCode; @@ -88,6 +89,7 @@ private static final class TestQueryExecution implements IQueryExecution { private final Queue> batches = new ArrayDeque<>(); private final DatasetHeader datasetHeader; + private final PendingResultFragment pendingResultFragment = new PendingResultFragment(); private TestQueryExecution(TsBlock... tsBlocks) { for (TsBlock tsBlock : tsBlocks) { @@ -128,6 +130,11 @@ public Optional getByteBufferBatchResult() { return Optional.empty(); } + @Override + public PendingResultFragment getPendingResultFragment() { + return pendingResultFragment; + } + @Override public boolean hasNextResult() { return !batches.isEmpty(); diff --git a/iotdb-client/isession/src/main/java/org/apache/iotdb/isession/SessionDataSet.java b/iotdb-client/isession/src/main/java/org/apache/iotdb/isession/SessionDataSet.java index 5076afd52d05b..511805e5b8fd8 100644 --- a/iotdb-client/isession/src/main/java/org/apache/iotdb/isession/SessionDataSet.java +++ b/iotdb-client/isession/src/main/java/org/apache/iotdb/isession/SessionDataSet.java @@ -61,7 +61,10 @@ public SessionDataSet( ZoneId zoneId, int timeFactor, boolean tableModel, - List columnIndex2TsBlockColumnIndexList) { + List columnIndex2TsBlockColumnIndexList, + boolean offsetSet, + int offset, + int totalLength) { this.ioTDBRpcDataSet = new IoTDBRpcDataSet( sql, @@ -81,7 +84,48 @@ public SessionDataSet( RpcUtils.DEFAULT_TIME_FORMAT, timeFactor, tableModel, - columnIndex2TsBlockColumnIndexList); + columnIndex2TsBlockColumnIndexList, + offsetSet, + offset, + totalLength); + } + + @SuppressWarnings("squid:S107") // ignore Methods should not have too many parameters + public SessionDataSet( + String sql, + List columnNameList, + List columnTypeList, + Map columnNameIndex, + long queryId, + long statementId, + IClientRPCService.Iface client, + long sessionId, + List queryResult, + boolean ignoreTimeStamp, + boolean moreData, + ZoneId zoneId, + int timeFactor, + boolean tableModel, + List columnIndex2TsBlockColumnIndexList) { + this( + sql, + columnNameList, + columnTypeList, + columnNameIndex, + queryId, + statementId, + client, + sessionId, + queryResult, + ignoreTimeStamp, + moreData, + zoneId, + timeFactor, + tableModel, + columnIndex2TsBlockColumnIndexList, + false, + 0, + 0); } @SuppressWarnings("squid:S107") // ignore Methods should not have too many parameters @@ -102,7 +146,10 @@ public SessionDataSet( ZoneId zoneId, int timeFactor, boolean tableModel, - List columnIndex2TsBlockColumnIndexList) { + List columnIndex2TsBlockColumnIndexList, + boolean offsetSet, + int offset, + int totalLength) { this.ioTDBRpcDataSet = new IoTDBRpcDataSet( sql, @@ -122,7 +169,52 @@ public SessionDataSet( RpcUtils.DEFAULT_TIME_FORMAT, timeFactor, tableModel, - columnIndex2TsBlockColumnIndexList); + columnIndex2TsBlockColumnIndexList, + offsetSet, + offset, + totalLength); + } + + @SuppressWarnings("squid:S107") // ignore Methods should not have too many parameters + public SessionDataSet( + String sql, + List columnNameList, + List columnTypeList, + Map columnNameIndex, + long queryId, + long statementId, + IClientRPCService.Iface client, + long sessionId, + List queryResult, + boolean ignoreTimeStamp, + long timeout, + boolean moreData, + int fetchSize, + ZoneId zoneId, + int timeFactor, + boolean tableModel, + List columnIndex2TsBlockColumnIndexList) { + this( + sql, + columnNameList, + columnTypeList, + columnNameIndex, + queryId, + statementId, + client, + sessionId, + queryResult, + ignoreTimeStamp, + timeout, + moreData, + fetchSize, + zoneId, + timeFactor, + tableModel, + columnIndex2TsBlockColumnIndexList, + false, + 0, + 0); } public int getFetchSize() { diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Config.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Config.java index 54aca784d15a5..79517b5b938c1 100644 --- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Config.java +++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/Config.java @@ -93,4 +93,6 @@ private Config() { public static final String SQL_DIALECT = "sql_dialect"; public static final String DATABASE = "db"; + + public static final String ENABLE_RESULT_CHUNKING = "enable_result_chunking"; } diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java index 96beecffe1fc4..f6385249e6b92 100644 --- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java +++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBConnection.java @@ -601,6 +601,8 @@ private void openSession() throws SQLException { openReq.setZoneId(getTimeZone()); openReq.putToConfiguration(Config.VERSION, params.getVersion().toString()); openReq.putToConfiguration(Config.SQL_DIALECT, params.getSqlDialect()); + // this client can assemble fragmented query results, see TSFetchResultsReq#offset + openReq.putToConfiguration(Config.ENABLE_RESULT_CHUNKING, Boolean.TRUE.toString()); params.getDb().ifPresent(db -> openReq.putToConfiguration(Config.DATABASE, db)); TSOpenSessionResp openResp = null; diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBJDBCResultSet.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBJDBCResultSet.java index 5d5cffdf5452e..8cf3312be12a2 100644 --- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBJDBCResultSet.java +++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBJDBCResultSet.java @@ -104,7 +104,10 @@ public IoTDBJDBCResultSet( ZoneId zoneId, Charset charset, boolean tableModel, - List columnIndex2TsBlockColumnIndexList) + List columnIndex2TsBlockColumnIndexList, + boolean offsetSet, + int offset, + int totalLength) throws SQLException { this.ioTDBRpcDataSet = new IoTDBRpcDataSet( @@ -125,7 +128,10 @@ public IoTDBJDBCResultSet( timeFormat, statement.getTimeFactor(), tableModel, - columnIndex2TsBlockColumnIndexList); + columnIndex2TsBlockColumnIndexList, + offsetSet, + offset, + totalLength); this.statement = statement; this.columnTypeList = columnTypeList; if (tracingInfo != null) { @@ -135,6 +141,49 @@ public IoTDBJDBCResultSet( this.charset = charset; } + @SuppressWarnings("squid:S107") // ignore Methods should not have too many parameters + public IoTDBJDBCResultSet( + IoTDBStatement statement, + List columnNameList, + List columnTypeList, + Map columnNameIndex, + boolean ignoreTimeStamp, + IClientRPCService.Iface client, + String sql, + long queryId, + long sessionId, + List dataSet, + TSTracingInfo tracingInfo, + long timeout, + boolean moreData, + ZoneId zoneId, + Charset charset, + boolean tableModel, + List columnIndex2TsBlockColumnIndexList) + throws SQLException { + this( + statement, + columnNameList, + columnTypeList, + columnNameIndex, + ignoreTimeStamp, + client, + sql, + queryId, + sessionId, + dataSet, + tracingInfo, + timeout, + moreData, + zoneId, + charset, + tableModel, + columnIndex2TsBlockColumnIndexList, + false, + 0, + 0); + } + @SuppressWarnings("squid:S107") // ignore Methods should not have too many parameters public IoTDBJDBCResultSet( Statement statement, @@ -150,7 +199,10 @@ public IoTDBJDBCResultSet( TSTracingInfo tracingInfo, long timeout, boolean moreData, - ZoneId zoneId) + ZoneId zoneId, + boolean offsetSet, + int offset, + int totalLength) throws SQLException { this.ioTDBRpcDataSet = new IoTDBRpcDataSet( @@ -171,7 +223,10 @@ public IoTDBJDBCResultSet( timeFormat, ((IoTDBStatement) statement).getTimeFactor(), false, - IntStream.range(0, columnNameList.size()).boxed().collect(Collectors.toList())); + IntStream.range(0, columnNameList.size()).boxed().collect(Collectors.toList()), + offsetSet, + offset, + totalLength); this.statement = (IoTDBStatement) statement; this.columnTypeList = columnTypeList; if (tracingInfo != null) { @@ -180,6 +235,43 @@ public IoTDBJDBCResultSet( } } + @SuppressWarnings("squid:S107") // ignore Methods should not have too many parameters + public IoTDBJDBCResultSet( + Statement statement, + List columnNameList, + List columnTypeList, + Map columnNameIndex, + boolean ignoreTimeStamp, + IClientRPCService.Iface client, + String sql, + long queryId, + long sessionId, + List dataSet, + TSTracingInfo tracingInfo, + long timeout, + boolean moreData, + ZoneId zoneId) + throws SQLException { + this( + statement, + columnNameList, + columnTypeList, + columnNameIndex, + ignoreTimeStamp, + client, + sql, + queryId, + sessionId, + dataSet, + tracingInfo, + timeout, + moreData, + zoneId, + false, + 0, + 0); + } + @Override public boolean isWrapperFor(Class iface) throws SQLException { throw new SQLException(Constant.METHOD_NOT_SUPPORTED); diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java index ff790cbd9f438..858462d6b2460 100644 --- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java +++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBStatement.java @@ -412,7 +412,10 @@ private boolean executeSQL(String sql) throws TException, SQLException { zoneId, charset, execResp.isSetTableModel() && execResp.isTableModel(), - execResp.getColumnIndex2TsBlockColumnIndexList()); + execResp.getColumnIndex2TsBlockColumnIndexList(), + execResp.isSetOffset(), + execResp.offset, + execResp.totalLength); } return true; } @@ -549,7 +552,10 @@ private ResultSet executeQuerySQL(String sql, long timeoutInMS) throws TExceptio zoneId, charset, execResp.isSetTableModel() && execResp.isTableModel(), - execResp.getColumnIndex2TsBlockColumnIndexList()); + execResp.getColumnIndex2TsBlockColumnIndexList(), + execResp.isSetOffset(), + execResp.offset, + execResp.totalLength); } return resultSet; } diff --git a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBTablePreparedStatement.java b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBTablePreparedStatement.java index 3c95afa107dd1..72cd6925498bc 100644 --- a/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBTablePreparedStatement.java +++ b/iotdb-client/jdbc/src/main/java/org/apache/iotdb/jdbc/IoTDBTablePreparedStatement.java @@ -233,7 +233,10 @@ private ResultSet processQueryResult(TSExecuteStatementResp resp) throws SQLExce resp.tracingInfo, (long) queryTimeout * 1000, resp.isSetMoreData() && resp.isMoreData(), - zoneId); + zoneId, + resp.isSetOffset(), + resp.offset, + resp.totalLength); return resultSet; } return null; diff --git a/iotdb-client/service-rpc/pom.xml b/iotdb-client/service-rpc/pom.xml index 206cc949fe440..d53db34b388bb 100644 --- a/iotdb-client/service-rpc/pom.xml +++ b/iotdb-client/service-rpc/pom.xml @@ -75,6 +75,11 @@ junit test + + org.mockito + mockito-core + test + org.slf4j slf4j-api diff --git a/iotdb-client/service-rpc/src/main/i18n/en/org/apache/iotdb/rpc/i18n/RpcMessages.java b/iotdb-client/service-rpc/src/main/i18n/en/org/apache/iotdb/rpc/i18n/RpcMessages.java index cd925b4863af1..862e15b3b90e8 100644 --- a/iotdb-client/service-rpc/src/main/i18n/en/org/apache/iotdb/rpc/i18n/RpcMessages.java +++ b/iotdb-client/service-rpc/src/main/i18n/en/org/apache/iotdb/rpc/i18n/RpcMessages.java @@ -127,4 +127,15 @@ private RpcMessages() {} public static final String EXCEPTION_DATA_TYPE_ARG_IS_NOT_SUPPORTED_31213160 = "Data type %s is not supported."; + + public static final String EXCEPTION_CHUNKED_RESULT_EMPTY_FRAGMENT_03A0EF39 = + "Empty fragment received when assembling a chunked query result"; + public static final String + EXCEPTION_CHUNKED_RESULT_FRAGMENT_OFFSET_ARG_NOT_GREATER_THAN_ACCUMULATED_LENGTH_ARG_A0F032E3 = + "Fragment offset %d is not greater than the accumulated length %d"; + public static final String EXCEPTION_CHUNKED_RESULT_MISSING_TOTAL_LENGTH_FOR_FIRST_FRAGMENT_1D11374F = + "Missing totalLength for the first fragment of a chunked TsBlock"; + public static final String + EXCEPTION_CHUNKED_RESULT_ASSEMBLED_LENGTH_ARG_DOES_NOT_MATCH_TOTAL_LENGTH_ARG_925DCB15 = + "Assembled fragment length %d does not match the total length %d"; } diff --git a/iotdb-client/service-rpc/src/main/i18n/zh/org/apache/iotdb/rpc/i18n/RpcMessages.java b/iotdb-client/service-rpc/src/main/i18n/zh/org/apache/iotdb/rpc/i18n/RpcMessages.java index fc059e26f5f32..0f448debed5ec 100644 --- a/iotdb-client/service-rpc/src/main/i18n/zh/org/apache/iotdb/rpc/i18n/RpcMessages.java +++ b/iotdb-client/service-rpc/src/main/i18n/zh/org/apache/iotdb/rpc/i18n/RpcMessages.java @@ -108,4 +108,15 @@ private RpcMessages() {} "通过列名 %s 获取的值为 NULL。"; public static final String EXCEPTION_DATA_TYPE_ARG_IS_NOT_SUPPORTED_31213160 = "不支持数据类型 %s。"; + + public static final String EXCEPTION_CHUNKED_RESULT_EMPTY_FRAGMENT_03A0EF39 = + "拼装分片查询结果时收到空分片"; + public static final String + EXCEPTION_CHUNKED_RESULT_FRAGMENT_OFFSET_ARG_NOT_GREATER_THAN_ACCUMULATED_LENGTH_ARG_A0F032E3 = + "分片 offset %d 不大于已累计的长度 %d"; + public static final String EXCEPTION_CHUNKED_RESULT_MISSING_TOTAL_LENGTH_FOR_FIRST_FRAGMENT_1D11374F = + "分片 TsBlock 的首个分片缺少 totalLength"; + public static final String + EXCEPTION_CHUNKED_RESULT_ASSEMBLED_LENGTH_ARG_DOES_NOT_MATCH_TOTAL_LENGTH_ARG_925DCB15 = + "拼装后的分片长度 %d 与 totalLength %d 不一致"; } diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java index 32f5ea5d45a32..f9669d56d7a71 100644 --- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java +++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java @@ -34,6 +34,7 @@ import org.apache.tsfile.read.common.type.Type; import org.apache.tsfile.utils.Binary; import org.apache.tsfile.utils.DateUtils; +import org.apache.tsfile.utils.PublicBAOS; import java.nio.ByteBuffer; import java.sql.Timestamp; @@ -84,6 +85,8 @@ public class IoTDBRpcDataSet { private int queryResultIndex; // the index of bytebuffer in queryResult private int tsBlockSize; // the size of current tsBlock private int tsBlockIndex; // the row index in current tsBlock + // the fragment of a TsBlock that is partially received; null if there is no pending fragment + private ResultFragmentBuffer fragmentBuffer; private final ZoneId zoneId; private final String timeFormat; @@ -111,6 +114,53 @@ public IoTDBRpcDataSet( int timeFactor, boolean tableModel, List columnIndex2TsBlockColumnIndexList) { + this( + sql, + columnNameList, + columnTypeList, + columnNameIndex, + ignoreTimeStamp, + moreData, + queryId, + statementId, + client, + sessionId, + queryResult, + fetchSize, + timeout, + zoneId, + timeFormat, + timeFactor, + tableModel, + columnIndex2TsBlockColumnIndexList, + false, + 0, + 0); + } + + @SuppressWarnings({"squid:S3776", "squid:S107"}) // Suppress high Cognitive Complexity warning + public IoTDBRpcDataSet( + String sql, + List columnNameList, + List columnTypeList, + Map columnNameIndex, + boolean ignoreTimeStamp, + boolean moreData, + long queryId, + long statementId, + IClientRPCService.Iface client, + long sessionId, + List queryResult, + int fetchSize, + long timeout, + ZoneId zoneId, + String timeFormat, + int timeFactor, + boolean tableModel, + List columnIndex2TsBlockColumnIndexList, + boolean offsetSet, + int offset, + int totalLength) { this.sessionId = sessionId; this.statementId = statementId; // only used for tree model, table model this field will always be true @@ -184,12 +234,13 @@ public IoTDBRpcDataSet( } } - this.queryResult = queryResult; + this.queryResult = new ArrayList<>(); + this.queryResultIndex = 0; this.queryResultSize = 0; if (queryResult != null) { - queryResultSize = queryResult.size(); + addResponse(queryResult, offsetSet, offset, totalLength); + this.queryResultSize = this.queryResult.size(); } - this.queryResultIndex = 0; this.tsBlockSize = 0; this.tsBlockIndex = -1; this.zoneId = zoneId; @@ -227,6 +278,7 @@ public void close() throws StatementExecutionException, TException { } client = null; isClosed = true; + fragmentBuffer = null; } public boolean next() throws StatementExecutionException, IoTDBConnectionException { @@ -241,7 +293,13 @@ public boolean next() throws StatementExecutionException, IoTDBConnectionExcepti return true; } - if (moreData && fetchResults() && hasCachedByteBuffer()) { + // one oversized TsBlock may need several fetch RPCs before it is fully assembled + while (moreData && !hasCachedByteBuffer()) { + if (!fetchResults()) { + break; + } + } + if (hasCachedByteBuffer()) { constructOneTsBlock(); constructOneRow(); return true; @@ -262,6 +320,9 @@ public boolean fetchResults() throws StatementExecutionException, IoTDBConnectio TSFetchResultsReq req = new TSFetchResultsReq(sessionId, sql, fetchSize, queryId, true); req.setStatementId(statementId); req.setTimeout(timeout); + // a new client always sets offset (0 if nothing pending), which enables bounded-payload + // responses on the server + req.setOffset(fragmentBuffer == null ? 0 : fragmentBuffer.getNextOffset()); try { TSFetchResultsResp resp = client.fetchResultsV2(req); RpcUtils.verifySuccess(resp.getStatus()); @@ -269,12 +330,11 @@ public boolean fetchResults() throws StatementExecutionException, IoTDBConnectio if (!resp.hasResultSet) { close(); } else { - queryResult = resp.getQueryResult(); + queryResult = new ArrayList<>(); queryResultIndex = 0; queryResultSize = 0; - if (queryResult != null) { - queryResultSize = queryResult.size(); - } + addResponse(resp.getQueryResult(), resp.isSetOffset(), resp.offset, resp.totalLength); + queryResultSize = queryResult.size(); this.tsBlockSize = 0; this.tsBlockIndex = -1; } @@ -286,6 +346,102 @@ public boolean fetchResults() throws StatementExecutionException, IoTDBConnectio } } + /** + * Adds the blocks of one response to {@link #queryResult}. If the response ends with a fragment, + * the fragment is either appended to the pending {@link ResultFragmentBuffer} or starts a new + * one; the corresponding TsBlock is only added to {@link #queryResult} once all of its bytes have + * been received. + * + * @param blocks the serialized TsBlocks in the response + * @param offsetSet whether the last element of {@code blocks} is a fragment + * @param nextOffset the start offset of the next fragment + * @param totalLength the total serialized length of the block the first fragment belongs to + */ + private void addResponse( + List blocks, boolean offsetSet, int nextOffset, int totalLength) { + if (blocks == null || blocks.isEmpty()) { + return; + } + int blockIndex = 0; + + if (fragmentBuffer != null) { + // the first element completes the pending fragment, if it is not the only element + fragmentBuffer.append(blocks.get(blockIndex++)); + if (offsetSet && blockIndex == blocks.size()) { + fragmentBuffer.updateOffset(nextOffset); + return; + } + queryResult.add(fragmentBuffer.toByteBuffer()); + fragmentBuffer = null; + } + + int lastCompleteBlockIndex = offsetSet ? blocks.size() - 1 : blocks.size(); + while (blockIndex < lastCompleteBlockIndex) { + queryResult.add(blocks.get(blockIndex++)); + } + + if (offsetSet) { + if (totalLength <= 0) { + throw new IllegalArgumentException( + RpcMessages.EXCEPTION_CHUNKED_RESULT_MISSING_TOTAL_LENGTH_FOR_FIRST_FRAGMENT_1D11374F); + } + fragmentBuffer = new ResultFragmentBuffer(totalLength); + fragmentBuffer.append(blocks.get(blockIndex)); + fragmentBuffer.updateOffset(nextOffset); + } + } + + /** Accumulates the fragments of one oversized serialized TsBlock until it is complete. */ + private static class ResultFragmentBuffer { + + private final PublicBAOS buffer; + private final int totalLength; + private int nextOffset; + + private ResultFragmentBuffer(int totalLength) { + this.buffer = new PublicBAOS(totalLength); + this.totalLength = totalLength; + } + + private int getNextOffset() { + return nextOffset; + } + + private void append(ByteBuffer fragment) { + if (!fragment.hasRemaining()) { + throw new IllegalArgumentException( + RpcMessages.EXCEPTION_CHUNKED_RESULT_EMPTY_FRAGMENT_03A0EF39); + } + // a fragment may be a slice of a larger buffer, so honor arrayOffset and position + buffer.write( + fragment.array(), fragment.arrayOffset() + fragment.position(), fragment.remaining()); + } + + private void updateOffset(int nextOffset) { + if (nextOffset <= this.nextOffset || nextOffset != buffer.size()) { + throw new IllegalArgumentException( + String.format( + RpcMessages + .EXCEPTION_CHUNKED_RESULT_FRAGMENT_OFFSET_ARG_NOT_GREATER_THAN_ACCUMULATED_LENGTH_ARG_A0F032E3, + nextOffset, + buffer.size())); + } + this.nextOffset = nextOffset; + } + + private ByteBuffer toByteBuffer() { + if (buffer.size() != totalLength) { + throw new IllegalArgumentException( + String.format( + RpcMessages + .EXCEPTION_CHUNKED_RESULT_ASSEMBLED_LENGTH_ARG_DOES_NOT_MATCH_TOTAL_LENGTH_ARG_925DCB15, + buffer.size(), + totalLength)); + } + return ByteBuffer.wrap(buffer.getBuf(), 0, buffer.size()); + } + } + public boolean hasCachedBlock() { return (curTsBlock != null && tsBlockIndex < tsBlockSize - 1); } diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java index f2158ef0ffacb..d0b6e8dba528b 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java @@ -246,6 +246,8 @@ private void init( openReq.setZoneId(zoneId.toString()); openReq.putToConfiguration("version", session.version.toString()); openReq.putToConfiguration("sql_dialect", sqlDialect); + // this client can assemble fragmented query results, see TSFetchResultsReq#offset + openReq.putToConfiguration("enable_result_chunking", Boolean.TRUE.toString()); if (session.useEncryptedPassword) { openReq.putToConfiguration(USE_ENCRYPTED_PASSWORD_KEY, Boolean.TRUE.toString()); } @@ -492,7 +494,10 @@ protected SessionDataSet executeQueryStatement(String sql, long timeout) zoneId, timeFactor, execResp.isSetTableModel() && execResp.isTableModel(), - execResp.getColumnIndex2TsBlockColumnIndexList()); + execResp.getColumnIndex2TsBlockColumnIndexList(), + execResp.isSetOffset(), + execResp.offset, + execResp.totalLength); } protected void executeNonQueryStatement(String sql) @@ -561,7 +566,10 @@ protected SessionDataSet executeRawDataQuery( zoneId, timeFactor, execResp.isSetTableModel() && execResp.isTableModel(), - execResp.getColumnIndex2TsBlockColumnIndexList()); + execResp.getColumnIndex2TsBlockColumnIndexList(), + execResp.isSetOffset(), + execResp.offset, + execResp.totalLength); } protected SessionDataSet executeLastDataQueryForOnePrefixPath(final List prefixes) @@ -601,7 +609,10 @@ protected SessionDataSet executeLastDataQueryForOnePrefixPath(final List zoneId, timeFactor, false, - tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList()); + tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList(), + tsExecuteStatementResp.isSetOffset(), + tsExecuteStatementResp.offset, + tsExecuteStatementResp.totalLength); } protected Pair executeLastDataQueryForOneDevice( @@ -651,7 +662,10 @@ protected Pair executeLastDataQueryForOneDevice( zoneId, timeFactor, tsExecuteStatementResp.isSetTableModel() && tsExecuteStatementResp.isTableModel(), - tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList()), + tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList(), + tsExecuteStatementResp.isSetOffset(), + tsExecuteStatementResp.offset, + tsExecuteStatementResp.totalLength), redirectedEndPoint); } @@ -694,7 +708,10 @@ protected SessionDataSet executeLastDataQuery(List paths, long time, lon zoneId, timeFactor, tsExecuteStatementResp.isSetTableModel() && tsExecuteStatementResp.isTableModel(), - tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList()); + tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList(), + tsExecuteStatementResp.isSetOffset(), + tsExecuteStatementResp.offset, + tsExecuteStatementResp.totalLength); } protected SessionDataSet executeAggregationQuery( @@ -776,7 +793,10 @@ private SessionDataSet executeAggregationQuery(TSAggregationQueryReq tsAggregati zoneId, timeFactor, tsExecuteStatementResp.isSetTableModel() && tsExecuteStatementResp.isTableModel(), - tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList()); + tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList(), + tsExecuteStatementResp.isSetOffset(), + tsExecuteStatementResp.offset, + tsExecuteStatementResp.totalLength); } private TSAggregationQueryReq createAggregationQueryReq( diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/ThriftConnection.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/ThriftConnection.java index e15320c1a1519..dd3a374c21b93 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/ThriftConnection.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/ThriftConnection.java @@ -147,6 +147,8 @@ public void init( openReq.setPassword(password); openReq.setZoneId(zoneId.toString()); openReq.putToConfiguration("version", version); + // this client can assemble fragmented query results, see TSFetchResultsReq#offset + openReq.putToConfiguration("enable_result_chunking", Boolean.TRUE.toString()); try { TSOpenSessionResp openResp = client.openSession(openReq); @@ -210,7 +212,10 @@ protected SessionDataSet executeQueryStatement(String sql, long timeout, int fet zoneId, timeFactor, execResp.isSetTableModel() && execResp.isTableModel(), - execResp.getColumnIndex2TsBlockColumnIndexList()); + execResp.getColumnIndex2TsBlockColumnIndexList(), + execResp.isSetOffset(), + execResp.offset, + execResp.totalLength); } /** Create outside the publication lock: TLS setup itself can perform network I/O. */ diff --git a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java index b84bebec93df3..8514a5c2db0a1 100644 --- a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java +++ b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java @@ -4930,4 +4930,11 @@ private DataNodeQueryMessages() {} public static final String EXCEPTION_ARG_ARG_MS_ARG_INVOCATIONS_ARG_APPLICATIONS_F056BD84 = "%s: %s ms, %s invocations, %s applications"; + + public static final String + EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_DOES_NOT_MATCH_PENDING_FRAGMENT_OFFSET_ARG_A1B13DBC = + "Request offset %d does not match the pending result fragment offset %d"; + public static final String + EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_IS_INVALID_WITHOUT_PENDING_FRAGMENT_C403454F = + "Request offset %d is invalid because no result fragment is pending"; } diff --git a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java index 1aef0814d9232..b9546ce0d6ed9 100644 --- a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java +++ b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java @@ -5630,4 +5630,11 @@ private DataNodeQueryMessages() {} public static final String EXCEPTION_ARG_ARG_MS_ARG_INVOCATIONS_ARG_APPLICATIONS_F056BD84 = "%s:%s ms,调用 %s 次,应用 %s 次"; + + public static final String + EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_DOES_NOT_MATCH_PENDING_FRAGMENT_OFFSET_ARG_A1B13DBC = + "请求 offset %d 与待发送结果分片的 offset %d 不一致"; + public static final String + EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_IS_INVALID_WITHOUT_PENDING_FRAGMENT_C403454F = + "请求 offset %d 无效:当前没有待发送的结果分片"; } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/session/IClientSession.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/session/IClientSession.java index aaf73db5c4dd6..1307f71741c24 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/session/IClientSession.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/session/IClientSession.java @@ -61,6 +61,9 @@ public abstract class IClientSession { private boolean enableAutoCreateSchema = true; + // set by clients that can assemble fragmented query results, see TSFetchResultsReq#offset + private boolean enableResultChunking = false; + private long lastActiveTime = CommonDateTimeUtils.currentTime(); public abstract String getClientAddress(); @@ -211,6 +214,14 @@ public void setEnableAutoCreateSchema(boolean enableAutoCreateSchema) { this.enableAutoCreateSchema = enableAutoCreateSchema; } + public boolean isEnableResultChunking() { + return enableResultChunking; + } + + public void setEnableResultChunking(boolean enableResultChunking) { + this.enableResultChunking = enableResultChunking; + } + /** * Add a prepared statement to this session. * diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java index 796957d17352e..614774a42afc0 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java @@ -273,6 +273,7 @@ public class ClientRPCServiceImpl implements IClientRPCServiceWithHandler { public static final String ERROR_CODE = DataNodeMiscMessages.ERROR_CODE; private static final String USE_ENCRYPTED_PASSWORD_KEY = "use_encrypted_password"; private static final String ENABLE_AUTO_CREATE_SCHEMA_KEY = "enable_auto_create_schema"; + private static final String ENABLE_RESULT_CHUNKING_KEY = "enable_result_chunking"; private static final TSProtocolVersion CURRENT_RPC_VERSION = TSProtocolVersion.IOTDB_SERVICE_PROTOCOL_V3; @@ -302,25 +303,55 @@ public class ClientRPCServiceImpl implements IClientRPCServiceWithHandler { @FunctionalInterface public interface SelectResult { - boolean apply(TSExecuteStatementResp resp, IQueryExecution queryExecution, int fetchSize) + boolean apply( + TSExecuteStatementResp resp, + IQueryExecution queryExecution, + int fetchSize, + int maxPayloadSize) throws IoTDBException, IOException; } private static final SelectResult SELECT_RESULT = - (resp, queryExecution, fetchSize) -> { - Pair, Boolean> pair = - QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, fetchSize); - resp.setQueryResult(pair.left); - return pair.right; + (resp, queryExecution, fetchSize, maxPayloadSize) -> { + QueryDataSetUtils.ResultChunk chunk; + if (maxPayloadSize > 0) { + // The execute request carries no offset, so it must start a new result set + chunk = + QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, fetchSize, 0, maxPayloadSize); + } else { + Pair, Boolean> pair = + QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, fetchSize); + chunk = QueryDataSetUtils.ResultChunk.complete(pair.left, pair.right); + } + resp.setQueryResult(chunk.getQueryResult()); + if (chunk.hasNextOffset()) { + resp.setOffset(chunk.getNextOffset()); + if (chunk.hasTotalLength()) { + resp.setTotalLength(chunk.getTotalLength()); + } + } + return chunk.isFinished(); }; private static final SelectResult OLD_SELECT_RESULT = - (resp, queryExecution, fetchSize) -> { + (resp, queryExecution, fetchSize, maxPayloadSize) -> { Pair pair = convertTsBlockByFetchSize(queryExecution, fetchSize); resp.setQueryDataSet(pair.left); return pair.right; }; + /** + * Returns the maximum payload size of one response carrying query results, or 0 if the session's + * client cannot assemble fragmented results. The payload bound is shared with the MPP data + * exchange RPC response, see mpp_data_exchange_max_payload_size_in_bytes. + */ + private static int getMaxResultPayloadSize(IClientSession clientSession) { + return clientSession.isEnableResultChunking() + ? config.getMppDataExchangeMaxPayloadSizeInBytes() + : 0; + } + public ClientRPCServiceImpl() { partitionFetcher = ClusterPartitionFetcher.getInstance(); schemaFetcher = ClusterSchemaFetcher.getInstance(); @@ -494,7 +525,12 @@ private TSExecuteStatementResp executeStatementInternal( statementType = statementType == null ? StatementType.QUERY : statementType; resp = createResponse(queryExecution.getDatasetHeader(), queryId); resp.setStatus(result.status); - finished = setResult.apply(resp, queryExecution, request.getFetchSize()); + finished = + setResult.apply( + resp, + queryExecution, + request.getFetchSize(), + getMaxResultPayloadSize(clientSession)); resp.setMoreData(!finished); if (quota != null) { quota.addReadResult(resp.getQueryResult()); @@ -767,7 +803,9 @@ private TSExecuteStatementResp executeRawDataQueryInternal( if (queryExecution.isQuery()) { resp = createResponse(queryExecution.getDatasetHeader(), queryId); resp.setStatus(result.status); - finished = setResult.apply(resp, queryExecution, req.fetchSize); + finished = + setResult.apply( + resp, queryExecution, req.fetchSize, getMaxResultPayloadSize(clientSession)); resp.setMoreData(!finished); quota.addReadResult(resp.getQueryResult()); } else { @@ -863,7 +901,9 @@ private TSExecuteStatementResp executeLastDataQueryInternal( if (queryExecution.isQuery()) { resp = createResponse(queryExecution.getDatasetHeader(), queryId); resp.setStatus(result.status); - finished = setResult.apply(resp, queryExecution, req.fetchSize); + finished = + setResult.apply( + resp, queryExecution, req.fetchSize, getMaxResultPayloadSize(clientSession)); resp.setMoreData(!finished); quota.addReadResult(resp.getQueryResult()); } else { @@ -961,7 +1001,9 @@ private TSExecuteStatementResp executeAggregationQueryInternal( if (queryExecution.isQuery()) { resp = createResponse(queryExecution.getDatasetHeader(), queryId); resp.setStatus(result.status); - finished = setResult.apply(resp, queryExecution, req.fetchSize); + finished = + setResult.apply( + resp, queryExecution, req.fetchSize, getMaxResultPayloadSize(clientSession)); resp.setMoreData(!finished); quota.addReadResult(resp.getQueryResult()); } else { @@ -1456,7 +1498,9 @@ public TSExecuteStatementResp executeFastLastDataQueryForOneDeviceV2( .get(0) .clientRpcEndPoint); resp.setStatus(tsstatus); - finished = SELECT_RESULT.apply(resp, queryExecution, req.fetchSize); + finished = + SELECT_RESULT.apply( + resp, queryExecution, req.fetchSize, getMaxResultPayloadSize(clientSession)); resp.setMoreData(!finished); quota.addReadResult(resp.getQueryResult()); } else { @@ -1571,6 +1615,27 @@ public TSFetchResultsResp fetchResultsV2(TSFetchResultsReq req) { statementType = queryExecution.getStatementType(); try (SetThreadName queryName = new SetThreadName(queryExecution.getQueryId())) { + if (req.isSetOffset()) { + // The client sets offset (0 for the first request) iff it can assemble fragment results + QueryDataSetUtils.ResultChunk chunk = + QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, + req.fetchSize, + req.getOffset(), + config.getMppDataExchangeMaxPayloadSizeInBytes()); + finished = chunk.isFinished(); + resp.setHasResultSet(!chunk.getQueryResult().isEmpty()); + resp.setIsAlign(true); + resp.setQueryResult(chunk.getQueryResult()); + if (chunk.hasNextOffset()) { + resp.setOffset(chunk.getNextOffset()); + if (chunk.hasTotalLength()) { + resp.setTotalLength(chunk.getTotalLength()); + } + } + resp.setMoreData(!finished); + return resp; + } Pair, Boolean> pair = QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, req.fetchSize); List result = pair.left; @@ -1644,6 +1709,7 @@ public TSOpenSessionResp openSession(TSOpenSessionReq req) throws TException { clientSession.setDatabaseName(database.get().toLowerCase(Locale.ENGLISH)); } clientSession.setEnableAutoCreateSchema(parseEnableAutoCreateSchema(req)); + clientSession.setEnableResultChunking(parseEnableResultChunking(req)); } TSOpenSessionResp resp = new TSOpenSessionResp(tsStatus, CURRENT_RPC_VERSION); Map configuration = new HashMap<>(); @@ -1694,6 +1760,12 @@ private boolean parseEnableAutoCreateSchema(TSOpenSessionReq req) { || Boolean.parseBoolean(configuration.get(ENABLE_AUTO_CREATE_SCHEMA_KEY)); } + private boolean parseEnableResultChunking(TSOpenSessionReq req) { + Map configuration = req.configuration; + return configuration != null + && Boolean.parseBoolean(configuration.get(ENABLE_RESULT_CHUNKING_KEY)); + } + @Override public TSStatus closeSession(TSCloseSessionReq req) { return new TSStatus( @@ -1805,12 +1877,26 @@ public TSStatus deallocatePreparedStatement(TSDeallocatePreparedReq req) { } private final SelectResult setResultForPrepared = - (resp, queryExecution, fetchSize) -> { + (resp, queryExecution, fetchSize, maxPayloadSize) -> { // Use V2 format (queryResult) to match IoTDBTablePreparedStatement client - Pair, Boolean> pair = - QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, fetchSize); - resp.setQueryResult(pair.left); - return pair.right; + QueryDataSetUtils.ResultChunk chunk; + if (maxPayloadSize > 0) { + chunk = + QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, fetchSize, 0, maxPayloadSize); + } else { + Pair, Boolean> pair = + QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, fetchSize); + chunk = QueryDataSetUtils.ResultChunk.complete(pair.left, pair.right); + } + resp.setQueryResult(chunk.getQueryResult()); + if (chunk.hasNextOffset()) { + resp.setOffset(chunk.getNextOffset()); + if (chunk.hasTotalLength()) { + resp.setTotalLength(chunk.getTotalLength()); + } + } + return chunk.isFinished(); }; // ========================= End PreparedStatement RPC Methods ========================= diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java index f4beb09917c27..1a7b96d7728c0 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java @@ -45,6 +45,12 @@ public interface IQueryExecution { Optional getByteBufferBatchResult() throws IoTDBException; + /** + * Returns the holder of the serialized TsBlock that is partially sent to the client. It is used + * to continue the fragmented transfer of one oversized TsBlock in the following RPC response. + */ + PendingResultFragment getPendingResultFragment(); + boolean hasNextResult(); int getOutputValueColumnCount(); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java index 2324bea787789..a1ff15a98a1b9 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java @@ -108,6 +108,9 @@ public class QueryExecution implements IQueryExecution { // used for cleaning resultHandle up exactly once private final AtomicBoolean resultHandleCleanUp; + // the serialized TsBlock that is partially sent to the client and waits for the next RPC + private final PendingResultFragment pendingResultFragment = new PendingResultFragment(); + private final AtomicBoolean stopped; // cost time in ns of finished rpc @@ -599,6 +602,11 @@ public Optional getByteBufferBatchResult() throws IoTDBException { return getResult(this::getSerializedTsBlock); } + @Override + public PendingResultFragment getPendingResultFragment() { + return pendingResultFragment; + } + private ByteBuffer getSerializedTsBlock() throws IoTDBException { return resultHandle.getSerializedTsBlock(); } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java index 148dab7de71fe..cfc8cfdeb98ac 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java @@ -31,6 +31,7 @@ import org.apache.iotdb.db.queryengine.plan.analyze.QueryType; import org.apache.iotdb.db.queryengine.plan.execution.ExecutionResult; import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution; +import org.apache.iotdb.db.queryengine.plan.execution.PendingResultFragment; import org.apache.iotdb.db.queryengine.plan.execution.config.executor.ClusterConfigTaskExecutor; import org.apache.iotdb.db.queryengine.plan.execution.config.executor.IConfigTaskExecutor; import org.apache.iotdb.db.queryengine.plan.statement.StatementType; @@ -115,6 +116,9 @@ public class ConfigExecution implements IQueryExecution { private final IConfigTask task; private final IConfigTaskExecutor configTaskExecutor; + // the serialized TsBlock that is partially sent to the client and waits for the next RPC + private final PendingResultFragment pendingResultFragment = new PendingResultFragment(); + private final StatementType statementType; private long totalExecutionTime; @@ -289,6 +293,11 @@ public Optional getByteBufferBatchResult() throws IoTDBException { return Optional.empty(); } + @Override + public PendingResultFragment getPendingResultFragment() { + return pendingResultFragment; + } + // According to the execution process of ConfigExecution, there is only one TsBlock for // this execution. Thus, the hasNextResult will be false once the TsBlock is consumed @Override diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java index bb40b38dc24b4..cb36050adba7a 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java @@ -20,7 +20,9 @@ package org.apache.iotdb.db.utils; import org.apache.iotdb.commons.exception.IoTDBException; +import org.apache.iotdb.db.i18n.DataNodeQueryMessages; import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution; +import org.apache.iotdb.db.queryengine.plan.execution.PendingResultFragment; import org.apache.iotdb.service.rpc.thrift.TSQueryDataSet; import org.apache.tsfile.block.column.Column; @@ -272,6 +274,159 @@ public static Pair, Boolean> convertQueryResultByFetchSize( return new Pair<>(res, !queryExecution.hasNextResult()); } + /** + * Fetches query results with a bounded payload size. A serialized TsBlock larger than {@code + * maxPayloadSize} is cut into fragments: the response only carries the bytes that fit into the + * payload, and the remaining bytes are kept in {@link PendingResultFragment} on the query + * execution, to be sent by the following request carrying the returned offset. + * + * @param queryExecution used to get TsBlock from and judge whether there is more data. + * @param fetchSize wanted row size + * @param requestOffset the offset of the pending fragment to continue, 0 if there is none + * @param maxPayloadSize the maximum total bytes of {@link ResultChunk#getQueryResult()} + */ + public static ResultChunk convertQueryResultByFetchSize( + IQueryExecution queryExecution, int fetchSize, int requestOffset, int maxPayloadSize) + throws IoTDBException { + fetchSize = fetchSize > 0 ? fetchSize : TSFLE_CONFIG.getMaxTsBlockLineNumber(); + List res = new ArrayList<>(); + int remainingPayloadSize = maxPayloadSize; + int rowCount = 0; + long memorySize = 0; + + // Continue the oversized TsBlock left by the previous response first + PendingResultFragment pendingFragment = queryExecution.getPendingResultFragment(); + ByteBuffer pendingBlock = pendingFragment.getBlock(); + if (pendingBlock != null) { + int nextOffset = pendingFragment.getNextOffset(); + if (requestOffset != nextOffset) { + throw new IllegalArgumentException( + String.format( + DataNodeQueryMessages + .EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_DOES_NOT_MATCH_PENDING_FRAGMENT_OFFSET_ARG_A1B13DBC, + requestOffset, + nextOffset)); + } + int fragmentSize = Math.min(remainingPayloadSize, pendingBlock.limit() - nextOffset); + res.add(slice(pendingBlock, nextOffset, fragmentSize)); + if (nextOffset + fragmentSize == pendingBlock.limit()) { + pendingFragment.clear(); + // The fragmented block is now complete. Do not fetch new blocks in the same response, + // otherwise the response would carry the tail of the big block plus up to fetchSize new + // rows, whose row count is not accounted for. One response either starts/continues a + // fragmented block or delivers new blocks, never both. + return ResultChunk.complete(res, !queryExecution.hasNextResult()); + } + pendingFragment.reset(pendingBlock, nextOffset + fragmentSize); + return ResultChunk.fragment(res, nextOffset + fragmentSize, -1, false); + } else if (requestOffset != 0) { + throw new IllegalArgumentException( + String.format( + DataNodeQueryMessages + .EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_IS_INVALID_WITHOUT_PENDING_FRAGMENT_C403454F, + requestOffset)); + } + + while (rowCount < fetchSize && memorySize < MAX_RETURN_SIZE && remainingPayloadSize > 0) { + Optional optionalByteBuffer = queryExecution.getByteBufferBatchResult(); + if (!optionalByteBuffer.isPresent()) { + break; + } + ByteBuffer byteBuffer = optionalByteBuffer.get(); + int positionCount = readPositionCount(byteBuffer); + if (positionCount == 0) { + continue; + } + // offsets kept in PendingResultFragment are absolute positions of the buffer + int blockStart = byteBuffer.position(); + int blockSize = byteBuffer.remaining(); + rowCount += positionCount; + if (blockSize <= remainingPayloadSize) { + res.add(byteBuffer); + remainingPayloadSize -= blockSize; + memorySize += blockSize; + } else { + // The block exceeds the remaining payload, send a leading fragment and keep the rest + res.add(slice(byteBuffer, blockStart, remainingPayloadSize)); + pendingFragment.reset(byteBuffer, blockStart + remainingPayloadSize); + return ResultChunk.fragment( + res, blockStart + remainingPayloadSize, blockStart == 0 ? blockSize : -1, false); + } + } + return ResultChunk.complete(res, !queryExecution.hasNextResult()); + } + + private static int readPositionCount(ByteBuffer byteBuffer) { + byteBuffer.mark(); + int valueColumnCount = byteBuffer.getInt(); + for (int i = 0; i < valueColumnCount; i++) { + byteBuffer.get(); + } + int positionCount = byteBuffer.getInt(); + byteBuffer.reset(); + return positionCount; + } + + /** Returns a view of {@code [offset, offset + length)} bytes of the buffer without copying. */ + private static ByteBuffer slice(ByteBuffer buffer, int offset, int length) { + ByteBuffer duplicate = buffer.duplicate(); + duplicate.position(offset); + duplicate.limit(offset + length); + return duplicate.slice(); + } + + /** The bounded-payload result of one query result RPC response. */ + public static class ResultChunk { + + private final List queryResult; + private final int nextOffset; + private final int totalLength; + private final boolean finished; + + private ResultChunk( + List queryResult, int nextOffset, int totalLength, boolean finished) { + this.queryResult = queryResult; + this.nextOffset = nextOffset; + this.totalLength = totalLength; + this.finished = finished; + } + + /** The response ends with a fragment, which starts at {@code nextOffset} of its TsBlock. */ + public static ResultChunk fragment( + List queryResult, int nextOffset, int totalLength, boolean finished) { + return new ResultChunk(queryResult, nextOffset, totalLength, finished); + } + + public static ResultChunk complete(List queryResult, boolean finished) { + return new ResultChunk(queryResult, -1, -1, finished); + } + + public List getQueryResult() { + return queryResult; + } + + public boolean hasNextOffset() { + return nextOffset >= 0; + } + + public int getNextOffset() { + return nextOffset; + } + + /** The total serialized length of the last block, set only when the fragment starts it. */ + public boolean hasTotalLength() { + return totalLength >= 0; + } + + public int getTotalLength() { + return totalLength; + } + + public boolean isFinished() { + return finished; + } + } + public static long[] readTimesFromBuffer(ByteBuffer buffer, int size) { long[] times = new long[size]; for (int i = 0; i < size; i++) { diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java index cfbcf0e26306c..ca22be2e6be51 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java @@ -50,6 +50,7 @@ import org.apache.iotdb.db.queryengine.plan.analyze.QueryType; import org.apache.iotdb.db.queryengine.plan.execution.ExecutionResult; import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution; +import org.apache.iotdb.db.queryengine.plan.execution.PendingResultFragment; import org.apache.iotdb.db.queryengine.plan.planner.plan.parameter.SeriesScanOptions; import org.apache.iotdb.db.queryengine.plan.statement.component.OrderByKey; import org.apache.iotdb.db.queryengine.plan.statement.component.Ordering; @@ -1808,6 +1809,7 @@ static class FakeQueryExecution implements IQueryExecution { private final long startTime; private final String queryId; private final String sql; + private final PendingResultFragment pendingResultFragment = new PendingResultFragment(); FakeQueryExecution(long startTime, String queryId, String sql) { this.startTime = startTime; @@ -1893,6 +1895,11 @@ public Optional getByteBufferBatchResult() { return Optional.empty(); } + @Override + public PendingResultFragment getPendingResultFragment() { + return pendingResultFragment; + } + @Override public boolean hasNextResult() { return false; diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java index 7b65562ce616d..3ea0cce94b690 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java @@ -21,6 +21,7 @@ import org.apache.iotdb.commons.exception.IoTDBException; import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution; +import org.apache.iotdb.db.queryengine.plan.execution.PendingResultFragment; import org.apache.iotdb.service.rpc.thrift.TSQueryDataSet; import org.apache.tsfile.common.conf.TSFileConfig; @@ -281,4 +282,157 @@ private void compareTsBlock(Pair, Boolean> res) { new Binary(BINARY_STR, TSFileConfig.STRING_CHARSET), tsBlock.getColumn(5).getBinary(0)); assertTrue(tsBlock.getColumn(5).isNull(1)); } + + @Test + public void testConvertQueryResultByFetchSizeWithSingleOversizedBlock() + throws IoTDBException, IOException { + TsBlock tsBlock = buildTsBlock(); + ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock); + int totalLength = serialized.remaining(); + // one byte smaller than the block so that the block has to be split into two fragments + int payloadSize = totalLength - 1; + + PendingResultFragment pendingFragment = new PendingResultFragment(); + IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); + Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); + Mockito.when(queryExecution.getByteBufferBatchResult()) + .thenReturn(Optional.of(serialized.duplicate()), Optional.empty()); + Mockito.when(queryExecution.hasNextResult()).thenReturn(false); + + QueryDataSetUtils.ResultChunk firstChunk = + QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 0, payloadSize); + assertFalse(firstChunk.isFinished()); + assertTrue(firstChunk.hasNextOffset()); + assertTrue(firstChunk.hasTotalLength()); + assertEquals(totalLength, firstChunk.getTotalLength()); + assertEquals(payloadSize, firstChunk.getNextOffset()); + assertEquals(1, firstChunk.getQueryResult().size()); + assertEquals(payloadSize, firstChunk.getQueryResult().get(0).remaining()); + assertTrue(pendingFragment.getBlock() != null); + + QueryDataSetUtils.ResultChunk secondChunk = + QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, 10, firstChunk.getNextOffset(), payloadSize); + assertTrue(secondChunk.isFinished()); + assertFalse(secondChunk.hasNextOffset()); + assertFalse(secondChunk.hasTotalLength()); + assertTrue(pendingFragment.getBlock() == null); + + TsBlock result = deserialize(firstChunk.getQueryResult().get(0), secondChunk.getQueryResult()); + assertEquals(2, result.getPositionCount()); + assertEquals(1L, result.getTimeColumn().getLong(0)); + assertEquals(2L, result.getTimeColumn().getLong(1)); + } + + @Test + public void testConvertQueryResultByFetchSizeWithTrailingFragment() + throws IoTDBException, IOException { + TsBlock tsBlock = buildTsBlock(); + ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock); + int blockSize = serialized.remaining(); + // a small block that fits into the payload, followed by the oversized block + ByteBuffer smallBlock = serialized.duplicate(); + int payloadSize = smallBlock.remaining() + blockSize / 2; + + PendingResultFragment pendingFragment = new PendingResultFragment(); + IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); + Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); + Mockito.when(queryExecution.getByteBufferBatchResult()) + .thenReturn(Optional.of(smallBlock), Optional.of(serialized.duplicate()), Optional.empty()); + Mockito.when(queryExecution.hasNextResult()).thenReturn(false); + + QueryDataSetUtils.ResultChunk firstChunk = + QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 0, payloadSize); + assertFalse(firstChunk.isFinished()); + assertTrue(firstChunk.hasNextOffset()); + assertEquals(2, firstChunk.getQueryResult().size()); + // the last element is the fragment of the second block + assertEquals(blockSize / 2, firstChunk.getQueryResult().get(1).remaining()); + assertTrue(firstChunk.hasTotalLength()); + assertEquals(blockSize, firstChunk.getTotalLength()); + + QueryDataSetUtils.ResultChunk secondChunk = + QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, 10, firstChunk.getNextOffset(), payloadSize); + assertTrue(secondChunk.isFinished()); + assertFalse(secondChunk.hasNextOffset()); + // only the rest of the oversized block remains + assertEquals(1, secondChunk.getQueryResult().size()); + assertEquals(blockSize - blockSize / 2, secondChunk.getQueryResult().get(0).remaining()); + } + + @Test + public void testContinuationCompletionDoesNotFetchNewBlocks() throws IoTDBException, IOException { + TsBlock tsBlock = buildTsBlock(); + ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock); + int blockSize = serialized.remaining(); + int payloadSize = blockSize / 2; + + PendingResultFragment pendingFragment = new PendingResultFragment(); + IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); + Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); + Mockito.when(queryExecution.getByteBufferBatchResult()) + .thenReturn( + Optional.of(serialized.duplicate()), + Optional.of(serialized.duplicate()), + Optional.empty()); + Mockito.when(queryExecution.hasNextResult()).thenReturn(true, false); + + QueryDataSetUtils.ResultChunk firstChunk = + QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 0, payloadSize); + assertFalse(firstChunk.isFinished()); + assertEquals(payloadSize, firstChunk.getNextOffset()); + + // The response that completes the fragmented block must not fetch new blocks, otherwise its + // row count would exceed fetchSize (the tail of the big block is not accounted). + QueryDataSetUtils.ResultChunk continuationChunk = + QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, 10, firstChunk.getNextOffset(), blockSize); + assertFalse(continuationChunk.isFinished()); + assertFalse(continuationChunk.hasNextOffset()); + assertEquals(1, continuationChunk.getQueryResult().size()); + assertEquals(blockSize - payloadSize, continuationChunk.getQueryResult().get(0).remaining()); + assertTrue(pendingFragment.getBlock() == null); + + // New blocks are delivered by the following request only + QueryDataSetUtils.ResultChunk nextChunk = + QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 0, blockSize); + assertTrue(nextChunk.isFinished()); + assertFalse(nextChunk.hasNextOffset()); + assertEquals(1, nextChunk.getQueryResult().size()); + assertEquals(blockSize, nextChunk.getQueryResult().get(0).remaining()); + } + + @Test(expected = IllegalArgumentException.class) + public void testConvertQueryResultByFetchSizeRejectsMismatchedOffset() throws IoTDBException { + PendingResultFragment pendingFragment = new PendingResultFragment(); + pendingFragment.reset(ByteBuffer.wrap(new byte[10]), 5); + IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); + Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); + + QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 3, 8); + } + + @Test(expected = IllegalArgumentException.class) + public void testConvertQueryResultByFetchSizeRejectsOffsetWithoutPendingFragment() + throws IoTDBException { + IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); + Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(new PendingResultFragment()); + + QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 5, 8); + } + + private TsBlock deserialize(ByteBuffer head, List tail) { + int size = head.remaining(); + for (ByteBuffer buffer : tail) { + size += buffer.remaining(); + } + ByteBuffer assembled = ByteBuffer.allocate(size); + assembled.put(head.duplicate()); + for (ByteBuffer buffer : tail) { + assembled.put(buffer.duplicate()); + } + assembled.flip(); + return new TsBlockSerde().deserialize(assembled); + } } diff --git a/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template b/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template index a3fe0970318c8..49a23190cf8d5 100644 --- a/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template +++ b/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template @@ -1241,6 +1241,9 @@ mpp_data_exchange_max_pool_size=10 mpp_data_exchange_keep_alive_time_in_ms=1000 # The maximum payload size of one MPP data exchange RPC response. +# It also bounds the payload of one query-result RPC response between client and DataNode: a +# serialized TsBlock larger than this value is transferred in fragments, so that one oversized +# row (e.g. an OBJECT/BLOB column) cannot inflate the RPC buffer of server and client connections. # <=0 use default value # The effective value is between 131072(128KB) and dn_thrift_max_frame_size minus 1024 bytes reserved for the RPC response envelope. # effectiveMode: hot_reload diff --git a/iotdb-protocol/thrift-datanode/src/main/thrift/client.thrift b/iotdb-protocol/thrift-datanode/src/main/thrift/client.thrift index 7ff12ceb02154..46d2e54554e49 100644 --- a/iotdb-protocol/thrift-datanode/src/main/thrift/client.thrift +++ b/iotdb-protocol/thrift-datanode/src/main/thrift/client.thrift @@ -74,6 +74,11 @@ struct TSExecuteStatementResp { 15: optional string database 16: optional bool tableModel 17: optional list columnIndex2TsBlockColumnIndexList + // Start offset of the next fragment. Set only when the last element in queryResult is a + // fragment. Only set for sessions that enabled chunked query results at openSession. + 18: optional i32 offset + // Total serialized length of the TsBlock when the response starts its first fragment. + 19: optional i32 totalLength } enum TSProtocolVersion { @@ -203,6 +208,9 @@ struct TSFetchResultsReq{ 5: required bool isAlign 6: optional i64 timeout 7: optional i64 statementId + // Start offset of the fragment to continue. The client sets this field (including 0 for the + // first request) iff it supports chunked query results. It is optional for rolling upgrade. + 8: optional i32 offset } struct TSFetchResultsResp{ @@ -213,6 +221,10 @@ struct TSFetchResultsResp{ 5: optional TSQueryNonAlignDataSet nonAlignQueryDataSet 6: optional list queryResult 7: optional bool moreData + // Start offset of the next fragment. Set only when the last element in queryResult is a fragment. + 8: optional i32 offset + // Total serialized length of the TsBlock when the response starts its first fragment. + 9: optional i32 totalLength } struct TSFetchMetadataResp{ From fcaf125b1a162bf01e2f89fe39215810b5b9de5a Mon Sep 17 00:00:00 2001 From: Weihao Li <18110526956@163.com> Date: Thu, 12 Nov 2026 16:46:40 +0800 Subject: [PATCH 02/11] draft Signed-off-by: Weihao Li <18110526956@163.com> --- .../rpc/IoTDBRpcDataSetFragmentTest.java | 249 ++++++++++++++++++ .../plan/execution/PendingResultFragment.java | 56 ++++ 2 files changed, 305 insertions(+) create mode 100644 iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/IoTDBRpcDataSetFragmentTest.java create mode 100644 iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java diff --git a/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/IoTDBRpcDataSetFragmentTest.java b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/IoTDBRpcDataSetFragmentTest.java new file mode 100644 index 0000000000000..c68553cb013f9 --- /dev/null +++ b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/IoTDBRpcDataSetFragmentTest.java @@ -0,0 +1,249 @@ +/* + * 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.rpc; + +import org.apache.iotdb.service.rpc.thrift.IClientRPCService; +import org.apache.iotdb.service.rpc.thrift.TSCloseOperationReq; +import org.apache.iotdb.service.rpc.thrift.TSFetchResultsReq; +import org.apache.iotdb.service.rpc.thrift.TSFetchResultsResp; + +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.read.common.block.TsBlock; +import org.apache.tsfile.read.common.block.TsBlockBuilder; +import org.apache.tsfile.read.common.block.column.TsBlockSerde; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import java.nio.ByteBuffer; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +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 IoTDBRpcDataSetFragmentTest { + + private static final String COLUMN = "s1"; + + @Test + public void testAssembleOneOversizedTsBlockAcrossResponses() throws Exception { + byte[] bytes = serializeInt64Block(); + int totalLength = bytes.length; + int split = totalLength / 2; + + IClientRPCService.Iface client = mockClient(); + when(client.fetchResultsV2(any(TSFetchResultsReq.class))) + .thenReturn( + fragmentResponse(ByteBuffer.wrap(bytes, 0, split), split, totalLength, true), + fragmentResponse(ByteBuffer.wrap(bytes, split, totalLength - split), -1, -1, false)); + + IoTDBRpcDataSet dataSet = newDataSet(client); + + assertTrue(dataSet.next()); + assertEquals(10L, dataSet.getLong(COLUMN)); + assertTrue(dataSet.next()); + assertEquals(20L, dataSet.getLong(COLUMN)); + assertFalse(dataSet.next()); + + ArgumentCaptor captor = ArgumentCaptor.forClass(TSFetchResultsReq.class); + verify(client, times(2)).fetchResultsV2(captor.capture()); + assertEquals(0, captor.getAllValues().get(0).getOffset()); + assertEquals(split, captor.getAllValues().get(1).getOffset()); + } + + @Test + public void testCompleteBlocksAndTrailingFragmentInOneResponse() throws Exception { + byte[] first = serializeInt64Block(); + byte[] second = serializeInt64Block(); + int split = second.length / 2; + + IClientRPCService.Iface client = mockClient(); + when(client.fetchResultsV2(any(TSFetchResultsReq.class))) + .thenReturn( + response( + List.of(ByteBuffer.wrap(first), ByteBuffer.wrap(second, 0, split)), + split, + second.length, + true), + fragmentResponse(ByteBuffer.wrap(second, split, second.length - split), -1, -1, false)); + + IoTDBRpcDataSet dataSet = newDataSet(client); + + assertTrue(dataSet.next()); + assertEquals(10L, dataSet.getLong(COLUMN)); + assertTrue(dataSet.next()); + assertEquals(20L, dataSet.getLong(COLUMN)); + assertTrue(dataSet.next()); + assertEquals(10L, dataSet.getLong(COLUMN)); + assertTrue(dataSet.next()); + assertEquals(20L, dataSet.getLong(COLUMN)); + assertFalse(dataSet.next()); + } + + @Test + public void testLegacyResponseWithoutOffset() throws Exception { + byte[] bytes = serializeInt64Block(); + IClientRPCService.Iface client = mockClient(); + when(client.fetchResultsV2(any(TSFetchResultsReq.class))) + .thenReturn(response(List.of(ByteBuffer.wrap(bytes)), -1, -1, false)); + + IoTDBRpcDataSet dataSet = newDataSet(client); + + assertTrue(dataSet.next()); + assertEquals(10L, dataSet.getLong(COLUMN)); + assertTrue(dataSet.next()); + assertEquals(20L, dataSet.getLong(COLUMN)); + assertFalse(dataSet.next()); + } + + @Test + public void testBlocksInExecuteResponseAreConsumedBeforeFetching() throws Exception { + byte[] bytes = serializeInt64Block(); + IClientRPCService.Iface client = mockClient(); + IoTDBRpcDataSet dataSet = + new IoTDBRpcDataSet( + "", + List.of(COLUMN), + List.of(TSDataType.INT64.name()), + null, + true, + false, + 1L, + 2L, + client, + 3L, + List.of(ByteBuffer.wrap(bytes)), + 100, + 0, + ZoneId.systemDefault(), + RpcUtils.DEFAULT_TIME_FORMAT, + 1_000_000, + true, + new ArrayList<>(List.of(0))); + + assertTrue(dataSet.next()); + assertEquals(10L, dataSet.getLong(COLUMN)); + assertTrue(dataSet.next()); + assertEquals(20L, dataSet.getLong(COLUMN)); + assertFalse(dataSet.next()); + verify(client, times(0)).fetchResultsV2(any(TSFetchResultsReq.class)); + } + + @Test(expected = IllegalArgumentException.class) + public void testRejectMissingTotalLengthForFirstFragment() throws Exception { + byte[] bytes = serializeInt64Block(); + IClientRPCService.Iface client = mockClient(); + when(client.fetchResultsV2(any(TSFetchResultsReq.class))) + .thenReturn( + fragmentResponse( + ByteBuffer.wrap(bytes, 0, bytes.length / 2), bytes.length / 2, -1, true)); + + IoTDBRpcDataSet dataSet = newDataSet(client); + dataSet.next(); + } + + @Test(expected = IllegalArgumentException.class) + public void testRejectNonIncreasingFragmentOffset() throws Exception { + byte[] bytes = serializeInt64Block(); + int split = bytes.length / 2; + IClientRPCService.Iface client = mockClient(); + // the offset must equal the accumulated length, otherwise the dataset must refuse to continue + when(client.fetchResultsV2(any(TSFetchResultsReq.class))) + .thenReturn( + fragmentResponse(ByteBuffer.wrap(bytes, 0, split), split - 1, bytes.length, true)); + + IoTDBRpcDataSet dataSet = newDataSet(client); + dataSet.next(); + } + + private static IoTDBRpcDataSet newDataSet(IClientRPCService.Iface client) { + return new IoTDBRpcDataSet( + "", + List.of(COLUMN), + List.of(TSDataType.INT64.name()), + null, + true, + true, + 1L, + 2L, + client, + 3L, + null, + 100, + 0, + ZoneId.systemDefault(), + RpcUtils.DEFAULT_TIME_FORMAT, + 1_000_000, + true, + new ArrayList<>(List.of(0))); + } + + private static IClientRPCService.Iface mockClient() throws Exception { + IClientRPCService.Iface client = mock(IClientRPCService.Iface.class); + when(client.closeOperation(any(TSCloseOperationReq.class))) + .thenReturn(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS)); + return client; + } + + private static TSFetchResultsResp fragmentResponse( + ByteBuffer fragment, int nextOffset, int totalLength, boolean moreData) { + return response(Collections.singletonList(fragment), nextOffset, totalLength, moreData); + } + + private static TSFetchResultsResp response( + List blocks, int nextOffset, int totalLength, boolean moreData) { + TSFetchResultsResp resp = + new TSFetchResultsResp(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS), true, true); + resp.setQueryResult(blocks); + resp.setMoreData(moreData); + if (nextOffset >= 0) { + resp.setOffset(nextOffset); + } + if (totalLength >= 0) { + resp.setTotalLength(totalLength); + } + return resp; + } + + /** [time=1, s1=10], [time=2, s1=20] as an INT64 column. */ + private static byte[] serializeInt64Block() throws Exception { + TsBlockBuilder builder = new TsBlockBuilder(Collections.singletonList(TSDataType.INT64)); + builder.getTimeColumnBuilder().writeLong(1L); + builder.getColumnBuilder(0).writeLong(10L); + builder.declarePosition(); + builder.getTimeColumnBuilder().writeLong(2L); + builder.getColumnBuilder(0).writeLong(20L); + builder.declarePosition(); + TsBlock tsBlock = builder.build(); + + ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock); + byte[] bytes = new byte[serialized.remaining()]; + serialized.duplicate().get(bytes); + return bytes; + } +} diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java new file mode 100644 index 0000000000000..ebb020ae8058d --- /dev/null +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java @@ -0,0 +1,56 @@ +/* + * 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.execution; + +import java.nio.ByteBuffer; + +/** + * The tail of a serialized TsBlock that did not fit into the previous RPC response and is waiting + * to be sent in the following response(s). The block is already serialized and removed from the + * result handle, so this fragment only holds a reference to it and never copies the payload. + */ +public class PendingResultFragment { + + private ByteBuffer block; + private int nextOffset; + + /** Records that {@code block} has been sent up to {@code nextOffset} bytes. */ + public synchronized void reset(ByteBuffer block, int nextOffset) { + if (block != this.block) { + this.block = block; + } + this.nextOffset = nextOffset; + } + + /** Returns the block that is partially sent, or null if no fragment is pending. */ + public synchronized ByteBuffer getBlock() { + return block; + } + + /** Returns the start offset of the next fragment. */ + public synchronized int getNextOffset() { + return nextOffset; + } + + public synchronized void clear() { + block = null; + nextOffset = 0; + } +} From c5d363c2cb961d3e7bbcfcfacb0f29aefb1c685b Mon Sep 17 00:00:00 2001 From: Weihao Li <18110526956@163.com> Date: Fri, 13 Nov 2026 16:47:36 +0800 Subject: [PATCH 03/11] done Signed-off-by: Weihao Li <18110526956@163.com> --- .../v1/handler/QueryDataSetHandlerTest.java | 4 +- .../org/apache/iotdb/rpc/IoTDBRpcDataSet.java | 11 +- .../iotdb/session/SessionConnection.java | 3 +- .../thrift/impl/ClientRPCServiceImpl.java | 125 ++++----- .../exchange/source/ISourceHandle.java | 12 +- .../exchange/source/LocalSourceHandle.java | 8 +- .../exchange/source/SourceHandle.java | 97 ++++--- .../plan/execution/IQueryExecution.java | 10 +- .../plan/execution/PendingResultFragment.java | 30 ++- .../plan/execution/QueryExecution.java | 16 +- .../execution/config/ConfigExecution.java | 7 +- .../execution/memory/MemorySourceHandle.java | 8 +- .../iotdb/db/utils/QueryDataSetUtils.java | 33 +-- .../protocol/session/QueryOwnershipTest.java | 2 +- .../execution/exchange/SourceHandleTest.java | 116 ++++++++ .../operator/MergeTreeSortOperatorTest.java | 4 +- .../iotdb/db/utils/QueryDataSetUtilsTest.java | 57 +++- .../db/utils/ResultChunkingEndToEndTest.java | 255 ++++++++++++++++++ 18 files changed, 615 insertions(+), 183 deletions(-) create mode 100644 iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java diff --git a/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java index f8aa9c95a2b72..37fa74b9bd5a6 100644 --- a/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java +++ b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java @@ -89,7 +89,7 @@ private static final class TestQueryExecution implements IQueryExecution { private final Queue> batches = new ArrayDeque<>(); private final DatasetHeader datasetHeader; - private final PendingResultFragment pendingResultFragment = new PendingResultFragment(); + private final PendingResultFragment pendingResultFragment = new PendingResultFragment(() -> {}); private TestQueryExecution(TsBlock... tsBlocks) { for (TsBlock tsBlock : tsBlocks) { @@ -126,7 +126,7 @@ public Optional getBatchResult() throws IoTDBException { } @Override - public Optional getByteBufferBatchResult() { + public Optional getByteBufferBatchResult(boolean retainMemory) { return Optional.empty(); } diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java index f9669d56d7a71..a88f473649f9f 100644 --- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java +++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java @@ -332,7 +332,6 @@ public boolean fetchResults() throws StatementExecutionException, IoTDBConnectio } else { queryResult = new ArrayList<>(); queryResultIndex = 0; - queryResultSize = 0; addResponse(resp.getQueryResult(), resp.isSetOffset(), resp.offset, resp.totalLength); queryResultSize = queryResult.size(); this.tsBlockSize = 0; @@ -351,11 +350,6 @@ public boolean fetchResults() throws StatementExecutionException, IoTDBConnectio * the fragment is either appended to the pending {@link ResultFragmentBuffer} or starts a new * one; the corresponding TsBlock is only added to {@link #queryResult} once all of its bytes have * been received. - * - * @param blocks the serialized TsBlocks in the response - * @param offsetSet whether the last element of {@code blocks} is a fragment - * @param nextOffset the start offset of the next fragment - * @param totalLength the total serialized length of the block the first fragment belongs to */ private void addResponse( List blocks, boolean offsetSet, int nextOffset, int totalLength) { @@ -365,7 +359,6 @@ private void addResponse( int blockIndex = 0; if (fragmentBuffer != null) { - // the first element completes the pending fragment, if it is not the only element fragmentBuffer.append(blocks.get(blockIndex++)); if (offsetSet && blockIndex == blocks.size()) { fragmentBuffer.updateOffset(nextOffset); @@ -418,7 +411,9 @@ private void append(ByteBuffer fragment) { } private void updateOffset(int nextOffset) { - if (nextOffset <= this.nextOffset || nextOffset != buffer.size()) { + // the accumulated buffer only grows with non-empty fragments, so this also rejects a + // non-increasing offset + if (nextOffset != buffer.size()) { throw new IllegalArgumentException( String.format( RpcMessages diff --git a/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java b/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java index d0b6e8dba528b..9808b69f96b06 100644 --- a/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java +++ b/iotdb-client/session/src/main/java/org/apache/iotdb/session/SessionConnection.java @@ -102,6 +102,7 @@ public class SessionConnection { private static final Logger logger = LoggerFactory.getLogger(SessionConnection.class); private static final String USE_ENCRYPTED_PASSWORD_KEY = "use_encrypted_password"; private static final String ENABLE_AUTO_CREATE_SCHEMA_KEY = "enable_auto_create_schema"; + private static final String ENABLE_RESULT_CHUNKING_KEY = "enable_result_chunking"; public static final String MSG_RECONNECTION_FAIL = SessionMessages.EXCEPTION_FAIL_TO_RECONNECT_TO_SERVER_PLEASE_CHECK_SERVER_STATUS_D9A2790F; protected Session session; @@ -247,7 +248,7 @@ private void init( openReq.putToConfiguration("version", session.version.toString()); openReq.putToConfiguration("sql_dialect", sqlDialect); // this client can assemble fragmented query results, see TSFetchResultsReq#offset - openReq.putToConfiguration("enable_result_chunking", Boolean.TRUE.toString()); + openReq.putToConfiguration(ENABLE_RESULT_CHUNKING_KEY, Boolean.TRUE.toString()); if (session.useEncryptedPassword) { openReq.putToConfiguration(USE_ENCRYPTED_PASSWORD_KEY, Boolean.TRUE.toString()); } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java index 614774a42afc0..c911e0bcf0ca1 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java @@ -312,27 +312,8 @@ boolean apply( } private static final SelectResult SELECT_RESULT = - (resp, queryExecution, fetchSize, maxPayloadSize) -> { - QueryDataSetUtils.ResultChunk chunk; - if (maxPayloadSize > 0) { - // The execute request carries no offset, so it must start a new result set - chunk = - QueryDataSetUtils.convertQueryResultByFetchSize( - queryExecution, fetchSize, 0, maxPayloadSize); - } else { - Pair, Boolean> pair = - QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, fetchSize); - chunk = QueryDataSetUtils.ResultChunk.complete(pair.left, pair.right); - } - resp.setQueryResult(chunk.getQueryResult()); - if (chunk.hasNextOffset()) { - resp.setOffset(chunk.getNextOffset()); - if (chunk.hasTotalLength()) { - resp.setTotalLength(chunk.getTotalLength()); - } - } - return chunk.isFinished(); - }; + (resp, queryExecution, fetchSize, maxPayloadSize) -> + fillQueryResult(resp, fetchResultChunk(queryExecution, fetchSize, 0, maxPayloadSize)); private static final SelectResult OLD_SELECT_RESULT = (resp, queryExecution, fetchSize, maxPayloadSize) -> { @@ -341,6 +322,46 @@ boolean apply( return pair.right; }; + /** + * Fetches one response-worth of query results. Fragmentation is disabled when {@code + * maxPayloadSize} is not positive, i.e. the client cannot assemble fragments. + */ + private static QueryDataSetUtils.ResultChunk fetchResultChunk( + IQueryExecution queryExecution, int fetchSize, int requestOffset, int maxPayloadSize) + throws IoTDBException { + if (maxPayloadSize > 0) { + return QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, fetchSize, requestOffset, maxPayloadSize); + } + Pair, Boolean> pair = + QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, fetchSize); + return QueryDataSetUtils.ResultChunk.complete(pair.left, pair.right); + } + + private static boolean fillQueryResult( + TSExecuteStatementResp resp, QueryDataSetUtils.ResultChunk chunk) { + resp.setQueryResult(chunk.getQueryResult()); + if (chunk.hasNextOffset()) { + resp.setOffset(chunk.getNextOffset()); + if (chunk.hasTotalLength()) { + resp.setTotalLength(chunk.getTotalLength()); + } + } + return chunk.isFinished(); + } + + private static boolean fillQueryResult( + TSFetchResultsResp resp, QueryDataSetUtils.ResultChunk chunk) { + resp.setQueryResult(chunk.getQueryResult()); + if (chunk.hasNextOffset()) { + resp.setOffset(chunk.getNextOffset()); + if (chunk.hasTotalLength()) { + resp.setTotalLength(chunk.getTotalLength()); + } + } + return chunk.isFinished(); + } + /** * Returns the maximum payload size of one response carrying query results, or 0 if the session's * client cannot assemble fragmented results. The payload bound is shared with the MPP data @@ -1615,35 +1636,15 @@ public TSFetchResultsResp fetchResultsV2(TSFetchResultsReq req) { statementType = queryExecution.getStatementType(); try (SetThreadName queryName = new SetThreadName(queryExecution.getQueryId())) { - if (req.isSetOffset()) { - // The client sets offset (0 for the first request) iff it can assemble fragment results - QueryDataSetUtils.ResultChunk chunk = - QueryDataSetUtils.convertQueryResultByFetchSize( - queryExecution, - req.fetchSize, - req.getOffset(), - config.getMppDataExchangeMaxPayloadSizeInBytes()); - finished = chunk.isFinished(); - resp.setHasResultSet(!chunk.getQueryResult().isEmpty()); - resp.setIsAlign(true); - resp.setQueryResult(chunk.getQueryResult()); - if (chunk.hasNextOffset()) { - resp.setOffset(chunk.getNextOffset()); - if (chunk.hasTotalLength()) { - resp.setTotalLength(chunk.getTotalLength()); - } - } - resp.setMoreData(!finished); - return resp; - } - Pair, Boolean> pair = - QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, req.fetchSize); - List result = pair.left; - finished = pair.right; - boolean hasResultSet = !result.isEmpty(); - resp.setHasResultSet(hasResultSet); + QueryDataSetUtils.ResultChunk chunk = + fetchResultChunk( + queryExecution, + req.fetchSize, + req.isSetOffset() ? req.getOffset() : 0, + req.isSetOffset() ? config.getMppDataExchangeMaxPayloadSizeInBytes() : 0); + finished = fillQueryResult(resp, chunk); + resp.setHasResultSet(!chunk.getQueryResult().isEmpty()); resp.setIsAlign(true); - resp.setQueryResult(result); resp.setMoreData(!finished); return resp; } @@ -1855,7 +1856,8 @@ public TSPrepareResp prepareStatement(TSPrepareReq req) { @Override public TSExecuteStatementResp executePreparedStatement(TSExecutePreparedReq req) { - return executeStatementInternal(new TSExecutePreparedReqAdapter(req), setResultForPrepared); + // Use V2 format (queryResult) to match IoTDBTablePreparedStatement client + return executeStatementInternal(new TSExecutePreparedReqAdapter(req), SELECT_RESULT); } @Override @@ -1876,29 +1878,6 @@ public TSStatus deallocatePreparedStatement(TSDeallocatePreparedReq req) { } } - private final SelectResult setResultForPrepared = - (resp, queryExecution, fetchSize, maxPayloadSize) -> { - // Use V2 format (queryResult) to match IoTDBTablePreparedStatement client - QueryDataSetUtils.ResultChunk chunk; - if (maxPayloadSize > 0) { - chunk = - QueryDataSetUtils.convertQueryResultByFetchSize( - queryExecution, fetchSize, 0, maxPayloadSize); - } else { - Pair, Boolean> pair = - QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, fetchSize); - chunk = QueryDataSetUtils.ResultChunk.complete(pair.left, pair.right); - } - resp.setQueryResult(chunk.getQueryResult()); - if (chunk.hasNextOffset()) { - resp.setOffset(chunk.getNextOffset()); - if (chunk.hasTotalLength()) { - resp.setTotalLength(chunk.getTotalLength()); - } - } - return chunk.isFinished(); - }; - // ========================= End PreparedStatement RPC Methods ========================= @Override diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java index 32e5557b8f922..210c2ea48008d 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java @@ -50,9 +50,19 @@ public interface ISourceHandle extends Accountable { * iterator with receive(). When one of these two methods is called, the cursor in iterator will * forward. * + * @param retainMemory if true, the memory that the returned block retains in the memory pool is + * not freed when the block is taken out of this handle. The caller must release it via {@link + * #freeRetainedMemory()} once the block is no longer needed. Implementations whose serialized + * blocks are not reserved from the memory pool ignore this flag. * @throws IoTDBException throw IoTDBException */ - ByteBuffer getSerializedTsBlock() throws IoTDBException; + ByteBuffer getSerializedTsBlock(boolean retainMemory) throws IoTDBException; + + /** + * Releases the memory retained by the blocks taken out with {@code retainMemory=true} and not + * released yet. It is safe to call when nothing is retained, and after abort/close. + */ + void freeRetainedMemory(); /** If there are more TsBlocks. */ boolean isFinished(); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java index 544421712669a..94f6e09fa058c 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java @@ -153,7 +153,8 @@ public TsBlock receive() { } @Override - public ByteBuffer getSerializedTsBlock() throws IoTDBException { + public ByteBuffer getSerializedTsBlock(boolean retainMemory) throws IoTDBException { + // the returned bytes are a fresh serialization, not reserved from the memory pool TsBlock tsBlock = receive(); if (LOGGER.isDebugEnabled()) { LOGGER.debug(DataNodeQueryMessages.GET_SERIALIZED_TSBLOCK, CommonUtils.toString(tsBlock)); @@ -174,6 +175,11 @@ public ByteBuffer getSerializedTsBlock() throws IoTDBException { } } + @Override + public void freeRetainedMemory() { + // nothing is reserved from the memory pool + } + @Override public boolean isFinished() { synchronized (queue) { diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java index 8ee7ad601ef61..703ce3407f6a3 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java @@ -109,6 +109,13 @@ public class SourceHandle implements ISourceHandle { /** The actual buffered memory in bytes, including the amount of memory being reserved. */ private long bufferRetainedSizeInBytes = 0L; + /** + * The memory in bytes taken out of the buffer with {@code retainMemory=true}, still reserved in + * the memory pool on behalf of the caller. Freed by {@link #freeRetainedMemory()}, or by + * abort/close as a fallback. + */ + private long pendingRetainedSizeInBytes = 0L; + private int currSequenceId = 0; private int nextSequenceId = 0; private int lastSequenceId = Integer.MAX_VALUE; @@ -232,7 +239,7 @@ public SourceHandle( @Override public synchronized TsBlock receive() { - ByteBuffer tsBlock = getSerializedTsBlock(); + ByteBuffer tsBlock = getSerializedTsBlock(false); if (tsBlock != null) { long startTime = System.nanoTime(); try { @@ -247,7 +254,7 @@ public synchronized TsBlock receive() { } @Override - public synchronized ByteBuffer getSerializedTsBlock() { + public synchronized ByteBuffer getSerializedTsBlock(boolean retainMemory) { long startTime = System.nanoTime(); try (SetThreadName sourceHandleName = new SetThreadName(threadName)) { checkState(); @@ -270,13 +277,17 @@ public synchronized ByteBuffer getSerializedTsBlock() { currSequenceId += 1; if (retainedSize > 0) { bufferRetainedSizeInBytes -= retainedSize; - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - retainedSize); + if (retainMemory) { + pendingRetainedSizeInBytes += retainedSize; + } else { + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + retainedSize); + } } if (sequenceIdToTsBlock.isEmpty() && !isFinished()) { @@ -296,6 +307,22 @@ public synchronized ByteBuffer getSerializedTsBlock() { } } + @Override + public synchronized void freeRetainedMemory() { + if (pendingRetainedSizeInBytes <= 0) { + return; + } + long retainedSize = pendingRetainedSizeInBytes; + pendingRetainedSizeInBytes = 0; + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + retainedSize); + } + private synchronized void trySubmitGetDataBlocksTask() { if (aborted || closed) { return; @@ -444,16 +471,7 @@ public synchronized void abort() { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } sequenceIdToDataBlockSize.clear(); - if (bufferRetainedSizeInBytes > 0) { - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - bufferRetainedSizeInBytes); - bufferRetainedSizeInBytes = 0; - } + freeAllRetainedMemory(); aborted = true; sourceHandleListener.onAborted(this); } @@ -472,16 +490,7 @@ public synchronized void abort(Throwable t) { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } sequenceIdToDataBlockSize.clear(); - if (bufferRetainedSizeInBytes > 0) { - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - bufferRetainedSizeInBytes); - bufferRetainedSizeInBytes = 0; - } + freeAllRetainedMemory(); aborted = true; sourceHandleListener.onAborted(this); } @@ -500,16 +509,7 @@ public synchronized void close() { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } sequenceIdToDataBlockSize.clear(); - if (bufferRetainedSizeInBytes > 0) { - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - bufferRetainedSizeInBytes); - bufferRetainedSizeInBytes = 0; - } + freeAllRetainedMemory(); closed = true; executorService.submit(new SendCloseSinkChannelEventTask()); currSequenceId = lastSequenceId + 1; @@ -517,6 +517,25 @@ public synchronized void close() { } } + /** + * Frees the buffered memory and the memory handed over with {@code retainMemory=true}, as a + * fallback for blocks the caller has not released yet. + */ + private void freeAllRetainedMemory() { + long totalRetainedSize = bufferRetainedSizeInBytes + pendingRetainedSizeInBytes; + if (totalRetainedSize > 0) { + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + totalRetainedSize); + bufferRetainedSizeInBytes = 0; + pendingRetainedSizeInBytes = 0; + } + } + @Override public boolean isFinished() { return remoteTsBlockedConsumedUp(); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java index 1a7b96d7728c0..4a5fa22e1c462 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java @@ -43,12 +43,14 @@ public interface IQueryExecution { Optional getBatchResult() throws IoTDBException; - Optional getByteBufferBatchResult() throws IoTDBException; - /** - * Returns the holder of the serialized TsBlock that is partially sent to the client. It is used - * to continue the fragmented transfer of one oversized TsBlock in the following RPC response. + * Gets the next serialized TsBlock of the result. If {@code retainMemory} is true, the memory + * that the returned block retains in the memory pool is not freed when the block is taken out of + * the result handle. The caller then hands the block over to {@link #getPendingResultFragment()}, + * which releases the memory once the block has been fully sent. */ + Optional getByteBufferBatchResult(boolean retainMemory) throws IoTDBException; + PendingResultFragment getPendingResultFragment(); boolean hasNextResult(); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java index ebb020ae8058d..21b988d2a77f5 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java @@ -25,32 +25,44 @@ * The tail of a serialized TsBlock that did not fit into the previous RPC response and is waiting * to be sent in the following response(s). The block is already serialized and removed from the * result handle, so this fragment only holds a reference to it and never copies the payload. + * + *

The memory the block retains stays reserved in the memory pool until the block is fully sent. + * The owner binds a {@code memoryReleaser} which is invoked by {@link #clear()}, so callers only + * interact with this holder and never release the memory themselves. */ public class PendingResultFragment { + private final Runnable memoryReleaser; + private ByteBuffer block; private int nextOffset; + public PendingResultFragment(Runnable memoryReleaser) { + this.memoryReleaser = memoryReleaser; + } + /** Records that {@code block} has been sent up to {@code nextOffset} bytes. */ - public synchronized void reset(ByteBuffer block, int nextOffset) { - if (block != this.block) { - this.block = block; - } + public void reset(ByteBuffer block, int nextOffset) { + this.block = block; this.nextOffset = nextOffset; } - /** Returns the block that is partially sent, or null if no fragment is pending. */ - public synchronized ByteBuffer getBlock() { + public ByteBuffer getBlock() { return block; } - /** Returns the start offset of the next fragment. */ - public synchronized int getNextOffset() { + public int getNextOffset() { return nextOffset; } - public synchronized void clear() { + /** + * Discards the pending fragment and releases the memory of the blocks that have been fully sent. + * It is also called right after a complete block has been added to a response, when nothing is + * pending. Calling it repeatedly is a no-op. + */ + public void clear() { block = null; nextOffset = 0; + memoryReleaser.run(); } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java index a1ff15a98a1b9..97727be83ce5f 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java @@ -109,7 +109,8 @@ public class QueryExecution implements IQueryExecution { private final AtomicBoolean resultHandleCleanUp; // the serialized TsBlock that is partially sent to the client and waits for the next RPC - private final PendingResultFragment pendingResultFragment = new PendingResultFragment(); + private final PendingResultFragment pendingResultFragment = + new PendingResultFragment(this::releaseResultHandleRetainedMemory); private final AtomicBoolean stopped; @@ -440,6 +441,9 @@ public void stopAndCleanup(Throwable t) { /** Release the resources that current QueryExecution hold with a specified exception */ private void releaseResource(Throwable t) { context.recordDeviceEntryDiskIOMetricsOnRelease(); + // the query will not serve any more fetch requests, release the pending fragment's memory and + // make sure a retried execution does not continue a stale fragment + pendingResultFragment.clear(); // close ResultHandle to unblock client's getResult request // Actually, we should not close the ResultHandle when the QueryExecution is Finished. // There are only two scenarios where the ResultHandle should be closed: @@ -598,8 +602,8 @@ private TsBlock getDeserializedTsBlock() { } @Override - public Optional getByteBufferBatchResult() throws IoTDBException { - return getResult(this::getSerializedTsBlock); + public Optional getByteBufferBatchResult(boolean retainMemory) throws IoTDBException { + return getResult(() -> resultHandle.getSerializedTsBlock(retainMemory)); } @Override @@ -607,8 +611,10 @@ public PendingResultFragment getPendingResultFragment() { return pendingResultFragment; } - private ByteBuffer getSerializedTsBlock() throws IoTDBException { - return resultHandle.getSerializedTsBlock(); + private void releaseResultHandleRetainedMemory() { + if (resultHandle != null) { + resultHandle.freeRetainedMemory(); + } } /** diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java index cfc8cfdeb98ac..621fd329d3b20 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java @@ -116,8 +116,8 @@ public class ConfigExecution implements IQueryExecution { private final IConfigTask task; private final IConfigTaskExecutor configTaskExecutor; - // the serialized TsBlock that is partially sent to the client and waits for the next RPC - private final PendingResultFragment pendingResultFragment = new PendingResultFragment(); + // the result set is not reserved from the memory pool, so nothing is released on clear + private final PendingResultFragment pendingResultFragment = new PendingResultFragment(() -> {}); private final StatementType statementType; private long totalExecutionTime; @@ -281,7 +281,8 @@ public Optional getBatchResult() { } @Override - public Optional getByteBufferBatchResult() throws IoTDBException { + public Optional getByteBufferBatchResult(boolean retainMemory) throws IoTDBException { + // the result set is not reserved from the memory pool, so there is no memory to retain if (!resultSetConsumed) { resultSetConsumed = true; try { diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java index 7a4919446cf92..a325fc671a15e 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java @@ -77,7 +77,8 @@ public synchronized TsBlock receive() { } @Override - public synchronized ByteBuffer getSerializedTsBlock() throws IoTDBException { + public synchronized ByteBuffer getSerializedTsBlock(boolean retainMemory) throws IoTDBException { + // serialized blocks are not reserved from the memory pool, so retainMemory is ignored hasNext = false; if (result.isEmpty()) { return null; @@ -90,6 +91,11 @@ public synchronized ByteBuffer getSerializedTsBlock() throws IoTDBException { } } + @Override + public void freeRetainedMemory() { + // nothing is reserved from the memory pool + } + @Override public synchronized boolean isFinished() { return !hasNext; diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java index cb36050adba7a..a398d2613e922 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java @@ -253,18 +253,12 @@ public static Pair, Boolean> convertQueryResultByFetchSize( long memorySize = 0; List res = new ArrayList<>(); while (rowCount < fetchSize && memorySize < MAX_RETURN_SIZE) { - Optional optionalByteBuffer = queryExecution.getByteBufferBatchResult(); + Optional optionalByteBuffer = queryExecution.getByteBufferBatchResult(false); if (!optionalByteBuffer.isPresent()) { break; } ByteBuffer byteBuffer = optionalByteBuffer.get(); - byteBuffer.mark(); - int valueColumnCount = byteBuffer.getInt(); - for (int i = 0; i < valueColumnCount; i++) { - byteBuffer.get(); - } - int positionCount = byteBuffer.getInt(); - byteBuffer.reset(); + int positionCount = readPositionCount(byteBuffer); if (positionCount != 0) { res.add(byteBuffer); } @@ -279,11 +273,6 @@ public static Pair, Boolean> convertQueryResultByFetchSize( * maxPayloadSize} is cut into fragments: the response only carries the bytes that fit into the * payload, and the remaining bytes are kept in {@link PendingResultFragment} on the query * execution, to be sent by the following request carrying the returned offset. - * - * @param queryExecution used to get TsBlock from and judge whether there is more data. - * @param fetchSize wanted row size - * @param requestOffset the offset of the pending fragment to continue, 0 if there is none - * @param maxPayloadSize the maximum total bytes of {@link ResultChunk#getQueryResult()} */ public static ResultChunk convertQueryResultByFetchSize( IQueryExecution queryExecution, int fetchSize, int requestOffset, int maxPayloadSize) @@ -294,7 +283,6 @@ public static ResultChunk convertQueryResultByFetchSize( int rowCount = 0; long memorySize = 0; - // Continue the oversized TsBlock left by the previous response first PendingResultFragment pendingFragment = queryExecution.getPendingResultFragment(); ByteBuffer pendingBlock = pendingFragment.getBlock(); if (pendingBlock != null) { @@ -311,10 +299,9 @@ public static ResultChunk convertQueryResultByFetchSize( res.add(slice(pendingBlock, nextOffset, fragmentSize)); if (nextOffset + fragmentSize == pendingBlock.limit()) { pendingFragment.clear(); - // The fragmented block is now complete. Do not fetch new blocks in the same response, - // otherwise the response would carry the tail of the big block plus up to fetchSize new - // rows, whose row count is not accounted for. One response either starts/continues a - // fragmented block or delivers new blocks, never both. + // Do not fetch new blocks in the same response, otherwise the response would carry the + // tail of the big block plus up to fetchSize new rows, whose row count is not accounted + // for. One response either continues a fragmented block or delivers new blocks. return ResultChunk.complete(res, !queryExecution.hasNextResult()); } pendingFragment.reset(pendingBlock, nextOffset + fragmentSize); @@ -328,16 +315,16 @@ public static ResultChunk convertQueryResultByFetchSize( } while (rowCount < fetchSize && memorySize < MAX_RETURN_SIZE && remainingPayloadSize > 0) { - Optional optionalByteBuffer = queryExecution.getByteBufferBatchResult(); + Optional optionalByteBuffer = queryExecution.getByteBufferBatchResult(true); if (!optionalByteBuffer.isPresent()) { break; } ByteBuffer byteBuffer = optionalByteBuffer.get(); int positionCount = readPositionCount(byteBuffer); if (positionCount == 0) { + pendingFragment.clear(); continue; } - // offsets kept in PendingResultFragment are absolute positions of the buffer int blockStart = byteBuffer.position(); int blockSize = byteBuffer.remaining(); rowCount += positionCount; @@ -345,8 +332,8 @@ public static ResultChunk convertQueryResultByFetchSize( res.add(byteBuffer); remainingPayloadSize -= blockSize; memorySize += blockSize; + pendingFragment.clear(); } else { - // The block exceeds the remaining payload, send a leading fragment and keep the rest res.add(slice(byteBuffer, blockStart, remainingPayloadSize)); pendingFragment.reset(byteBuffer, blockStart + remainingPayloadSize); return ResultChunk.fragment( @@ -367,7 +354,6 @@ private static int readPositionCount(ByteBuffer byteBuffer) { return positionCount; } - /** Returns a view of {@code [offset, offset + length)} bytes of the buffer without copying. */ private static ByteBuffer slice(ByteBuffer buffer, int offset, int length) { ByteBuffer duplicate = buffer.duplicate(); duplicate.position(offset); @@ -391,7 +377,7 @@ private ResultChunk( this.finished = finished; } - /** The response ends with a fragment, which starts at {@code nextOffset} of its TsBlock. */ + /** Creates a chunk whose last element is a fragment starting at {@code nextOffset}. */ public static ResultChunk fragment( List queryResult, int nextOffset, int totalLength, boolean finished) { return new ResultChunk(queryResult, nextOffset, totalLength, finished); @@ -413,7 +399,6 @@ public int getNextOffset() { return nextOffset; } - /** The total serialized length of the last block, set only when the fragment starts it. */ public boolean hasTotalLength() { return totalLength >= 0; } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java index b8c53352c09a5..e277418c2a328 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java @@ -298,7 +298,7 @@ private IQueryExecution mockQueryExecution(boolean hasNextResult) throws Excepti IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); Mockito.when(queryExecution.getQueryId()).thenReturn("query"); // the mock carries no buffered batch, only the "is there anything left" flag - Mockito.when(queryExecution.getByteBufferBatchResult()).thenReturn(Optional.empty()); + Mockito.when(queryExecution.getByteBufferBatchResult(false)).thenReturn(Optional.empty()); Mockito.when(queryExecution.hasNextResult()).thenReturn(hasNextResult); return queryExecution; } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java index 95c9b810ce195..f61266b198a44 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java @@ -1079,4 +1079,120 @@ public void testForceClose() { Mockito.verify(mockSourceHandleListener, Mockito.timeout(10_0000).times(1)) .onAborted(sourceHandle); } + + @Test + public void testRetainedMemoryIsFreedWhenTheHandedOverBlockIsReleased() throws Exception { + final String queryId = "q0"; + final String localPlanNodeId = "exchange_0"; + final HandleWithPool handleWithPool = + createSourceHandleWithOneFetchedBlock(queryId, localPlanNodeId); + final SourceHandle sourceHandle = handleWithPool.handle; + final MemoryPool memoryPool = handleWithPool.memoryPool; + final String fragmentInstanceId = + FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId( + sourceHandle.getLocalFragmentInstanceId()); + + // The block is taken out of the buffer, but its memory stays reserved in the pool + Assert.assertNotNull(sourceHandle.getSerializedTsBlock(true)); + Assert.assertEquals(0L, sourceHandle.getBufferRetainedSizeInBytes()); + Mockito.verify(memoryPool, Mockito.never()) + .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); + + // The caller releases the memory once the block is fully sent + sourceHandle.freeRetainedMemory(); + Mockito.verify(memoryPool, Mockito.times(1)) + .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); + + // Releasing again is a no-op + sourceHandle.freeRetainedMemory(); + Mockito.verify(memoryPool, Mockito.times(1)) + .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); + } + + @Test + public void testAbortReleasesTheHandedOverMemory() throws Exception { + final String queryId = "q0"; + final String localPlanNodeId = "exchange_0"; + final HandleWithPool handleWithPool = + createSourceHandleWithOneFetchedBlock(queryId, localPlanNodeId); + final SourceHandle sourceHandle = handleWithPool.handle; + final MemoryPool memoryPool = handleWithPool.memoryPool; + final String fragmentInstanceId = + FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId( + sourceHandle.getLocalFragmentInstanceId()); + + Assert.assertNotNull(sourceHandle.getSerializedTsBlock(true)); + Mockito.verify(memoryPool, Mockito.never()) + .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); + + // abort frees the memory that was not released by the caller (fallback path) + sourceHandle.abort(); + Mockito.verify(memoryPool, Mockito.times(1)) + .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); + + // a late release after abort must not free the same memory twice + sourceHandle.freeRetainedMemory(); + Mockito.verify(memoryPool, Mockito.times(1)) + .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); + } + + private static final class HandleWithPool { + private final SourceHandle handle; + private final MemoryPool memoryPool; + + private HandleWithPool(SourceHandle handle, MemoryPool memoryPool) { + this.handle = handle; + this.memoryPool = memoryPool; + } + } + + /** Creates a SourceHandle whose single pending block has already been fetched. */ + private HandleWithPool createSourceHandleWithOneFetchedBlock( + String queryId, String localPlanNodeId) throws Exception { + final TEndPoint remoteEndpoint = + new TEndPoint("remote", IoTDBDescriptor.getInstance().getConfig().getMppDataExchangePort()); + final TFragmentInstanceId remoteFragmentInstanceId = new TFragmentInstanceId(queryId, 1, "0"); + final TFragmentInstanceId localFragmentInstanceId = new TFragmentInstanceId(queryId, 0, "0"); + + final LocalMemoryManager mockLocalMemoryManager = Mockito.mock(LocalMemoryManager.class); + final MemoryPool mockMemoryPool = Utils.createMockNonBlockedMemoryPool(); + Mockito.when(mockLocalMemoryManager.getQueryPool()).thenReturn(mockMemoryPool); + final IClientManager mockClientManager = + Mockito.mock(IClientManager.class); + final SyncDataNodeMPPDataExchangeServiceClient mockClient = + Mockito.mock(SyncDataNodeMPPDataExchangeServiceClient.class); + Mockito.when(mockClientManager.borrowClient(remoteEndpoint)).thenReturn(mockClient); + Mockito.doAnswer( + invocation -> { + TGetDataBlockRequest req = invocation.getArgument(0); + List byteBuffers = new ArrayList<>(); + for (int i = req.getStartSequenceId(); i < req.getEndSequenceId(); i++) { + byteBuffers.add(ByteBuffer.allocate(0)); + } + return new TGetDataBlockResponse(byteBuffers); + }) + .when(mockClient) + .getDataBlock(Mockito.any(TGetDataBlockRequest.class)); + + final SourceHandle sourceHandle = + new SourceHandle( + remoteEndpoint, + remoteFragmentInstanceId, + localFragmentInstanceId, + localPlanNodeId, + 0, + mockLocalMemoryManager, + Executors.newSingleThreadExecutor(), + Utils.createMockTsBlockSerde(MOCK_TSBLOCK_SIZE), + Mockito.mock(SourceHandleListener.class), + mockClientManager); + + sourceHandle.isBlocked(); + sourceHandle.updatePendingDataBlockInfo(0, List.of(MOCK_TSBLOCK_SIZE)); + Mockito.verify(mockClient, Mockito.timeout(10_000).times(1)) + .getDataBlock(Mockito.any(TGetDataBlockRequest.class)); + Assert.assertTrue(sourceHandle.isBlocked().isDone()); + Assert.assertEquals(MOCK_TSBLOCK_SIZE, sourceHandle.getBufferRetainedSizeInBytes()); + return new HandleWithPool(sourceHandle, mockMemoryPool); + } } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java index ca22be2e6be51..a21757411cfcd 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java @@ -1809,7 +1809,7 @@ static class FakeQueryExecution implements IQueryExecution { private final long startTime; private final String queryId; private final String sql; - private final PendingResultFragment pendingResultFragment = new PendingResultFragment(); + private final PendingResultFragment pendingResultFragment = new PendingResultFragment(() -> {}); FakeQueryExecution(long startTime, String queryId, String sql) { this.startTime = startTime; @@ -1891,7 +1891,7 @@ public long getTimeout() { } @Override - public Optional getByteBufferBatchResult() { + public Optional getByteBufferBatchResult(boolean retainMemory) { return Optional.empty(); } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java index 3ea0cce94b690..876d792593686 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java @@ -42,6 +42,7 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -157,7 +158,7 @@ private IQueryExecution buildQueryExecution() throws IoTDBException, IOException TsBlock tsBlock = buildTsBlock(); Mockito.when(queryExecution.getBatchResult()) .thenReturn(Optional.of(tsBlock), Optional.empty()); - Mockito.when(queryExecution.getByteBufferBatchResult()) + Mockito.when(queryExecution.getByteBufferBatchResult(false)) .thenReturn(Optional.of(new TsBlockSerde().serialize(tsBlock)), Optional.empty()); return queryExecution; } @@ -292,10 +293,11 @@ public void testConvertQueryResultByFetchSizeWithSingleOversizedBlock() // one byte smaller than the block so that the block has to be split into two fragments int payloadSize = totalLength - 1; - PendingResultFragment pendingFragment = new PendingResultFragment(); IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); + Runnable memoryReleaser = Mockito.mock(Runnable.class); + PendingResultFragment pendingFragment = new PendingResultFragment(memoryReleaser); Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); - Mockito.when(queryExecution.getByteBufferBatchResult()) + Mockito.when(queryExecution.getByteBufferBatchResult(true)) .thenReturn(Optional.of(serialized.duplicate()), Optional.empty()); Mockito.when(queryExecution.hasNextResult()).thenReturn(false); @@ -309,6 +311,8 @@ public void testConvertQueryResultByFetchSizeWithSingleOversizedBlock() assertEquals(1, firstChunk.getQueryResult().size()); assertEquals(payloadSize, firstChunk.getQueryResult().get(0).remaining()); assertTrue(pendingFragment.getBlock() != null); + // the memory of the pending block is still retained + Mockito.verify(memoryReleaser, Mockito.never()).run(); QueryDataSetUtils.ResultChunk secondChunk = QueryDataSetUtils.convertQueryResultByFetchSize( @@ -317,6 +321,8 @@ public void testConvertQueryResultByFetchSizeWithSingleOversizedBlock() assertFalse(secondChunk.hasNextOffset()); assertFalse(secondChunk.hasTotalLength()); assertTrue(pendingFragment.getBlock() == null); + // the memory is released once the fragmented block has been fully sent + Mockito.verify(memoryReleaser, Mockito.times(1)).run(); TsBlock result = deserialize(firstChunk.getQueryResult().get(0), secondChunk.getQueryResult()); assertEquals(2, result.getPositionCount()); @@ -334,10 +340,11 @@ public void testConvertQueryResultByFetchSizeWithTrailingFragment() ByteBuffer smallBlock = serialized.duplicate(); int payloadSize = smallBlock.remaining() + blockSize / 2; - PendingResultFragment pendingFragment = new PendingResultFragment(); IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); + Runnable memoryReleaser = Mockito.mock(Runnable.class); + PendingResultFragment pendingFragment = new PendingResultFragment(memoryReleaser); Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); - Mockito.when(queryExecution.getByteBufferBatchResult()) + Mockito.when(queryExecution.getByteBufferBatchResult(true)) .thenReturn(Optional.of(smallBlock), Optional.of(serialized.duplicate()), Optional.empty()); Mockito.when(queryExecution.hasNextResult()).thenReturn(false); @@ -350,6 +357,8 @@ public void testConvertQueryResultByFetchSizeWithTrailingFragment() assertEquals(blockSize / 2, firstChunk.getQueryResult().get(1).remaining()); assertTrue(firstChunk.hasTotalLength()); assertEquals(blockSize, firstChunk.getTotalLength()); + // the complete block is released at once, the fragmented one is released when it completes + Mockito.verify(memoryReleaser, Mockito.times(1)).run(); QueryDataSetUtils.ResultChunk secondChunk = QueryDataSetUtils.convertQueryResultByFetchSize( @@ -359,6 +368,7 @@ public void testConvertQueryResultByFetchSizeWithTrailingFragment() // only the rest of the oversized block remains assertEquals(1, secondChunk.getQueryResult().size()); assertEquals(blockSize - blockSize / 2, secondChunk.getQueryResult().get(0).remaining()); + Mockito.verify(memoryReleaser, Mockito.times(2)).run(); } @Test @@ -368,10 +378,11 @@ public void testContinuationCompletionDoesNotFetchNewBlocks() throws IoTDBExcept int blockSize = serialized.remaining(); int payloadSize = blockSize / 2; - PendingResultFragment pendingFragment = new PendingResultFragment(); IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); + Runnable memoryReleaser = Mockito.mock(Runnable.class); + PendingResultFragment pendingFragment = new PendingResultFragment(memoryReleaser); Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); - Mockito.when(queryExecution.getByteBufferBatchResult()) + Mockito.when(queryExecution.getByteBufferBatchResult(true)) .thenReturn( Optional.of(serialized.duplicate()), Optional.of(serialized.duplicate()), @@ -382,6 +393,7 @@ public void testContinuationCompletionDoesNotFetchNewBlocks() throws IoTDBExcept QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 0, payloadSize); assertFalse(firstChunk.isFinished()); assertEquals(payloadSize, firstChunk.getNextOffset()); + Mockito.verify(memoryReleaser, Mockito.never()).run(); // The response that completes the fragmented block must not fetch new blocks, otherwise its // row count would exceed fetchSize (the tail of the big block is not accounted). @@ -393,6 +405,7 @@ public void testContinuationCompletionDoesNotFetchNewBlocks() throws IoTDBExcept assertEquals(1, continuationChunk.getQueryResult().size()); assertEquals(blockSize - payloadSize, continuationChunk.getQueryResult().get(0).remaining()); assertTrue(pendingFragment.getBlock() == null); + Mockito.verify(memoryReleaser, Mockito.times(1)).run(); // New blocks are delivered by the following request only QueryDataSetUtils.ResultChunk nextChunk = @@ -401,11 +414,36 @@ public void testContinuationCompletionDoesNotFetchNewBlocks() throws IoTDBExcept assertFalse(nextChunk.hasNextOffset()); assertEquals(1, nextChunk.getQueryResult().size()); assertEquals(blockSize, nextChunk.getQueryResult().get(0).remaining()); + Mockito.verify(memoryReleaser, Mockito.times(2)).run(); + } + + @Test + public void testEmptyBlockReleasesItsMemory() throws IoTDBException, IOException { + TsBlock emptyTsBlock = new TsBlockBuilder(Collections.singletonList(TSDataType.INT64)).build(); + ByteBuffer emptyBlock = new TsBlockSerde().serialize(emptyTsBlock); + TsBlock tsBlock = buildTsBlock(); + ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock); + + IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); + Runnable memoryReleaser = Mockito.mock(Runnable.class); + Mockito.when(queryExecution.getPendingResultFragment()) + .thenReturn(new PendingResultFragment(memoryReleaser)); + Mockito.when(queryExecution.getByteBufferBatchResult(true)) + .thenReturn(Optional.of(emptyBlock), Optional.of(serialized), Optional.empty()); + Mockito.when(queryExecution.hasNextResult()).thenReturn(false); + + QueryDataSetUtils.ResultChunk chunk = + QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, 10, 0, serialized.remaining()); + assertTrue(chunk.isFinished()); + assertEquals(1, chunk.getQueryResult().size()); + // the empty block and the complete block are both released immediately + Mockito.verify(memoryReleaser, Mockito.times(2)).run(); } @Test(expected = IllegalArgumentException.class) public void testConvertQueryResultByFetchSizeRejectsMismatchedOffset() throws IoTDBException { - PendingResultFragment pendingFragment = new PendingResultFragment(); + PendingResultFragment pendingFragment = new PendingResultFragment(() -> {}); pendingFragment.reset(ByteBuffer.wrap(new byte[10]), 5); IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); @@ -417,7 +455,8 @@ public void testConvertQueryResultByFetchSizeRejectsMismatchedOffset() throws Io public void testConvertQueryResultByFetchSizeRejectsOffsetWithoutPendingFragment() throws IoTDBException { IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); - Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(new PendingResultFragment()); + Mockito.when(queryExecution.getPendingResultFragment()) + .thenReturn(new PendingResultFragment(() -> {})); QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 5, 8); } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java new file mode 100644 index 0000000000000..e7bb00011e68a --- /dev/null +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java @@ -0,0 +1,255 @@ +/* + * 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.utils; + +import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution; +import org.apache.iotdb.db.queryengine.plan.execution.PendingResultFragment; +import org.apache.iotdb.rpc.IoTDBRpcDataSet; +import org.apache.iotdb.rpc.RpcUtils; +import org.apache.iotdb.rpc.TSStatusCode; +import org.apache.iotdb.service.rpc.thrift.IClientRPCService; +import org.apache.iotdb.service.rpc.thrift.TSCloseOperationReq; +import org.apache.iotdb.service.rpc.thrift.TSFetchResultsReq; +import org.apache.iotdb.service.rpc.thrift.TSFetchResultsResp; + +import org.apache.tsfile.common.conf.TSFileConfig; +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.read.common.block.TsBlock; +import org.apache.tsfile.read.common.block.TsBlockBuilder; +import org.apache.tsfile.read.common.block.column.TsBlockSerde; +import org.apache.tsfile.utils.Binary; +import org.junit.Test; +import org.mockito.Mockito; + +import java.nio.ByteBuffer; +import java.time.ZoneId; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * End-to-end verification of the fragmented query result protocol: the server side produces + * bounded-payload chunks via {@link QueryDataSetUtils} and the client side ({@code + * IoTDBRpcDataSet}) assembles them back into the original rows. + */ +public class ResultChunkingEndToEndTest { + + private static final String COLUMN = "s1"; + private static final String TEXT_COLUMN = "s2"; + private static final int LARGE_VALUE_SIZE = 512 * 1024; + private static final int MAX_PAYLOAD_SIZE = 256 * 1024; + + @Test(timeout = 60_000) + public void testFragmentedResultRoundTrip() throws Exception { + TsBlock tsBlock = buildTsBlockWithLargeValue(); + ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock); + int totalLength = serialized.remaining(); + assertTrue( + "the block must be larger than the payload to trigger fragmentation", + totalLength > MAX_PAYLOAD_SIZE); + + ServerSideSimulation server = + new ServerSideSimulation(serialized.duplicate(), tsBlock.getPositionCount(), totalLength); + + IClientRPCService.Iface client = mock(IClientRPCService.Iface.class); + when(client.closeOperation(any(TSCloseOperationReq.class))) + .thenReturn(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS)); + when(client.fetchResultsV2(any(TSFetchResultsReq.class))) + .thenAnswer(invocation -> server.handleFetchResults(invocation.getArgument(0))); + + IoTDBRpcDataSet dataSet = + new IoTDBRpcDataSet( + "", + List.of(COLUMN, TEXT_COLUMN), + List.of(TSDataType.INT64.name(), TSDataType.TEXT.name()), + null, + true, + true, + 1L, + 2L, + client, + 3L, + null, + 100, + 0, + ZoneId.systemDefault(), + RpcUtils.DEFAULT_TIME_FORMAT, + 1_000_000, + true, + new ArrayList<>(Arrays.asList(0, 1))); + + // two rows: [time=1, v=10, text="small"] and [time=2, v=20, text=<512KB>] + assertTrue(dataSet.next()); + assertEquals(10L, dataSet.getLong(COLUMN)); + assertEquals("small", dataSet.getString(TEXT_COLUMN)); + assertTrue(dataSet.next()); + assertEquals(20L, dataSet.getLong(COLUMN)); + assertEquals(LARGE_VALUE_SIZE, dataSet.getString(TEXT_COLUMN).length()); + assertFalse(dataSet.next()); + + assertTrue( + "the result must have been transferred in several responses", server.rpcCount.get() > 1); + for (int payload : server.payloadSizes) { + assertTrue( + String.format("one response payload %d exceeds the bound %d", payload, MAX_PAYLOAD_SIZE), + payload <= MAX_PAYLOAD_SIZE); + } + // every fragmented block was fully released + assertTrue(server.releaseCount.get() > 0); + assertEquals("the pending fragment must be cleared in the end", null, server.getPendingBlock()); + } + + @Test(timeout = 60_000) + public void testSmallResultUsesOneResponseWithoutFragment() throws Exception { + TsBlockBuilder builder = new TsBlockBuilder(List.of(TSDataType.INT64)); + builder.getTimeColumnBuilder().writeLong(1L); + builder.getColumnBuilder(0).writeLong(10L); + builder.declarePosition(); + TsBlock tsBlock = builder.build(); + ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock); + + ServerSideSimulation server = + new ServerSideSimulation(serialized.duplicate(), tsBlock.getPositionCount(), 0); + + IClientRPCService.Iface client = mock(IClientRPCService.Iface.class); + when(client.closeOperation(any(TSCloseOperationReq.class))) + .thenReturn(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS)); + when(client.fetchResultsV2(any(TSFetchResultsReq.class))) + .thenAnswer(invocation -> server.handleFetchResults(invocation.getArgument(0))); + // the whole result fits into the execute response + IoTDBRpcDataSet dataSet = + new IoTDBRpcDataSet( + "", + List.of(COLUMN), + List.of(TSDataType.INT64.name()), + null, + true, + false, + 1L, + 2L, + client, + 3L, + List.of(serialized.duplicate()), + 100, + 0, + ZoneId.systemDefault(), + RpcUtils.DEFAULT_TIME_FORMAT, + 1_000_000, + true, + new ArrayList<>(List.of(0))); + + assertTrue(dataSet.next()); + assertEquals(10L, dataSet.getLong(COLUMN)); + assertFalse(dataSet.next()); + assertEquals("a small result must not trigger any fetch", 0, server.rpcCount.get()); + } + + /** Mimics ClientRPCServiceImpl#fetchResultsV2 with bounded payloads. */ + private static class ServerSideSimulation { + private final ByteBuffer serialized; + private final int positionCount; + private final int totalLength; + + private final AtomicInteger rpcCount = new AtomicInteger(); + private final AtomicInteger releaseCount = new AtomicInteger(); + private final List payloadSizes = new ArrayList<>(); + private final List remainingBlocks = new ArrayList<>(); + private final PendingResultFragment pendingFragment = + new PendingResultFragment(releaseCount::incrementAndGet); + private final IQueryExecution queryExecution; + + private ServerSideSimulation(ByteBuffer serialized, int positionCount, int totalLength) + throws Exception { + this.serialized = serialized; + this.positionCount = positionCount; + this.totalLength = totalLength; + this.remainingBlocks.add(serialized); + this.queryExecution = Mockito.mock(IQueryExecution.class); + Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); + Mockito.when(queryExecution.getByteBufferBatchResult(true)) + .thenAnswer( + invocation -> + remainingBlocks.isEmpty() + ? Optional.empty() + : Optional.of(remainingBlocks.remove(0))); + Mockito.when(queryExecution.hasNextResult()) + .thenAnswer( + invocation -> !remainingBlocks.isEmpty() || pendingFragment.getBlock() != null); + } + + private TSFetchResultsResp handleFetchResults(TSFetchResultsReq req) throws Exception { + rpcCount.incrementAndGet(); + QueryDataSetUtils.ResultChunk chunk = + QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, + Math.max(positionCount, 1), + req.isSetOffset() ? req.getOffset() : 0, + MAX_PAYLOAD_SIZE); + + int payloadSize = 0; + for (ByteBuffer block : chunk.getQueryResult()) { + payloadSize += block.remaining(); + } + payloadSizes.add(payloadSize); + + TSFetchResultsResp resp = + new TSFetchResultsResp(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS), true, true); + resp.setQueryResult(chunk.getQueryResult()); + resp.setMoreData(!chunk.isFinished()); + if (chunk.hasNextOffset()) { + resp.setOffset(chunk.getNextOffset()); + if (chunk.hasTotalLength()) { + resp.setTotalLength(chunk.getTotalLength()); + } + } + return resp; + } + + private ByteBuffer getPendingBlock() { + return pendingFragment.getBlock(); + } + } + + /** [time=1, v=10, text="small"], [time=2, v=20, text=]. */ + private static TsBlock buildTsBlockWithLargeValue() { + byte[] largeValue = new byte[LARGE_VALUE_SIZE]; + Arrays.fill(largeValue, (byte) 'x'); + + TsBlockBuilder builder = new TsBlockBuilder(Arrays.asList(TSDataType.INT64, TSDataType.TEXT)); + builder.getTimeColumnBuilder().writeLong(1L); + builder.getColumnBuilder(0).writeLong(10L); + builder.getColumnBuilder(1).writeBinary(new Binary("small", TSFileConfig.STRING_CHARSET)); + builder.declarePosition(); + builder.getTimeColumnBuilder().writeLong(2L); + builder.getColumnBuilder(0).writeLong(20L); + builder.getColumnBuilder(1).writeBinary(new Binary(largeValue)); + builder.declarePosition(); + return builder.build(); + } +} From 4fa419586a9dfc4752f72d06e778691389b1a03f Mon Sep 17 00:00:00 2001 From: Weihao Li <18110526956@163.com> Date: Mon, 16 Nov 2026 16:53:50 +0800 Subject: [PATCH 04/11] Account for pending fragments on the local result path and charge read quota by whole blocks - SharedTsBlockQueue.remove(retainMemory) keeps the reservation of a block that outlives the RPC in PendingResultFragment; freeRetainedMemory() releases it and close/abort free the buffered and handed-over memory together. - LocalSourceHandle delegates the retain/release to its queue, so single-node deployments keep fragmented transfers accounted in the memory pool. - OperationQuota only exposes addReadResult(long bytes); responses charge a block whose fragmentation starts in them by its totalLength, so a fragmented result is charged exactly once against the read quota. --- .../thrift/impl/ClientRPCServiceImpl.java | 30 +++++- .../exchange/SharedTsBlockQueue.java | 91 ++++++++++------ .../exchange/source/LocalSourceHandle.java | 13 ++- .../rescon/quotas/DefaultOperationQuota.java | 14 +-- .../rescon/quotas/NoopOperationQuota.java | 5 +- .../rescon/quotas/OperationQuota.java | 5 +- .../exchange/LocalSourceHandleTest.java | 102 ++++++++++++++++++ 7 files changed, 196 insertions(+), 64 deletions(-) diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java index 67d59b59ddfe7..889e36236c01a 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java @@ -363,6 +363,26 @@ private static boolean fillQueryResult( return chunk.isFinished(); } + /** + * The last element being a fragment means the block just starts, so it is charged by {@code + * totalLength}: its tails sent by the following responses are not charged again. + */ + private static long chargedResultBytes(TSExecuteStatementResp resp) { + List blocks = resp.getQueryResult(); + if (blocks == null) { + return 0L; + } + long size = 0L; + for (int i = 0, last = blocks.size() - 1; i <= last; i++) { + if (resp.isSetOffset() && i == last) { + size += resp.isSetTotalLength() ? resp.getTotalLength() : 0L; + } else { + size += blocks.get(i).remaining(); + } + } + return size; + } + /** * Returns the maximum payload size of one response carrying query results, or 0 if the session's * client cannot assemble fragmented results. The payload bound is shared with the MPP data @@ -555,7 +575,7 @@ private TSExecuteStatementResp executeStatementInternal( getMaxResultPayloadSize(clientSession)); resp.setMoreData(!finished); if (quota != null) { - quota.addReadResult(resp.getQueryResult()); + quota.addReadResult(chargedResultBytes(resp)); } // Should return SUCCESS_MESSAGE for insert into query if (queryExecution.getQueryType() == QueryType.READ_WRITE) { @@ -829,7 +849,7 @@ private TSExecuteStatementResp executeRawDataQueryInternal( setResult.apply( resp, queryExecution, req.fetchSize, getMaxResultPayloadSize(clientSession)); resp.setMoreData(!finished); - quota.addReadResult(resp.getQueryResult()); + quota.addReadResult(chargedResultBytes(resp)); } else { resp = RpcUtils.getTSExecuteStatementResp(result.status); } @@ -927,7 +947,7 @@ private TSExecuteStatementResp executeLastDataQueryInternal( setResult.apply( resp, queryExecution, req.fetchSize, getMaxResultPayloadSize(clientSession)); resp.setMoreData(!finished); - quota.addReadResult(resp.getQueryResult()); + quota.addReadResult(chargedResultBytes(resp)); } else { resp = RpcUtils.getTSExecuteStatementResp(result.status); } @@ -1027,7 +1047,7 @@ private TSExecuteStatementResp executeAggregationQueryInternal( setResult.apply( resp, queryExecution, req.fetchSize, getMaxResultPayloadSize(clientSession)); resp.setMoreData(!finished); - quota.addReadResult(resp.getQueryResult()); + quota.addReadResult(chargedResultBytes(resp)); } else { resp = RpcUtils.getTSExecuteStatementResp(result.status); } @@ -1522,7 +1542,7 @@ public TSExecuteStatementResp executeFastLastDataQueryForOneDeviceV2( SELECT_RESULT.apply( resp, queryExecution, req.fetchSize, getMaxResultPayloadSize(clientSession)); resp.setMoreData(!finished); - quota.addReadResult(resp.getQueryResult()); + quota.addReadResult(chargedResultBytes(resp)); } else { resp = RpcUtils.getTSExecuteStatementResp(result.status); } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java index 0d8423803618d..aac8064f58ba0 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java @@ -67,6 +67,13 @@ public class SharedTsBlockQueue { private long bufferRetainedSizeInBytes = 0L; + /** + * The memory in bytes taken out with {@code retainMemory=true}. Not counted in {@link + * #bufferRetainedSizeInBytes}: the block is no longer buffered here, so it must not block the + * producer. + */ + private long pendingRetainedSizeInBytes = 0L; + private final Queue> queue = new LinkedList<>(); private SettableFuture blocked = SettableFuture.create(); @@ -199,13 +206,19 @@ public void setNoMoreTsBlocks(boolean noMoreTsBlocks) { } } + public TsBlock remove() { + return remove(false); + } + /** * Remove a TsBlock from the head of the queue and return. Should be invoked only when the future - * returned by {@link #isBlocked()} completes. + * returned by {@link #isBlocked()} completes. If {@code retainMemory} is true, the memory + * reserved for the block is not freed; the caller must release it via {@link + * #freeRetainedMemory()}. * * @throws IllegalStateException Cannot remove TsBlock from closed queue. */ - public TsBlock remove() { + public TsBlock remove(boolean retainMemory) { if (closed) { // try throw underlying exception instead of "Source handle is aborted." if (abortedCause != null) { @@ -224,14 +237,18 @@ public TsBlock remove() { Pair tsBlockWithReservedBytes = queue.remove(); long reservedBytes = tsBlockWithReservedBytes.right; if (reservedBytes > 0) { - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - reservedBytes); bufferRetainedSizeInBytes -= reservedBytes; + if (retainMemory) { + pendingRetainedSizeInBytes += reservedBytes; + } else { + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + reservedBytes); + } } // Every time LocalSourceHandle consumes a TsBlock, it needs to send the event // to @@ -245,6 +262,22 @@ public TsBlock remove() { return tsBlockWithReservedBytes.left; } + /** Releases the memory of the blocks taken out with {@code retainMemory=true}. */ + public void freeRetainedMemory() { + if (pendingRetainedSizeInBytes <= 0) { + return; + } + long retainedSize = pendingRetainedSizeInBytes; + pendingRetainedSizeInBytes = 0; + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + retainedSize); + } + /** * Add TsBlocks to the queue. Except the first invocation, this method should be invoked only when * the returned future of last invocation completes. @@ -343,16 +376,7 @@ public void close() { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } queue.clear(); - if (bufferRetainedSizeInBytes > 0L) { - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - bufferRetainedSizeInBytes); - bufferRetainedSizeInBytes = 0; - } + freeAllRetainedMemory(); if (sinkChannel != null) { // attention: LocalSinkChannel of this SharedTsBlockQueue could be null when we // close @@ -387,16 +411,7 @@ public void abort() { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } queue.clear(); - if (bufferRetainedSizeInBytes > 0L) { - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - bufferRetainedSizeInBytes); - bufferRetainedSizeInBytes = 0; - } + freeAllRetainedMemory(); } /** Destroy the queue and cancel the future. Should only be called in abnormal case */ @@ -416,19 +431,25 @@ public void abort(Throwable t) { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } queue.clear(); - if (bufferRetainedSizeInBytes > 0L) { + freeAllRetainedMemory(); + } + + public Optional getAbortedCause() { + return Optional.ofNullable(abortedCause); + } + + private void freeAllRetainedMemory() { + long totalRetainedSize = bufferRetainedSizeInBytes + pendingRetainedSizeInBytes; + if (totalRetainedSize > 0L) { localMemoryManager .getQueryPool() .free( localFragmentInstanceId.getQueryId(), fullFragmentInstanceId, localPlanNodeId, - bufferRetainedSizeInBytes); + totalRetainedSize); bufferRetainedSizeInBytes = 0; + pendingRetainedSizeInBytes = 0; } } - - public Optional getAbortedCause() { - return Optional.ofNullable(abortedCause); - } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java index 94f6e09fa058c..7ae26323c2e23 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java @@ -124,6 +124,10 @@ public long getBufferRetainedSizeInBytes() { @Override public TsBlock receive() { + return receive(false); + } + + private TsBlock receive(boolean retainMemory) { long startTime = System.nanoTime(); try (SetThreadName sourceHandleName = new SetThreadName(threadName)) { checkState(); @@ -133,7 +137,7 @@ public TsBlock receive() { } TsBlock tsBlock; synchronized (queue) { - tsBlock = queue.remove(); + tsBlock = queue.remove(retainMemory); } if (tsBlock != null) { if (LOGGER.isDebugEnabled()) { @@ -154,8 +158,7 @@ public TsBlock receive() { @Override public ByteBuffer getSerializedTsBlock(boolean retainMemory) throws IoTDBException { - // the returned bytes are a fresh serialization, not reserved from the memory pool - TsBlock tsBlock = receive(); + TsBlock tsBlock = receive(retainMemory); if (LOGGER.isDebugEnabled()) { LOGGER.debug(DataNodeQueryMessages.GET_SERIALIZED_TSBLOCK, CommonUtils.toString(tsBlock)); } @@ -177,7 +180,9 @@ public ByteBuffer getSerializedTsBlock(boolean retainMemory) throws IoTDBExcepti @Override public void freeRetainedMemory() { - // nothing is reserved from the memory pool + synchronized (queue) { + queue.freeRetainedMemory(); + } } @Override diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/DefaultOperationQuota.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/DefaultOperationQuota.java index 764d6763184ac..b8fca051147e4 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/DefaultOperationQuota.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/DefaultOperationQuota.java @@ -36,9 +36,6 @@ import org.apache.tsfile.enums.TSDataType; import org.apache.tsfile.utils.BitMap; -import java.nio.ByteBuffer; -import java.util.List; - public class DefaultOperationQuota implements OperationQuota { protected final QuotaLimiter limiter; // the available read/write quota size in bytes @@ -76,15 +73,8 @@ public void checkQuota(int numWrites, int numReads, Statement s) throws RpcThrot } @Override - public void addReadResult(List queryResult) { - if (queryResult == null) { - return; - } - long size = 0; - for (ByteBuffer buffer : queryResult) { - size += buffer.limit(); - } - operationSize[OperationType.READ.ordinal()] += size; + public void addReadResult(long bytes) { + operationSize[OperationType.READ.ordinal()] += bytes; } /** diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/NoopOperationQuota.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/NoopOperationQuota.java index c35e6135be584..1044df96720ba 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/NoopOperationQuota.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/NoopOperationQuota.java @@ -20,9 +20,6 @@ import org.apache.iotdb.db.queryengine.plan.statement.Statement; -import java.nio.ByteBuffer; -import java.util.List; - /** Noop operation quota returned when no quota is associated to the user/table */ class NoopOperationQuota implements OperationQuota { private static OperationQuota instance = new NoopOperationQuota(); @@ -39,7 +36,7 @@ public static OperationQuota get() { public void checkQuota(int numWrites, int numReads, Statement s) {} @Override - public void addReadResult(List queryResult) {} + public void addReadResult(long bytes) {} @Override public void close() { diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/OperationQuota.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/OperationQuota.java index 16b660f28bdda..cbdacd833cc2e 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/OperationQuota.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/quotas/OperationQuota.java @@ -22,9 +22,6 @@ import org.apache.iotdb.commons.exception.RpcThrottlingException; import org.apache.iotdb.db.queryengine.plan.statement.Statement; -import java.nio.ByteBuffer; -import java.util.List; - public interface OperationQuota { public enum OperationType { @@ -43,7 +40,7 @@ public enum OperationType { */ void checkQuota(int numWrites, int numReads, Statement s) throws RpcThrottlingException; - void addReadResult(List queryResult); + void addReadResult(long bytes); /** Cleanup method on operation completion */ void close(); diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/LocalSourceHandleTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/LocalSourceHandleTest.java index c4378f82efc9d..d67913df78b21 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/LocalSourceHandleTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/LocalSourceHandleTest.java @@ -19,6 +19,7 @@ package org.apache.iotdb.db.queryengine.execution.exchange; +import org.apache.iotdb.db.queryengine.common.FragmentInstanceId; import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeManager.SourceHandleListener; import org.apache.iotdb.db.queryengine.execution.exchange.source.LocalSourceHandle; import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager; @@ -26,10 +27,15 @@ import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId; import com.google.common.util.concurrent.ListenableFuture; +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.read.common.block.TsBlock; +import org.apache.tsfile.read.common.block.TsBlockBuilder; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; +import java.util.Collections; + import static com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService; public class LocalSourceHandleTest { @@ -81,6 +87,93 @@ public void testReceive() { Mockito.verify(mockSourceHandleListener, Mockito.times(1)).onFinished(localSourceHandle); } + @Test + public void testRetainedMemoryIsFreedWhenTheHandedOverBlockIsReleased() throws Exception { + final String queryId = "q0"; + final String localPlanNodeId = "exchange_0"; + final TFragmentInstanceId localFragmentInstanceId = new TFragmentInstanceId(queryId, 0, "0"); + final String fullFragmentInstanceId = + FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(localFragmentInstanceId); + + LocalMemoryManager mockLocalMemoryManager = Mockito.mock(LocalMemoryManager.class); + MemoryPool mockMemoryPool = Utils.createMockNonBlockedMemoryPool(); + Mockito.when(mockLocalMemoryManager.getQueryPool()).thenReturn(mockMemoryPool); + SharedTsBlockQueue queue = + new SharedTsBlockQueue( + localFragmentInstanceId, + localPlanNodeId, + mockLocalMemoryManager, + newDirectExecutorService()); + LocalSourceHandle localSourceHandle = + new LocalSourceHandle( + localFragmentInstanceId, + localPlanNodeId, + queue, + Mockito.mock(SourceHandleListener.class)); + + TsBlock tsBlock = createRealTsBlock(); + long reservedSize = tsBlock.getSizeInBytes(); + queue.add(tsBlock); + queue.setNoMoreTsBlocks(true); + + localSourceHandle.getSerializedTsBlock(true); + Assert.assertEquals(0L, localSourceHandle.getBufferRetainedSizeInBytes()); + Mockito.verify(mockMemoryPool, Mockito.never()) + .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); + + localSourceHandle.freeRetainedMemory(); + Mockito.verify(mockMemoryPool, Mockito.times(1)) + .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); + + // releasing again is a no-op + localSourceHandle.freeRetainedMemory(); + Mockito.verify(mockMemoryPool, Mockito.times(1)) + .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); + } + + @Test + public void testAbortReleasesTheHandedOverMemory() throws Exception { + final String queryId = "q0"; + final String localPlanNodeId = "exchange_0"; + final TFragmentInstanceId localFragmentInstanceId = new TFragmentInstanceId(queryId, 0, "0"); + final String fullFragmentInstanceId = + FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(localFragmentInstanceId); + + LocalMemoryManager mockLocalMemoryManager = Mockito.mock(LocalMemoryManager.class); + MemoryPool mockMemoryPool = Utils.createMockNonBlockedMemoryPool(); + Mockito.when(mockLocalMemoryManager.getQueryPool()).thenReturn(mockMemoryPool); + SharedTsBlockQueue queue = + new SharedTsBlockQueue( + localFragmentInstanceId, + localPlanNodeId, + mockLocalMemoryManager, + newDirectExecutorService()); + LocalSourceHandle localSourceHandle = + new LocalSourceHandle( + localFragmentInstanceId, + localPlanNodeId, + queue, + Mockito.mock(SourceHandleListener.class)); + + TsBlock tsBlock = createRealTsBlock(); + long reservedSize = tsBlock.getSizeInBytes(); + queue.add(tsBlock); + queue.setNoMoreTsBlocks(true); + localSourceHandle.getSerializedTsBlock(true); + Mockito.verify(mockMemoryPool, Mockito.never()) + .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); + + // abort frees the memory that was not released by the caller (fallback path) + localSourceHandle.abort(); + Mockito.verify(mockMemoryPool, Mockito.times(1)) + .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); + + // a late release after abort must not free the same memory twice + localSourceHandle.freeRetainedMemory(); + Mockito.verify(mockMemoryPool, Mockito.times(1)) + .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); + } + @Test public void testAbort() { final String queryId = "q0"; @@ -119,4 +212,13 @@ public void testAbort() { Assert.assertFalse(localSourceHandle.isFinished()); Mockito.verify(mockSourceHandleListener, Mockito.times(1)).onAborted(localSourceHandle); } + + /** A real TsBlock so that the real serde can serialize it. */ + private static TsBlock createRealTsBlock() { + TsBlockBuilder builder = new TsBlockBuilder(Collections.singletonList(TSDataType.INT64)); + builder.getTimeColumnBuilder().writeLong(1L); + builder.getColumnBuilder(0).writeLong(10L); + builder.declarePosition(); + return builder.build(); + } } From 4b91bef4576dae3ee60a81262844cb0eb4248daa Mon Sep 17 00:00:00 2001 From: Weihao Li <18110526956@163.com> Date: Mon, 16 Nov 2026 18:17:02 +0800 Subject: [PATCH 05/11] Apply the payload limit to Fast LAST cache hits by materializing the result The cache-hit branches of executeFastLastDataQueryForOneDeviceV2 and executeFastLastDataQueryForOnePrefixPath serialized the whole result into one response, so an oversized cached value could still exceed the Thrift frame limit. The cached result is now registered as a MaterializedResultExecution and sent through the regular fragmented transfer: fetchResultsV2 continues from its pending fragment, and the serialized bytes are reserved from the query memory pool (best effort) until the last fragment is sent or the query is cleaned up. --- .../thrift/impl/ClientRPCServiceImpl.java | 79 ++++- .../db/queryengine/plan/Coordinator.java | 5 + .../MaterializedResultExecution.java | 297 ++++++++++++++++++ .../MaterializedResultExecutionTest.java | 153 +++++++++ 4 files changed, 524 insertions(+), 10 deletions(-) create mode 100644 iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java create mode 100644 iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java index 889e36236c01a..10a7447f69324 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java @@ -88,6 +88,7 @@ import org.apache.iotdb.db.queryengine.plan.analyze.schema.ISchemaFetcher; import org.apache.iotdb.db.queryengine.plan.execution.ExecutionResult; import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution; +import org.apache.iotdb.db.queryengine.plan.execution.MaterializedResultExecution; import org.apache.iotdb.db.queryengine.plan.execution.config.session.PreparedStatementHelper; import org.apache.iotdb.db.queryengine.plan.parser.ASTVisitor; import org.apache.iotdb.db.queryengine.plan.parser.StatementGenerator; @@ -394,6 +395,49 @@ private static int getMaxResultPayloadSize(IClientSession clientSession) { : 0; } + private static boolean canFitInOneResponse( + ByteBuffer cachedResult, IClientSession clientSession) { + if (cachedResult == null) { + return true; + } + int maxPayloadSize = getMaxResultPayloadSize(clientSession); + return maxPayloadSize <= 0 || cachedResult.remaining() <= maxPayloadSize; + } + + /** + * Transfers an oversized materialized result in fragments. The execution is registered so that + * {@code fetchResultsV2} can continue from the pending fragment, and it is cleaned up once the + * last fragment is sent or the query is closed. + */ + private TSExecuteStatementResp respondWithFragmentedResult( + long queryId, + ByteBuffer cachedResult, + int fetchSize, + long timeout, + IClientSession clientSession) + throws IoTDBException, IOException { + MaterializedResultExecution execution = + new MaterializedResultExecution( + queryId, + cachedResult, + DatasetHeaderFactory.getLastQueryHeader(), + StatementType.FAST_LAST_QUERY.name(), + timeout, + clientSession.getUsername(), + clientSession.getClientAddress()); + COORDINATOR.registerQueryExecution(queryId, execution); + TSExecuteStatementResp resp = + createResponse(DatasetHeaderFactory.getLastQueryHeader(), queryId); + resp.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS, "")); + boolean finished = + SELECT_RESULT.apply(resp, execution, fetchSize, getMaxResultPayloadSize(clientSession)); + resp.setMoreData(!finished); + if (finished) { + COORDINATOR.cleanupQueryExecution(queryId); + } + return resp; + } + public ClientRPCServiceImpl() { partitionFetcher = ClusterPartitionFetcher.getInstance(); schemaFetcher = ClusterSchemaFetcher.getInstance(); @@ -1312,14 +1356,21 @@ public TSExecuteStatementResp executeFastLastDataQueryForOnePrefixPath( } } + ByteBuffer cachedResult = builder.isEmpty() ? null : serde.serialize(builder.build()); + if (!canFitInOneResponse(cachedResult, clientSession)) { + return respondWithFragmentedResult( + queryId, + cachedResult, + req.isSetFetchSize() ? req.getFetchSize() : -1, + req.isSetTimeout() ? req.getTimeout() : config.getQueryTimeoutThreshold(), + clientSession); + } + final TSExecuteStatementResp resp = createResponse(DatasetHeaderFactory.getLastQueryHeader(), queryId); resp.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS, "")); - if (builder.isEmpty()) { - resp.setQueryResult(Collections.emptyList()); - } else { - resp.setQueryResult(Collections.singletonList(serde.serialize(builder.build()))); - } + resp.setQueryResult( + cachedResult == null ? Collections.emptyList() : Collections.singletonList(cachedResult)); resp.setMoreData(false); @@ -1489,14 +1540,22 @@ public TSExecuteStatementResp executeFastLastDataQueryForOneDeviceV2( } // cache hit if (allCached) { + ByteBuffer cachedResult = builder.isEmpty() ? null : serde.serialize(builder.build()); + if (!canFitInOneResponse(cachedResult, clientSession)) { + return respondWithFragmentedResult( + queryId, + cachedResult, + req.isSetFetchSize() ? req.getFetchSize() : -1, + req.isSetTimeout() ? req.getTimeout() : config.getQueryTimeoutThreshold(), + clientSession); + } TSExecuteStatementResp resp = createResponse(DatasetHeaderFactory.getLastQueryHeader(), queryId); resp.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS, "")); - if (builder.isEmpty()) { - resp.setQueryResult(Collections.emptyList()); - } else { - resp.setQueryResult(Collections.singletonList(serde.serialize(builder.build()))); - } + resp.setQueryResult( + cachedResult == null + ? Collections.emptyList() + : Collections.singletonList(cachedResult)); finished = true; resp.setMoreData(false); sampleForCacheHitFastLastDataQueryForOneDevice(req); 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 eda408bd7f59d..b01e744bbff4b 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 @@ -1037,6 +1037,11 @@ public void cleanUpStaleQueries() { }); } + /** Registers an already materialized execution, e.g. an oversized Fast LAST cache hit. */ + public void registerQueryExecution(Long queryId, IQueryExecution queryExecution) { + queryExecutionMap.put(queryId, queryExecution); + } + public void cleanupQueryExecution(Long queryId) { cleanupQueryExecution(queryId, (org.apache.thrift.TBase) null, null); } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java new file mode 100644 index 0000000000000..07e6ec8bf0892 --- /dev/null +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java @@ -0,0 +1,297 @@ +/* + * 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.execution; + +import org.apache.iotdb.commons.queryengine.common.SqlDialect; +import org.apache.iotdb.commons.utils.TestOnly; +import org.apache.iotdb.db.queryengine.common.FragmentInstanceId; +import org.apache.iotdb.db.queryengine.common.QueryId; +import org.apache.iotdb.db.queryengine.common.header.DatasetHeader; +import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeService; +import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager; +import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool; +import org.apache.iotdb.db.queryengine.execution.memory.MemorySide; +import org.apache.iotdb.db.queryengine.plan.analyze.QueryType; +import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId; +import org.apache.iotdb.rpc.RpcUtils; +import org.apache.iotdb.rpc.TSStatusCode; + +import org.apache.tsfile.read.common.block.TsBlock; + +import java.nio.ByteBuffer; +import java.util.Optional; + +/** + * An already materialized serialized result (e.g. a Fast LAST cache hit) registered as a query + * execution, so that {@code fetchResultsV2} can continue the fragmented transfer of an oversized + * block. Like other pending fragments, the serialized bytes are reserved from the query memory pool + * (best effort) until the last fragment is sent or the query is cleaned up. + */ +public class MaterializedResultExecution implements IQueryExecution { + + private static final String PLAN_NODE_ID = "lastCacheResult"; + + private final long queryId; + private final DatasetHeader datasetHeader; + private final String statementType; + private final long startExecutionTime = System.currentTimeMillis(); + private final long timeout; + private final String user; + private final String clientHostname; + + private final LocalMemoryManager localMemoryManager; + private final TFragmentInstanceId fragmentInstanceId; + private final String fullFragmentInstanceId; + + private final PendingResultFragment pendingResultFragment = + new PendingResultFragment(this::freeReservedMemory); + + /** The serialized result, handed over once. */ + private ByteBuffer serializedResult; + + private long reservedBytes; + + private volatile long startTimeOfCurrentRpc = System.nanoTime(); + private long totalExecutionTime; + private boolean cleanedUp; + + public MaterializedResultExecution( + long queryId, + ByteBuffer serializedResult, + DatasetHeader datasetHeader, + String statementType, + long timeout, + String user, + String clientHostname) { + this( + queryId, + serializedResult, + datasetHeader, + statementType, + timeout, + user, + clientHostname, + MPPDataExchangeService.getInstance().getMPPDataExchangeManager().getLocalMemoryManager()); + } + + @TestOnly + MaterializedResultExecution( + long queryId, + ByteBuffer serializedResult, + DatasetHeader datasetHeader, + String statementType, + long timeout, + String user, + String clientHostname, + LocalMemoryManager localMemoryManager) { + this.queryId = queryId; + this.serializedResult = serializedResult; + this.datasetHeader = datasetHeader; + this.statementType = statementType; + this.timeout = timeout; + this.user = user; + this.clientHostname = clientHostname; + + this.localMemoryManager = localMemoryManager; + this.fragmentInstanceId = new TFragmentInstanceId(Long.toString(queryId), 0, PLAN_NODE_ID); + this.fullFragmentInstanceId = + FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(fragmentInstanceId); + reserveResult(); + } + + /** Best-effort accounting: a result too large for one reservation still gets transferred. */ + private void reserveResult() { + MemoryPool queryPool = localMemoryManager.getQueryPool(); + queryPool.registerPlanNodeIdToQueryMemoryMap( + Long.toString(queryId), fullFragmentInstanceId, PLAN_NODE_ID, MemorySide.OUTPUT); + long maxBytesCanReserve = queryPool.getMaxBytes(); + long bytesToReserve = Math.min(serializedResult.remaining(), maxBytesCanReserve); + if (queryPool.tryReserve( + Long.toString(queryId), + fullFragmentInstanceId, + PLAN_NODE_ID, + bytesToReserve, + maxBytesCanReserve)) { + reservedBytes = bytesToReserve; + } + } + + private synchronized void freeReservedMemory() { + if (reservedBytes > 0) { + localMemoryManager + .getQueryPool() + .free(Long.toString(queryId), fullFragmentInstanceId, PLAN_NODE_ID, reservedBytes); + reservedBytes = 0; + } + } + + private synchronized void cleanup() { + if (cleanedUp) { + return; + } + cleanedUp = true; + pendingResultFragment.clear(); + freeReservedMemory(); + localMemoryManager + .getQueryPool() + .deRegisterFragmentInstanceFromQueryMemoryMap( + Long.toString(queryId), fullFragmentInstanceId, false); + } + + @Override + public void start() { + // nothing to start: the result is already materialized + } + + @Override + public void stop(Throwable t) { + cleanup(); + } + + @Override + public void stopAndCleanup(Throwable t) { + cleanup(); + } + + @Override + public void cancel() { + cleanup(); + } + + @Override + public ExecutionResult getStatus() { + return new ExecutionResult( + new QueryId(Long.toString(queryId)), RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS)); + } + + @Override + public Optional getBatchResult() { + return Optional.empty(); + } + + @Override + public Optional getByteBufferBatchResult(boolean retainMemory) { + ByteBuffer result = serializedResult; + serializedResult = null; + return Optional.ofNullable(result); + } + + @Override + public PendingResultFragment getPendingResultFragment() { + return pendingResultFragment; + } + + @Override + public boolean hasNextResult() { + return serializedResult != null; + } + + @Override + public int getOutputValueColumnCount() { + return datasetHeader.getOutputValueColumnCount(); + } + + @Override + public DatasetHeader getDatasetHeader() { + return datasetHeader; + } + + @Override + public QueryType getQueryType() { + return QueryType.READ; + } + + @Override + public boolean isQuery() { + return true; + } + + @Override + public boolean isUserQuery() { + return false; + } + + @Override + public String getQueryId() { + return Long.toString(queryId); + } + + @Override + public long getStartExecutionTime() { + return startExecutionTime; + } + + @Override + public synchronized void recordExecutionTime(long executionTime) { + totalExecutionTime += executionTime; + this.startTimeOfCurrentRpc = -1; + } + + @Override + public synchronized void updateCurrentRpcStartTime(long startTime) { + this.startTimeOfCurrentRpc = startTime; + } + + @Override + public synchronized boolean isActive() { + return startTimeOfCurrentRpc == -1; + } + + @Override + public synchronized long getTotalExecutionTime() { + return totalExecutionTime + + (startTimeOfCurrentRpc == -1 ? 0 : System.nanoTime() - startTimeOfCurrentRpc); + } + + @Override + public long getTimeout() { + return timeout; + } + + @Override + public Optional getExecuteSQL() { + return Optional.empty(); + } + + @Override + public String getStatementType() { + return statementType; + } + + @Override + public SqlDialect getSQLDialect() { + return SqlDialect.TREE; + } + + @Override + public String getUser() { + return user; + } + + @Override + public String getClientHostname() { + return clientHostname; + } + + @Override + public boolean isDebug() { + return false; + } +} diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java new file mode 100644 index 0000000000000..e21ea0dab7622 --- /dev/null +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java @@ -0,0 +1,153 @@ +/* + * 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.execution; + +import org.apache.iotdb.commons.memory.MemoryManager; +import org.apache.iotdb.db.queryengine.common.header.DatasetHeaderFactory; +import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager; +import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool; +import org.apache.iotdb.db.utils.QueryDataSetUtils; + +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.read.common.block.TsBlock; +import org.apache.tsfile.read.common.block.TsBlockBuilder; +import org.apache.tsfile.read.common.block.column.TsBlockSerde; +import org.junit.Test; +import org.mockito.Mockito; + +import java.nio.ByteBuffer; +import java.util.Collections; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class MaterializedResultExecutionTest { + + private static final long POOL_CAPACITY = 16L * 1024 * 1024; + + @Test + public void testFragmentedTransferAndMemoryAccounting() throws Exception { + ByteBuffer serialized = new TsBlockSerde().serialize(buildTsBlock()); + int totalLength = serialized.remaining(); + int payloadSize = totalLength / 2; + MemoryPool memoryPool = new MemoryPool("test", new MemoryManager(POOL_CAPACITY), POOL_CAPACITY); + LocalMemoryManager localMemoryManager = Mockito.mock(LocalMemoryManager.class); + Mockito.when(localMemoryManager.getQueryPool()).thenReturn(memoryPool); + + MaterializedResultExecution execution = + new MaterializedResultExecution( + 1L, + serialized.duplicate(), + DatasetHeaderFactory.getLastQueryHeader(), + "FAST_LAST_QUERY", + 60_000L, + "root", + "127.0.0.1", + localMemoryManager); + + // the serialized bytes are reserved from the query pool + assertEquals(totalLength, memoryPool.getReservedBytes()); + + // the result is served once; the first response carries a fragment of it + QueryDataSetUtils.ResultChunk firstChunk = + QueryDataSetUtils.convertQueryResultByFetchSize(execution, 10, 0, payloadSize); + assertFalse(firstChunk.isFinished()); + assertTrue(firstChunk.hasNextOffset()); + assertEquals(totalLength, firstChunk.getTotalLength()); + assertEquals(payloadSize, firstChunk.getQueryResult().get(0).remaining()); + assertFalse(execution.hasNextResult()); + assertTrue(execution.getByteBufferBatchResult(true).isEmpty()); + + // the following fetch completes the block and releases the reservation + QueryDataSetUtils.ResultChunk secondChunk = + QueryDataSetUtils.convertQueryResultByFetchSize( + execution, 10, firstChunk.getNextOffset(), totalLength); + assertTrue(secondChunk.isFinished()); + assertFalse(secondChunk.hasNextOffset()); + assertEquals(0L, memoryPool.getReservedBytes()); + + // cleanup is idempotent + execution.stopAndCleanup(null); + execution.stopAndCleanup(null); + } + + @Test + public void testCleanupReleasesReservationWithoutFetch() throws Exception { + ByteBuffer serialized = new TsBlockSerde().serialize(buildTsBlock()); + MemoryPool memoryPool = new MemoryPool("test", new MemoryManager(POOL_CAPACITY), POOL_CAPACITY); + LocalMemoryManager localMemoryManager = Mockito.mock(LocalMemoryManager.class); + Mockito.when(localMemoryManager.getQueryPool()).thenReturn(memoryPool); + + MaterializedResultExecution execution = + new MaterializedResultExecution( + 2L, + serialized.duplicate(), + DatasetHeaderFactory.getLastQueryHeader(), + "FAST_LAST_QUERY", + 60_000L, + "root", + "127.0.0.1", + localMemoryManager); + assertEquals(serialized.remaining(), memoryPool.getReservedBytes()); + + // the client never fetches the rest + execution.stopAndCleanup(null); + assertEquals(0L, memoryPool.getReservedBytes()); + } + + @Test + public void testPartialReservationWhenResultExceedsPool() throws Exception { + ByteBuffer serialized = new TsBlockSerde().serialize(buildTsBlock()); + // a pool smaller than the result still serves it, with a partial reservation + long smallCapacity = 1024L; + MemoryPool memoryPool = new MemoryPool("test", new MemoryManager(smallCapacity), smallCapacity); + LocalMemoryManager localMemoryManager = Mockito.mock(LocalMemoryManager.class); + Mockito.when(localMemoryManager.getQueryPool()).thenReturn(memoryPool); + + MaterializedResultExecution execution = + new MaterializedResultExecution( + 3L, + serialized.duplicate(), + DatasetHeaderFactory.getLastQueryHeader(), + "FAST_LAST_QUERY", + 60_000L, + "root", + "127.0.0.1", + localMemoryManager); + assertEquals(smallCapacity, memoryPool.getReservedBytes()); + + QueryDataSetUtils.ResultChunk chunk = + QueryDataSetUtils.convertQueryResultByFetchSize(execution, 10, 0, 64); + assertTrue(chunk.hasNextOffset()); + execution.stopAndCleanup(null); + assertEquals(0L, memoryPool.getReservedBytes()); + } + + private static TsBlock buildTsBlock() { + TsBlockBuilder builder = new TsBlockBuilder(Collections.singletonList(TSDataType.INT64)); + for (int i = 0; i < 100; i++) { + builder.getTimeColumnBuilder().writeLong(i); + builder.getColumnBuilder(0).writeLong(i * 10L); + builder.declarePosition(); + } + return builder.build(); + } +} From 2e0302df37b97031f12bd287e5e28ce3f7d89867 Mon Sep 17 00:00:00 2001 From: Weihao Li <18110526956@163.com> Date: Mon, 16 Nov 2026 19:22:42 +0800 Subject: [PATCH 06/11] Assert the first Fast LAST response is always a fragment respondWithFragmentedResult is only entered when the cached result exceeds the payload bound, so the materialized transfer always starts with a fragment; replace the defensive finish branch with checkState. --- .../apache/iotdb/db/i18n/DataNodeMiscMessages.java | 4 ++++ .../apache/iotdb/db/i18n/DataNodeMiscMessages.java | 4 ++++ .../db/protocol/thrift/impl/ClientRPCServiceImpl.java | 11 +++++++---- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java index 963e9deceaa59..a338d3d9640ba 100644 --- a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java +++ b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java @@ -2009,4 +2009,8 @@ private DataNodeMiscMessages() {} public static final String LOG_FAILED_TO_CLEAN_UP_INTERNAL_CLIENT_SESSION_AFTER_CONNECTION_CLOSED_9BD1700E = "Failed to clean up internal client session after connection closed"; + + public static final String + EXCEPTION_THE_FIRST_RESPONSE_OF_A_FRAGMENTED_FAST_LAST_RESULT_MUST_NOT_BE_COMPLETE_DEF121D5 = + "The first response of a fragmented Fast LAST result must not be complete"; } diff --git a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java index 334010c873919..81d2a0abaae18 100644 --- a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java +++ b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java @@ -1958,4 +1958,8 @@ private DataNodeMiscMessages() {} public static final String LOG_FAILED_TO_CLEAN_UP_INTERNAL_CLIENT_SESSION_AFTER_CONNECTION_CLOSED_9BD1700E = "连接关闭后清理内部客户端会话失败"; + + public static final String + EXCEPTION_THE_FIRST_RESPONSE_OF_A_FRAGMENTED_FAST_LAST_RESULT_MUST_NOT_BE_COMPLETE_DEF121D5 = + "分片的 Fast LAST 结果的首个响应不能是完整的"; } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java index 10a7447f69324..41d7f514319dd 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java @@ -243,6 +243,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Supplier; +import static com.google.common.base.Preconditions.checkState; import static org.apache.iotdb.commons.partition.DataPartition.NOT_ASSIGNED; import static org.apache.iotdb.db.queryengine.common.DataNodeEndPoints.isSameNode; import static org.apache.iotdb.db.queryengine.execution.operator.AggregationUtil.initTimeRangeIterator; @@ -431,10 +432,12 @@ private TSExecuteStatementResp respondWithFragmentedResult( resp.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS, "")); boolean finished = SELECT_RESULT.apply(resp, execution, fetchSize, getMaxResultPayloadSize(clientSession)); - resp.setMoreData(!finished); - if (finished) { - COORDINATOR.cleanupQueryExecution(queryId); - } + // the cached result exceeds the payload bound, so the first response is always a fragment + checkState( + !finished, + DataNodeMiscMessages + .EXCEPTION_THE_FIRST_RESPONSE_OF_A_FRAGMENTED_FAST_LAST_RESULT_MUST_NOT_BE_COMPLETE_DEF121D5); + resp.setMoreData(true); return resp; } From 3bed5ec64e5b58c482f88ff4b881ac65a498e0f0 Mon Sep 17 00:00:00 2001 From: Weihao Li <18110526956@163.com> Date: Thu, 19 Nov 2026 16:41:53 +0800 Subject: [PATCH 07/11] Address the second review round of result fragmentation - Account a pending fragment under a query-owned memory account instead of the result handles: SharedTsBlockQueue.close() could release the last block's reservation (sink finish -> LocalSinkChannel.close -> queue.close) while the fragment still held it, and the queue's account is force-deregistered with the upstream fragment instance. The handle-level retainMemory/freeRetainedMemory API is removed again; PendingResultFragment reserves the serialized length on its first reset and releases it on clear. - Give the Fast LAST materialized execution a real QueryId from Coordinator#createQueryId(), so SHOW QUERIES parses the DataNodeId correctly, and return a readable SQL from getExecuteSQL(). - Decide fragmentation in fetchResultsV2 by the session capability, so a continuation without offset fails the offset check instead of dropping the tail; read the payload bound once and pass it down; slice the block before computing offsets so they are block-relative; clean up the registered execution when the Fast LAST transfer fails. - Rename the new i18n constants after their full English text and fix the offset mismatch message. - Add integration tests: IoTDBSessionResultChunkingIT (tree model, standalone and cluster) and IoTDBTableResultChunkingIT (table model). --- .../v1/handler/QueryDataSetHandlerTest.java | 4 +- .../db/it/IoTDBSessionResultChunkingIT.java | 278 ++++++++++++++++++ .../it/db/it/IoTDBTableResultChunkingIT.java | 122 ++++++++ .../apache/iotdb/rpc/i18n/RpcMessages.java | 10 +- .../apache/iotdb/rpc/i18n/RpcMessages.java | 10 +- .../org/apache/iotdb/rpc/IoTDBRpcDataSet.java | 10 +- .../iotdb/db/i18n/DataNodeQueryMessages.java | 4 +- .../iotdb/db/i18n/DataNodeQueryMessages.java | 4 +- .../thrift/impl/ClientRPCServiceImpl.java | 62 ++-- .../exchange/SharedTsBlockQueue.java | 91 +++--- .../exchange/source/ISourceHandle.java | 12 +- .../exchange/source/LocalSourceHandle.java | 17 +- .../exchange/source/SourceHandle.java | 97 +++--- .../plan/execution/IQueryExecution.java | 8 +- .../MaterializedResultExecution.java | 82 ++---- .../plan/execution/PendingResultFragment.java | 59 +++- .../plan/execution/QueryExecution.java | 22 +- .../execution/QueryOwnedFragmentAccount.java | 97 ++++++ .../execution/config/ConfigExecution.java | 7 +- .../execution/memory/MemorySourceHandle.java | 8 +- .../iotdb/db/utils/QueryDataSetUtils.java | 19 +- .../protocol/session/QueryOwnershipTest.java | 2 +- .../exchange/LocalSourceHandleTest.java | 102 ------- .../execution/exchange/SourceHandleTest.java | 116 -------- .../operator/MergeTreeSortOperatorTest.java | 4 +- .../MaterializedResultExecutionTest.java | 24 +- .../execution/PendingResultFragmentTest.java | 100 +++++++ .../iotdb/db/utils/QueryDataSetUtilsTest.java | 36 +-- .../db/utils/ResultChunkingEndToEndTest.java | 15 +- 29 files changed, 871 insertions(+), 551 deletions(-) create mode 100644 integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBSessionResultChunkingIT.java create mode 100644 integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTableResultChunkingIT.java create mode 100644 iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java create mode 100644 iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragmentTest.java diff --git a/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java index 37fa74b9bd5a6..8bcd4d9445dd6 100644 --- a/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java +++ b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java @@ -89,7 +89,7 @@ private static final class TestQueryExecution implements IQueryExecution { private final Queue> batches = new ArrayDeque<>(); private final DatasetHeader datasetHeader; - private final PendingResultFragment pendingResultFragment = new PendingResultFragment(() -> {}); + private final PendingResultFragment pendingResultFragment = new PendingResultFragment(null); private TestQueryExecution(TsBlock... tsBlocks) { for (TsBlock tsBlock : tsBlocks) { @@ -126,7 +126,7 @@ public Optional getBatchResult() throws IoTDBException { } @Override - public Optional getByteBufferBatchResult(boolean retainMemory) { + public Optional getByteBufferBatchResult() { return Optional.empty(); } diff --git a/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBSessionResultChunkingIT.java b/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBSessionResultChunkingIT.java new file mode 100644 index 0000000000000..251e585f35fe1 --- /dev/null +++ b/integration-test/src/test/java/org/apache/iotdb/db/it/IoTDBSessionResultChunkingIT.java @@ -0,0 +1,278 @@ +/* + * 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.it; + +import org.apache.iotdb.isession.SessionDataSet; +import org.apache.iotdb.it.env.EnvFactory; +import org.apache.iotdb.it.env.cluster.node.DataNodeWrapper; +import org.apache.iotdb.it.framework.IoTDBTestRunner; +import org.apache.iotdb.itbase.category.ClusterIT; +import org.apache.iotdb.itbase.category.LocalStandaloneIT; +import org.apache.iotdb.session.Session; + +import org.apache.tsfile.common.conf.TSFileConfig; +import org.apache.tsfile.enums.TSDataType; +import org.apache.tsfile.read.common.Field; +import org.apache.tsfile.read.common.RowRecord; +import org.apache.tsfile.utils.Binary; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; +import java.util.Arrays; +import java.util.List; +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.assertTrue; + +/** + * End-to-end coverage of fragmented query results through a real DataNode: the payload bound is set + * to its minimum so that a single oversized row is split into several responses. Runs on a single + * node (local result path) and on a cluster (remote result path). + */ +@RunWith(IoTDBTestRunner.class) +@Category({LocalStandaloneIT.class, ClusterIT.class}) +public class IoTDBSessionResultChunkingIT { + + private static final int PAYLOAD_SIZE_IN_BYTES = 128 * 1024; + private static final int LARGE_VALUE_SIZE = 300 * 1024; + + private static final byte[] LARGE_BLOB = createBlob(); + private static final String LARGE_TEXT = createText(); + private static final byte[] SMALL_BLOB = "small".getBytes(TSFileConfig.STRING_CHARSET); + + private static Session session; + private static String ip; + private static int port; + + @BeforeClass + public static void setUp() throws Exception { + EnvFactory.getEnv() + .getConfig() + .getDataNodeConfig() + .setMppDataExchangeMaxPayloadSizeInBytes(PAYLOAD_SIZE_IN_BYTES); + EnvFactory.getEnv().initClusterEnvironment(); + ip = EnvFactory.getEnv().getIP(); + port = Integer.parseInt(EnvFactory.getEnv().getPort()); + + try (Connection connection = EnvFactory.getEnv().getConnection(); + Statement statement = connection.createStatement()) { + statement.execute("CREATE DATABASE root.chunk_it"); + statement.execute("CREATE TIMESERIES root.chunk_it.d1.blob BLOB ENCODING=PLAIN"); + // a dedicated database holding only text, so that the prefix-path last query returns a + // single oversized value + statement.execute("CREATE DATABASE root.chunk_text_it"); + statement.execute("CREATE TIMESERIES root.chunk_text_it.d1.t TEXT ENCODING=PLAIN"); + } + + session = new Session(ip, port); + session.open(); + } + + @AfterClass + public static void tearDown() { + if (session != null) { + try { + session.close(); + } catch (Exception ignored) { + // best effort + } + } + EnvFactory.getEnv().cleanClusterEnvironment(); + } + + @Test + public void testOversizedRowThroughSessionAndJdbc() throws Exception { + assertEquals( + "a value larger than the payload bound is required", LARGE_VALUE_SIZE, LARGE_BLOB.length); + session.insertRecord( + "root.chunk_it.d1", + 1_000_000L, + List.of("blob"), + List.of(TSDataType.BLOB), + new Binary(LARGE_BLOB)); + + SessionDataSet dataSet = + session.executeQueryStatement("SELECT blob FROM root.chunk_it.d1 WHERE time = 1000000"); + try { + assertTrue(dataSet.hasNext()); + assertArrayEquals(LARGE_BLOB, dataSet.next().getFields().get(0).getBinaryV().getValues()); + assertFalse(dataSet.hasNext()); + } finally { + dataSet.closeOperationHandle(); + } + + try (Connection connection = EnvFactory.getEnv().getConnection(); + Statement statement = connection.createStatement(); + ResultSet resultSet = + statement.executeQuery("SELECT blob FROM root.chunk_it.d1 WHERE time = 1000000")) { + assertTrue(resultSet.next()); + // in the tree model the first JDBC column is Time + assertArrayEquals(LARGE_BLOB, resultSet.getBytes("root.chunk_it.d1.blob")); + assertFalse(resultSet.next()); + } + } + + @Test + public void testMixedRowsWithSmallFetchSize() throws Exception { + for (int time = 1; time <= 3; time++) { + session.insertRecord( + "root.chunk_it.d1", + time, + List.of("blob"), + List.of(TSDataType.BLOB), + new Binary(SMALL_BLOB)); + } + session.insertRecord( + "root.chunk_it.d1", 4L, List.of("blob"), List.of(TSDataType.BLOB), new Binary(LARGE_BLOB)); + session.insertRecord( + "root.chunk_it.d1", 5L, List.of("blob"), List.of(TSDataType.BLOB), new Binary(SMALL_BLOB)); + + Session smallFetchSession = new Session(ip, port); + smallFetchSession.open(); + smallFetchSession.setFetchSize(1); + try { + SessionDataSet dataSet = + smallFetchSession.executeQueryStatement( + "SELECT blob FROM root.chunk_it.d1 WHERE time >= 1 AND time <= 5"); + try { + for (int time = 1; time <= 5; time++) { + assertTrue("missing row " + time, dataSet.hasNext()); + RowRecord row = dataSet.next(); + assertEquals(time, row.getTimestamp()); + assertArrayEquals( + time == 4 ? LARGE_BLOB : SMALL_BLOB, row.getFields().get(0).getBinaryV().getValues()); + } + assertFalse(dataSet.hasNext()); + } finally { + dataSet.closeOperationHandle(); + } + } finally { + smallFetchSession.close(); + } + } + + @Test + public void testBothFastLastRpcsOnCachedOversizedValue() throws Exception { + session.insertRecord( + "root.chunk_text_it.d1", + 2_000_000L, + List.of("t"), + List.of(TSDataType.TEXT), + new Binary(LARGE_TEXT, TSFileConfig.STRING_CHARSET)); + + // one-device RPC: the second call hits the last cache, i.e. the materialized result path + for (int i = 0; i < 2; i++) { + SessionDataSet dataSet = + session.executeLastDataQueryForOneDevice( + "root.chunk_text_it", "root.chunk_text_it.d1", List.of("t"), true); + try { + assertEquals(LARGE_TEXT, lastTextValue(dataSet)); + } finally { + dataSet.closeOperationHandle(); + } + } + + // one-prefix-path RPC: it is served from the last cache of the connected DataNode without + // redirection, so query every DataNode; the one owning the device must return the full value + boolean served = false; + for (DataNodeWrapper dataNode : EnvFactory.getEnv().getDataNodeWrapperList()) { + try (Session nodeSession = new Session(dataNode.getIp(), dataNode.getPort())) { + nodeSession.open(); + for (int i = 0; i < 2; i++) { + SessionDataSet dataSet = + nodeSession.executeFastLastDataQueryForOnePrefixPath( + List.of("root", "chunk_text_it")); + try { + String value = lastTextValue(dataSet); + if (LARGE_TEXT.equals(value)) { + served = true; + } + } finally { + dataSet.closeOperationHandle(); + } + } + } + } + assertTrue("no DataNode served the cached oversized value", served); + } + + @Test + public void testCloseWhileFragmentIsPending() throws Exception { + session.insertRecord( + "root.chunk_it.d1", + 3_000_000L, + List.of("blob"), + List.of(TSDataType.BLOB), + new Binary(LARGE_BLOB)); + + // the execute response carries the first fragment only; abandon it without fetching the rest + for (int i = 0; i < 3; i++) { + SessionDataSet dataSet = + session.executeQueryStatement("SELECT blob FROM root.chunk_it.d1 WHERE time = 3000000"); + assertTrue(dataSet.hasNext()); + dataSet.closeOperationHandle(); + } + + // the node still serves the complete result + SessionDataSet dataSet = + session.executeQueryStatement("SELECT blob FROM root.chunk_it.d1 WHERE time = 3000000"); + try { + assertTrue(dataSet.hasNext()); + assertArrayEquals(LARGE_BLOB, dataSet.next().getFields().get(0).getBinaryV().getValues()); + assertFalse(dataSet.hasNext()); + } finally { + dataSet.closeOperationHandle(); + } + } + + private static String lastTextValue(SessionDataSet dataSet) throws Exception { + String value = null; + while (dataSet.hasNext()) { + for (Field field : dataSet.next().getFields()) { + if (field.getBinaryV() != null + && (value == null || field.getBinaryV().getLength() > value.length())) { + value = field.getBinaryV().getStringValue(TSFileConfig.STRING_CHARSET); + } + } + } + return value; + } + + private static byte[] createBlob() { + byte[] blob = new byte[LARGE_VALUE_SIZE]; + new Random(42).nextBytes(blob); + return blob; + } + + private static String createText() { + char[] chars = new char[LARGE_VALUE_SIZE]; + Arrays.fill(chars, 'a'); + return new String(chars); + } +} diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTableResultChunkingIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTableResultChunkingIT.java new file mode 100644 index 0000000000000..a0f2a510ce785 --- /dev/null +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTableResultChunkingIT.java @@ -0,0 +1,122 @@ +/* + * 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.relational.it.db.it; + +import org.apache.iotdb.it.env.EnvFactory; +import org.apache.iotdb.it.framework.IoTDBTestRunner; +import org.apache.iotdb.itbase.category.TableClusterIT; +import org.apache.iotdb.itbase.category.TableLocalStandaloneIT; +import org.apache.iotdb.itbase.runtime.ClusterTestConnection; + +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; +import org.junit.runner.RunWith; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.Random; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +/** + * End-to-end coverage of fragmented query results of the table model through JDBC, with the payload + * bound at its minimum. + */ +@RunWith(IoTDBTestRunner.class) +@Category({TableLocalStandaloneIT.class, TableClusterIT.class}) +public class IoTDBTableResultChunkingIT { + + private static final int PAYLOAD_SIZE_IN_BYTES = 128 * 1024; + private static final int LARGE_VALUE_SIZE = 300 * 1024; + + private static final byte[] LARGE_BLOB = createBlob(); + + @BeforeClass + public static void setUp() throws Exception { + EnvFactory.getEnv() + .getConfig() + .getDataNodeConfig() + .setMppDataExchangeMaxPayloadSizeInBytes(PAYLOAD_SIZE_IN_BYTES); + EnvFactory.getEnv().initClusterEnvironment(); + + try (Connection connection = EnvFactory.getEnv().getTableConnection(); + Statement statement = connection.createStatement()) { + statement.execute("CREATE DATABASE chunk_it_table"); + statement.execute( + "CREATE TABLE chunk_it_table.blob_table(time TIMESTAMP TIME, payload BLOB FIELD)"); + } + + try (Connection connection = getRealTableConnection(); + PreparedStatement preparedStatement = + connection.prepareStatement( + "INSERT INTO chunk_it_table.blob_table(time, payload) VALUES (?, ?)")) { + // three oversized rows mixed with one small row + for (int i = 0; i < 4; i++) { + preparedStatement.setLong(1, i); + preparedStatement.setBytes(2, i == 3 ? new byte[] {1, 2, 3} : LARGE_BLOB); + preparedStatement.execute(); + } + } + } + + @AfterClass + public static void tearDown() { + EnvFactory.getEnv().cleanClusterEnvironment(); + } + + private static Connection getRealTableConnection() throws SQLException, IOException { + Connection connection = EnvFactory.getEnv().getTableConnection(); + if (connection instanceof ClusterTestConnection) { + // the test connection proxy does not support prepareStatement + return ((ClusterTestConnection) connection).writeConnection.getUnderlyingConnection(); + } + return connection; + } + + @Test + public void testOversizedRowsThroughJdbc() throws Exception { + try (Connection connection = getRealTableConnection(); + Statement statement = connection.createStatement()) { + statement.setFetchSize(1); + try (ResultSet resultSet = + statement.executeQuery("SELECT payload FROM chunk_it_table.blob_table ORDER BY time")) { + for (int i = 0; i < 4; i++) { + assertTrue("missing row " + i, resultSet.next()); + assertArrayEquals(i == 3 ? new byte[] {1, 2, 3} : LARGE_BLOB, resultSet.getBytes(1)); + } + assertFalse(resultSet.next()); + } + } + } + + private static byte[] createBlob() { + byte[] blob = new byte[LARGE_VALUE_SIZE]; + new Random(7).nextBytes(blob); + return blob; + } +} diff --git a/iotdb-client/service-rpc/src/main/i18n/en/org/apache/iotdb/rpc/i18n/RpcMessages.java b/iotdb-client/service-rpc/src/main/i18n/en/org/apache/iotdb/rpc/i18n/RpcMessages.java index 5686418c85065..510b2c5ce0ee9 100644 --- a/iotdb-client/service-rpc/src/main/i18n/en/org/apache/iotdb/rpc/i18n/RpcMessages.java +++ b/iotdb-client/service-rpc/src/main/i18n/en/org/apache/iotdb/rpc/i18n/RpcMessages.java @@ -128,15 +128,15 @@ private RpcMessages() {} public static final String EXCEPTION_DATA_TYPE_ARG_IS_NOT_SUPPORTED_31213160 = "Data type %s is not supported."; - public static final String EXCEPTION_CHUNKED_RESULT_EMPTY_FRAGMENT_03A0EF39 = + public static final String EXCEPTION_EMPTY_FRAGMENT_RECEIVED_WHEN_ASSEMBLING_A_CHUNKED_QUERY_RESULT_03A0EF39 = "Empty fragment received when assembling a chunked query result"; public static final String - EXCEPTION_CHUNKED_RESULT_FRAGMENT_OFFSET_ARG_NOT_GREATER_THAN_ACCUMULATED_LENGTH_ARG_A0F032E3 = - "Fragment offset %d is not greater than the accumulated length %d"; - public static final String EXCEPTION_CHUNKED_RESULT_MISSING_TOTAL_LENGTH_FOR_FIRST_FRAGMENT_1D11374F = + EXCEPTION_FRAGMENT_OFFSET_ARG_DOES_NOT_MATCH_THE_ACCUMULATED_LENGTH_ARG_1EB45347 = + "Fragment offset %d does not match the accumulated length %d"; + public static final String EXCEPTION_MISSING_TOTALLENGTH_FOR_THE_FIRST_FRAGMENT_OF_A_CHUNKED_TSBLOCK_1D11374F = "Missing totalLength for the first fragment of a chunked TsBlock"; public static final String - EXCEPTION_CHUNKED_RESULT_ASSEMBLED_LENGTH_ARG_DOES_NOT_MATCH_TOTAL_LENGTH_ARG_925DCB15 = + EXCEPTION_ASSEMBLED_FRAGMENT_LENGTH_ARG_DOES_NOT_MATCH_THE_TOTAL_LENGTH_ARG_925DCB15 = "Assembled fragment length %d does not match the total length %d"; public static final String EXCEPTION_INVALID_BUFFER_SIZE_ARG_5131C6CA = diff --git a/iotdb-client/service-rpc/src/main/i18n/zh/org/apache/iotdb/rpc/i18n/RpcMessages.java b/iotdb-client/service-rpc/src/main/i18n/zh/org/apache/iotdb/rpc/i18n/RpcMessages.java index 309c96ba9508c..d17dfe89a190c 100644 --- a/iotdb-client/service-rpc/src/main/i18n/zh/org/apache/iotdb/rpc/i18n/RpcMessages.java +++ b/iotdb-client/service-rpc/src/main/i18n/zh/org/apache/iotdb/rpc/i18n/RpcMessages.java @@ -109,15 +109,15 @@ private RpcMessages() {} public static final String EXCEPTION_DATA_TYPE_ARG_IS_NOT_SUPPORTED_31213160 = "不支持数据类型 %s。"; - public static final String EXCEPTION_CHUNKED_RESULT_EMPTY_FRAGMENT_03A0EF39 = + public static final String EXCEPTION_EMPTY_FRAGMENT_RECEIVED_WHEN_ASSEMBLING_A_CHUNKED_QUERY_RESULT_03A0EF39 = "拼装分片查询结果时收到空分片"; public static final String - EXCEPTION_CHUNKED_RESULT_FRAGMENT_OFFSET_ARG_NOT_GREATER_THAN_ACCUMULATED_LENGTH_ARG_A0F032E3 = - "分片 offset %d 不大于已累计的长度 %d"; - public static final String EXCEPTION_CHUNKED_RESULT_MISSING_TOTAL_LENGTH_FOR_FIRST_FRAGMENT_1D11374F = + EXCEPTION_FRAGMENT_OFFSET_ARG_DOES_NOT_MATCH_THE_ACCUMULATED_LENGTH_ARG_1EB45347 = + "分片 offset %d 与已累计的长度 %d 不一致"; + public static final String EXCEPTION_MISSING_TOTALLENGTH_FOR_THE_FIRST_FRAGMENT_OF_A_CHUNKED_TSBLOCK_1D11374F = "分片 TsBlock 的首个分片缺少 totalLength"; public static final String - EXCEPTION_CHUNKED_RESULT_ASSEMBLED_LENGTH_ARG_DOES_NOT_MATCH_TOTAL_LENGTH_ARG_925DCB15 = + EXCEPTION_ASSEMBLED_FRAGMENT_LENGTH_ARG_DOES_NOT_MATCH_THE_TOTAL_LENGTH_ARG_925DCB15 = "拼装后的分片长度 %d 与 totalLength %d 不一致"; public static final String EXCEPTION_INVALID_BUFFER_SIZE_ARG_5131C6CA = diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java index d3b42c8bbebad..a9f40317e8ea3 100644 --- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java +++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java @@ -386,7 +386,8 @@ private void addResponse( if (offsetSet) { if (totalLength <= 0) { throw new IllegalArgumentException( - RpcMessages.EXCEPTION_CHUNKED_RESULT_MISSING_TOTAL_LENGTH_FOR_FIRST_FRAGMENT_1D11374F); + RpcMessages + .EXCEPTION_MISSING_TOTALLENGTH_FOR_THE_FIRST_FRAGMENT_OF_A_CHUNKED_TSBLOCK_1D11374F); } fragmentBuffer = new ResultFragmentBuffer(totalLength); fragmentBuffer.append(blocks.get(blockIndex)); @@ -413,7 +414,8 @@ private int getNextOffset() { private void append(ByteBuffer fragment) { if (!fragment.hasRemaining()) { throw new IllegalArgumentException( - RpcMessages.EXCEPTION_CHUNKED_RESULT_EMPTY_FRAGMENT_03A0EF39); + RpcMessages + .EXCEPTION_EMPTY_FRAGMENT_RECEIVED_WHEN_ASSEMBLING_A_CHUNKED_QUERY_RESULT_03A0EF39); } // a fragment may be a slice of a larger buffer, so honor arrayOffset and position buffer.write( @@ -427,7 +429,7 @@ private void updateOffset(int nextOffset) { throw new IllegalArgumentException( String.format( RpcMessages - .EXCEPTION_CHUNKED_RESULT_FRAGMENT_OFFSET_ARG_NOT_GREATER_THAN_ACCUMULATED_LENGTH_ARG_A0F032E3, + .EXCEPTION_FRAGMENT_OFFSET_ARG_DOES_NOT_MATCH_THE_ACCUMULATED_LENGTH_ARG_1EB45347, nextOffset, buffer.size())); } @@ -439,7 +441,7 @@ private ByteBuffer toByteBuffer() { throw new IllegalArgumentException( String.format( RpcMessages - .EXCEPTION_CHUNKED_RESULT_ASSEMBLED_LENGTH_ARG_DOES_NOT_MATCH_TOTAL_LENGTH_ARG_925DCB15, + .EXCEPTION_ASSEMBLED_FRAGMENT_LENGTH_ARG_DOES_NOT_MATCH_THE_TOTAL_LENGTH_ARG_925DCB15, buffer.size(), totalLength)); } diff --git a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java index 51ec654ad1fdf..639c9240c3bb2 100644 --- a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java +++ b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java @@ -4976,10 +4976,10 @@ private DataNodeQueryMessages() {} "%s: %s ms, %s invocations, %s applications"; public static final String - EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_DOES_NOT_MATCH_PENDING_FRAGMENT_OFFSET_ARG_A1B13DBC = + EXCEPTION_REQUEST_OFFSET_ARG_DOES_NOT_MATCH_THE_PENDING_RESULT_FRAGMENT_OFFSET_ARG_A1B13DBC = "Request offset %d does not match the pending result fragment offset %d"; public static final String - EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_IS_INVALID_WITHOUT_PENDING_FRAGMENT_C403454F = + EXCEPTION_REQUEST_OFFSET_ARG_IS_INVALID_BECAUSE_NO_RESULT_FRAGMENT_IS_PENDING_C403454F = "Request offset %d is invalid because no result fragment is pending"; public static final String EXCEPTION_NOT_ENOUGH_MEMORY_WHILE_ANALYZING_TIME_PARTITIONS_NARROW_THE_QUERY_TIME_RANGE_OR_INCREASE_QUERY_MEMORY_MEMORY_DETAILS_REQUESTED_THIS_TIME_ARG_CURRENT_FREE_MEMORY_ARG_ORIGINAL_ERROR_ARG_37676B2A = diff --git a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java index 8fe4714c4d0b9..c9db6e545002f 100644 --- a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java +++ b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java @@ -5677,10 +5677,10 @@ private DataNodeQueryMessages() {} "%s:%s ms,调用 %s 次,应用 %s 次"; public static final String - EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_DOES_NOT_MATCH_PENDING_FRAGMENT_OFFSET_ARG_A1B13DBC = + EXCEPTION_REQUEST_OFFSET_ARG_DOES_NOT_MATCH_THE_PENDING_RESULT_FRAGMENT_OFFSET_ARG_A1B13DBC = "请求 offset %d 与待发送结果分片的 offset %d 不一致"; public static final String - EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_IS_INVALID_WITHOUT_PENDING_FRAGMENT_C403454F = + EXCEPTION_REQUEST_OFFSET_ARG_IS_INVALID_BECAUSE_NO_RESULT_FRAGMENT_IS_PENDING_C403454F = "请求 offset %d 无效:当前没有待发送的结果分片"; public static final String EXCEPTION_NOT_ENOUGH_MEMORY_WHILE_ANALYZING_TIME_PARTITIONS_NARROW_THE_QUERY_TIME_RANGE_OR_INCREASE_QUERY_MEMORY_MEMORY_DETAILS_REQUESTED_THIS_TIME_ARG_CURRENT_FREE_MEMORY_ARG_ORIGINAL_ERROR_ARG_37676B2A = diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java index 41d7f514319dd..0bbf5f23f0fa4 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java @@ -396,49 +396,56 @@ private static int getMaxResultPayloadSize(IClientSession clientSession) { : 0; } - private static boolean canFitInOneResponse( - ByteBuffer cachedResult, IClientSession clientSession) { - if (cachedResult == null) { - return true; - } - int maxPayloadSize = getMaxResultPayloadSize(clientSession); - return maxPayloadSize <= 0 || cachedResult.remaining() <= maxPayloadSize; + private static boolean canFitInOneResponse(ByteBuffer cachedResult, int maxPayloadSize) { + return cachedResult == null + || maxPayloadSize <= 0 + || cachedResult.remaining() <= maxPayloadSize; } /** * Transfers an oversized materialized result in fragments. The execution is registered so that * {@code fetchResultsV2} can continue from the pending fragment, and it is cleaned up once the * last fragment is sent or the query is closed. + * + * @param maxPayloadSize the payload bound read once by the caller, so that the decision to + * fragment and the transfer itself use the same hot-reloadable value */ private TSExecuteStatementResp respondWithFragmentedResult( long queryId, ByteBuffer cachedResult, int fetchSize, long timeout, + int maxPayloadSize, + String sql, IClientSession clientSession) throws IoTDBException, IOException { MaterializedResultExecution execution = new MaterializedResultExecution( - queryId, + COORDINATOR.createQueryId(), cachedResult, DatasetHeaderFactory.getLastQueryHeader(), StatementType.FAST_LAST_QUERY.name(), + sql, timeout, clientSession.getUsername(), clientSession.getClientAddress()); COORDINATOR.registerQueryExecution(queryId, execution); - TSExecuteStatementResp resp = - createResponse(DatasetHeaderFactory.getLastQueryHeader(), queryId); - resp.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS, "")); - boolean finished = - SELECT_RESULT.apply(resp, execution, fetchSize, getMaxResultPayloadSize(clientSession)); - // the cached result exceeds the payload bound, so the first response is always a fragment - checkState( - !finished, - DataNodeMiscMessages - .EXCEPTION_THE_FIRST_RESPONSE_OF_A_FRAGMENTED_FAST_LAST_RESULT_MUST_NOT_BE_COMPLETE_DEF121D5); - resp.setMoreData(true); - return resp; + try { + TSExecuteStatementResp resp = + createResponse(DatasetHeaderFactory.getLastQueryHeader(), queryId); + resp.setStatus(RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS, "")); + boolean finished = SELECT_RESULT.apply(resp, execution, fetchSize, maxPayloadSize); + // the cached result exceeds the payload bound, so the first response is always a fragment + checkState( + !finished, + DataNodeMiscMessages + .EXCEPTION_THE_FIRST_RESPONSE_OF_A_FRAGMENTED_FAST_LAST_RESULT_MUST_NOT_BE_COMPLETE_DEF121D5); + resp.setMoreData(true); + return resp; + } catch (Throwable t) { + COORDINATOR.cleanupQueryExecution(queryId); + throw t; + } } public ClientRPCServiceImpl() { @@ -1360,12 +1367,15 @@ public TSExecuteStatementResp executeFastLastDataQueryForOnePrefixPath( } ByteBuffer cachedResult = builder.isEmpty() ? null : serde.serialize(builder.build()); - if (!canFitInOneResponse(cachedResult, clientSession)) { + int maxPayloadSize = getMaxResultPayloadSize(clientSession); + if (!canFitInOneResponse(cachedResult, maxPayloadSize)) { return respondWithFragmentedResult( queryId, cachedResult, req.isSetFetchSize() ? req.getFetchSize() : -1, req.isSetTimeout() ? req.getTimeout() : config.getQueryTimeoutThreshold(), + maxPayloadSize, + "SELECT LAST " + String.join(".", req.getPrefixes()) + ".**", clientSession); } @@ -1544,12 +1554,15 @@ public TSExecuteStatementResp executeFastLastDataQueryForOneDeviceV2( // cache hit if (allCached) { ByteBuffer cachedResult = builder.isEmpty() ? null : serde.serialize(builder.build()); - if (!canFitInOneResponse(cachedResult, clientSession)) { + int maxPayloadSize = getMaxResultPayloadSize(clientSession); + if (!canFitInOneResponse(cachedResult, maxPayloadSize)) { return respondWithFragmentedResult( queryId, cachedResult, req.isSetFetchSize() ? req.getFetchSize() : -1, req.isSetTimeout() ? req.getTimeout() : config.getQueryTimeoutThreshold(), + maxPayloadSize, + "SELECT LAST " + String.join(", ", convert(req).getPaths()), clientSession); } TSExecuteStatementResp resp = @@ -1717,12 +1730,15 @@ public TSFetchResultsResp fetchResultsV2(TSFetchResultsReq req) { statementType = queryExecution.getStatementType(); try (SetThreadName queryName = new SetThreadName(queryExecution.getQueryId())) { + // the chunking capability is announced once at openSession, so the continuation path + // must not decide by the presence of offset: a request without offset while a fragment is + // pending then fails the offset check loudly instead of silently dropping the tail QueryDataSetUtils.ResultChunk chunk = fetchResultChunk( queryExecution, req.fetchSize, req.isSetOffset() ? req.getOffset() : 0, - req.isSetOffset() ? config.getMppDataExchangeMaxPayloadSizeInBytes() : 0); + getMaxResultPayloadSize(clientSession)); finished = fillQueryResult(resp, chunk); resp.setHasResultSet(!chunk.getQueryResult().isEmpty()); resp.setIsAlign(true); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java index aac8064f58ba0..0d8423803618d 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/SharedTsBlockQueue.java @@ -67,13 +67,6 @@ public class SharedTsBlockQueue { private long bufferRetainedSizeInBytes = 0L; - /** - * The memory in bytes taken out with {@code retainMemory=true}. Not counted in {@link - * #bufferRetainedSizeInBytes}: the block is no longer buffered here, so it must not block the - * producer. - */ - private long pendingRetainedSizeInBytes = 0L; - private final Queue> queue = new LinkedList<>(); private SettableFuture blocked = SettableFuture.create(); @@ -206,19 +199,13 @@ public void setNoMoreTsBlocks(boolean noMoreTsBlocks) { } } - public TsBlock remove() { - return remove(false); - } - /** * Remove a TsBlock from the head of the queue and return. Should be invoked only when the future - * returned by {@link #isBlocked()} completes. If {@code retainMemory} is true, the memory - * reserved for the block is not freed; the caller must release it via {@link - * #freeRetainedMemory()}. + * returned by {@link #isBlocked()} completes. * * @throws IllegalStateException Cannot remove TsBlock from closed queue. */ - public TsBlock remove(boolean retainMemory) { + public TsBlock remove() { if (closed) { // try throw underlying exception instead of "Source handle is aborted." if (abortedCause != null) { @@ -237,18 +224,14 @@ public TsBlock remove(boolean retainMemory) { Pair tsBlockWithReservedBytes = queue.remove(); long reservedBytes = tsBlockWithReservedBytes.right; if (reservedBytes > 0) { + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + reservedBytes); bufferRetainedSizeInBytes -= reservedBytes; - if (retainMemory) { - pendingRetainedSizeInBytes += reservedBytes; - } else { - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - reservedBytes); - } } // Every time LocalSourceHandle consumes a TsBlock, it needs to send the event // to @@ -262,22 +245,6 @@ public TsBlock remove(boolean retainMemory) { return tsBlockWithReservedBytes.left; } - /** Releases the memory of the blocks taken out with {@code retainMemory=true}. */ - public void freeRetainedMemory() { - if (pendingRetainedSizeInBytes <= 0) { - return; - } - long retainedSize = pendingRetainedSizeInBytes; - pendingRetainedSizeInBytes = 0; - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - retainedSize); - } - /** * Add TsBlocks to the queue. Except the first invocation, this method should be invoked only when * the returned future of last invocation completes. @@ -376,7 +343,16 @@ public void close() { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } queue.clear(); - freeAllRetainedMemory(); + if (bufferRetainedSizeInBytes > 0L) { + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + bufferRetainedSizeInBytes); + bufferRetainedSizeInBytes = 0; + } if (sinkChannel != null) { // attention: LocalSinkChannel of this SharedTsBlockQueue could be null when we // close @@ -411,7 +387,16 @@ public void abort() { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } queue.clear(); - freeAllRetainedMemory(); + if (bufferRetainedSizeInBytes > 0L) { + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + bufferRetainedSizeInBytes); + bufferRetainedSizeInBytes = 0; + } } /** Destroy the queue and cancel the future. Should only be called in abnormal case */ @@ -431,25 +416,19 @@ public void abort(Throwable t) { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } queue.clear(); - freeAllRetainedMemory(); - } - - public Optional getAbortedCause() { - return Optional.ofNullable(abortedCause); - } - - private void freeAllRetainedMemory() { - long totalRetainedSize = bufferRetainedSizeInBytes + pendingRetainedSizeInBytes; - if (totalRetainedSize > 0L) { + if (bufferRetainedSizeInBytes > 0L) { localMemoryManager .getQueryPool() .free( localFragmentInstanceId.getQueryId(), fullFragmentInstanceId, localPlanNodeId, - totalRetainedSize); + bufferRetainedSizeInBytes); bufferRetainedSizeInBytes = 0; - pendingRetainedSizeInBytes = 0; } } + + public Optional getAbortedCause() { + return Optional.ofNullable(abortedCause); + } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java index 210c2ea48008d..32e5557b8f922 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/ISourceHandle.java @@ -50,19 +50,9 @@ public interface ISourceHandle extends Accountable { * iterator with receive(). When one of these two methods is called, the cursor in iterator will * forward. * - * @param retainMemory if true, the memory that the returned block retains in the memory pool is - * not freed when the block is taken out of this handle. The caller must release it via {@link - * #freeRetainedMemory()} once the block is no longer needed. Implementations whose serialized - * blocks are not reserved from the memory pool ignore this flag. * @throws IoTDBException throw IoTDBException */ - ByteBuffer getSerializedTsBlock(boolean retainMemory) throws IoTDBException; - - /** - * Releases the memory retained by the blocks taken out with {@code retainMemory=true} and not - * released yet. It is safe to call when nothing is retained, and after abort/close. - */ - void freeRetainedMemory(); + ByteBuffer getSerializedTsBlock() throws IoTDBException; /** If there are more TsBlocks. */ boolean isFinished(); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java index 7ae26323c2e23..544421712669a 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/LocalSourceHandle.java @@ -124,10 +124,6 @@ public long getBufferRetainedSizeInBytes() { @Override public TsBlock receive() { - return receive(false); - } - - private TsBlock receive(boolean retainMemory) { long startTime = System.nanoTime(); try (SetThreadName sourceHandleName = new SetThreadName(threadName)) { checkState(); @@ -137,7 +133,7 @@ private TsBlock receive(boolean retainMemory) { } TsBlock tsBlock; synchronized (queue) { - tsBlock = queue.remove(retainMemory); + tsBlock = queue.remove(); } if (tsBlock != null) { if (LOGGER.isDebugEnabled()) { @@ -157,8 +153,8 @@ private TsBlock receive(boolean retainMemory) { } @Override - public ByteBuffer getSerializedTsBlock(boolean retainMemory) throws IoTDBException { - TsBlock tsBlock = receive(retainMemory); + public ByteBuffer getSerializedTsBlock() throws IoTDBException { + TsBlock tsBlock = receive(); if (LOGGER.isDebugEnabled()) { LOGGER.debug(DataNodeQueryMessages.GET_SERIALIZED_TSBLOCK, CommonUtils.toString(tsBlock)); } @@ -178,13 +174,6 @@ public ByteBuffer getSerializedTsBlock(boolean retainMemory) throws IoTDBExcepti } } - @Override - public void freeRetainedMemory() { - synchronized (queue) { - queue.freeRetainedMemory(); - } - } - @Override public boolean isFinished() { synchronized (queue) { diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java index 703ce3407f6a3..8ee7ad601ef61 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/execution/exchange/source/SourceHandle.java @@ -109,13 +109,6 @@ public class SourceHandle implements ISourceHandle { /** The actual buffered memory in bytes, including the amount of memory being reserved. */ private long bufferRetainedSizeInBytes = 0L; - /** - * The memory in bytes taken out of the buffer with {@code retainMemory=true}, still reserved in - * the memory pool on behalf of the caller. Freed by {@link #freeRetainedMemory()}, or by - * abort/close as a fallback. - */ - private long pendingRetainedSizeInBytes = 0L; - private int currSequenceId = 0; private int nextSequenceId = 0; private int lastSequenceId = Integer.MAX_VALUE; @@ -239,7 +232,7 @@ public SourceHandle( @Override public synchronized TsBlock receive() { - ByteBuffer tsBlock = getSerializedTsBlock(false); + ByteBuffer tsBlock = getSerializedTsBlock(); if (tsBlock != null) { long startTime = System.nanoTime(); try { @@ -254,7 +247,7 @@ public synchronized TsBlock receive() { } @Override - public synchronized ByteBuffer getSerializedTsBlock(boolean retainMemory) { + public synchronized ByteBuffer getSerializedTsBlock() { long startTime = System.nanoTime(); try (SetThreadName sourceHandleName = new SetThreadName(threadName)) { checkState(); @@ -277,17 +270,13 @@ public synchronized ByteBuffer getSerializedTsBlock(boolean retainMemory) { currSequenceId += 1; if (retainedSize > 0) { bufferRetainedSizeInBytes -= retainedSize; - if (retainMemory) { - pendingRetainedSizeInBytes += retainedSize; - } else { - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - retainedSize); - } + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + retainedSize); } if (sequenceIdToTsBlock.isEmpty() && !isFinished()) { @@ -307,22 +296,6 @@ public synchronized ByteBuffer getSerializedTsBlock(boolean retainMemory) { } } - @Override - public synchronized void freeRetainedMemory() { - if (pendingRetainedSizeInBytes <= 0) { - return; - } - long retainedSize = pendingRetainedSizeInBytes; - pendingRetainedSizeInBytes = 0; - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - retainedSize); - } - private synchronized void trySubmitGetDataBlocksTask() { if (aborted || closed) { return; @@ -471,7 +444,16 @@ public synchronized void abort() { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } sequenceIdToDataBlockSize.clear(); - freeAllRetainedMemory(); + if (bufferRetainedSizeInBytes > 0) { + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + bufferRetainedSizeInBytes); + bufferRetainedSizeInBytes = 0; + } aborted = true; sourceHandleListener.onAborted(this); } @@ -490,7 +472,16 @@ public synchronized void abort(Throwable t) { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } sequenceIdToDataBlockSize.clear(); - freeAllRetainedMemory(); + if (bufferRetainedSizeInBytes > 0) { + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + bufferRetainedSizeInBytes); + bufferRetainedSizeInBytes = 0; + } aborted = true; sourceHandleListener.onAborted(this); } @@ -509,7 +500,16 @@ public synchronized void close() { bufferRetainedSizeInBytes -= localMemoryManager.getQueryPool().tryCancel(blockedOnMemory); } sequenceIdToDataBlockSize.clear(); - freeAllRetainedMemory(); + if (bufferRetainedSizeInBytes > 0) { + localMemoryManager + .getQueryPool() + .free( + localFragmentInstanceId.getQueryId(), + fullFragmentInstanceId, + localPlanNodeId, + bufferRetainedSizeInBytes); + bufferRetainedSizeInBytes = 0; + } closed = true; executorService.submit(new SendCloseSinkChannelEventTask()); currSequenceId = lastSequenceId + 1; @@ -517,25 +517,6 @@ public synchronized void close() { } } - /** - * Frees the buffered memory and the memory handed over with {@code retainMemory=true}, as a - * fallback for blocks the caller has not released yet. - */ - private void freeAllRetainedMemory() { - long totalRetainedSize = bufferRetainedSizeInBytes + pendingRetainedSizeInBytes; - if (totalRetainedSize > 0) { - localMemoryManager - .getQueryPool() - .free( - localFragmentInstanceId.getQueryId(), - fullFragmentInstanceId, - localPlanNodeId, - totalRetainedSize); - bufferRetainedSizeInBytes = 0; - pendingRetainedSizeInBytes = 0; - } - } - @Override public boolean isFinished() { return remoteTsBlockedConsumedUp(); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java index 4a5fa22e1c462..f077815c33755 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java @@ -43,13 +43,7 @@ public interface IQueryExecution { Optional getBatchResult() throws IoTDBException; - /** - * Gets the next serialized TsBlock of the result. If {@code retainMemory} is true, the memory - * that the returned block retains in the memory pool is not freed when the block is taken out of - * the result handle. The caller then hands the block over to {@link #getPendingResultFragment()}, - * which releases the memory once the block has been fully sent. - */ - Optional getByteBufferBatchResult(boolean retainMemory) throws IoTDBException; + Optional getByteBufferBatchResult() throws IoTDBException; PendingResultFragment getPendingResultFragment(); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java index 07e6ec8bf0892..36a2bdbdc3d20 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java @@ -21,15 +21,11 @@ import org.apache.iotdb.commons.queryengine.common.SqlDialect; import org.apache.iotdb.commons.utils.TestOnly; -import org.apache.iotdb.db.queryengine.common.FragmentInstanceId; import org.apache.iotdb.db.queryengine.common.QueryId; import org.apache.iotdb.db.queryengine.common.header.DatasetHeader; import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeService; import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager; -import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool; -import org.apache.iotdb.db.queryengine.execution.memory.MemorySide; import org.apache.iotdb.db.queryengine.plan.analyze.QueryType; -import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId; import org.apache.iotdb.rpc.RpcUtils; import org.apache.iotdb.rpc.TSStatusCode; @@ -46,37 +42,31 @@ */ public class MaterializedResultExecution implements IQueryExecution { - private static final String PLAN_NODE_ID = "lastCacheResult"; - - private final long queryId; + private final QueryId queryId; private final DatasetHeader datasetHeader; private final String statementType; + private final String executeSQL; private final long startExecutionTime = System.currentTimeMillis(); private final long timeout; private final String user; private final String clientHostname; - private final LocalMemoryManager localMemoryManager; - private final TFragmentInstanceId fragmentInstanceId; - private final String fullFragmentInstanceId; - - private final PendingResultFragment pendingResultFragment = - new PendingResultFragment(this::freeReservedMemory); + private final QueryOwnedFragmentAccount fragmentAccount; + private final PendingResultFragment pendingResultFragment; /** The serialized result, handed over once. */ private ByteBuffer serializedResult; - private long reservedBytes; - private volatile long startTimeOfCurrentRpc = System.nanoTime(); private long totalExecutionTime; private boolean cleanedUp; public MaterializedResultExecution( - long queryId, + QueryId queryId, ByteBuffer serializedResult, DatasetHeader datasetHeader, String statementType, + String executeSQL, long timeout, String user, String clientHostname) { @@ -85,6 +75,7 @@ public MaterializedResultExecution( serializedResult, datasetHeader, statementType, + executeSQL, timeout, user, clientHostname, @@ -93,10 +84,11 @@ public MaterializedResultExecution( @TestOnly MaterializedResultExecution( - long queryId, + QueryId queryId, ByteBuffer serializedResult, DatasetHeader datasetHeader, String statementType, + String executeSQL, long timeout, String user, String clientHostname, @@ -105,41 +97,13 @@ public MaterializedResultExecution( this.serializedResult = serializedResult; this.datasetHeader = datasetHeader; this.statementType = statementType; + this.executeSQL = executeSQL; this.timeout = timeout; this.user = user; this.clientHostname = clientHostname; - this.localMemoryManager = localMemoryManager; - this.fragmentInstanceId = new TFragmentInstanceId(Long.toString(queryId), 0, PLAN_NODE_ID); - this.fullFragmentInstanceId = - FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(fragmentInstanceId); - reserveResult(); - } - - /** Best-effort accounting: a result too large for one reservation still gets transferred. */ - private void reserveResult() { - MemoryPool queryPool = localMemoryManager.getQueryPool(); - queryPool.registerPlanNodeIdToQueryMemoryMap( - Long.toString(queryId), fullFragmentInstanceId, PLAN_NODE_ID, MemorySide.OUTPUT); - long maxBytesCanReserve = queryPool.getMaxBytes(); - long bytesToReserve = Math.min(serializedResult.remaining(), maxBytesCanReserve); - if (queryPool.tryReserve( - Long.toString(queryId), - fullFragmentInstanceId, - PLAN_NODE_ID, - bytesToReserve, - maxBytesCanReserve)) { - reservedBytes = bytesToReserve; - } - } - - private synchronized void freeReservedMemory() { - if (reservedBytes > 0) { - localMemoryManager - .getQueryPool() - .free(Long.toString(queryId), fullFragmentInstanceId, PLAN_NODE_ID, reservedBytes); - reservedBytes = 0; - } + this.fragmentAccount = new QueryOwnedFragmentAccount(localMemoryManager, queryId.getId()); + this.pendingResultFragment = new PendingResultFragment(fragmentAccount); } private synchronized void cleanup() { @@ -148,11 +112,12 @@ private synchronized void cleanup() { } cleanedUp = true; pendingResultFragment.clear(); - freeReservedMemory(); - localMemoryManager - .getQueryPool() - .deRegisterFragmentInstanceFromQueryMemoryMap( - Long.toString(queryId), fullFragmentInstanceId, false); + serializeResult(null); + fragmentAccount.deregister(); + } + + private synchronized void serializeResult(ByteBuffer result) { + serializedResult = result; } @Override @@ -177,8 +142,7 @@ public void cancel() { @Override public ExecutionResult getStatus() { - return new ExecutionResult( - new QueryId(Long.toString(queryId)), RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS)); + return new ExecutionResult(queryId, RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS)); } @Override @@ -187,7 +151,7 @@ public Optional getBatchResult() { } @Override - public Optional getByteBufferBatchResult(boolean retainMemory) { + public synchronized Optional getByteBufferBatchResult() { ByteBuffer result = serializedResult; serializedResult = null; return Optional.ofNullable(result); @@ -199,7 +163,7 @@ public PendingResultFragment getPendingResultFragment() { } @Override - public boolean hasNextResult() { + public synchronized boolean hasNextResult() { return serializedResult != null; } @@ -230,7 +194,7 @@ public boolean isUserQuery() { @Override public String getQueryId() { - return Long.toString(queryId); + return queryId.getId(); } @Override @@ -267,7 +231,7 @@ public long getTimeout() { @Override public Optional getExecuteSQL() { - return Optional.empty(); + return Optional.of(executeSQL); } @Override diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java index 21b988d2a77f5..da65df1b17173 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java @@ -26,43 +26,70 @@ * to be sent in the following response(s). The block is already serialized and removed from the * result handle, so this fragment only holds a reference to it and never copies the payload. * - *

The memory the block retains stays reserved in the memory pool until the block is fully sent. - * The owner binds a {@code memoryReleaser} which is invoked by {@link #clear()}, so callers only - * interact with this holder and never release the memory themselves. + *

The memory of the pending block is reserved from the query memory pool through an {@link + * Account} owned by the query, and released when the fragment is {@link #clear() cleared}. The + * account lives with the query rather than with the result handle, so the reservation covers the + * whole lifetime of the fragment on the local, the remote and the in-memory result paths. */ public class PendingResultFragment { - private final Runnable memoryReleaser; + /** Reserves and releases the memory of a pending block; {@code null} disables accounting. */ + public interface Account { + + /** Returns the bytes actually reserved, which may be fewer than requested. */ + long reserve(long bytes); + + void free(long bytes); + } + + private final Account account; private ByteBuffer block; private int nextOffset; + private long reservedBytes; - public PendingResultFragment(Runnable memoryReleaser) { - this.memoryReleaser = memoryReleaser; + public PendingResultFragment(Account account) { + this.account = account; } - /** Records that {@code block} has been sent up to {@code nextOffset} bytes. */ - public void reset(ByteBuffer block, int nextOffset) { - this.block = block; + /** + * Records that {@code block} has been sent up to {@code nextOffset} bytes. When a new block + * becomes pending, its serialized length is reserved from the query memory pool. + */ + public synchronized void reset(ByteBuffer block, int nextOffset) { + if (block != this.block) { + release(); + this.block = block; + if (account != null) { + reservedBytes = account.reserve(block.remaining()); + } + } this.nextOffset = nextOffset; } - public ByteBuffer getBlock() { + public synchronized ByteBuffer getBlock() { return block; } - public int getNextOffset() { + public synchronized int getNextOffset() { return nextOffset; } /** - * Discards the pending fragment and releases the memory of the blocks that have been fully sent. - * It is also called right after a complete block has been added to a response, when nothing is - * pending. Calling it repeatedly is a no-op. + * Discards the pending fragment and releases the memory of the pending block. It is also called + * right after a complete block has been added to a response, when nothing is pending. Calling it + * repeatedly is a no-op. */ - public void clear() { + public synchronized void clear() { block = null; nextOffset = 0; - memoryReleaser.run(); + release(); + } + + private void release() { + if (reservedBytes > 0) { + account.free(reservedBytes); + reservedBytes = 0; + } } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java index d07319e44c641..cbfab2500212d 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java @@ -113,9 +113,10 @@ public class QueryExecution implements IQueryExecution { // used for cleaning resultHandle up exactly once private final AtomicBoolean resultHandleCleanUp; - // the serialized TsBlock that is partially sent to the client and waits for the next RPC - private final PendingResultFragment pendingResultFragment = - new PendingResultFragment(this::releaseResultHandleRetainedMemory); + // the serialized TsBlock that is partially sent to the client and waits for the next RPC; its + // memory is reserved under the account below, which lives as long as the query + private final PendingResultFragment pendingResultFragment; + private final QueryOwnedFragmentAccount pendingFragmentAccount; private final UDFClassLoaderManager.QueryScope udfQueryScope; @@ -138,6 +139,8 @@ public class QueryExecution implements IQueryExecution { public QueryExecution(IPlanner planner, MPPQueryContext context, ExecutorService executor) { this.context = context; this.planner = planner; + this.pendingFragmentAccount = new QueryOwnedFragmentAccount(context.getQueryId().getId()); + this.pendingResultFragment = new PendingResultFragment(pendingFragmentAccount); UDFClassLoaderManager classLoaderManager = UDFClassLoaderManager.getInstance(); this.udfQueryScope = classLoaderManager == null @@ -556,6 +559,9 @@ private void releaseResource(Throwable t, boolean isRetry) { // the query will not serve any more fetch requests, release the pending fragment's memory and // make sure a retried execution does not continue a stale fragment pendingResultFragment.clear(); + if (!isRetry) { + pendingFragmentAccount.deregister(); + } // close ResultHandle to unblock client's getResult request // Actually, we should not close the ResultHandle when the QueryExecution is Finished. // There are only two scenarios where the ResultHandle should be closed: @@ -724,8 +730,8 @@ private TsBlock getDeserializedTsBlock() { } @Override - public Optional getByteBufferBatchResult(boolean retainMemory) throws IoTDBException { - return getResult(() -> resultHandle.getSerializedTsBlock(retainMemory)); + public Optional getByteBufferBatchResult() throws IoTDBException { + return getResult(this::getSerializedTsBlock); } @Override @@ -733,10 +739,8 @@ public PendingResultFragment getPendingResultFragment() { return pendingResultFragment; } - private void releaseResultHandleRetainedMemory() { - if (resultHandle != null) { - resultHandle.freeRetainedMemory(); - } + private ByteBuffer getSerializedTsBlock() throws IoTDBException { + return resultHandle.getSerializedTsBlock(); } /** diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java new file mode 100644 index 0000000000000..3ed925011d61b --- /dev/null +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java @@ -0,0 +1,97 @@ +/* + * 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.execution; + +import org.apache.iotdb.db.queryengine.common.FragmentInstanceId; +import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeService; +import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager; +import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool; +import org.apache.iotdb.db.queryengine.execution.memory.MemorySide; +import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId; + +/** + * Reserves the memory of a pending result fragment under an account owned by the query. The account + * outlives the result handle that produced the block, so a block waiting for the following RPCs + * stays accounted from the moment it becomes pending until the last fragment is sent. Reservations + * are best effort: a block larger than the budget is still transferred. + */ +public class QueryOwnedFragmentAccount implements PendingResultFragment.Account { + + private static final String PLAN_NODE_ID = "pendingResultFragment"; + + private final String queryId; + private final String fragmentInstanceId; + private LocalMemoryManager localMemoryManager; + private boolean closed; + + /** Resolves the local memory manager lazily, so constructing a query does not start it. */ + public QueryOwnedFragmentAccount(String queryId) { + this.queryId = queryId; + this.fragmentInstanceId = + FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId( + new TFragmentInstanceId(queryId, 0, PLAN_NODE_ID)); + } + + public QueryOwnedFragmentAccount(LocalMemoryManager localMemoryManager, String queryId) { + this(queryId); + this.localMemoryManager = localMemoryManager; + } + + private LocalMemoryManager getLocalMemoryManager() { + if (localMemoryManager == null) { + localMemoryManager = + MPPDataExchangeService.getInstance().getMPPDataExchangeManager().getLocalMemoryManager(); + } + return localMemoryManager; + } + + @Override + public synchronized long reserve(long bytes) { + if (closed) { + return 0; + } + MemoryPool queryPool = getLocalMemoryManager().getQueryPool(); + long maxBytesCanReserve = queryPool.getMaxBytes(); + long bytesToReserve = Math.min(bytes, maxBytesCanReserve); + if (bytesToReserve <= 0) { + return 0; + } + queryPool.registerPlanNodeIdToQueryMemoryMap( + queryId, fragmentInstanceId, PLAN_NODE_ID, MemorySide.OUTPUT); + if (queryPool.tryReserve( + queryId, fragmentInstanceId, PLAN_NODE_ID, bytesToReserve, maxBytesCanReserve)) { + return bytesToReserve; + } + return 0; + } + + @Override + public synchronized void free(long bytes) { + getLocalMemoryManager().getQueryPool().free(queryId, fragmentInstanceId, PLAN_NODE_ID, bytes); + } + + /** Stops accepting reservations and removes the account; call once the query is cleaned up. */ + public synchronized void deregister() { + closed = true; + getLocalMemoryManager() + .getQueryPool() + .deRegisterFragmentInstanceFromQueryMemoryMap(queryId, fragmentInstanceId, false); + } +} diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java index 621fd329d3b20..723a2030452fe 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java @@ -116,8 +116,8 @@ public class ConfigExecution implements IQueryExecution { private final IConfigTask task; private final IConfigTaskExecutor configTaskExecutor; - // the result set is not reserved from the memory pool, so nothing is released on clear - private final PendingResultFragment pendingResultFragment = new PendingResultFragment(() -> {}); + // the result set is not reserved from the memory pool, so nothing is accounted + private final PendingResultFragment pendingResultFragment = new PendingResultFragment(null); private final StatementType statementType; private long totalExecutionTime; @@ -281,8 +281,7 @@ public Optional getBatchResult() { } @Override - public Optional getByteBufferBatchResult(boolean retainMemory) throws IoTDBException { - // the result set is not reserved from the memory pool, so there is no memory to retain + public Optional getByteBufferBatchResult() throws IoTDBException { if (!resultSetConsumed) { resultSetConsumed = true; try { diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java index a325fc671a15e..7a4919446cf92 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/memory/MemorySourceHandle.java @@ -77,8 +77,7 @@ public synchronized TsBlock receive() { } @Override - public synchronized ByteBuffer getSerializedTsBlock(boolean retainMemory) throws IoTDBException { - // serialized blocks are not reserved from the memory pool, so retainMemory is ignored + public synchronized ByteBuffer getSerializedTsBlock() throws IoTDBException { hasNext = false; if (result.isEmpty()) { return null; @@ -91,11 +90,6 @@ public synchronized ByteBuffer getSerializedTsBlock(boolean retainMemory) throws } } - @Override - public void freeRetainedMemory() { - // nothing is reserved from the memory pool - } - @Override public synchronized boolean isFinished() { return !hasNext; diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java index a398d2613e922..79b3ea865479a 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java @@ -253,7 +253,7 @@ public static Pair, Boolean> convertQueryResultByFetchSize( long memorySize = 0; List res = new ArrayList<>(); while (rowCount < fetchSize && memorySize < MAX_RETURN_SIZE) { - Optional optionalByteBuffer = queryExecution.getByteBufferBatchResult(false); + Optional optionalByteBuffer = queryExecution.getByteBufferBatchResult(); if (!optionalByteBuffer.isPresent()) { break; } @@ -291,7 +291,7 @@ public static ResultChunk convertQueryResultByFetchSize( throw new IllegalArgumentException( String.format( DataNodeQueryMessages - .EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_DOES_NOT_MATCH_PENDING_FRAGMENT_OFFSET_ARG_A1B13DBC, + .EXCEPTION_REQUEST_OFFSET_ARG_DOES_NOT_MATCH_THE_PENDING_RESULT_FRAGMENT_OFFSET_ARG_A1B13DBC, requestOffset, nextOffset)); } @@ -310,22 +310,22 @@ public static ResultChunk convertQueryResultByFetchSize( throw new IllegalArgumentException( String.format( DataNodeQueryMessages - .EXCEPTION_RESULT_FRAGMENT_REQUEST_OFFSET_ARG_IS_INVALID_WITHOUT_PENDING_FRAGMENT_C403454F, + .EXCEPTION_REQUEST_OFFSET_ARG_IS_INVALID_BECAUSE_NO_RESULT_FRAGMENT_IS_PENDING_C403454F, requestOffset)); } while (rowCount < fetchSize && memorySize < MAX_RETURN_SIZE && remainingPayloadSize > 0) { - Optional optionalByteBuffer = queryExecution.getByteBufferBatchResult(true); + Optional optionalByteBuffer = queryExecution.getByteBufferBatchResult(); if (!optionalByteBuffer.isPresent()) { break; } - ByteBuffer byteBuffer = optionalByteBuffer.get(); + // make the offsets block-relative regardless of where the buffer comes from + ByteBuffer byteBuffer = optionalByteBuffer.get().slice(); int positionCount = readPositionCount(byteBuffer); if (positionCount == 0) { pendingFragment.clear(); continue; } - int blockStart = byteBuffer.position(); int blockSize = byteBuffer.remaining(); rowCount += positionCount; if (blockSize <= remainingPayloadSize) { @@ -334,10 +334,9 @@ public static ResultChunk convertQueryResultByFetchSize( memorySize += blockSize; pendingFragment.clear(); } else { - res.add(slice(byteBuffer, blockStart, remainingPayloadSize)); - pendingFragment.reset(byteBuffer, blockStart + remainingPayloadSize); - return ResultChunk.fragment( - res, blockStart + remainingPayloadSize, blockStart == 0 ? blockSize : -1, false); + res.add(slice(byteBuffer, 0, remainingPayloadSize)); + pendingFragment.reset(byteBuffer, remainingPayloadSize); + return ResultChunk.fragment(res, remainingPayloadSize, blockSize, false); } } return ResultChunk.complete(res, !queryExecution.hasNextResult()); diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java index e277418c2a328..b8c53352c09a5 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/protocol/session/QueryOwnershipTest.java @@ -298,7 +298,7 @@ private IQueryExecution mockQueryExecution(boolean hasNextResult) throws Excepti IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); Mockito.when(queryExecution.getQueryId()).thenReturn("query"); // the mock carries no buffered batch, only the "is there anything left" flag - Mockito.when(queryExecution.getByteBufferBatchResult(false)).thenReturn(Optional.empty()); + Mockito.when(queryExecution.getByteBufferBatchResult()).thenReturn(Optional.empty()); Mockito.when(queryExecution.hasNextResult()).thenReturn(hasNextResult); return queryExecution; } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/LocalSourceHandleTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/LocalSourceHandleTest.java index d67913df78b21..c4378f82efc9d 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/LocalSourceHandleTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/LocalSourceHandleTest.java @@ -19,7 +19,6 @@ package org.apache.iotdb.db.queryengine.execution.exchange; -import org.apache.iotdb.db.queryengine.common.FragmentInstanceId; import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeManager.SourceHandleListener; import org.apache.iotdb.db.queryengine.execution.exchange.source.LocalSourceHandle; import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager; @@ -27,15 +26,10 @@ import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId; import com.google.common.util.concurrent.ListenableFuture; -import org.apache.tsfile.enums.TSDataType; -import org.apache.tsfile.read.common.block.TsBlock; -import org.apache.tsfile.read.common.block.TsBlockBuilder; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; -import java.util.Collections; - import static com.google.common.util.concurrent.MoreExecutors.newDirectExecutorService; public class LocalSourceHandleTest { @@ -87,93 +81,6 @@ public void testReceive() { Mockito.verify(mockSourceHandleListener, Mockito.times(1)).onFinished(localSourceHandle); } - @Test - public void testRetainedMemoryIsFreedWhenTheHandedOverBlockIsReleased() throws Exception { - final String queryId = "q0"; - final String localPlanNodeId = "exchange_0"; - final TFragmentInstanceId localFragmentInstanceId = new TFragmentInstanceId(queryId, 0, "0"); - final String fullFragmentInstanceId = - FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(localFragmentInstanceId); - - LocalMemoryManager mockLocalMemoryManager = Mockito.mock(LocalMemoryManager.class); - MemoryPool mockMemoryPool = Utils.createMockNonBlockedMemoryPool(); - Mockito.when(mockLocalMemoryManager.getQueryPool()).thenReturn(mockMemoryPool); - SharedTsBlockQueue queue = - new SharedTsBlockQueue( - localFragmentInstanceId, - localPlanNodeId, - mockLocalMemoryManager, - newDirectExecutorService()); - LocalSourceHandle localSourceHandle = - new LocalSourceHandle( - localFragmentInstanceId, - localPlanNodeId, - queue, - Mockito.mock(SourceHandleListener.class)); - - TsBlock tsBlock = createRealTsBlock(); - long reservedSize = tsBlock.getSizeInBytes(); - queue.add(tsBlock); - queue.setNoMoreTsBlocks(true); - - localSourceHandle.getSerializedTsBlock(true); - Assert.assertEquals(0L, localSourceHandle.getBufferRetainedSizeInBytes()); - Mockito.verify(mockMemoryPool, Mockito.never()) - .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); - - localSourceHandle.freeRetainedMemory(); - Mockito.verify(mockMemoryPool, Mockito.times(1)) - .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); - - // releasing again is a no-op - localSourceHandle.freeRetainedMemory(); - Mockito.verify(mockMemoryPool, Mockito.times(1)) - .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); - } - - @Test - public void testAbortReleasesTheHandedOverMemory() throws Exception { - final String queryId = "q0"; - final String localPlanNodeId = "exchange_0"; - final TFragmentInstanceId localFragmentInstanceId = new TFragmentInstanceId(queryId, 0, "0"); - final String fullFragmentInstanceId = - FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId(localFragmentInstanceId); - - LocalMemoryManager mockLocalMemoryManager = Mockito.mock(LocalMemoryManager.class); - MemoryPool mockMemoryPool = Utils.createMockNonBlockedMemoryPool(); - Mockito.when(mockLocalMemoryManager.getQueryPool()).thenReturn(mockMemoryPool); - SharedTsBlockQueue queue = - new SharedTsBlockQueue( - localFragmentInstanceId, - localPlanNodeId, - mockLocalMemoryManager, - newDirectExecutorService()); - LocalSourceHandle localSourceHandle = - new LocalSourceHandle( - localFragmentInstanceId, - localPlanNodeId, - queue, - Mockito.mock(SourceHandleListener.class)); - - TsBlock tsBlock = createRealTsBlock(); - long reservedSize = tsBlock.getSizeInBytes(); - queue.add(tsBlock); - queue.setNoMoreTsBlocks(true); - localSourceHandle.getSerializedTsBlock(true); - Mockito.verify(mockMemoryPool, Mockito.never()) - .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); - - // abort frees the memory that was not released by the caller (fallback path) - localSourceHandle.abort(); - Mockito.verify(mockMemoryPool, Mockito.times(1)) - .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); - - // a late release after abort must not free the same memory twice - localSourceHandle.freeRetainedMemory(); - Mockito.verify(mockMemoryPool, Mockito.times(1)) - .free(queryId, fullFragmentInstanceId, localPlanNodeId, reservedSize); - } - @Test public void testAbort() { final String queryId = "q0"; @@ -212,13 +119,4 @@ public void testAbort() { Assert.assertFalse(localSourceHandle.isFinished()); Mockito.verify(mockSourceHandleListener, Mockito.times(1)).onAborted(localSourceHandle); } - - /** A real TsBlock so that the real serde can serialize it. */ - private static TsBlock createRealTsBlock() { - TsBlockBuilder builder = new TsBlockBuilder(Collections.singletonList(TSDataType.INT64)); - builder.getTimeColumnBuilder().writeLong(1L); - builder.getColumnBuilder(0).writeLong(10L); - builder.declarePosition(); - return builder.build(); - } } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java index f61266b198a44..95c9b810ce195 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/exchange/SourceHandleTest.java @@ -1079,120 +1079,4 @@ public void testForceClose() { Mockito.verify(mockSourceHandleListener, Mockito.timeout(10_0000).times(1)) .onAborted(sourceHandle); } - - @Test - public void testRetainedMemoryIsFreedWhenTheHandedOverBlockIsReleased() throws Exception { - final String queryId = "q0"; - final String localPlanNodeId = "exchange_0"; - final HandleWithPool handleWithPool = - createSourceHandleWithOneFetchedBlock(queryId, localPlanNodeId); - final SourceHandle sourceHandle = handleWithPool.handle; - final MemoryPool memoryPool = handleWithPool.memoryPool; - final String fragmentInstanceId = - FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId( - sourceHandle.getLocalFragmentInstanceId()); - - // The block is taken out of the buffer, but its memory stays reserved in the pool - Assert.assertNotNull(sourceHandle.getSerializedTsBlock(true)); - Assert.assertEquals(0L, sourceHandle.getBufferRetainedSizeInBytes()); - Mockito.verify(memoryPool, Mockito.never()) - .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); - - // The caller releases the memory once the block is fully sent - sourceHandle.freeRetainedMemory(); - Mockito.verify(memoryPool, Mockito.times(1)) - .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); - - // Releasing again is a no-op - sourceHandle.freeRetainedMemory(); - Mockito.verify(memoryPool, Mockito.times(1)) - .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); - } - - @Test - public void testAbortReleasesTheHandedOverMemory() throws Exception { - final String queryId = "q0"; - final String localPlanNodeId = "exchange_0"; - final HandleWithPool handleWithPool = - createSourceHandleWithOneFetchedBlock(queryId, localPlanNodeId); - final SourceHandle sourceHandle = handleWithPool.handle; - final MemoryPool memoryPool = handleWithPool.memoryPool; - final String fragmentInstanceId = - FragmentInstanceId.createFragmentInstanceIdFromTFragmentInstanceId( - sourceHandle.getLocalFragmentInstanceId()); - - Assert.assertNotNull(sourceHandle.getSerializedTsBlock(true)); - Mockito.verify(memoryPool, Mockito.never()) - .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); - - // abort frees the memory that was not released by the caller (fallback path) - sourceHandle.abort(); - Mockito.verify(memoryPool, Mockito.times(1)) - .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); - - // a late release after abort must not free the same memory twice - sourceHandle.freeRetainedMemory(); - Mockito.verify(memoryPool, Mockito.times(1)) - .free(queryId, fragmentInstanceId, localPlanNodeId, MOCK_TSBLOCK_SIZE); - } - - private static final class HandleWithPool { - private final SourceHandle handle; - private final MemoryPool memoryPool; - - private HandleWithPool(SourceHandle handle, MemoryPool memoryPool) { - this.handle = handle; - this.memoryPool = memoryPool; - } - } - - /** Creates a SourceHandle whose single pending block has already been fetched. */ - private HandleWithPool createSourceHandleWithOneFetchedBlock( - String queryId, String localPlanNodeId) throws Exception { - final TEndPoint remoteEndpoint = - new TEndPoint("remote", IoTDBDescriptor.getInstance().getConfig().getMppDataExchangePort()); - final TFragmentInstanceId remoteFragmentInstanceId = new TFragmentInstanceId(queryId, 1, "0"); - final TFragmentInstanceId localFragmentInstanceId = new TFragmentInstanceId(queryId, 0, "0"); - - final LocalMemoryManager mockLocalMemoryManager = Mockito.mock(LocalMemoryManager.class); - final MemoryPool mockMemoryPool = Utils.createMockNonBlockedMemoryPool(); - Mockito.when(mockLocalMemoryManager.getQueryPool()).thenReturn(mockMemoryPool); - final IClientManager mockClientManager = - Mockito.mock(IClientManager.class); - final SyncDataNodeMPPDataExchangeServiceClient mockClient = - Mockito.mock(SyncDataNodeMPPDataExchangeServiceClient.class); - Mockito.when(mockClientManager.borrowClient(remoteEndpoint)).thenReturn(mockClient); - Mockito.doAnswer( - invocation -> { - TGetDataBlockRequest req = invocation.getArgument(0); - List byteBuffers = new ArrayList<>(); - for (int i = req.getStartSequenceId(); i < req.getEndSequenceId(); i++) { - byteBuffers.add(ByteBuffer.allocate(0)); - } - return new TGetDataBlockResponse(byteBuffers); - }) - .when(mockClient) - .getDataBlock(Mockito.any(TGetDataBlockRequest.class)); - - final SourceHandle sourceHandle = - new SourceHandle( - remoteEndpoint, - remoteFragmentInstanceId, - localFragmentInstanceId, - localPlanNodeId, - 0, - mockLocalMemoryManager, - Executors.newSingleThreadExecutor(), - Utils.createMockTsBlockSerde(MOCK_TSBLOCK_SIZE), - Mockito.mock(SourceHandleListener.class), - mockClientManager); - - sourceHandle.isBlocked(); - sourceHandle.updatePendingDataBlockInfo(0, List.of(MOCK_TSBLOCK_SIZE)); - Mockito.verify(mockClient, Mockito.timeout(10_000).times(1)) - .getDataBlock(Mockito.any(TGetDataBlockRequest.class)); - Assert.assertTrue(sourceHandle.isBlocked().isDone()); - Assert.assertEquals(MOCK_TSBLOCK_SIZE, sourceHandle.getBufferRetainedSizeInBytes()); - return new HandleWithPool(sourceHandle, mockMemoryPool); - } } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java index a21757411cfcd..7087955329591 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java @@ -1809,7 +1809,7 @@ static class FakeQueryExecution implements IQueryExecution { private final long startTime; private final String queryId; private final String sql; - private final PendingResultFragment pendingResultFragment = new PendingResultFragment(() -> {}); + private final PendingResultFragment pendingResultFragment = new PendingResultFragment(null); FakeQueryExecution(long startTime, String queryId, String sql) { this.startTime = startTime; @@ -1891,7 +1891,7 @@ public long getTimeout() { } @Override - public Optional getByteBufferBatchResult(boolean retainMemory) { + public Optional getByteBufferBatchResult() { return Optional.empty(); } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java index e21ea0dab7622..0c3c6fa2a2a86 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java @@ -20,6 +20,7 @@ package org.apache.iotdb.db.queryengine.plan.execution; import org.apache.iotdb.commons.memory.MemoryManager; +import org.apache.iotdb.db.queryengine.common.QueryId; import org.apache.iotdb.db.queryengine.common.header.DatasetHeaderFactory; import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager; import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool; @@ -54,19 +55,19 @@ public void testFragmentedTransferAndMemoryAccounting() throws Exception { MaterializedResultExecution execution = new MaterializedResultExecution( - 1L, + new QueryId("20261119_050035_03054_1"), serialized.duplicate(), DatasetHeaderFactory.getLastQueryHeader(), "FAST_LAST_QUERY", + "SELECT LAST root.sg.d1.s1", 60_000L, "root", "127.0.0.1", localMemoryManager); + assertEquals(0L, memoryPool.getReservedBytes()); - // the serialized bytes are reserved from the query pool - assertEquals(totalLength, memoryPool.getReservedBytes()); - - // the result is served once; the first response carries a fragment of it + // the result is served once; the first response carries a fragment of it, and the serialized + // bytes of the pending block are reserved from the query pool QueryDataSetUtils.ResultChunk firstChunk = QueryDataSetUtils.convertQueryResultByFetchSize(execution, 10, 0, payloadSize); assertFalse(firstChunk.isFinished()); @@ -74,7 +75,8 @@ public void testFragmentedTransferAndMemoryAccounting() throws Exception { assertEquals(totalLength, firstChunk.getTotalLength()); assertEquals(payloadSize, firstChunk.getQueryResult().get(0).remaining()); assertFalse(execution.hasNextResult()); - assertTrue(execution.getByteBufferBatchResult(true).isEmpty()); + assertTrue(execution.getByteBufferBatchResult().isEmpty()); + assertEquals(totalLength, memoryPool.getReservedBytes()); // the following fetch completes the block and releases the reservation QueryDataSetUtils.ResultChunk secondChunk = @@ -98,14 +100,17 @@ public void testCleanupReleasesReservationWithoutFetch() throws Exception { MaterializedResultExecution execution = new MaterializedResultExecution( - 2L, + new QueryId("20261119_050035_03054_2"), serialized.duplicate(), DatasetHeaderFactory.getLastQueryHeader(), "FAST_LAST_QUERY", + "SELECT LAST root.sg.d1.s1", 60_000L, "root", "127.0.0.1", localMemoryManager); + + QueryDataSetUtils.convertQueryResultByFetchSize(execution, 10, 0, 64); assertEquals(serialized.remaining(), memoryPool.getReservedBytes()); // the client never fetches the rest @@ -124,19 +129,20 @@ public void testPartialReservationWhenResultExceedsPool() throws Exception { MaterializedResultExecution execution = new MaterializedResultExecution( - 3L, + new QueryId("20261119_050035_03054_3"), serialized.duplicate(), DatasetHeaderFactory.getLastQueryHeader(), "FAST_LAST_QUERY", + "SELECT LAST root.sg.d1.s1", 60_000L, "root", "127.0.0.1", localMemoryManager); - assertEquals(smallCapacity, memoryPool.getReservedBytes()); QueryDataSetUtils.ResultChunk chunk = QueryDataSetUtils.convertQueryResultByFetchSize(execution, 10, 0, 64); assertTrue(chunk.hasNextOffset()); + assertEquals(smallCapacity, memoryPool.getReservedBytes()); execution.stopAndCleanup(null); assertEquals(0L, memoryPool.getReservedBytes()); } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragmentTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragmentTest.java new file mode 100644 index 0000000000000..76e8daabc1282 --- /dev/null +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragmentTest.java @@ -0,0 +1,100 @@ +/* + * 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.execution; + +import org.junit.Test; + +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; + +public class PendingResultFragmentTest { + + @Test + public void testReserveWhenBlockBecomesPendingAndReleaseOnClear() { + RecordingAccount account = new RecordingAccount(); + PendingResultFragment fragment = new PendingResultFragment(account); + ByteBuffer block = ByteBuffer.allocate(100); + + fragment.reset(block, 30); + assertEquals(List.of(100L), account.reserved); + assertEquals(0, account.freed); + + // continuing the same block must not reserve again + fragment.reset(block, 60); + assertEquals(List.of(100L), account.reserved); + assertSame(block, fragment.getBlock()); + assertEquals(60, fragment.getNextOffset()); + + fragment.clear(); + assertEquals(100L, account.freed); + assertEquals(0L, account.current); + assertEquals(null, fragment.getBlock()); + + // repeated clear is a no-op + fragment.clear(); + assertEquals(100L, account.freed); + } + + @Test + public void testNewBlockReplacesPendingOne() { + RecordingAccount account = new RecordingAccount(); + PendingResultFragment fragment = new PendingResultFragment(account); + + fragment.reset(ByteBuffer.allocate(100), 30); + fragment.reset(ByteBuffer.allocate(50), 10); + + // the first reservation is released, the second one is taken + assertEquals(List.of(100L, 50L), account.reserved); + assertEquals(100L, account.freed); + assertEquals(50L, account.current); + } + + @Test + public void testNullAccountDisablesAccounting() { + PendingResultFragment fragment = new PendingResultFragment(null); + fragment.reset(ByteBuffer.allocate(100), 30); + fragment.clear(); + assertEquals(null, fragment.getBlock()); + } + + private static final class RecordingAccount implements PendingResultFragment.Account { + + private final List reserved = new ArrayList<>(); + private long freed; + private long current; + + @Override + public long reserve(long bytes) { + reserved.add(bytes); + current += bytes; + return bytes; + } + + @Override + public void free(long bytes) { + freed += bytes; + current -= bytes; + } + } +} diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java index 876d792593686..febb70146006b 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/QueryDataSetUtilsTest.java @@ -158,7 +158,7 @@ private IQueryExecution buildQueryExecution() throws IoTDBException, IOException TsBlock tsBlock = buildTsBlock(); Mockito.when(queryExecution.getBatchResult()) .thenReturn(Optional.of(tsBlock), Optional.empty()); - Mockito.when(queryExecution.getByteBufferBatchResult(false)) + Mockito.when(queryExecution.getByteBufferBatchResult()) .thenReturn(Optional.of(new TsBlockSerde().serialize(tsBlock)), Optional.empty()); return queryExecution; } @@ -294,10 +294,9 @@ public void testConvertQueryResultByFetchSizeWithSingleOversizedBlock() int payloadSize = totalLength - 1; IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); - Runnable memoryReleaser = Mockito.mock(Runnable.class); - PendingResultFragment pendingFragment = new PendingResultFragment(memoryReleaser); + PendingResultFragment pendingFragment = new PendingResultFragment(null); Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); - Mockito.when(queryExecution.getByteBufferBatchResult(true)) + Mockito.when(queryExecution.getByteBufferBatchResult()) .thenReturn(Optional.of(serialized.duplicate()), Optional.empty()); Mockito.when(queryExecution.hasNextResult()).thenReturn(false); @@ -311,8 +310,6 @@ public void testConvertQueryResultByFetchSizeWithSingleOversizedBlock() assertEquals(1, firstChunk.getQueryResult().size()); assertEquals(payloadSize, firstChunk.getQueryResult().get(0).remaining()); assertTrue(pendingFragment.getBlock() != null); - // the memory of the pending block is still retained - Mockito.verify(memoryReleaser, Mockito.never()).run(); QueryDataSetUtils.ResultChunk secondChunk = QueryDataSetUtils.convertQueryResultByFetchSize( @@ -321,8 +318,6 @@ public void testConvertQueryResultByFetchSizeWithSingleOversizedBlock() assertFalse(secondChunk.hasNextOffset()); assertFalse(secondChunk.hasTotalLength()); assertTrue(pendingFragment.getBlock() == null); - // the memory is released once the fragmented block has been fully sent - Mockito.verify(memoryReleaser, Mockito.times(1)).run(); TsBlock result = deserialize(firstChunk.getQueryResult().get(0), secondChunk.getQueryResult()); assertEquals(2, result.getPositionCount()); @@ -341,10 +336,9 @@ public void testConvertQueryResultByFetchSizeWithTrailingFragment() int payloadSize = smallBlock.remaining() + blockSize / 2; IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); - Runnable memoryReleaser = Mockito.mock(Runnable.class); - PendingResultFragment pendingFragment = new PendingResultFragment(memoryReleaser); + PendingResultFragment pendingFragment = new PendingResultFragment(null); Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); - Mockito.when(queryExecution.getByteBufferBatchResult(true)) + Mockito.when(queryExecution.getByteBufferBatchResult()) .thenReturn(Optional.of(smallBlock), Optional.of(serialized.duplicate()), Optional.empty()); Mockito.when(queryExecution.hasNextResult()).thenReturn(false); @@ -358,7 +352,6 @@ public void testConvertQueryResultByFetchSizeWithTrailingFragment() assertTrue(firstChunk.hasTotalLength()); assertEquals(blockSize, firstChunk.getTotalLength()); // the complete block is released at once, the fragmented one is released when it completes - Mockito.verify(memoryReleaser, Mockito.times(1)).run(); QueryDataSetUtils.ResultChunk secondChunk = QueryDataSetUtils.convertQueryResultByFetchSize( @@ -368,7 +361,6 @@ public void testConvertQueryResultByFetchSizeWithTrailingFragment() // only the rest of the oversized block remains assertEquals(1, secondChunk.getQueryResult().size()); assertEquals(blockSize - blockSize / 2, secondChunk.getQueryResult().get(0).remaining()); - Mockito.verify(memoryReleaser, Mockito.times(2)).run(); } @Test @@ -379,10 +371,9 @@ public void testContinuationCompletionDoesNotFetchNewBlocks() throws IoTDBExcept int payloadSize = blockSize / 2; IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); - Runnable memoryReleaser = Mockito.mock(Runnable.class); - PendingResultFragment pendingFragment = new PendingResultFragment(memoryReleaser); + PendingResultFragment pendingFragment = new PendingResultFragment(null); Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); - Mockito.when(queryExecution.getByteBufferBatchResult(true)) + Mockito.when(queryExecution.getByteBufferBatchResult()) .thenReturn( Optional.of(serialized.duplicate()), Optional.of(serialized.duplicate()), @@ -393,7 +384,6 @@ public void testContinuationCompletionDoesNotFetchNewBlocks() throws IoTDBExcept QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 0, payloadSize); assertFalse(firstChunk.isFinished()); assertEquals(payloadSize, firstChunk.getNextOffset()); - Mockito.verify(memoryReleaser, Mockito.never()).run(); // The response that completes the fragmented block must not fetch new blocks, otherwise its // row count would exceed fetchSize (the tail of the big block is not accounted). @@ -405,7 +395,6 @@ public void testContinuationCompletionDoesNotFetchNewBlocks() throws IoTDBExcept assertEquals(1, continuationChunk.getQueryResult().size()); assertEquals(blockSize - payloadSize, continuationChunk.getQueryResult().get(0).remaining()); assertTrue(pendingFragment.getBlock() == null); - Mockito.verify(memoryReleaser, Mockito.times(1)).run(); // New blocks are delivered by the following request only QueryDataSetUtils.ResultChunk nextChunk = @@ -414,7 +403,6 @@ public void testContinuationCompletionDoesNotFetchNewBlocks() throws IoTDBExcept assertFalse(nextChunk.hasNextOffset()); assertEquals(1, nextChunk.getQueryResult().size()); assertEquals(blockSize, nextChunk.getQueryResult().get(0).remaining()); - Mockito.verify(memoryReleaser, Mockito.times(2)).run(); } @Test @@ -425,10 +413,9 @@ public void testEmptyBlockReleasesItsMemory() throws IoTDBException, IOException ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock); IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); - Runnable memoryReleaser = Mockito.mock(Runnable.class); Mockito.when(queryExecution.getPendingResultFragment()) - .thenReturn(new PendingResultFragment(memoryReleaser)); - Mockito.when(queryExecution.getByteBufferBatchResult(true)) + .thenReturn(new PendingResultFragment(null)); + Mockito.when(queryExecution.getByteBufferBatchResult()) .thenReturn(Optional.of(emptyBlock), Optional.of(serialized), Optional.empty()); Mockito.when(queryExecution.hasNextResult()).thenReturn(false); @@ -438,12 +425,11 @@ public void testEmptyBlockReleasesItsMemory() throws IoTDBException, IOException assertTrue(chunk.isFinished()); assertEquals(1, chunk.getQueryResult().size()); // the empty block and the complete block are both released immediately - Mockito.verify(memoryReleaser, Mockito.times(2)).run(); } @Test(expected = IllegalArgumentException.class) public void testConvertQueryResultByFetchSizeRejectsMismatchedOffset() throws IoTDBException { - PendingResultFragment pendingFragment = new PendingResultFragment(() -> {}); + PendingResultFragment pendingFragment = new PendingResultFragment(null); pendingFragment.reset(ByteBuffer.wrap(new byte[10]), 5); IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); @@ -456,7 +442,7 @@ public void testConvertQueryResultByFetchSizeRejectsOffsetWithoutPendingFragment throws IoTDBException { IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); Mockito.when(queryExecution.getPendingResultFragment()) - .thenReturn(new PendingResultFragment(() -> {})); + .thenReturn(new PendingResultFragment(null)); QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 5, 8); } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java index e7bb00011e68a..98c22a3203577 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java @@ -181,7 +181,18 @@ private static class ServerSideSimulation { private final List payloadSizes = new ArrayList<>(); private final List remainingBlocks = new ArrayList<>(); private final PendingResultFragment pendingFragment = - new PendingResultFragment(releaseCount::incrementAndGet); + new PendingResultFragment( + new PendingResultFragment.Account() { + @Override + public long reserve(long bytes) { + return bytes; + } + + @Override + public void free(long bytes) { + releaseCount.incrementAndGet(); + } + }); private final IQueryExecution queryExecution; private ServerSideSimulation(ByteBuffer serialized, int positionCount, int totalLength) @@ -192,7 +203,7 @@ private ServerSideSimulation(ByteBuffer serialized, int positionCount, int total this.remainingBlocks.add(serialized); this.queryExecution = Mockito.mock(IQueryExecution.class); Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment); - Mockito.when(queryExecution.getByteBufferBatchResult(true)) + Mockito.when(queryExecution.getByteBufferBatchResult()) .thenAnswer( invocation -> remainingBlocks.isEmpty() From 81f92b4f661e457e2ab443760f332ccb90dfb77a Mon Sep 17 00:00:00 2001 From: Weihao Li <18110526956@163.com> Date: Thu, 19 Nov 2026 17:37:44 +0800 Subject: [PATCH 08/11] Keep the config template untouched and use String.format for the Fast LAST SQL --- .../iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java | 4 ++-- .../assembly/resources/conf/iotdb-system.properties.template | 3 --- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java index 0bbf5f23f0fa4..b412527ea202f 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java @@ -1375,7 +1375,7 @@ public TSExecuteStatementResp executeFastLastDataQueryForOnePrefixPath( req.isSetFetchSize() ? req.getFetchSize() : -1, req.isSetTimeout() ? req.getTimeout() : config.getQueryTimeoutThreshold(), maxPayloadSize, - "SELECT LAST " + String.join(".", req.getPrefixes()) + ".**", + String.format("SELECT LAST %s.**", String.join(".", req.getPrefixes())), clientSession); } @@ -1562,7 +1562,7 @@ public TSExecuteStatementResp executeFastLastDataQueryForOneDeviceV2( req.isSetFetchSize() ? req.getFetchSize() : -1, req.isSetTimeout() ? req.getTimeout() : config.getQueryTimeoutThreshold(), maxPayloadSize, - "SELECT LAST " + String.join(", ", convert(req).getPaths()), + String.format("SELECT LAST %s", String.join(", ", convert(req).getPaths())), clientSession); } TSExecuteStatementResp resp = diff --git a/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template b/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template index 49a23190cf8d5..a3fe0970318c8 100644 --- a/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template +++ b/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template @@ -1241,9 +1241,6 @@ mpp_data_exchange_max_pool_size=10 mpp_data_exchange_keep_alive_time_in_ms=1000 # The maximum payload size of one MPP data exchange RPC response. -# It also bounds the payload of one query-result RPC response between client and DataNode: a -# serialized TsBlock larger than this value is transferred in fragments, so that one oversized -# row (e.g. an OBJECT/BLOB column) cannot inflate the RPC buffer of server and client connections. # <=0 use default value # The effective value is between 131072(128KB) and dn_thrift_max_frame_size minus 1024 bytes reserved for the RPC response envelope. # effectiveMode: hot_reload From c2b73ccef7f013ebb87266c85be3d047cfa8a3da Mon Sep 17 00:00:00 2001 From: Weihao Li <18110526956@163.com> Date: Thu, 19 Nov 2026 18:07:21 +0800 Subject: [PATCH 09/11] fix according review --- .../v1/handler/QueryDataSetHandlerTest.java | 7 ------- .../queryengine/plan/execution/IQueryExecution.java | 9 ++++++++- .../plan/execution/config/ConfigExecution.java | 9 --------- .../apache/iotdb/db/utils/QueryDataSetUtils.java | 13 +++++++++++++ .../operator/MergeTreeSortOperatorTest.java | 7 ------- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java index 8bcd4d9445dd6..36a2ec1292e17 100644 --- a/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java +++ b/external-service-impl/rest/src/test/java/org/apache/iotdb/rest/protocol/v1/handler/QueryDataSetHandlerTest.java @@ -26,7 +26,6 @@ import org.apache.iotdb.db.queryengine.plan.analyze.QueryType; import org.apache.iotdb.db.queryengine.plan.execution.ExecutionResult; import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution; -import org.apache.iotdb.db.queryengine.plan.execution.PendingResultFragment; import org.apache.iotdb.rest.protocol.model.ExecutionStatus; import org.apache.iotdb.rest.protocol.v1.model.QueryDataSet; import org.apache.iotdb.rpc.TSStatusCode; @@ -89,7 +88,6 @@ private static final class TestQueryExecution implements IQueryExecution { private final Queue> batches = new ArrayDeque<>(); private final DatasetHeader datasetHeader; - private final PendingResultFragment pendingResultFragment = new PendingResultFragment(null); private TestQueryExecution(TsBlock... tsBlocks) { for (TsBlock tsBlock : tsBlocks) { @@ -130,11 +128,6 @@ public Optional getByteBufferBatchResult() { return Optional.empty(); } - @Override - public PendingResultFragment getPendingResultFragment() { - return pendingResultFragment; - } - @Override public boolean hasNextResult() { return !batches.isEmpty(); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java index f077815c33755..6f916c2b695ea 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java @@ -45,7 +45,14 @@ public interface IQueryExecution { Optional getByteBufferBatchResult() throws IoTDBException; - PendingResultFragment getPendingResultFragment(); + /** + * Returns the holder of the tail of the serialized TsBlock that is partially sent to the client, + * or null if this execution does not fragment its result (e.g. config tasks, whose results are + * small). Implementations that can fragment override it. + */ + default PendingResultFragment getPendingResultFragment() { + return null; + } boolean hasNextResult(); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java index 723a2030452fe..148dab7de71fe 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/config/ConfigExecution.java @@ -31,7 +31,6 @@ import org.apache.iotdb.db.queryengine.plan.analyze.QueryType; import org.apache.iotdb.db.queryengine.plan.execution.ExecutionResult; import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution; -import org.apache.iotdb.db.queryengine.plan.execution.PendingResultFragment; import org.apache.iotdb.db.queryengine.plan.execution.config.executor.ClusterConfigTaskExecutor; import org.apache.iotdb.db.queryengine.plan.execution.config.executor.IConfigTaskExecutor; import org.apache.iotdb.db.queryengine.plan.statement.StatementType; @@ -116,9 +115,6 @@ public class ConfigExecution implements IQueryExecution { private final IConfigTask task; private final IConfigTaskExecutor configTaskExecutor; - // the result set is not reserved from the memory pool, so nothing is accounted - private final PendingResultFragment pendingResultFragment = new PendingResultFragment(null); - private final StatementType statementType; private long totalExecutionTime; @@ -293,11 +289,6 @@ public Optional getByteBufferBatchResult() throws IoTDBException { return Optional.empty(); } - @Override - public PendingResultFragment getPendingResultFragment() { - return pendingResultFragment; - } - // According to the execution process of ConfigExecution, there is only one TsBlock for // this execution. Thus, the hasNextResult will be false once the TsBlock is consumed @Override diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java index 79b3ea865479a..dc6e755977f7b 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/QueryDataSetUtils.java @@ -284,6 +284,19 @@ public static ResultChunk convertQueryResultByFetchSize( long memorySize = 0; PendingResultFragment pendingFragment = queryExecution.getPendingResultFragment(); + if (pendingFragment == null) { + // executions without a pending holder (e.g. ConfigExecution) keep the whole-block behavior + if (requestOffset != 0) { + throw new IllegalArgumentException( + String.format( + DataNodeQueryMessages + .EXCEPTION_REQUEST_OFFSET_ARG_IS_INVALID_BECAUSE_NO_RESULT_FRAGMENT_IS_PENDING_C403454F, + requestOffset)); + } + Pair, Boolean> pair = + convertQueryResultByFetchSize(queryExecution, fetchSize); + return ResultChunk.complete(pair.left, pair.right); + } ByteBuffer pendingBlock = pendingFragment.getBlock(); if (pendingBlock != null) { int nextOffset = pendingFragment.getNextOffset(); diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java index 7087955329591..cfbcf0e26306c 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/execution/operator/MergeTreeSortOperatorTest.java @@ -50,7 +50,6 @@ import org.apache.iotdb.db.queryengine.plan.analyze.QueryType; import org.apache.iotdb.db.queryengine.plan.execution.ExecutionResult; import org.apache.iotdb.db.queryengine.plan.execution.IQueryExecution; -import org.apache.iotdb.db.queryengine.plan.execution.PendingResultFragment; import org.apache.iotdb.db.queryengine.plan.planner.plan.parameter.SeriesScanOptions; import org.apache.iotdb.db.queryengine.plan.statement.component.OrderByKey; import org.apache.iotdb.db.queryengine.plan.statement.component.Ordering; @@ -1809,7 +1808,6 @@ static class FakeQueryExecution implements IQueryExecution { private final long startTime; private final String queryId; private final String sql; - private final PendingResultFragment pendingResultFragment = new PendingResultFragment(null); FakeQueryExecution(long startTime, String queryId, String sql) { this.startTime = startTime; @@ -1895,11 +1893,6 @@ public Optional getByteBufferBatchResult() { return Optional.empty(); } - @Override - public PendingResultFragment getPendingResultFragment() { - return pendingResultFragment; - } - @Override public boolean hasNextResult() { return false; From 16f16cc83098691b1b11898bfd82fdaeb586046b Mon Sep 17 00:00:00 2001 From: Weihao Li <18110526956@163.com> Date: Thu, 19 Nov 2026 20:59:28 +0800 Subject: [PATCH 10/11] fix some --- .../it/db/it/IoTDBTableResultChunkingIT.java | 22 +++++++++ .../org/apache/iotdb/rpc/IoTDBRpcDataSet.java | 6 +-- .../thrift/impl/ClientRPCServiceImpl.java | 14 +++--- .../plan/execution/IQueryExecution.java | 6 +-- .../MaterializedResultExecution.java | 10 +--- .../plan/execution/QueryExecution.java | 6 +-- .../execution/QueryOwnedFragmentAccount.java | 25 +++++----- .../MaterializedResultExecutionTest.java | 48 +++++++++++++++++++ 8 files changed, 97 insertions(+), 40 deletions(-) diff --git a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTableResultChunkingIT.java b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTableResultChunkingIT.java index a0f2a510ce785..836dfd767a076 100644 --- a/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTableResultChunkingIT.java +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTableResultChunkingIT.java @@ -19,12 +19,15 @@ package org.apache.iotdb.relational.it.db.it; +import org.apache.iotdb.isession.ITableSession; +import org.apache.iotdb.isession.SessionDataSet; import org.apache.iotdb.it.env.EnvFactory; import org.apache.iotdb.it.framework.IoTDBTestRunner; import org.apache.iotdb.itbase.category.TableClusterIT; import org.apache.iotdb.itbase.category.TableLocalStandaloneIT; import org.apache.iotdb.itbase.runtime.ClusterTestConnection; +import org.apache.tsfile.utils.Binary; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; @@ -89,6 +92,25 @@ public static void tearDown() { EnvFactory.getEnv().cleanClusterEnvironment(); } + @Test + public void testOversizedRowsThroughTableSession() throws Exception { + try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) { + SessionDataSet dataSet = + session.executeQueryStatement( + "SELECT payload FROM chunk_it_table.blob_table ORDER BY time"); + try { + for (int i = 0; i < 4; i++) { + assertTrue("missing row " + i, dataSet.hasNext()); + Binary value = dataSet.next().getFields().get(0).getBinaryV(); + assertArrayEquals(i == 3 ? new byte[] {1, 2, 3} : LARGE_BLOB, value.getValues()); + } + assertFalse(dataSet.hasNext()); + } finally { + dataSet.closeOperationHandle(); + } + } + } + private static Connection getRealTableConnection() throws SQLException, IOException { Connection connection = EnvFactory.getEnv().getTableConnection(); if (connection instanceof ClusterTestConnection) { diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java index a9f40317e8ea3..689403fc937d1 100644 --- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java +++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/IoTDBRpcDataSet.java @@ -356,10 +356,8 @@ public boolean fetchResults() throws StatementExecutionException, IoTDBConnectio } /** - * Adds the blocks of one response to {@link #queryResult}. If the response ends with a fragment, - * the fragment is either appended to the pending {@link ResultFragmentBuffer} or starts a new - * one; the corresponding TsBlock is only added to {@link #queryResult} once all of its bytes have - * been received. + * Adds the blocks of one response to {@link #queryResult}, reassembling the fragment that ends + * the response before the block is consumed. */ private void addResponse( List blocks, boolean offsetSet, int nextOffset, int totalLength) { diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java index b412527ea202f..1f9106240b80f 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/protocol/thrift/impl/ClientRPCServiceImpl.java @@ -403,12 +403,11 @@ private static boolean canFitInOneResponse(ByteBuffer cachedResult, int maxPaylo } /** - * Transfers an oversized materialized result in fragments. The execution is registered so that - * {@code fetchResultsV2} can continue from the pending fragment, and it is cleaned up once the - * last fragment is sent or the query is closed. + * Transfers an oversized materialized result in fragments; the registered execution lets {@code + * fetchResultsV2} continue from the pending fragment. * - * @param maxPayloadSize the payload bound read once by the caller, so that the decision to - * fragment and the transfer itself use the same hot-reloadable value + * @param maxPayloadSize the bound read once by the caller, so that the decision to fragment and + * the transfer use the same hot-reloadable value */ private TSExecuteStatementResp respondWithFragmentedResult( long queryId, @@ -1730,9 +1729,8 @@ public TSFetchResultsResp fetchResultsV2(TSFetchResultsReq req) { statementType = queryExecution.getStatementType(); try (SetThreadName queryName = new SetThreadName(queryExecution.getQueryId())) { - // the chunking capability is announced once at openSession, so the continuation path - // must not decide by the presence of offset: a request without offset while a fragment is - // pending then fails the offset check loudly instead of silently dropping the tail + // decide by the session capability, so a continuation without offset fails the offset + // check instead of silently dropping the tail of a pending fragment QueryDataSetUtils.ResultChunk chunk = fetchResultChunk( queryExecution, diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java index 6f916c2b695ea..235ee5f81dad9 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java @@ -45,11 +45,7 @@ public interface IQueryExecution { Optional getByteBufferBatchResult() throws IoTDBException; - /** - * Returns the holder of the tail of the serialized TsBlock that is partially sent to the client, - * or null if this execution does not fragment its result (e.g. config tasks, whose results are - * small). Implementations that can fragment override it. - */ + /** Returns the pending fragment holder, or null if this execution does not fragment results. */ default PendingResultFragment getPendingResultFragment() { return null; } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java index 36a2bdbdc3d20..013d6d08792f2 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.java @@ -36,9 +36,7 @@ /** * An already materialized serialized result (e.g. a Fast LAST cache hit) registered as a query - * execution, so that {@code fetchResultsV2} can continue the fragmented transfer of an oversized - * block. Like other pending fragments, the serialized bytes are reserved from the query memory pool - * (best effort) until the last fragment is sent or the query is cleaned up. + * execution, so that {@code fetchResultsV2} can continue its fragmented transfer. */ public class MaterializedResultExecution implements IQueryExecution { @@ -112,14 +110,10 @@ private synchronized void cleanup() { } cleanedUp = true; pendingResultFragment.clear(); - serializeResult(null); + serializedResult = null; fragmentAccount.deregister(); } - private synchronized void serializeResult(ByteBuffer result) { - serializedResult = result; - } - @Override public void start() { // nothing to start: the result is already materialized diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java index cbfab2500212d..c69a911d60227 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java @@ -113,8 +113,7 @@ public class QueryExecution implements IQueryExecution { // used for cleaning resultHandle up exactly once private final AtomicBoolean resultHandleCleanUp; - // the serialized TsBlock that is partially sent to the client and waits for the next RPC; its - // memory is reserved under the account below, which lives as long as the query + // the serialized TsBlock partially sent to the client, accounted by the query-owned account private final PendingResultFragment pendingResultFragment; private final QueryOwnedFragmentAccount pendingFragmentAccount; @@ -556,8 +555,7 @@ private void cleanupForRetry(Throwable t) { */ private void releaseResource(Throwable t, boolean isRetry) { context.recordDeviceEntryDiskIOMetricsOnRelease(); - // the query will not serve any more fetch requests, release the pending fragment's memory and - // make sure a retried execution does not continue a stale fragment + // release the pending fragment, so that a retried execution does not continue a stale one pendingResultFragment.clear(); if (!isRetry) { pendingFragmentAccount.deregister(); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java index 3ed925011d61b..47f867b8456ac 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java @@ -23,14 +23,17 @@ import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeService; import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager; import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool; +import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool.MemoryReservationResult; import org.apache.iotdb.db.queryengine.execution.memory.MemorySide; import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId; +import com.google.common.util.concurrent.Futures; + /** - * Reserves the memory of a pending result fragment under an account owned by the query. The account - * outlives the result handle that produced the block, so a block waiting for the following RPCs - * stays accounted from the moment it becomes pending until the last fragment is sent. Reservations - * are best effort: a block larger than the budget is still transferred. + * Reserves the memory of a pending result fragment under an account owned by the query, so that a + * block waiting for the following RPCs stays accounted after its result handle released it. When + * the query pool is short of memory, the reservation waits until other queries release theirs; only + * the part of a block that exceeds the whole pool cannot be accounted. */ public class QueryOwnedFragmentAccount implements PendingResultFragment.Account { @@ -70,16 +73,16 @@ public synchronized long reserve(long bytes) { MemoryPool queryPool = getLocalMemoryManager().getQueryPool(); long maxBytesCanReserve = queryPool.getMaxBytes(); long bytesToReserve = Math.min(bytes, maxBytesCanReserve); - if (bytesToReserve <= 0) { - return 0; - } queryPool.registerPlanNodeIdToQueryMemoryMap( queryId, fragmentInstanceId, PLAN_NODE_ID, MemorySide.OUTPUT); - if (queryPool.tryReserve( - queryId, fragmentInstanceId, PLAN_NODE_ID, bytesToReserve, maxBytesCanReserve)) { - return bytesToReserve; + MemoryReservationResult result = + queryPool.reserveWithPriority( + queryId, fragmentInstanceId, PLAN_NODE_ID, bytesToReserve, maxBytesCanReserve, false); + if (!result.isReserveSuccess()) { + // the block is already in memory, so waiting for the pool is the only way to account it + Futures.getUnchecked(result.getFuture()); } - return 0; + return result.getReservedBytes(); } @Override diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java index 0c3c6fa2a2a86..3c0d27af3a164 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java @@ -24,6 +24,7 @@ import org.apache.iotdb.db.queryengine.common.header.DatasetHeaderFactory; import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager; import org.apache.iotdb.db.queryengine.execution.memory.MemoryPool; +import org.apache.iotdb.db.queryengine.execution.memory.MemorySide; import org.apache.iotdb.db.utils.QueryDataSetUtils; import org.apache.tsfile.enums.TSDataType; @@ -147,6 +148,53 @@ public void testPartialReservationWhenResultExceedsPool() throws Exception { assertEquals(0L, memoryPool.getReservedBytes()); } + @Test(timeout = 60_000) + public void testReservationWaitsUntilMemoryIsAvailable() throws Exception { + ByteBuffer serialized = new TsBlockSerde().serialize(buildTsBlock()); + int totalLength = serialized.remaining(); + long capacity = 4096L; + assertTrue(totalLength < capacity); + MemoryPool memoryPool = new MemoryPool("test", new MemoryManager(capacity), capacity); + // occupy the whole pool so that the reservation of the pending block has to wait + memoryPool.registerPlanNodeIdToQueryMemoryMap("holder", "holder", "holder", MemorySide.OUTPUT); + assertTrue(memoryPool.tryReserve("holder", "holder", "holder", capacity, capacity)); + + LocalMemoryManager localMemoryManager = Mockito.mock(LocalMemoryManager.class); + Mockito.when(localMemoryManager.getQueryPool()).thenReturn(memoryPool); + MaterializedResultExecution execution = + new MaterializedResultExecution( + new QueryId("20261119_050035_03054_4"), + serialized.duplicate(), + DatasetHeaderFactory.getLastQueryHeader(), + "FAST_LAST_QUERY", + "SELECT LAST root.sg.d1.s1", + 60_000L, + "root", + "127.0.0.1", + localMemoryManager); + + Thread transfer = + new Thread( + () -> { + try { + QueryDataSetUtils.convertQueryResultByFetchSize(execution, 10, 0, 64); + } catch (Exception e) { + throw new IllegalStateException(e); + } + }); + transfer.start(); + Thread.sleep(200); + assertTrue("the transfer must wait while the pool is exhausted", transfer.isAlive()); + + memoryPool.free("holder", "holder", "holder", capacity); + transfer.join(30_000); + assertFalse(transfer.isAlive()); + // the pending block is accounted now that memory is available + assertEquals(totalLength, memoryPool.getReservedBytes()); + execution.stopAndCleanup(null); + assertEquals(0L, memoryPool.getReservedBytes()); + } + private static TsBlock buildTsBlock() { TsBlockBuilder builder = new TsBlockBuilder(Collections.singletonList(TSDataType.INT64)); for (int i = 0; i < 100; i++) { From d061e88089535a4baecc5f615ccea2f00e516a4f Mon Sep 17 00:00:00 2001 From: Weihao Li <18110526956@163.com> Date: Fri, 20 Nov 2026 09:52:50 +0800 Subject: [PATCH 11/11] fix some --- .../iotdb/db/i18n/DataNodeQueryMessages.java | 4 + .../iotdb/db/i18n/DataNodeQueryMessages.java | 4 + .../plan/execution/PendingResultFragment.java | 71 ++++++---- .../execution/QueryOwnedFragmentAccount.java | 133 +++++++++++++++--- .../execution/PendingResultFragmentTest.java | 96 ++++++++++--- .../db/utils/ResultChunkingEndToEndTest.java | 6 +- 6 files changed, 240 insertions(+), 74 deletions(-) diff --git a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java index 639c9240c3bb2..bebcad89c33c4 100644 --- a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java +++ b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java @@ -4982,6 +4982,10 @@ private DataNodeQueryMessages() {} EXCEPTION_REQUEST_OFFSET_ARG_IS_INVALID_BECAUSE_NO_RESULT_FRAGMENT_IS_PENDING_C403454F = "Request offset %d is invalid because no result fragment is pending"; + public static final String + EXCEPTION_WAITING_FOR_THE_MEMORY_OF_A_PENDING_RESULT_FRAGMENT_WAS_ABORTED_BECAUSE_THE_QUERY_WAS_CLEANED_UP_D51EC67C = + "Waiting for the memory of a pending result fragment was aborted because the query was cleaned up"; + public static final String EXCEPTION_NOT_ENOUGH_MEMORY_WHILE_ANALYZING_TIME_PARTITIONS_NARROW_THE_QUERY_TIME_RANGE_OR_INCREASE_QUERY_MEMORY_MEMORY_DETAILS_REQUESTED_THIS_TIME_ARG_CURRENT_FREE_MEMORY_ARG_ORIGINAL_ERROR_ARG_37676B2A = "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"; diff --git a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java index c9db6e545002f..e04db65e3b038 100644 --- a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java +++ b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java @@ -5683,6 +5683,10 @@ private DataNodeQueryMessages() {} EXCEPTION_REQUEST_OFFSET_ARG_IS_INVALID_BECAUSE_NO_RESULT_FRAGMENT_IS_PENDING_C403454F = "请求 offset %d 无效:当前没有待发送的结果分片"; + public static final String + EXCEPTION_WAITING_FOR_THE_MEMORY_OF_A_PENDING_RESULT_FRAGMENT_WAS_ABORTED_BECAUSE_THE_QUERY_WAS_CLEANED_UP_D51EC67C = + "等待待发送结果分片的内存时查询已被清理,预留已取消"; + public static final String EXCEPTION_NOT_ENOUGH_MEMORY_WHILE_ANALYZING_TIME_PARTITIONS_NARROW_THE_QUERY_TIME_RANGE_OR_INCREASE_QUERY_MEMORY_MEMORY_DETAILS_REQUESTED_THIS_TIME_ARG_CURRENT_FREE_MEMORY_ARG_ORIGINAL_ERROR_ARG_37676B2A = "分析时间分区时内存不足。请缩小查询时间范围或增加查询内存。内存详情:本次请求 %s,当前空闲内存 %s。原始错误:%s"; diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java index da65df1b17173..80040958f503d 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java @@ -30,41 +30,59 @@ * Account} owned by the query, and released when the fragment is {@link #clear() cleared}. The * account lives with the query rather than with the result handle, so the reservation covers the * whole lifetime of the fragment on the local, the remote and the in-memory result paths. + * + *

Reserving under memory pressure waits outside every lock of this class, and {@link #clear()} + * cancels such a wait, so cleanup never blocks behind a pending reservation. */ public class PendingResultFragment { /** Reserves and releases the memory of a pending block; {@code null} disables accounting. */ public interface Account { - /** Returns the bytes actually reserved, which may be fewer than requested. */ - long reserve(long bytes); + /** + * Reserves {@code bytes}, waiting for memory if the pool is exhausted. Throws {@link + * IllegalStateException} if the wait is cancelled by {@link #release()}. + */ + void reserve(long bytes); - void free(long bytes); + /** Releases all bytes reserved through {@link #reserve(long)}, cancelling a waiting one. */ + void release(); } private final Account account; private ByteBuffer block; private int nextOffset; - private long reservedBytes; public PendingResultFragment(Account account) { this.account = account; } - /** - * Records that {@code block} has been sent up to {@code nextOffset} bytes. When a new block - * becomes pending, its serialized length is reserved from the query memory pool. - */ - public synchronized void reset(ByteBuffer block, int nextOffset) { - if (block != this.block) { - release(); + /** Records that {@code block} has been sent up to {@code nextOffset} bytes. */ + public void reset(ByteBuffer block, int nextOffset) { + boolean newBlock; + boolean hadPending; + synchronized (this) { + hadPending = this.block != null; + newBlock = block != this.block; this.block = block; - if (account != null) { - reservedBytes = account.reserve(block.remaining()); - } + this.nextOffset = nextOffset; + } + if (!newBlock || account == null) { + return; + } + if (hadPending) { + account.release(); + } + account.reserve(block.remaining()); + boolean cleared; + synchronized (this) { + cleared = this.block == null; + } + if (cleared) { + // the fragment was cleared while the reservation waited, which released it already + account.release(); } - this.nextOffset = nextOffset; } public synchronized ByteBuffer getBlock() { @@ -76,20 +94,17 @@ public synchronized int getNextOffset() { } /** - * Discards the pending fragment and releases the memory of the pending block. It is also called - * right after a complete block has been added to a response, when nothing is pending. Calling it - * repeatedly is a no-op. + * Discards the pending fragment and releases the memory of the pending block, cancelling a + * reservation that is still waiting. It is also called right after a complete block has been + * added to a response, when nothing is pending. Calling it repeatedly is a no-op. */ - public synchronized void clear() { - block = null; - nextOffset = 0; - release(); - } - - private void release() { - if (reservedBytes > 0) { - account.free(reservedBytes); - reservedBytes = 0; + public void clear() { + synchronized (this) { + block = null; + nextOffset = 0; + } + if (account != null) { + account.release(); } } } diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java index 47f867b8456ac..433cf8b2340e9 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java @@ -19,6 +19,7 @@ package org.apache.iotdb.db.queryengine.plan.execution; +import org.apache.iotdb.db.i18n.DataNodeQueryMessages; import org.apache.iotdb.db.queryengine.common.FragmentInstanceId; import org.apache.iotdb.db.queryengine.execution.exchange.MPPDataExchangeService; import org.apache.iotdb.db.queryengine.execution.memory.LocalMemoryManager; @@ -27,13 +28,15 @@ import org.apache.iotdb.db.queryengine.execution.memory.MemorySide; import org.apache.iotdb.mpp.rpc.thrift.TFragmentInstanceId; -import com.google.common.util.concurrent.Futures; +import java.util.concurrent.CancellationException; +import java.util.concurrent.ExecutionException; /** * Reserves the memory of a pending result fragment under an account owned by the query, so that a - * block waiting for the following RPCs stays accounted after its result handle released it. When - * the query pool is short of memory, the reservation waits until other queries release theirs; only - * the part of a block that exceeds the whole pool cannot be accounted. + * block waiting for the following RPCs stays accounted after its result handle released it. A + * reservation under memory pressure waits for other queries to release memory; the wait is + * cancelled when the fragment is released, so it can never block the cleanup that would free the + * memory it waits for. */ public class QueryOwnedFragmentAccount implements PendingResultFragment.Account { @@ -42,6 +45,11 @@ public class QueryOwnedFragmentAccount implements PendingResultFragment.Account private final String queryId; private final String fragmentInstanceId; private LocalMemoryManager localMemoryManager; + + // guarded by this + private long reservedBytes; + private MemoryReservationResult waitingReservation; + private boolean released; private boolean closed; /** Resolves the local memory manager lazily, so constructing a query does not start it. */ @@ -66,35 +74,114 @@ private LocalMemoryManager getLocalMemoryManager() { } @Override - public synchronized long reserve(long bytes) { - if (closed) { - return 0; + public void reserve(long bytes) { + MemoryReservationResult result; + synchronized (this) { + if (closed) { + throw aborted(); + } + released = false; + MemoryPool queryPool = getLocalMemoryManager().getQueryPool(); + long maxBytesCanReserve = queryPool.getMaxBytes(); + long bytesToReserve = Math.min(bytes, maxBytesCanReserve); + queryPool.registerPlanNodeIdToQueryMemoryMap( + queryId, fragmentInstanceId, PLAN_NODE_ID, MemorySide.OUTPUT); + result = + queryPool.reserveWithPriority( + queryId, fragmentInstanceId, PLAN_NODE_ID, bytesToReserve, maxBytesCanReserve, false); + if (result.isReserveSuccess()) { + reservedBytes += result.getReservedBytes(); + return; + } + waitingReservation = result; + } + // wait outside the lock, so that release() can cancel it + try { + result.getFuture().get(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + abandon(result); + throw aborted(); + } catch (CancellationException | ExecutionException e) { + abandon(result); + throw aborted(); + } + boolean keep; + synchronized (this) { + waitingReservation = null; + keep = !released && !closed; + if (keep) { + reservedBytes += result.getReservedBytes(); + } + notifyAll(); } - MemoryPool queryPool = getLocalMemoryManager().getQueryPool(); - long maxBytesCanReserve = queryPool.getMaxBytes(); - long bytesToReserve = Math.min(bytes, maxBytesCanReserve); - queryPool.registerPlanNodeIdToQueryMemoryMap( - queryId, fragmentInstanceId, PLAN_NODE_ID, MemorySide.OUTPUT); - MemoryReservationResult result = - queryPool.reserveWithPriority( - queryId, fragmentInstanceId, PLAN_NODE_ID, bytesToReserve, maxBytesCanReserve, false); - if (!result.isReserveSuccess()) { - // the block is already in memory, so waiting for the pool is the only way to account it - Futures.getUnchecked(result.getFuture()); + if (!keep) { + // the fragment was released while the reservation waited + freePool(result.getReservedBytes()); + throw aborted(); + } + } + + /** Stops waiting for a reservation that failed or was interrupted; nothing was reserved. */ + private void abandon(MemoryReservationResult result) { + synchronized (this) { + if (waitingReservation == result) { + waitingReservation = null; + } + notifyAll(); } - return result.getReservedBytes(); } @Override - public synchronized void free(long bytes) { - getLocalMemoryManager().getQueryPool().free(queryId, fragmentInstanceId, PLAN_NODE_ID, bytes); + public void release() { + long toFree; + MemoryReservationResult waiting; + synchronized (this) { + released = true; + toFree = reservedBytes; + reservedBytes = 0; + waiting = waitingReservation; + waitingReservation = null; + } + if (toFree > 0) { + freePool(toFree); + } + if (waiting != null) { + // wake the waiting reservation; it frees its bytes itself if it completed in the meantime + getLocalMemoryManager().getQueryPool().tryCancel(waiting.getFuture()); + awaitWaitingReservationSettled(); + } + } + + private synchronized void awaitWaitingReservationSettled() { + while (waitingReservation != null) { + try { + wait(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + return; + } + } } /** Stops accepting reservations and removes the account; call once the query is cleaned up. */ - public synchronized void deregister() { - closed = true; + public void deregister() { + synchronized (this) { + closed = true; + } + release(); getLocalMemoryManager() .getQueryPool() .deRegisterFragmentInstanceFromQueryMemoryMap(queryId, fragmentInstanceId, false); } + + private void freePool(long bytes) { + getLocalMemoryManager().getQueryPool().free(queryId, fragmentInstanceId, PLAN_NODE_ID, bytes); + } + + private static IllegalStateException aborted() { + return new IllegalStateException( + DataNodeQueryMessages + .EXCEPTION_WAITING_FOR_THE_MEMORY_OF_A_PENDING_RESULT_FRAGMENT_WAS_ABORTED_BECAUSE_THE_QUERY_WAS_CLEANED_UP_D51EC67C); + } } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragmentTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragmentTest.java index 76e8daabc1282..e1118399c5354 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragmentTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragmentTest.java @@ -24,9 +24,14 @@ import java.nio.ByteBuffer; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; public class PendingResultFragmentTest { @@ -38,7 +43,7 @@ public void testReserveWhenBlockBecomesPendingAndReleaseOnClear() { fragment.reset(block, 30); assertEquals(List.of(100L), account.reserved); - assertEquals(0, account.freed); + assertEquals(0, account.releases); // continuing the same block must not reserve again fragment.reset(block, 60); @@ -47,13 +52,8 @@ public void testReserveWhenBlockBecomesPendingAndReleaseOnClear() { assertEquals(60, fragment.getNextOffset()); fragment.clear(); - assertEquals(100L, account.freed); - assertEquals(0L, account.current); - assertEquals(null, fragment.getBlock()); - - // repeated clear is a no-op - fragment.clear(); - assertEquals(100L, account.freed); + assertEquals(1, account.releases); + assertNull(fragment.getBlock()); } @Test @@ -64,10 +64,10 @@ public void testNewBlockReplacesPendingOne() { fragment.reset(ByteBuffer.allocate(100), 30); fragment.reset(ByteBuffer.allocate(50), 10); - // the first reservation is released, the second one is taken + // the previous reservation is released before the new one is taken assertEquals(List.of(100L, 50L), account.reserved); - assertEquals(100L, account.freed); - assertEquals(50L, account.current); + assertEquals(1, account.releases); + assertEquals(50L, account.accounted); } @Test @@ -75,26 +75,82 @@ public void testNullAccountDisablesAccounting() { PendingResultFragment fragment = new PendingResultFragment(null); fragment.reset(ByteBuffer.allocate(100), 30); fragment.clear(); - assertEquals(null, fragment.getBlock()); + assertNull(fragment.getBlock()); + } + + @Test(timeout = 30_000) + public void testClearCancelsWaitingReservation() throws Exception { + WaitingAccount account = new WaitingAccount(); + PendingResultFragment fragment = new PendingResultFragment(account); + CountDownLatch resetReturned = new CountDownLatch(1); + AtomicReference failure = new AtomicReference<>(); + Thread fetcher = + new Thread( + () -> { + try { + fragment.reset(ByteBuffer.allocate(100), 30); + } catch (IllegalStateException expected) { + // the reservation was cancelled by the cleanup + failure.set(expected); + } finally { + resetReturned.countDown(); + } + }); + fetcher.start(); + assertTrue("reset() must wait for memory", account.waiting.await(10, TimeUnit.SECONDS)); + + // cleanup must not block behind the waiting reservation + fragment.clear(); + assertTrue( + "clear() must cancel the waiting reservation", + account.released.await(10, TimeUnit.SECONDS)); + assertTrue("the fetcher must be woken up", resetReturned.await(10, TimeUnit.SECONDS)); + assertTrue(failure.get() instanceof IllegalStateException); + assertNull(fragment.getBlock()); + assertEquals(0L, account.accounted); } private static final class RecordingAccount implements PendingResultFragment.Account { private final List reserved = new ArrayList<>(); - private long freed; - private long current; + private long accounted; + private int releases; @Override - public long reserve(long bytes) { + public void reserve(long bytes) { reserved.add(bytes); - current += bytes; - return bytes; + accounted += bytes; + } + + @Override + public void release() { + releases++; + accounted = 0; + } + } + + /** A reservation that waits until it is released, as when the pool is exhausted. */ + private static final class WaitingAccount implements PendingResultFragment.Account { + + private final CountDownLatch waiting = new CountDownLatch(1); + private final CountDownLatch released = new CountDownLatch(1); + private long accounted; + + @Override + public void reserve(long bytes) { + waiting.countDown(); + try { + released.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + throw new IllegalStateException("aborted by release"); } @Override - public void free(long bytes) { - freed += bytes; - current -= bytes; + public void release() { + accounted = 0; + released.countDown(); } } } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java index 98c22a3203577..525cb4b2e173a 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java @@ -184,12 +184,12 @@ private static class ServerSideSimulation { new PendingResultFragment( new PendingResultFragment.Account() { @Override - public long reserve(long bytes) { - return bytes; + public void reserve(long bytes) { + // nothing to reserve in this simulation } @Override - public void free(long bytes) { + public void release() { releaseCount.incrementAndGet(); } });