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..836dfd767a076 --- /dev/null +++ b/integration-test/src/test/java/org/apache/iotdb/relational/it/db/it/IoTDBTableResultChunkingIT.java @@ -0,0 +1,144 @@ +/* + * 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.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; +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(); + } + + @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) { + // 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/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 c42ddf505b704..9647a0006f889 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 @@ -704,6 +704,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 c5d351d1e99be..fe3ebbcd83913 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 @@ -106,7 +106,10 @@ public IoTDBJDBCResultSet( ZoneId zoneId, Charset charset, boolean tableModel, - List columnIndex2TsBlockColumnIndexList) + List columnIndex2TsBlockColumnIndexList, + boolean offsetSet, + int offset, + int totalLength) throws SQLException { this.queryId = queryId; this.ioTDBRpcDataSet = @@ -128,7 +131,10 @@ public IoTDBJDBCResultSet( timeFormat, statement.getTimeFactor(), tableModel, - columnIndex2TsBlockColumnIndexList); + columnIndex2TsBlockColumnIndexList, + offsetSet, + offset, + totalLength); this.statement = statement; this.columnTypeList = columnTypeList; if (tracingInfo != null) { @@ -138,6 +144,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, @@ -153,7 +202,10 @@ public IoTDBJDBCResultSet( TSTracingInfo tracingInfo, long timeout, boolean moreData, - ZoneId zoneId) + ZoneId zoneId, + boolean offsetSet, + int offset, + int totalLength) throws SQLException { this.queryId = queryId; this.ioTDBRpcDataSet = @@ -175,7 +227,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) { @@ -184,6 +239,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); + } + private static IClientRPCService.Iface getResultSetClient( IClientRPCService.Iface client, long queryId) { return queryId == -1 ? null : client; 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 4d42145b571cb..cb75321c89cf8 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 @@ -471,7 +471,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); } catch (SQLException | RuntimeException e) { throw closeQueryOperationOnResultSetCreationFailure(queryId, e); } @@ -617,7 +620,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); } catch (SQLException | RuntimeException e) { throw closeQueryOperationOnResultSetCreationFailure(queryId, e); } 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 341f91efb022c..0987407b37b00 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 @@ -282,7 +282,10 @@ private ResultSet processQueryResult(TSExecuteStatementResp resp) throws SQLExce resp.isSetTableModel() && resp.isTableModel(), resp.getColumnIndex2TsBlockColumnIndexList() == null ? null - : new ArrayList<>(resp.getColumnIndex2TsBlockColumnIndexList())); + : new ArrayList<>(resp.getColumnIndex2TsBlockColumnIndexList()), + resp.isSetOffset(), + resp.offset, + resp.totalLength); } catch (SQLException | RuntimeException e) { throw closeQueryOperationOnResultSetCreationFailure(queryId, e); } diff --git a/iotdb-client/service-rpc/pom.xml b/iotdb-client/service-rpc/pom.xml index 3ecc9e0af3f5d..a99167402d7e6 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 fe70f9cbe3cf4..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 @@ -127,6 +127,18 @@ 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_EMPTY_FRAGMENT_RECEIVED_WHEN_ASSEMBLING_A_CHUNKED_QUERY_RESULT_03A0EF39 = + "Empty fragment received when assembling a chunked query result"; + public static final String + 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_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 = "Invalid buffer size: %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 d4b26bba60b2a..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 @@ -108,6 +108,18 @@ private RpcMessages() {} "通过列名 %s 获取的值为 NULL。"; public static final String EXCEPTION_DATA_TYPE_ARG_IS_NOT_SUPPORTED_31213160 = "不支持数据类型 %s。"; + + public static final String EXCEPTION_EMPTY_FRAGMENT_RECEIVED_WHEN_ASSEMBLING_A_CHUNKED_QUERY_RESULT_03A0EF39 = + "拼装分片查询结果时收到空分片"; + public static final String + 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_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 = "无效的缓冲区大小:%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 eb3d50f032756..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 @@ -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; @@ -85,6 +86,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; @@ -112,6 +115,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 @@ -185,12 +235,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; @@ -233,6 +284,7 @@ private void close(boolean explicitlyClosed) throws StatementExecutionException, } client = null; isClosed = true; + fragmentBuffer = null; this.explicitlyClosed = explicitlyClosed; } @@ -251,7 +303,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; @@ -272,6 +330,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()); @@ -279,12 +340,10 @@ public boolean fetchResults() throws StatementExecutionException, IoTDBConnectio if (!resp.hasResultSet) { close(false); } 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; } @@ -296,6 +355,98 @@ public boolean fetchResults() throws StatementExecutionException, IoTDBConnectio } } + /** + * 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) { + if (blocks == null || blocks.isEmpty()) { + return; + } + int blockIndex = 0; + + if (fragmentBuffer != null) { + 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_MISSING_TOTALLENGTH_FOR_THE_FIRST_FRAGMENT_OF_A_CHUNKED_TSBLOCK_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_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( + fragment.array(), fragment.arrayOffset() + fragment.position(), fragment.remaining()); + } + + private void updateOffset(int nextOffset) { + // 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 + .EXCEPTION_FRAGMENT_OFFSET_ARG_DOES_NOT_MATCH_THE_ACCUMULATED_LENGTH_ARG_1EB45347, + nextOffset, + buffer.size())); + } + this.nextOffset = nextOffset; + } + + private ByteBuffer toByteBuffer() { + if (buffer.size() != totalLength) { + throw new IllegalArgumentException( + String.format( + RpcMessages + .EXCEPTION_ASSEMBLED_FRAGMENT_LENGTH_ARG_DOES_NOT_MATCH_THE_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/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-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 68f3a48f60a74..88a72f0f49870 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 @@ -104,6 +104,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; @@ -254,6 +255,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_KEY, Boolean.TRUE.toString()); if (session.useEncryptedPassword) { openReq.putToConfiguration(USE_ENCRYPTED_PASSWORD_KEY, Boolean.TRUE.toString()); } @@ -501,7 +504,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) @@ -570,7 +576,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) @@ -610,7 +619,10 @@ protected SessionDataSet executeLastDataQueryForOnePrefixPath(final List zoneId, timeFactor, false, - tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList()); + tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList(), + tsExecuteStatementResp.isSetOffset(), + tsExecuteStatementResp.offset, + tsExecuteStatementResp.totalLength); } protected Pair executeLastDataQueryForOneDevice( @@ -660,7 +672,10 @@ protected Pair executeLastDataQueryForOneDevice( zoneId, timeFactor, tsExecuteStatementResp.isSetTableModel() && tsExecuteStatementResp.isTableModel(), - tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList()), + tsExecuteStatementResp.getColumnIndex2TsBlockColumnIndexList(), + tsExecuteStatementResp.isSetOffset(), + tsExecuteStatementResp.offset, + tsExecuteStatementResp.totalLength), redirectedEndPoint); } @@ -703,7 +718,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( @@ -785,7 +803,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/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/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeQueryMessages.java index 730c64ca40ed5..13815ce3e9905 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 @@ -4974,6 +4974,18 @@ 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_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_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 MESSAGE_COLUMN_DELETION_IS_ASYNCHRONOUS_THE_SPECIFIED_TABLE_OR_COLUMN_DOES_NOT_EXIST_NO_TASK_WAS_SUBMITTED_08F7DB64 = "Column deletion is asynchronous. The specified table or column does not exist; no task was submitted."; public static final String MESSAGE_TABLE_DELETION_IS_ASYNCHRONOUS_THE_SPECIFIED_TABLE_DOES_NOT_EXIST_NO_TASK_WAS_SUBMITTED_74E6521A = 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/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 d07666517a8c3..77703a8fd5b6d 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 @@ -5675,6 +5675,18 @@ 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_REQUEST_OFFSET_ARG_DOES_NOT_MATCH_THE_PENDING_RESULT_FRAGMENT_OFFSET_ARG_A1B13DBC = + "请求 offset %d 与待发送结果分片的 offset %d 不一致"; + public static final String + 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 MESSAGE_COLUMN_DELETION_IS_ASYNCHRONOUS_THE_SPECIFIED_TABLE_OR_COLUMN_DOES_NOT_EXIST_NO_TASK_WAS_SUBMITTED_08F7DB64 = "列删除采用异步执行。指定的表或列不存在,未提交任务。"; public static final String MESSAGE_TABLE_DELETION_IS_ASYNCHRONOUS_THE_SPECIFIED_TABLE_DOES_NOT_EXIST_NO_TASK_WAS_SUBMITTED_74E6521A = 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 590e87044cb9d..26e0252a19b35 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 @@ -66,6 +66,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(); @@ -224,6 +227,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 54b09bdf8899e..ab0c432399a7c 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; @@ -242,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; @@ -274,6 +276,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; @@ -303,25 +306,147 @@ 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) -> + fillQueryResult(resp, fetchResultChunk(queryExecution, fetchSize, 0, maxPayloadSize)); 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; }; + /** + * 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(); + } + + /** + * 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 + * exchange RPC response, see mpp_data_exchange_max_payload_size_in_bytes. + */ + private static int getMaxResultPayloadSize(IClientSession clientSession) { + return clientSession.isEnableResultChunking() + ? config.getMppDataExchangeMaxPayloadSizeInBytes() + : 0; + } + + private static boolean canFitInOneResponse(ByteBuffer cachedResult, int maxPayloadSize) { + return cachedResult == null + || maxPayloadSize <= 0 + || cachedResult.remaining() <= maxPayloadSize; + } + + /** + * Transfers an oversized materialized result in fragments; the registered execution lets {@code + * fetchResultsV2} continue from the pending fragment. + * + * @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, + ByteBuffer cachedResult, + int fetchSize, + long timeout, + int maxPayloadSize, + String sql, + IClientSession clientSession) + throws IoTDBException, IOException { + MaterializedResultExecution execution = + new MaterializedResultExecution( + COORDINATOR.createQueryId(), + cachedResult, + DatasetHeaderFactory.getLastQueryHeader(), + StatementType.FAST_LAST_QUERY.name(), + sql, + timeout, + clientSession.getUsername(), + clientSession.getClientAddress()); + COORDINATOR.registerQueryExecution(queryId, execution); + 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() { partitionFetcher = ClusterPartitionFetcher.getInstance(); schemaFetcher = ClusterSchemaFetcher.getInstance(); @@ -495,10 +620,15 @@ 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()); + quota.addReadResult(chargedResultBytes(resp)); } // Should return SUCCESS_MESSAGE for insert into query if (queryExecution.getQueryType() == QueryType.READ_WRITE) { @@ -781,9 +911,11 @@ 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()); + quota.addReadResult(chargedResultBytes(resp)); } else { resp = RpcUtils.getTSExecuteStatementResp(result.status); } @@ -877,9 +1009,11 @@ 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()); + quota.addReadResult(chargedResultBytes(resp)); } else { resp = RpcUtils.getTSExecuteStatementResp(result.status); } @@ -975,9 +1109,11 @@ 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()); + quota.addReadResult(chargedResultBytes(resp)); } else { resp = RpcUtils.getTSExecuteStatementResp(result.status); } @@ -1242,14 +1378,24 @@ public TSExecuteStatementResp executeFastLastDataQueryForOnePrefixPath( } } + ByteBuffer cachedResult = builder.isEmpty() ? null : serde.serialize(builder.build()); + int maxPayloadSize = getMaxResultPayloadSize(clientSession); + if (!canFitInOneResponse(cachedResult, maxPayloadSize)) { + return respondWithFragmentedResult( + queryId, + cachedResult, + req.isSetFetchSize() ? req.getFetchSize() : -1, + req.isSetTimeout() ? req.getTimeout() : config.getQueryTimeoutThreshold(), + maxPayloadSize, + String.format("SELECT LAST %s.**", String.join(".", req.getPrefixes())), + 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); @@ -1419,14 +1565,25 @@ public TSExecuteStatementResp executeFastLastDataQueryForOneDeviceV2( } // cache hit if (allCached) { + ByteBuffer cachedResult = builder.isEmpty() ? null : serde.serialize(builder.build()); + int maxPayloadSize = getMaxResultPayloadSize(clientSession); + if (!canFitInOneResponse(cachedResult, maxPayloadSize)) { + return respondWithFragmentedResult( + queryId, + cachedResult, + req.isSetFetchSize() ? req.getFetchSize() : -1, + req.isSetTimeout() ? req.getTimeout() : config.getQueryTimeoutThreshold(), + maxPayloadSize, + String.format("SELECT LAST %s", String.join(", ", convert(req).getPaths())), + 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); @@ -1468,9 +1625,11 @@ public TSExecuteStatementResp executeFastLastDataQueryForOneDeviceV2( regionReplicaSets.get(regionReplicaSets.size() - 1).dataNodeLocations.get(0), clientSession.isInternalRpcSession())); 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()); + quota.addReadResult(chargedResultBytes(resp)); } else { resp = RpcUtils.getTSExecuteStatementResp(result.status); } @@ -1583,14 +1742,17 @@ public TSFetchResultsResp fetchResultsV2(TSFetchResultsReq req) { statementType = queryExecution.getStatementType(); try (SetThreadName queryName = new SetThreadName(queryExecution.getQueryId())) { - Pair, Boolean> pair = - QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, req.fetchSize); - List result = pair.left; - finished = pair.right; - boolean hasResultSet = !result.isEmpty(); - resp.setHasResultSet(hasResultSet); + // 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, + req.fetchSize, + req.isSetOffset() ? req.getOffset() : 0, + getMaxResultPayloadSize(clientSession)); + finished = fillQueryResult(resp, chunk); + resp.setHasResultSet(!chunk.getQueryResult().isEmpty()); resp.setIsAlign(true); - resp.setQueryResult(result); resp.setMoreData(!finished); return resp; } @@ -1647,6 +1809,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<>(); @@ -1721,6 +1884,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( @@ -1810,7 +1979,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 @@ -1831,15 +2001,6 @@ public TSStatus deallocatePreparedStatement(TSDeallocatePreparedReq req) { } } - private final SelectResult setResultForPrepared = - (resp, queryExecution, fetchSize) -> { - // Use V2 format (queryResult) to match IoTDBTablePreparedStatement client - Pair, Boolean> pair = - QueryDataSetUtils.convertQueryResultByFetchSize(queryExecution, fetchSize); - resp.setQueryResult(pair.left); - return pair.right; - }; - // ========================= End PreparedStatement RPC Methods ========================= @Override 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 0de299952262c..0c7a688420223 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 @@ -1050,6 +1050,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/IQueryExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/IQueryExecution.java index f79443a7827f3..c848b7b2cad66 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,11 @@ public interface IQueryExecution { Optional getByteBufferBatchResult() throws IoTDBException; + /** Returns the pending fragment holder, or null if this execution does not fragment results. */ + default PendingResultFragment getPendingResultFragment() { + return null; + } + boolean hasNextResult(); int getOutputValueColumnCount(); 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..013d6d08792f2 --- /dev/null +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecution.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.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.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.plan.analyze.QueryType; +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 its fragmented transfer. + */ +public class MaterializedResultExecution implements IQueryExecution { + + 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 QueryOwnedFragmentAccount fragmentAccount; + private final PendingResultFragment pendingResultFragment; + + /** The serialized result, handed over once. */ + private ByteBuffer serializedResult; + + private volatile long startTimeOfCurrentRpc = System.nanoTime(); + private long totalExecutionTime; + private boolean cleanedUp; + + public MaterializedResultExecution( + QueryId queryId, + ByteBuffer serializedResult, + DatasetHeader datasetHeader, + String statementType, + String executeSQL, + long timeout, + String user, + String clientHostname) { + this( + queryId, + serializedResult, + datasetHeader, + statementType, + executeSQL, + timeout, + user, + clientHostname, + MPPDataExchangeService.getInstance().getMPPDataExchangeManager().getLocalMemoryManager()); + } + + @TestOnly + MaterializedResultExecution( + QueryId queryId, + ByteBuffer serializedResult, + DatasetHeader datasetHeader, + String statementType, + String executeSQL, + long timeout, + String user, + String clientHostname, + LocalMemoryManager localMemoryManager) { + this.queryId = queryId; + this.serializedResult = serializedResult; + this.datasetHeader = datasetHeader; + this.statementType = statementType; + this.executeSQL = executeSQL; + this.timeout = timeout; + this.user = user; + this.clientHostname = clientHostname; + + this.fragmentAccount = new QueryOwnedFragmentAccount(localMemoryManager, queryId.getId()); + this.pendingResultFragment = new PendingResultFragment(fragmentAccount); + } + + private synchronized void cleanup() { + if (cleanedUp) { + return; + } + cleanedUp = true; + pendingResultFragment.clear(); + serializedResult = null; + fragmentAccount.deregister(); + } + + @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(queryId, RpcUtils.getStatus(TSStatusCode.SUCCESS_STATUS)); + } + + @Override + public Optional getBatchResult() { + return Optional.empty(); + } + + @Override + public synchronized Optional getByteBufferBatchResult() { + ByteBuffer result = serializedResult; + serializedResult = null; + return Optional.ofNullable(result); + } + + @Override + public PendingResultFragment getPendingResultFragment() { + return pendingResultFragment; + } + + @Override + public synchronized 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 queryId.getId(); + } + + @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.of(executeSQL); + } + + @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/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..80040958f503d --- /dev/null +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragment.java @@ -0,0 +1,110 @@ +/* + * 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. + * + *

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. + * + *

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 { + + /** + * 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); + + /** Releases all bytes reserved through {@link #reserve(long)}, cancelling a waiting one. */ + void release(); + } + + private final Account account; + + private ByteBuffer block; + private int nextOffset; + + 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) { + boolean newBlock; + boolean hadPending; + synchronized (this) { + hadPending = this.block != null; + newBlock = block != this.block; + this.block = block; + 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(); + } + } + + public synchronized ByteBuffer getBlock() { + return block; + } + + public synchronized int getNextOffset() { + return nextOffset; + } + + /** + * 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 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/QueryExecution.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryExecution.java index 422c99da2472a..f6ba26a0fb716 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,6 +113,10 @@ public class QueryExecution implements IQueryExecution { // used for cleaning resultHandle up exactly once private final AtomicBoolean resultHandleCleanUp; + // the serialized TsBlock partially sent to the client, accounted by the query-owned account + private final PendingResultFragment pendingResultFragment; + private final QueryOwnedFragmentAccount pendingFragmentAccount; + private final UDFClassLoaderManager.QueryScope udfQueryScope; // cost time in ns of finished rpc @@ -134,6 +138,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 @@ -549,6 +555,11 @@ private void cleanupForRetry(Throwable t) { */ private void releaseResource(Throwable t, boolean isRetry) { context.recordDeviceEntryDiskIOMetricsOnRelease(); + // release the pending fragment, so that a retried execution does not continue a stale one + 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: @@ -721,6 +732,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/QueryOwnedFragmentAccount.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java new file mode 100644 index 0000000000000..433cf8b2340e9 --- /dev/null +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/execution/QueryOwnedFragmentAccount.java @@ -0,0 +1,187 @@ +/* + * 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.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; +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 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. 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 { + + private static final String PLAN_NODE_ID = "pendingResultFragment"; + + 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. */ + 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 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(); + } + 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(); + } + } + + @Override + 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 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/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/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..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 @@ -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; @@ -256,13 +258,7 @@ public static Pair, Boolean> convertQueryResultByFetchSize( 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); } @@ -272,6 +268,162 @@ 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. + */ + 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; + + 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(); + if (requestOffset != nextOffset) { + throw new IllegalArgumentException( + String.format( + DataNodeQueryMessages + .EXCEPTION_REQUEST_OFFSET_ARG_DOES_NOT_MATCH_THE_PENDING_RESULT_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(); + // 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); + return ResultChunk.fragment(res, nextOffset + fragmentSize, -1, false); + } else if (requestOffset != 0) { + throw new IllegalArgumentException( + String.format( + DataNodeQueryMessages + .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(); + if (!optionalByteBuffer.isPresent()) { + break; + } + // 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 blockSize = byteBuffer.remaining(); + rowCount += positionCount; + if (blockSize <= remainingPayloadSize) { + res.add(byteBuffer); + remainingPayloadSize -= blockSize; + memorySize += blockSize; + pendingFragment.clear(); + } else { + res.add(slice(byteBuffer, 0, remainingPayloadSize)); + pendingFragment.reset(byteBuffer, remainingPayloadSize); + return ResultChunk.fragment(res, remainingPayloadSize, blockSize, 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; + } + + 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; + } + + /** 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); + } + + 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; + } + + 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/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..3c0d27af3a164 --- /dev/null +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/MaterializedResultExecutionTest.java @@ -0,0 +1,207 @@ +/* + * 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.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; +import org.apache.iotdb.db.queryengine.execution.memory.MemorySide; +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( + 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 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()); + assertTrue(firstChunk.hasNextOffset()); + assertEquals(totalLength, firstChunk.getTotalLength()); + assertEquals(payloadSize, firstChunk.getQueryResult().get(0).remaining()); + assertFalse(execution.hasNextResult()); + assertTrue(execution.getByteBufferBatchResult().isEmpty()); + assertEquals(totalLength, memoryPool.getReservedBytes()); + + // 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( + 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 + 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( + 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); + + QueryDataSetUtils.ResultChunk chunk = + QueryDataSetUtils.convertQueryResultByFetchSize(execution, 10, 0, 64); + assertTrue(chunk.hasNextOffset()); + assertEquals(smallCapacity, memoryPool.getReservedBytes()); + execution.stopAndCleanup(null); + 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++) { + builder.getTimeColumnBuilder().writeLong(i); + builder.getColumnBuilder(0).writeLong(i * 10L); + builder.declarePosition(); + } + return builder.build(); + } +} 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..e1118399c5354 --- /dev/null +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/queryengine/plan/execution/PendingResultFragmentTest.java @@ -0,0 +1,156 @@ +/* + * 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 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 { + + @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.releases); + + // 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(1, account.releases); + assertNull(fragment.getBlock()); + } + + @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 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); + } + + @Test + public void testNullAccountDisablesAccounting() { + PendingResultFragment fragment = new PendingResultFragment(null); + fragment.reset(ByteBuffer.allocate(100), 30); + fragment.clear(); + 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 accounted; + private int releases; + + @Override + public void reserve(long bytes) { + reserved.add(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 release() { + accounted = 0; + released.countDown(); + } + } +} 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..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 @@ -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; @@ -41,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; @@ -281,4 +283,181 @@ 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; + + 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.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; + + IQueryExecution queryExecution = Mockito.mock(IQueryExecution.class); + PendingResultFragment pendingFragment = new PendingResultFragment(null); + 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()); + // the complete block is released at once, the fragmented one is released when it completes + + 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; + + 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); + + 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 + 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); + Mockito.when(queryExecution.getPendingResultFragment()) + .thenReturn(new PendingResultFragment(null)); + Mockito.when(queryExecution.getByteBufferBatchResult()) + .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 + } + + @Test(expected = IllegalArgumentException.class) + public void testConvertQueryResultByFetchSizeRejectsMismatchedOffset() throws IoTDBException { + 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); + + 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(null)); + + 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/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..525cb4b2e173a --- /dev/null +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/ResultChunkingEndToEndTest.java @@ -0,0 +1,266 @@ +/* + * 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( + new PendingResultFragment.Account() { + @Override + public void reserve(long bytes) { + // nothing to reserve in this simulation + } + + @Override + public void release() { + 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()) + .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(); + } +} 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{