|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pinot.core.operator.combine; |
| 20 | + |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.Collection; |
| 23 | +import java.util.Iterator; |
| 24 | +import java.util.List; |
| 25 | +import java.util.concurrent.CountDownLatch; |
| 26 | +import java.util.concurrent.ExecutorService; |
| 27 | +import java.util.concurrent.TimeUnit; |
| 28 | +import java.util.concurrent.TimeoutException; |
| 29 | +import org.apache.pinot.core.common.Operator; |
| 30 | +import org.apache.pinot.core.data.table.IndexedTable; |
| 31 | +import org.apache.pinot.core.data.table.IntermediateRecord; |
| 32 | +import org.apache.pinot.core.data.table.Key; |
| 33 | +import org.apache.pinot.core.data.table.Record; |
| 34 | +import org.apache.pinot.core.operator.AcquireReleaseColumnsSegmentOperator; |
| 35 | +import org.apache.pinot.core.operator.blocks.results.BaseResultsBlock; |
| 36 | +import org.apache.pinot.core.operator.blocks.results.ExceptionResultsBlock; |
| 37 | +import org.apache.pinot.core.operator.blocks.results.GroupByResultsBlock; |
| 38 | +import org.apache.pinot.core.query.aggregation.function.AggregationFunction; |
| 39 | +import org.apache.pinot.core.query.aggregation.groupby.AggregationGroupByResult; |
| 40 | +import org.apache.pinot.core.query.aggregation.groupby.GroupKeyGenerator; |
| 41 | +import org.apache.pinot.core.query.request.context.QueryContext; |
| 42 | +import org.apache.pinot.core.util.GroupByUtils; |
| 43 | +import org.apache.pinot.spi.trace.Tracing; |
| 44 | +import org.slf4j.Logger; |
| 45 | +import org.slf4j.LoggerFactory; |
| 46 | + |
| 47 | + |
| 48 | +/** |
| 49 | + * Combine operator for group-by queries. |
| 50 | + * TODO: Use CombineOperatorUtils.getNumThreadsForQuery() to get the parallelism of the query instead of using |
| 51 | + * all threads |
| 52 | + */ |
| 53 | +@SuppressWarnings("rawtypes") |
| 54 | +public class NonblockingGroupByCombineOperator extends BaseSingleBlockCombineOperator<GroupByResultsBlock> { |
| 55 | + public static final int MAX_TRIM_THRESHOLD = 1_000_000_000; |
| 56 | + public static final String ALGORITHM = "non-blocking"; |
| 57 | + |
| 58 | + private static final Logger LOGGER = LoggerFactory.getLogger(NonblockingGroupByCombineOperator.class); |
| 59 | + private static final String EXPLAIN_NAME = "NON_BLOCKING_COMBINE_GROUP_BY"; |
| 60 | + |
| 61 | + private final int _numAggregationFunctions; |
| 62 | + private final int _numGroupByExpressions; |
| 63 | + private final int _numColumns; |
| 64 | + // We use a CountDownLatch to track if all Futures are finished by the query timeout, and cancel the unfinished |
| 65 | + // _futures (try to interrupt the execution if it already started). |
| 66 | + private final CountDownLatch _operatorLatch; |
| 67 | + |
| 68 | + private volatile IndexedTable _indexedTable; |
| 69 | + private volatile boolean _numGroupsLimitReached; |
| 70 | + |
| 71 | + public NonblockingGroupByCombineOperator(List<Operator> operators, QueryContext queryContext, |
| 72 | + ExecutorService executorService) { |
| 73 | + super(null, operators, overrideMaxExecutionThreads(queryContext, operators.size()), executorService); |
| 74 | + |
| 75 | + AggregationFunction[] aggregationFunctions = _queryContext.getAggregationFunctions(); |
| 76 | + assert aggregationFunctions != null; |
| 77 | + _numAggregationFunctions = aggregationFunctions.length; |
| 78 | + assert _queryContext.getGroupByExpressions() != null; |
| 79 | + _numGroupByExpressions = _queryContext.getGroupByExpressions().size(); |
| 80 | + _numColumns = _numGroupByExpressions + _numAggregationFunctions; |
| 81 | + _operatorLatch = new CountDownLatch(_numTasks); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * For group-by queries, when maxExecutionThreads is not explicitly configured, create one task per operator. |
| 86 | + */ |
| 87 | + private static QueryContext overrideMaxExecutionThreads(QueryContext queryContext, int numOperators) { |
| 88 | + int maxExecutionThreads = queryContext.getMaxExecutionThreads(); |
| 89 | + if (maxExecutionThreads <= 0) { |
| 90 | + queryContext.setMaxExecutionThreads(numOperators); |
| 91 | + } |
| 92 | + return queryContext; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public String toExplainString() { |
| 97 | + return EXPLAIN_NAME; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Executes query on one segment in a worker thread and merges the results into the indexed table. |
| 102 | + */ |
| 103 | + @Override |
| 104 | + protected void processSegments() { |
| 105 | + int operatorId; |
| 106 | + while (_processingException.get() == null && (operatorId = _nextOperatorId.getAndIncrement()) < _numOperators) { |
| 107 | + Operator operator = _operators.get(operatorId); |
| 108 | + try { |
| 109 | + if (operator instanceof AcquireReleaseColumnsSegmentOperator) { |
| 110 | + ((AcquireReleaseColumnsSegmentOperator) operator).acquire(); |
| 111 | + } |
| 112 | + GroupByResultsBlock resultsBlock = (GroupByResultsBlock) operator.nextBlock(); |
| 113 | + IndexedTable indexedTable = null; |
| 114 | + if (_indexedTable != null) { |
| 115 | + synchronized (this) { |
| 116 | + if (_indexedTable != null) { |
| 117 | + indexedTable = _indexedTable; |
| 118 | + _indexedTable = null; |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + if (indexedTable == null) { |
| 123 | + indexedTable = GroupByUtils.createIndexedTableForCombineOperator(resultsBlock, _queryContext, 1); |
| 124 | + } |
| 125 | + |
| 126 | + // Set groups limit reached flag. |
| 127 | + if (resultsBlock.isNumGroupsLimitReached()) { |
| 128 | + _numGroupsLimitReached = true; |
| 129 | + } |
| 130 | + |
| 131 | + // Merge aggregation group-by result. |
| 132 | + // Iterate over the group-by keys, for each key, update the group-by result in the indexedTable |
| 133 | + Collection<IntermediateRecord> intermediateRecords = resultsBlock.getIntermediateRecords(); |
| 134 | + // Count the number of merged keys |
| 135 | + int mergedKeys = 0; |
| 136 | + // For now, only GroupBy OrderBy query has pre-constructed intermediate records |
| 137 | + if (intermediateRecords == null) { |
| 138 | + // Merge aggregation group-by result. |
| 139 | + AggregationGroupByResult aggregationGroupByResult = resultsBlock.getAggregationGroupByResult(); |
| 140 | + if (aggregationGroupByResult != null) { |
| 141 | + // Iterate over the group-by keys, for each key, update the group-by result in the indexedTable |
| 142 | + Iterator<GroupKeyGenerator.GroupKey> dicGroupKeyIterator = aggregationGroupByResult.getGroupKeyIterator(); |
| 143 | + while (dicGroupKeyIterator.hasNext()) { |
| 144 | + GroupKeyGenerator.GroupKey groupKey = dicGroupKeyIterator.next(); |
| 145 | + Object[] keys = groupKey._keys; |
| 146 | + Object[] values = Arrays.copyOf(keys, _numColumns); |
| 147 | + int groupId = groupKey._groupId; |
| 148 | + for (int i = 0; i < _numAggregationFunctions; i++) { |
| 149 | + values[_numGroupByExpressions + i] = aggregationGroupByResult.getResultForGroupId(i, groupId); |
| 150 | + } |
| 151 | + indexedTable.upsert(new Key(keys), new Record(values)); |
| 152 | + Tracing.ThreadAccountantOps.sampleAndCheckInterruptionPeriodically(mergedKeys); |
| 153 | + mergedKeys++; |
| 154 | + } |
| 155 | + } |
| 156 | + } else { |
| 157 | + for (IntermediateRecord intermediateResult : intermediateRecords) { |
| 158 | + //TODO: change upsert api so that it accepts intermediateRecord directly |
| 159 | + indexedTable.upsert(intermediateResult._key, intermediateResult._record); |
| 160 | + Tracing.ThreadAccountantOps.sampleAndCheckInterruptionPeriodically(mergedKeys); |
| 161 | + mergedKeys++; |
| 162 | + } |
| 163 | + } |
| 164 | + boolean setGroupByResult = false; |
| 165 | + while (!setGroupByResult) { |
| 166 | + IndexedTable indexedTableToMerge = null; |
| 167 | + synchronized (this) { |
| 168 | + if (_indexedTable == null) { |
| 169 | + _indexedTable = indexedTable; |
| 170 | + setGroupByResult = true; |
| 171 | + } else { |
| 172 | + indexedTableToMerge = _indexedTable; |
| 173 | + _indexedTable = null; |
| 174 | + } |
| 175 | + } |
| 176 | + if (indexedTableToMerge != null) { |
| 177 | + indexedTable.merge(indexedTableToMerge); |
| 178 | + } |
| 179 | + } |
| 180 | + } catch (RuntimeException e) { |
| 181 | + throw wrapOperatorException(operator, e); |
| 182 | + } finally { |
| 183 | + if (operator instanceof AcquireReleaseColumnsSegmentOperator) { |
| 184 | + ((AcquireReleaseColumnsSegmentOperator) operator).release(); |
| 185 | + } |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public void onProcessSegmentsException(Throwable t) { |
| 192 | + _processingException.compareAndSet(null, t); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public void onProcessSegmentsFinish() { |
| 197 | + _operatorLatch.countDown(); |
| 198 | + } |
| 199 | + |
| 200 | + /** |
| 201 | + * {@inheritDoc} |
| 202 | + * |
| 203 | + * <p>Combines intermediate selection result blocks from underlying operators and returns a merged one. |
| 204 | + * <ul> |
| 205 | + * <li> |
| 206 | + * Merges multiple intermediate selection result blocks as a merged one. |
| 207 | + * </li> |
| 208 | + * <li> |
| 209 | + * Set all exceptions encountered during execution into the merged result block |
| 210 | + * </li> |
| 211 | + * </ul> |
| 212 | + */ |
| 213 | + @Override |
| 214 | + public BaseResultsBlock mergeResults() |
| 215 | + throws Exception { |
| 216 | + long timeoutMs = _queryContext.getEndTimeMs() - System.currentTimeMillis(); |
| 217 | + boolean opCompleted = _operatorLatch.await(timeoutMs, TimeUnit.MILLISECONDS); |
| 218 | + if (!opCompleted) { |
| 219 | + // If this happens, the broker side should already timed out, just log the error and return |
| 220 | + String errorMessage = |
| 221 | + String.format("Timed out while combining group-by order-by results after %dms, queryContext = %s", timeoutMs, |
| 222 | + _queryContext); |
| 223 | + LOGGER.error(errorMessage); |
| 224 | + return new ExceptionResultsBlock(new TimeoutException(errorMessage)); |
| 225 | + } |
| 226 | + |
| 227 | + Throwable processingException = _processingException.get(); |
| 228 | + if (processingException != null) { |
| 229 | + return new ExceptionResultsBlock(processingException); |
| 230 | + } |
| 231 | + |
| 232 | + IndexedTable indexedTable = _indexedTable; |
| 233 | + if (_queryContext.isServerReturnFinalResult()) { |
| 234 | + indexedTable.finish(true, true); |
| 235 | + } else if (_queryContext.isServerReturnFinalResultKeyUnpartitioned()) { |
| 236 | + indexedTable.finish(false, true); |
| 237 | + } else { |
| 238 | + indexedTable.finish(false); |
| 239 | + } |
| 240 | + GroupByResultsBlock mergedBlock = new GroupByResultsBlock(indexedTable, _queryContext); |
| 241 | + mergedBlock.setNumGroupsLimitReached(_numGroupsLimitReached); |
| 242 | + mergedBlock.setNumResizes(indexedTable.getNumResizes()); |
| 243 | + mergedBlock.setResizeTimeMs(indexedTable.getResizeTimeMs()); |
| 244 | + return mergedBlock; |
| 245 | + } |
| 246 | +} |
0 commit comments