yiguolei commented on code in PR #41264:
URL: https://github.com/apache/doris/pull/41264#discussion_r1774427538


##########
be/src/runtime/query_context.cpp:
##########
@@ -442,6 +446,68 @@ size_t QueryContext::get_revocable_size() const {
     return revocable_size;
 }
 
+Status QueryContext::revoke_memory() {
+    std::vector<std::pair<size_t, pipeline::PipelineTask*>> tasks;
+    std::vector<std::shared_ptr<pipeline::PipelineFragmentContext>> fragments;
+    for (auto&& [fragment_id, fragment_wptr] : _fragment_id_to_pipeline_ctx) {
+        auto fragment_ctx = fragment_wptr.lock();
+        if (!fragment_ctx) {
+            continue;
+        }
+
+        auto tasks_of_fragment = fragment_ctx->get_revocable_tasks();
+        for (auto* task : tasks_of_fragment) {
+            tasks.emplace_back(task->get_revocable_size(), task);
+        }
+        fragments.emplace_back(std::move(fragment_ctx));
+    }
+
+    std::sort(tasks.begin(), tasks.end(), [](auto&& l, auto&& r) { return 
l.first > r.first; });
+
+    const auto mem_limit = query_mem_tracker->limit();
+    const auto target_revoking_size = mem_limit * 0.2;
+    size_t revoked_size = 0;
+    size_t revoked_tasks_count = 0;
+    _need_reset_memory_sufficient = true;
+    CHECK_EQ(_revoking_tasks_count, 0);
+    _revoking_tasks_count = 0;
+    for (auto&& [revocable_size, task] : tasks) {
+        RETURN_IF_ERROR(task->revoke_memory());
+        ++revoked_tasks_count;
+
+        revoked_size += revocable_size;
+        if (revoked_size >= target_revoking_size) {
+            break;
+        }
+    }
+
+    LOG(INFO) << "query: " << print_id(_query_id) << " total revoked size: " 
<< revoked_size
+              << ", target_size: " << target_revoking_size
+              << ", tasks count: " << revoked_tasks_count << "/" << 
tasks.size();
+
+    std::lock_guard<std::mutex> lock(_revoking_tasks_mutex);
+    if (_need_reset_memory_sufficient && _revoking_tasks_count == 0) {
+        _memory_sufficient_dependency->set_ready();
+        _need_reset_memory_sufficient = false;
+        LOG(INFO) << "query: " << print_id(_query_id)
+                  << " all revoking tasks done(no task really run), resume 
it.";
+    }
+    return Status::OK();
+}
+
+void QueryContext::decrease_revoking_tasks_count() {
+    std::lock_guard<std::mutex> lock(_revoking_tasks_mutex);
+    auto count = _revoking_tasks_count.fetch_sub(1);
+    LOG(INFO) << "query: " << print_id(_query_id) << ", one revoking task 
done, " << (count - 1)
+              << " tasks are remaining running. _need_reset_memory_sufficient: 
"
+              << _need_reset_memory_sufficient;
+    if (count == 1 && _need_reset_memory_sufficient) {
+        _memory_sufficient_dependency->set_ready();
+        _need_reset_memory_sufficient = false;

Review Comment:
   为什么要有这个标记位?



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