wangbo commented on code in PR #19924:
URL: https://github.com/apache/doris/pull/19924#discussion_r1218106822


##########
be/src/vec/exec/scan/scan_task_queue.cpp:
##########
@@ -0,0 +1,227 @@
+// 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.
+
+#include "scan_task_queue.h"
+
+#include "pipeline/pipeline_task.h"
+#include "runtime/task_group/task_group.h"
+#include "vec/exec/scan/scanner_context.h"
+
+namespace doris {
+namespace taskgroup {
+static void empty_function() {}
+ScanTask::ScanTask() : ScanTask(empty_function, nullptr) {}
+
+ScanTask::ScanTask(WorkFunction scan_func, vectorized::ScannerContext* 
scanner_context)
+        : scan_func(std::move(scan_func)), scanner_context(scanner_context) {}
+
+ScanTaskQueue::ScanTaskQueue() : 
_queue(config::doris_scanner_thread_pool_queue_size) {}
+
+Status ScanTaskQueue::try_push_back(ScanTask scan_task) {
+    if (_queue.try_put(std::move(scan_task))) {
+        return Status::OK();
+    } else {
+        return Status::InternalError("failed to submit scan task to 
ScanTaskQueue");
+    }
+}
+bool ScanTaskQueue::try_get(ScanTask* scan_task, uint32_t timeout_ms) {
+    return _queue.blocking_get(scan_task, timeout_ms);
+}
+
+ScanTaskTaskGroupQueue::ScanTaskTaskGroupQueue(size_t core_size) : 
_core_size(core_size) {}
+ScanTaskTaskGroupQueue::~ScanTaskTaskGroupQueue() = default;
+
+void ScanTaskTaskGroupQueue::close() {
+    std::unique_lock<std::mutex> lock(_rs_mutex);
+    _closed = true;
+    _wait_task.notify_all();
+}
+
+bool ScanTaskTaskGroupQueue::take(ScanTask* scan_task) {
+    std::unique_lock<std::mutex> lock(_rs_mutex);
+    taskgroup::TGSTEntityPtr entity = nullptr;
+    while (entity == nullptr) {
+        if (_closed) {
+            return false;
+        }
+        if (_group_entities.empty()) {
+            _wait_task.wait(lock);
+        } else {
+            entity = _next_tg_entity();
+            if (!entity) {
+                _wait_task.wait_for(lock, 
std::chrono::milliseconds(WAIT_CORE_TASK_TIMEOUT_MS));
+            }
+        }
+    }
+    DCHECK(entity->task_size() > 0);
+    if (entity->task_size() == 1) {
+        _dequeue_task_group(entity);
+    }
+    return entity->task_queue()->try_get(scan_task, WAIT_CORE_TASK_TIMEOUT_MS 
/* timeout_ms */);
+}
+
+template bool 
ScanTaskTaskGroupQueue::push_back<TabletStorageType::STORAGE_TYPE_LOCAL>(ScanTask);
+template bool 
ScanTaskTaskGroupQueue::push_back<TabletStorageType::STORAGE_TYPE_REMOTE>(ScanTask);
+template <TabletStorageType storage_type>
+bool ScanTaskTaskGroupQueue::push_back(ScanTask scan_task) {
+    auto* entity = _task_entity<storage_type>(scan_task);
+    std::unique_lock<std::mutex> lock(_rs_mutex);
+    auto status = entity->task_queue()->try_push_back(scan_task);
+    if (!status.ok()) {
+        LOG(WARNING) << "try_push_back scan task fail: " << status;
+        return false;
+    }
+    if (_group_entities.find(entity) == _group_entities.end()) {
+        _enqueue_task_group(entity);
+    }
+    _wait_task.notify_one();
+    return true;
+}
+
+template void 
ScanTaskTaskGroupQueue::update_statistics<TabletStorageType::STORAGE_TYPE_REMOTE>(
+        ScanTask scan_task, int64_t time_spent);
+template void 
ScanTaskTaskGroupQueue::update_statistics<TabletStorageType::STORAGE_TYPE_LOCAL>(
+        ScanTask scan_task, int64_t time_spent);
+template <TabletStorageType storage_type>
+void ScanTaskTaskGroupQueue::update_statistics(ScanTask scan_task, int64_t 
time_spent) {
+    auto* entity = _task_entity<storage_type>(scan_task);
+    std::unique_lock<std::mutex> lock(_rs_mutex);
+    auto find_entity = _group_entities.find(entity);
+    bool is_in_queue = find_entity != _group_entities.end();
+    VLOG_DEBUG << storage_type << " update_statistics " << 
entity->debug_string()
+               << ", in queue:" << is_in_queue << ", time_spent: " << 
time_spent;
+    if (is_in_queue) {
+        _group_entities.erase(entity);
+    }
+    entity->incr_runtime_ns(time_spent);
+    if (is_in_queue) {
+        _group_entities.emplace(entity);
+        _update_min_tg();
+    }
+}
+
+template TGSTEntityPtr 
ScanTaskTaskGroupQueue::_task_entity<TabletStorageType::STORAGE_TYPE_LOCAL>(
+        ScanTask& scan_task);
+template TGSTEntityPtr 
ScanTaskTaskGroupQueue::_task_entity<TabletStorageType::STORAGE_TYPE_REMOTE>(
+        ScanTask& scan_task);
+template <TabletStorageType storage_type>
+TGSTEntityPtr ScanTaskTaskGroupQueue::_task_entity(ScanTask& scan_task) {
+    auto* group = scan_task.scanner_context->get_task_group();

Review Comment:
   Maybe we can move this branch(local or remote) to scan_task



-- 
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