xinyiZzz commented on a change in pull request #7198:
URL: https://github.com/apache/incubator-doris/pull/7198#discussion_r764799713



##########
File path: be/src/runtime/thread_mem_tracker.cpp
##########
@@ -0,0 +1,129 @@
+// 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 "runtime/thread_mem_tracker.h"
+
+#include "service/backend_options.h"
+
+namespace doris {
+
+void ThreadMemTracker::attach_query(const std::string& query_id,
+                                    const TUniqueId& fragment_instance_id) {
+#ifdef BE_TEST
+    if (ExecEnv::GetInstance()->query_mem_tracker_registry() == nullptr) {
+        return;
+    }
+#endif
+    update_query_mem_tracker(
+            
ExecEnv::GetInstance()->query_mem_tracker_registry()->get_query_mem_tracker(query_id));
+    _fragment_instance_id = fragment_instance_id;
+}
+
+void ThreadMemTracker::detach_query() {
+    update_query_mem_tracker(std::weak_ptr<MemTracker>());
+    _fragment_instance_id = TUniqueId();
+}
+
+void ThreadMemTracker::update_query_mem_tracker(std::weak_ptr<MemTracker> 
mem_tracker) {
+    if (_untracked_mem != 0) {
+        consume();
+        _untracked_mem = 0;
+    }
+    _query_mem_tracker = mem_tracker;
+}
+
+void ThreadMemTracker::query_mem_limit_exceeded(int64_t mem_usage) {
+    if (_fragment_instance_id != TUniqueId() && 
ExecEnv::GetInstance()->initialized() &&
+        
ExecEnv::GetInstance()->fragment_mgr()->is_canceling(_fragment_instance_id).ok())
 {
+        std::string detail = "Query Memory exceed limit in TCMalloc Hook New.";
+        auto st = _query_mem_tracker.lock()->MemLimitExceeded(nullptr, detail, 
mem_usage);
+
+        detail +=
+                " Query Memory exceed limit in TCMalloc Hook New, Backend: {}, 
Fragment: {}, Used: "
+                "{}, Limit: {}. You can change the limit by session variable 
exec_mem_limit.";
+        fmt::format(detail, BackendOptions::get_localhost(), 
print_id(_fragment_instance_id),
+                    std::to_string(_query_mem_tracker.lock()->consumption()),
+                    std::to_string(_query_mem_tracker.lock()->limit()));
+        ExecEnv::GetInstance()->fragment_mgr()->cancel(

Review comment:
       I will pay attention to the problem that `PlanFragmentExecutor::cancel` 
cannot cancel instance in time.
   
   After that, can consider printing the thread context recorded in 
ThreadContext, but it may be useless, because ThreadMemTracker will accumulate 
a batch of memory requests to call consume, and the memory request 
mem_limit_exceeded may be small. 
   It may be more effective to print the status of the query and all instance 
trackers.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to