This is an automated email from the ASF dual-hosted git repository. Wei-hao-Li pushed a commit to branch modifyTransport in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit ed7bf8c37f933c46ed26bc1e35803aec09520c12 Author: Weihao Li <[email protected]> AuthorDate: Mon Nov 30 17:03:13 2026 +0800 modify Signed-off-by: Weihao Li <[email protected]> --- .../iotdb/rpc/IoTDBRpcDataSetFragmentTest.java | 43 +++++++++++++++ .../plan/execution/PendingResultFragment.java | 22 ++++++-- .../apache/iotdb/db/utils/QueryDataSetUtils.java | 26 +++++---- .../plan/execution/PendingResultFragmentTest.java | 15 +++--- .../iotdb/db/utils/QueryDataSetUtilsTest.java | 63 ++++++++++++++++++---- 5 files changed, 141 insertions(+), 28 deletions(-) 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 index c68553cb013..8159969a3f7 100644 --- 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 @@ -105,6 +105,49 @@ public class IoTDBRpcDataSetFragmentTest { assertFalse(dataSet.next()); } + @Test + public void testCompleteFragmentWithFollowingBlocksAndTrailingFragmentInOneResponse() + throws Exception { + byte[] first = serializeInt64Block(); + byte[] second = serializeInt64Block(); + byte[] third = serializeInt64Block(); + int firstSplit = first.length / 2; + int thirdSplit = third.length / 2; + + IClientRPCService.Iface client = mockClient(); + when(client.fetchResultsV2(any(TSFetchResultsReq.class))) + .thenReturn( + fragmentResponse(ByteBuffer.wrap(first, 0, firstSplit), firstSplit, first.length, true), + response( + List.of( + ByteBuffer.wrap(first, firstSplit, first.length - firstSplit), + ByteBuffer.wrap(second), + ByteBuffer.wrap(third, 0, thirdSplit)), + thirdSplit, + third.length, + true), + fragmentResponse( + ByteBuffer.wrap(third, thirdSplit, third.length - thirdSplit), -1, -1, false)); + + IoTDBRpcDataSet dataSet = newDataSet(client); + + // the response that completes the first fragment also carries the next block, and one + // response can end with a new fragment again + for (int i = 0; i < 3; i++) { + assertTrue(dataSet.next()); + assertEquals(10L, dataSet.getLong(COLUMN)); + assertTrue(dataSet.next()); + assertEquals(20L, dataSet.getLong(COLUMN)); + } + assertFalse(dataSet.next()); + + ArgumentCaptor<TSFetchResultsReq> captor = ArgumentCaptor.forClass(TSFetchResultsReq.class); + verify(client, times(3)).fetchResultsV2(captor.capture()); + assertEquals(0, captor.getAllValues().get(0).getOffset()); + assertEquals(firstSplit, captor.getAllValues().get(1).getOffset()); + assertEquals(thirdSplit, captor.getAllValues().get(2).getOffset()); + } + @Test public void testLegacyResponseWithoutOffset() throws Exception { byte[] bytes = serializeInt64Block(); 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 80040958f50..3e06cb67ef3 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 @@ -24,7 +24,9 @@ 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. + * result handle, so this fragment only holds a reference to it and never copies the payload. The + * block's row count is recorded so that the response completing the fragment can keep packing new + * blocks into the same response while respecting the fetch size. * * <p>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 @@ -53,13 +55,17 @@ public class PendingResultFragment { private ByteBuffer block; private int nextOffset; + private int rowCount; 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) { + /** + * Records that {@code block}, with {@code rowCount} rows, has been sent up to {@code nextOffset} + * bytes. + */ + public void reset(ByteBuffer block, int nextOffset, int rowCount) { boolean newBlock; boolean hadPending; synchronized (this) { @@ -67,6 +73,7 @@ public class PendingResultFragment { newBlock = block != this.block; this.block = block; this.nextOffset = nextOffset; + this.rowCount = rowCount; } if (!newBlock || account == null) { return; @@ -93,6 +100,14 @@ public class PendingResultFragment { return nextOffset; } + /** + * Returns the row count of the pending block, so that the response completing it can account for + * its rows when packing more blocks into the same response. + */ + public synchronized int getRowCount() { + return rowCount; + } + /** * 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 @@ -102,6 +117,7 @@ public class PendingResultFragment { synchronized (this) { block = null; nextOffset = 0; + rowCount = 0; } if (account != null) { account.release(); 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 dc6e755977f..3bad405d619 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 @@ -272,7 +272,9 @@ public class QueryDataSetUtils { * 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. + * execution, to be sent by the following request carrying the returned offset. The response that + * completes a pending fragment keeps packing subsequent TsBlocks while the payload and the fetch + * size still have room, so a fragmented block does not cost an extra round trip. */ public static ResultChunk convertQueryResultByFetchSize( IQueryExecution queryExecution, int fetchSize, int requestOffset, int maxPayloadSize) @@ -300,6 +302,7 @@ public class QueryDataSetUtils { ByteBuffer pendingBlock = pendingFragment.getBlock(); if (pendingBlock != null) { int nextOffset = pendingFragment.getNextOffset(); + int pendingRowCount = pendingFragment.getRowCount(); if (requestOffset != nextOffset) { throw new IllegalArgumentException( String.format( @@ -310,15 +313,18 @@ public class QueryDataSetUtils { } int fragmentSize = Math.min(remainingPayloadSize, pendingBlock.limit() - nextOffset); res.add(slice(pendingBlock, nextOffset, fragmentSize)); - if (nextOffset + fragmentSize == pendingBlock.limit()) { - pendingFragment.clear(); - // 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()); + if (nextOffset + fragmentSize < pendingBlock.limit()) { + pendingFragment.reset(pendingBlock, nextOffset + fragmentSize, pendingRowCount); + return ResultChunk.fragment(res, nextOffset + fragmentSize, -1, false); } - pendingFragment.reset(pendingBlock, nextOffset + fragmentSize); - return ResultChunk.fragment(res, nextOffset + fragmentSize, -1, false); + // The tail completes the fragmented block, but it usually leaves room in this response. + // The rows of the completed block count against fetchSize, then the response continues with + // new blocks instead of ending here, so an ordinary block that got fragmented does not cost + // an extra round trip. + rowCount = pendingRowCount; + pendingFragment.clear(); + remainingPayloadSize -= fragmentSize; + memorySize += fragmentSize; } else if (requestOffset != 0) { throw new IllegalArgumentException( String.format( @@ -348,7 +354,7 @@ public class QueryDataSetUtils { pendingFragment.clear(); } else { res.add(slice(byteBuffer, 0, remainingPayloadSize)); - pendingFragment.reset(byteBuffer, remainingPayloadSize); + pendingFragment.reset(byteBuffer, remainingPayloadSize, positionCount); return ResultChunk.fragment(res, remainingPayloadSize, blockSize, false); } } 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 e1118399c53..817f6a8a237 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 @@ -41,19 +41,21 @@ public class PendingResultFragmentTest { PendingResultFragment fragment = new PendingResultFragment(account); ByteBuffer block = ByteBuffer.allocate(100); - fragment.reset(block, 30); + fragment.reset(block, 30, 2); assertEquals(List.of(100L), account.reserved); assertEquals(0, account.releases); // continuing the same block must not reserve again - fragment.reset(block, 60); + fragment.reset(block, 60, 2); assertEquals(List.of(100L), account.reserved); assertSame(block, fragment.getBlock()); assertEquals(60, fragment.getNextOffset()); + assertEquals(2, fragment.getRowCount()); fragment.clear(); assertEquals(1, account.releases); assertNull(fragment.getBlock()); + assertEquals(0, fragment.getRowCount()); } @Test @@ -61,19 +63,20 @@ public class PendingResultFragmentTest { RecordingAccount account = new RecordingAccount(); PendingResultFragment fragment = new PendingResultFragment(account); - fragment.reset(ByteBuffer.allocate(100), 30); - fragment.reset(ByteBuffer.allocate(50), 10); + fragment.reset(ByteBuffer.allocate(100), 30, 1); + fragment.reset(ByteBuffer.allocate(50), 10, 3); // the previous reservation is released before the new one is taken assertEquals(List.of(100L, 50L), account.reserved); assertEquals(1, account.releases); assertEquals(50L, account.accounted); + assertEquals(3, fragment.getRowCount()); } @Test public void testNullAccountDisablesAccounting() { PendingResultFragment fragment = new PendingResultFragment(null); - fragment.reset(ByteBuffer.allocate(100), 30); + fragment.reset(ByteBuffer.allocate(100), 30, 1); fragment.clear(); assertNull(fragment.getBlock()); } @@ -88,7 +91,7 @@ public class PendingResultFragmentTest { new Thread( () -> { try { - fragment.reset(ByteBuffer.allocate(100), 30); + fragment.reset(ByteBuffer.allocate(100), 30, 1); } catch (IllegalStateException expected) { // the reservation was cancelled by the cleanup failure.set(expected); 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 febb7014600..9acf3e91da2 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 @@ -48,6 +48,7 @@ import java.util.Optional; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; public class QueryDataSetUtilsTest { @@ -364,7 +365,8 @@ public class QueryDataSetUtilsTest { } @Test - public void testContinuationCompletionDoesNotFetchNewBlocks() throws IoTDBException, IOException { + public void testContinuationCompletesFragmentAndDeliversFollowingBlocks() + throws IoTDBException, IOException { TsBlock tsBlock = buildTsBlock(); ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock); int blockSize = serialized.remaining(); @@ -378,27 +380,70 @@ public class QueryDataSetUtilsTest { Optional.of(serialized.duplicate()), Optional.of(serialized.duplicate()), Optional.empty()); - Mockito.when(queryExecution.hasNextResult()).thenReturn(true, false); + Mockito.when(queryExecution.hasNextResult()).thenReturn(false); QueryDataSetUtils.ResultChunk firstChunk = QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 0, payloadSize); assertFalse(firstChunk.isFinished()); assertEquals(payloadSize, firstChunk.getNextOffset()); + assertEquals(tsBlock.getPositionCount(), pendingFragment.getRowCount()); + + // The response that completes the fragmented block continues with the following blocks while + // the payload and the fetch size still have room, instead of ending here and costing an extra + // round trip. + QueryDataSetUtils.ResultChunk continuationChunk = + QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, 10, firstChunk.getNextOffset(), 2 * blockSize); + assertTrue(continuationChunk.isFinished()); + assertFalse(continuationChunk.hasNextOffset()); + assertEquals(2, continuationChunk.getQueryResult().size()); + // the tail of the fragmented block comes first + assertEquals(blockSize - payloadSize, continuationChunk.getQueryResult().get(0).remaining()); + // then the following block, delivered by the same response + assertEquals(blockSize, continuationChunk.getQueryResult().get(1).remaining()); + assertNull(pendingFragment.getBlock()); + } + + @Test + public void testContinuationCountsPendingRowsAgainstFetchSize() + throws IoTDBException, IOException { + TsBlock tsBlock = buildTsBlock(); + ByteBuffer serialized = new TsBlockSerde().serialize(tsBlock); + int blockSize = serialized.remaining(); + int payloadSize = blockSize / 2; - // 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). + IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); + PendingResultFragment pendingFragment = new PendingResultFragment(null); + 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); + + // fetchSize equals the rows of one block + QueryDataSetUtils.ResultChunk firstChunk = + QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, tsBlock.getPositionCount(), 0, payloadSize); + assertFalse(firstChunk.isFinished()); + assertEquals(tsBlock.getPositionCount(), pendingFragment.getRowCount()); + + // The rows of the completed block already fill the fetch size, so the continuation response + // stops after the tail even though the payload still has room. QueryDataSetUtils.ResultChunk continuationChunk = QueryDataSetUtils.convertQueryResultByFetchSize( - queryExecution, 10, firstChunk.getNextOffset(), blockSize); + queryExecution, tsBlock.getPositionCount(), firstChunk.getNextOffset(), 2 * blockSize); assertFalse(continuationChunk.isFinished()); assertFalse(continuationChunk.hasNextOffset()); assertEquals(1, continuationChunk.getQueryResult().size()); assertEquals(blockSize - payloadSize, continuationChunk.getQueryResult().get(0).remaining()); - assertTrue(pendingFragment.getBlock() == null); + assertNull(pendingFragment.getBlock()); - // New blocks are delivered by the following request only + // The following block is delivered by the next request. QueryDataSetUtils.ResultChunk nextChunk = - QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, 10, 0, blockSize); + QueryDataSetUtils.convertQueryResultByFetchSize( + queryExecution, tsBlock.getPositionCount(), 0, 2 * blockSize); assertTrue(nextChunk.isFinished()); assertFalse(nextChunk.hasNextOffset()); assertEquals(1, nextChunk.getQueryResult().size()); @@ -430,7 +475,7 @@ public class QueryDataSetUtilsTest { @Test(expected = IllegalArgumentException.class) public void testConvertQueryResultByFetchSizeRejectsMismatchedOffset() throws IoTDBException { PendingResultFragment pendingFragment = new PendingResultFragment(null); - pendingFragment.reset(ByteBuffer.wrap(new byte[10]), 5); + pendingFragment.reset(ByteBuffer.wrap(new byte[10]), 5, 0); IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); Mockito.when(queryExecution.getPendingResultFragment()).thenReturn(pendingFragment);
