ericpai commented on a change in pull request #4514: URL: https://github.com/apache/iotdb/pull/4514#discussion_r762724390
########## File path: server/src/main/java/org/apache/iotdb/db/query/dataset/udf/UDTFFragmentDataSetTask.java ########## @@ -0,0 +1,84 @@ +/* + * 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.query.dataset.udf; + +import org.apache.iotdb.db.concurrent.WrappedRunnable; +import org.apache.iotdb.tsfile.read.common.RowRecord; +import org.apache.iotdb.tsfile.read.query.dataset.QueryDataSet; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.BlockingQueue; + +public class UDTFFragmentDataSetTask extends WrappedRunnable { + + private static final Logger LOGGER = LoggerFactory.getLogger(UDTFFragmentDataSetTask.class); + + private final int fetchSize; + private final QueryDataSet queryDataSet; + + // there are 3 elements in Object[]. + // [0]: RowRecord[] or Throwable. + // [2]: Integer. actual length of produced row records in [0]. note that the element is -1 when Review comment: It seems that this comment is mismatched with the data actually returned: [1] is an integer and [2] is a boolean. ########## File path: server/src/main/java/org/apache/iotdb/db/query/dataset/udf/UDTFFragmentDataSet.java ########## @@ -0,0 +1,122 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.iotdb.db.query.dataset.udf; + +import org.apache.iotdb.db.exception.query.QueryProcessException; +import org.apache.iotdb.db.query.pool.DataSetFragmentExecutionPoolManager; +import org.apache.iotdb.db.query.udf.core.layer.RawQueryInputLayer; +import org.apache.iotdb.db.query.udf.core.reader.LayerPointReader; +import org.apache.iotdb.tsfile.read.common.RowRecord; +import org.apache.iotdb.tsfile.read.query.dataset.QueryDataSet; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.LinkedBlockingQueue; + +public class UDTFFragmentDataSet extends QueryDataSet { + + private static final Logger LOGGER = LoggerFactory.getLogger(UDTFFragmentDataSet.class); + + private static final int BLOCKING_QUEUE_CAPACITY = 2; + private static final DataSetFragmentExecutionPoolManager + DATA_SET_FRAGMENT_EXECUTION_POOL_MANAGER = DataSetFragmentExecutionPoolManager.getInstance(); + + private final QueryDataSet fragmentDataSet; + private final BlockingQueue<Object[]> productionBlockingQueue; + + private RowRecord[] rowRecords = null; + private int rowRecordsLength = 0; + private int rowRecordsIndexConsumed = -1; + + private boolean hasNextRowRecords = true; + + public UDTFFragmentDataSet(RawQueryInputLayer rawQueryInputLayer, LayerPointReader[] transformers) + throws QueryProcessException, IOException { + fragmentDataSet = new UDTFAlignByTimeDataSet(rawQueryInputLayer, transformers); + productionBlockingQueue = new LinkedBlockingQueue<>(BLOCKING_QUEUE_CAPACITY); + submitTask(); + } + + @Override + public boolean hasNextWithoutConstraint() { + try { + return rowRecords != null || tryToTakeNextRowRecords(); + } catch (InterruptedException e) { + return onThrowable(e); + } + } + + private boolean tryToTakeNextRowRecords() throws InterruptedException { + if (!hasNextRowRecords) { + return false; + } + + Object[] production = productionBlockingQueue.take(); + + Object rowRecordArrayOrThrowable = production[0]; + if (rowRecordArrayOrThrowable instanceof Throwable) { + return onThrowable((Throwable) rowRecordArrayOrThrowable); + } + + rowRecords = (RowRecord[]) production[0]; + rowRecordsLength = (int) production[1]; + rowRecordsIndexConsumed = -1; + hasNextRowRecords = (boolean) production[2]; + + if (rowRecordsLength == 0) { + rowRecords = null; + // assert !hasNextRowRecords; + return false; + } + + if (hasNextRowRecords) { + submitTask(); + } + + return true; + } + + @Override + public RowRecord nextWithoutConstraint() { + RowRecord rowRecord = rowRecords[++rowRecordsIndexConsumed]; + if (rowRecordsIndexConsumed == rowRecordsLength - 1) { + rowRecords = null; + } + return rowRecord; + } + + private void submitTask() { + if (productionBlockingQueue.remainingCapacity() > 0) { Review comment: What's the consquence of running a query when `productionBlockingQueue.remainingCapacity() == 0`? It's more likely that the task will be dropped and the next time we call `tryToTakeNextRowRecords()`, it will be blocked in `productionBlockingQueue.take()` forever(`hasNextRoweRecords == true` and the previous call of `tryToTakeNextRowRecords()` is true). I think it's better to throw an exception when the queue is full. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
