walterddr commented on code in PR #10779:
URL: https://github.com/apache/pinot/pull/10779#discussion_r1198015757


##########
pinot-query-runtime/src/main/java/org/apache/pinot/query/runtime/plan/pipeline/PipelineBreakOperator.java:
##########
@@ -0,0 +1,118 @@
+/**
+ * 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.pinot.query.runtime.plan.pipeline;
+
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Deque;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import javax.annotation.Nullable;
+import org.apache.pinot.common.exception.QueryException;
+import org.apache.pinot.core.common.Operator;
+import org.apache.pinot.query.runtime.blocks.TransferableBlock;
+import org.apache.pinot.query.runtime.blocks.TransferableBlockUtils;
+import org.apache.pinot.query.runtime.operator.MultiStageOperator;
+import org.apache.pinot.query.runtime.plan.OpChainExecutionContext;
+
+
+public class PipelineBreakOperator extends MultiStageOperator {
+  private static final String EXPLAIN_NAME = "PIPELINE_BREAKER";
+  private final Deque<Map.Entry<Integer, Operator<TransferableBlock>>> 
_workerEntries;
+  private final Map<Integer, List<TransferableBlock>> _resultMap;
+  private final CountDownLatch _workerDoneLatch;
+  private TransferableBlock _errorBlock;
+
+
+  public PipelineBreakOperator(OpChainExecutionContext context,
+      Map<Integer, Operator<TransferableBlock>> pipelineWorkerMap) {
+    super(context);
+    _resultMap = new HashMap<>();
+    _workerEntries = new ArrayDeque<>();
+    _workerEntries.addAll(pipelineWorkerMap.entrySet());
+    _workerDoneLatch = new CountDownLatch(1);
+  }
+
+  public Map<Integer, List<TransferableBlock>> getResult() {
+    try {
+      boolean isWorkerDone =
+          _workerDoneLatch.await(_context.getDeadlineMs() - 
System.currentTimeMillis(), TimeUnit.MILLISECONDS);
+      if (isWorkerDone && _errorBlock == null) {
+        return _resultMap;
+      }
+    } catch (Exception e) {
+      _errorBlock = TransferableBlockUtils.getErrorTransferableBlock(e);
+    }
+    return Collections.singletonMap(-1, 
Collections.singletonList(_errorBlock));
+  }
+
+  @Nullable
+  @Override
+  public String toExplainString() {
+    return EXPLAIN_NAME;
+  }
+
+  @Override
+  protected TransferableBlock getNextBlock() {
+    if (System.currentTimeMillis() > _context.getDeadlineMs()) {
+      _errorBlock = 
TransferableBlockUtils.getErrorTransferableBlock(QueryException.EXECUTION_TIMEOUT_ERROR);
+      _workerDoneLatch.countDown();
+      return _errorBlock;
+    }
+
+    // Poll from every mailbox operator in round-robin fashion:
+    // - Return the first content block
+    // - If no content block found but there are mailboxes not finished, 
return no-op block
+    // - If all content blocks are already returned, return end-of-stream block
+    int numWorkers = _workerEntries.size();
+    for (int i = 0; i < numWorkers; i++) {
+      Map.Entry<Integer, Operator<TransferableBlock>> worker = 
_workerEntries.remove();
+      TransferableBlock block = worker.getValue().nextBlock();
+
+      // Release the mailbox when the block is end-of-stream
+      if (block != null && block.isSuccessfulEndOfStreamBlock()) {
+        continue;
+      }
+
+      // Add the worker back to the queue if the block is not end-of-stream
+      _workerEntries.add(worker);
+      if (block != null) {
+        if (block.isErrorBlock()) {
+          _errorBlock = block;
+          _workerDoneLatch.countDown();
+          return _errorBlock;
+        }
+        List<TransferableBlock> blockList = 
_resultMap.computeIfAbsent(worker.getKey(), (k) -> new ArrayList<>());
+        blockList.add(block);

Review Comment:
   TODO: 
   1. only data block should be added, b/c non-datablock are not useful for 
pipeline breaker results
   2. consider a way to pass around stats and metadata, currently these info 
are discarded.



-- 
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: commits-unsubscr...@pinot.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to