morningman commented on code in PR #47915:
URL: https://github.com/apache/doris/pull/47915#discussion_r1961770254


##########
fe/fe-core/src/main/java/org/apache/doris/qe/ResultReceiverConsumer.java:
##########
@@ -0,0 +1,124 @@
+// 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.doris.qe;
+
+import org.apache.doris.common.Status;
+import org.apache.doris.common.UserException;
+
+import com.google.common.collect.Lists;
+import org.apache.thrift.TException;
+
+import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+public class ResultReceiverConsumer {
+    class ReceiverContext {
+        public ReceiverContext(ResultReceiver receiver, int offset) {
+            this.receiver = receiver;
+            this.offset = offset;
+        }
+
+        public void createFuture() {
+            if (errMsg != null) {
+                return;
+            }
+            try {
+                future = executor.submit(() -> {
+                    RowBatch rowBatch = null;
+                    try {
+                        rowBatch = receiver.getNext(status);
+                    } catch (TException e) {
+                        setErrMsg(e.getMessage());
+                    }
+                    readyOffsets.offer(offset);
+                    return rowBatch;
+                });
+            } catch (Throwable e) {
+                setErrMsg(e.getMessage());
+                readyOffsets.offer(offset);
+            }
+        }
+
+        ResultReceiver receiver;
+        Status status = new Status();
+        Future<RowBatch> future;
+        final int offset;
+    }
+
+    private final ExecutorService executor;
+    private List<ReceiverContext> contexts = Lists.newArrayList();
+    private boolean futureInitialized = false;
+    private String errMsg;
+
+    void setErrMsg(String errMsg) {
+        this.errMsg = errMsg;
+        executor.shutdownNow();
+    }
+
+    BlockingQueue<Integer> readyOffsets;
+    int finishedReceivers = 0;
+
+    public ResultReceiverConsumer(List<ResultReceiver> resultReceivers) {
+        for (int i = 0; i < resultReceivers.size(); i++) {
+            ReceiverContext context = new 
ReceiverContext(resultReceivers.get(i), i);
+            contexts.add(context);
+        }
+        this.executor = Executors.newFixedThreadPool(resultReceivers.size());

Review Comment:
   and you are not closing the thread pool?



##########
fe/fe-core/src/main/java/org/apache/doris/qe/ResultReceiverConsumer.java:
##########
@@ -0,0 +1,124 @@
+// 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.doris.qe;
+
+import org.apache.doris.common.Status;
+import org.apache.doris.common.UserException;
+
+import com.google.common.collect.Lists;
+import org.apache.thrift.TException;
+
+import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+public class ResultReceiverConsumer {
+    class ReceiverContext {
+        public ReceiverContext(ResultReceiver receiver, int offset) {
+            this.receiver = receiver;
+            this.offset = offset;
+        }
+
+        public void createFuture() {
+            if (errMsg != null) {
+                return;
+            }
+            try {
+                future = executor.submit(() -> {
+                    RowBatch rowBatch = null;
+                    try {
+                        rowBatch = receiver.getNext(status);
+                    } catch (TException e) {
+                        setErrMsg(e.getMessage());
+                    }
+                    readyOffsets.offer(offset);
+                    return rowBatch;
+                });
+            } catch (Throwable e) {
+                setErrMsg(e.getMessage());
+                readyOffsets.offer(offset);
+            }
+        }
+
+        ResultReceiver receiver;
+        Status status = new Status();
+        Future<RowBatch> future;
+        final int offset;
+    }
+
+    private final ExecutorService executor;
+    private List<ReceiverContext> contexts = Lists.newArrayList();
+    private boolean futureInitialized = false;
+    private String errMsg;
+
+    void setErrMsg(String errMsg) {
+        this.errMsg = errMsg;
+        executor.shutdownNow();
+    }
+
+    BlockingQueue<Integer> readyOffsets;
+    int finishedReceivers = 0;
+
+    public ResultReceiverConsumer(List<ResultReceiver> resultReceivers) {

Review Comment:
   I suggest to write a ut for this class
   Here is a example written by cursor, I didn't not run it, but may help you
   
[ResultReceiverConsumerTest.java.txt](https://github.com/user-attachments/files/18870715/ResultReceiverConsumerTest.java.txt)
   



##########
fe/fe-core/src/main/java/org/apache/doris/qe/ResultReceiverConsumer.java:
##########
@@ -0,0 +1,124 @@
+// 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.doris.qe;
+
+import org.apache.doris.common.Status;
+import org.apache.doris.common.UserException;
+
+import com.google.common.collect.Lists;
+import org.apache.thrift.TException;
+
+import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+
+public class ResultReceiverConsumer {
+    class ReceiverContext {
+        public ReceiverContext(ResultReceiver receiver, int offset) {
+            this.receiver = receiver;
+            this.offset = offset;
+        }
+
+        public void createFuture() {
+            if (errMsg != null) {
+                return;
+            }
+            try {
+                future = executor.submit(() -> {
+                    RowBatch rowBatch = null;
+                    try {
+                        rowBatch = receiver.getNext(status);
+                    } catch (TException e) {
+                        setErrMsg(e.getMessage());
+                    }
+                    readyOffsets.offer(offset);
+                    return rowBatch;
+                });
+            } catch (Throwable e) {
+                setErrMsg(e.getMessage());
+                readyOffsets.offer(offset);
+            }
+        }
+
+        ResultReceiver receiver;
+        Status status = new Status();
+        Future<RowBatch> future;
+        final int offset;
+    }
+
+    private final ExecutorService executor;
+    private List<ReceiverContext> contexts = Lists.newArrayList();
+    private boolean futureInitialized = false;
+    private String errMsg;
+
+    void setErrMsg(String errMsg) {
+        this.errMsg = errMsg;
+        executor.shutdownNow();
+    }
+
+    BlockingQueue<Integer> readyOffsets;
+    int finishedReceivers = 0;
+
+    public ResultReceiverConsumer(List<ResultReceiver> resultReceivers) {
+        for (int i = 0; i < resultReceivers.size(); i++) {
+            ReceiverContext context = new 
ReceiverContext(resultReceivers.get(i), i);
+            contexts.add(context);
+        }
+        this.executor = Executors.newFixedThreadPool(resultReceivers.size());

Review Comment:
   how about use a global thread pool with unlimited size?
   to save thread creation time



-- 
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...@doris.apache.org

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


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

Reply via email to