Copilot commented on code in PR #62285:
URL: https://github.com/apache/doris/pull/62285#discussion_r3056867234
##########
be/src/runtime/workload_group/workload_group_manager.cpp:
##########
@@ -714,11 +714,20 @@ bool WorkloadGroupMgr::handle_single_query_(const
std::shared_ptr<ResourceContex
return true;
}
} else if (paused_reason.is<ErrorCode::WORKLOAD_GROUP_MEMORY_EXCEEDED>()) {
- if (!wg->exceed_limit()) {
+ bool exceed_low_watermark = false;
+ bool exceed_high_watermark = false;
+ wg->check_mem_used(size_to_reserve, &exceed_low_watermark,
&exceed_high_watermark);
+ if (!exceed_high_watermark) {
LOG(INFO) << "Query: " << query_id
<< " paused caused by WORKLOAD_GROUP_MEMORY_EXCEEDED,
now resume it.";
requestor->task_controller()->set_memory_sufficient(true);
return true;
+ } else if (memory_usage <= limit) {
+ LOG(INFO) << "Query: " << query_id
+ << " paused caused by WORKLOAD_GROUP_MEMORY_EXCEEDED,
keep it paused. "
+ << "Reserve size: " <<
PrettyPrinter::print_bytes(size_to_reserve)
+ << ", wg info: " << wg->memory_debug_string();
+ return false;
} else {
Status error_status = Status::MemoryLimitExceeded(
"Query {} workload group memory is exceeded"
Review Comment:
In the `memory_usage > limit` branch, the error is constructed with the
message \"workload group memory is exceeded\", but the branch condition
indicates the query/requestor has exceeded its own limit (`memory_usage >
limit`). This makes the cancellation reason misleading for users and logs.
Consider updating the message (and/or error type) to reflect query memory limit
exceeded (optionally including usage/limit values), reserving the
workload-group message for the watermark/limit path.
```suggestion
"Query {} memory limit is exceeded"
```
##########
be/src/runtime/workload_group/workload_group_manager.cpp:
##########
@@ -714,11 +714,20 @@ bool WorkloadGroupMgr::handle_single_query_(const
std::shared_ptr<ResourceContex
return true;
}
} else if (paused_reason.is<ErrorCode::WORKLOAD_GROUP_MEMORY_EXCEEDED>()) {
- if (!wg->exceed_limit()) {
+ bool exceed_low_watermark = false;
+ bool exceed_high_watermark = false;
+ wg->check_mem_used(size_to_reserve, &exceed_low_watermark,
&exceed_high_watermark);
+ if (!exceed_high_watermark) {
Review Comment:
PR description currently contains duplicated template sections and
placeholder references (e.g., `close #xxx`, `Related PR: #xxx`) which conflict
with the earlier stated summary and make the intended linkage/closure
ambiguous. Please clean up the PR description so it reflects the actual
issue/PR references and the single intended problem statement.
##########
be/test/runtime/workload_group/workload_group_manager_test.cpp:
##########
@@ -118,10 +121,9 @@ class WorkloadGroupManagerTest : public testing::Test {
return query_context;
}
- void _run_checking_loop(const std::shared_ptr<WorkloadGroup>& wg) {
+ void _run_checking_loop(const std::shared_ptr<WorkloadGroup>& wg, size_t
check_times = 300) {
CountDownLatch latch(1);
- size_t check_times = 300;
- while (--check_times > 0) {
+ while (check_times-- > 0) {
Review Comment:
`while (check_times-- > 0)` causes an unsigned underflow when `check_times`
is 0 (the value wraps even though the loop body won’t run). Consider rewriting
to avoid wraparound (e.g., `while (check_times > 0) { --check_times; ... }` or
a `for` loop) to keep the intent clearer and avoid surprising state changes.
```suggestion
while (check_times > 0) {
--check_times;
```
##########
be/test/runtime/workload_group/workload_group_manager_test.cpp:
##########
@@ -219,6 +225,70 @@ TEST_F(WorkloadGroupManagerTest, wg_exceed2) {
ASSERT_EQ(query_context->is_cancelled(), false) << "query should be
canceled";
}
+TEST_F(WorkloadGroupManagerTest,
wg_reserve_failed_before_query_limit_and_high_watermark) {
+ WorkloadGroupInfo wg_info {.id = 1,
+ .memory_limit = 5000,
+ .memory_low_watermark = 50,
+ .memory_high_watermark = 60,
+ .slot_mem_policy = TWgSlotMemoryPolicy::NONE};
+ auto wg = _wg_manager->get_or_create_workload_group(wg_info);
+ auto query_context = _generate_on_query(wg, 4096);
+ ThreadContext thread_context;
+ thread_context.attach_task(query_context->resource_ctx());
+
+ auto st = thread_context.thread_mem_tracker_mgr->try_reserve(2048);
+ ASSERT_TRUE(st.ok()) << st.to_string();
+
ASSERT_EQ(query_context->resource_ctx()->memory_context()->current_memory_bytes(),
2048);
+
ASSERT_EQ(query_context->resource_ctx()->memory_context()->reserved_consumption(),
2048);
+
ASSERT_LT(query_context->resource_ctx()->memory_context()->current_memory_bytes(),
+ query_context->resource_ctx()->memory_context()->mem_limit());
+
ASSERT_LT(query_context->resource_ctx()->memory_context()->current_memory_bytes()
+ 1024,
+ query_context->resource_ctx()->memory_context()->mem_limit());
+
+ bool exceed_low_watermark = false;
+ bool exceed_high_watermark = false;
+ wg->check_mem_used(&exceed_low_watermark, &exceed_high_watermark);
+ ASSERT_FALSE(exceed_low_watermark);
+ ASSERT_FALSE(exceed_high_watermark);
+
+ st = thread_context.thread_mem_tracker_mgr->try_reserve(1024);
+ ASSERT_TRUE(st.is<ErrorCode::WORKLOAD_GROUP_MEMORY_EXCEEDED>()) <<
st.to_string();
+ ASSERT_FALSE(st.is<ErrorCode::QUERY_MEMORY_EXCEEDED>()) << st.to_string();
+
ASSERT_EQ(query_context->resource_ctx()->memory_context()->current_memory_bytes(),
2048);
+
ASSERT_EQ(query_context->resource_ctx()->memory_context()->reserved_consumption(),
2048);
+
+ _wg_manager->add_paused_query(query_context->resource_ctx(), 1024, st);
+ ASSERT_FALSE(query_context->get_memory_sufficient_dependency()->ready());
+ {
+ std::unique_lock<std::mutex> lock(_wg_manager->_paused_queries_lock);
+ ASSERT_EQ(_wg_manager->_paused_queries_list[wg].size(), 1)
+ << "paused queue should not be empty";
+ }
+
+ _run_checking_loop(wg, 3);
+
+ ASSERT_FALSE(query_context->get_memory_sufficient_dependency()->ready());
+ ASSERT_TRUE(
+
query_context->resource_ctx()->task_controller()->paused_reason().is<
+ ErrorCode::WORKLOAD_GROUP_MEMORY_EXCEEDED>());
+ ASSERT_FALSE(query_context->is_cancelled());
+ {
+ std::unique_lock<std::mutex> lock(_wg_manager->_paused_queries_lock);
+ ASSERT_EQ(_wg_manager->_paused_queries_list[wg].size(), 1)
+ << "paused queue should keep the query";
+ }
+
+ {
+ std::unique_lock<std::mutex> lock(_wg_manager->_paused_queries_lock);
+ _wg_manager->_paused_queries_list.erase(wg);
+ }
+ query_context->set_memory_sufficient(true);
+ thread_context.thread_mem_tracker_mgr->shrink_reserved();
+ thread_context.detach_task();
Review Comment:
This test mutates `_paused_queries_list` directly for cleanup and relies on
reaching the bottom of the test to call `shrink_reserved()` / `detach_task()`.
Any earlier `ASSERT_*` failure will skip this cleanup and can leak paused-query
state and thread attachment into subsequent tests, causing order-dependent
failures. Prefer a scope guard/RAII cleanup (or a test-only helper API on the
manager) so `_paused_queries_list` cleanup and `detach_task()` always happen.
```suggestion
{
using WorkloadGroupPtr = decltype(wg);
using QueryContextPtr = decltype(query_context);
struct ScopedTestCleanup {
WorkloadGroupMgr* wg_manager;
WorkloadGroupPtr workload_group;
QueryContextPtr query_ctx;
ThreadContext& thread_ctx;
~ScopedTestCleanup() {
{
std::unique_lock<std::mutex>
lock(wg_manager->_paused_queries_lock);
wg_manager->_paused_queries_list.erase(workload_group);
}
query_ctx->set_memory_sufficient(true);
thread_ctx.thread_mem_tracker_mgr->shrink_reserved();
thread_ctx.detach_task();
}
} cleanup {_wg_manager.get(), wg, query_context, thread_context};
auto st = thread_context.thread_mem_tracker_mgr->try_reserve(2048);
ASSERT_TRUE(st.ok()) << st.to_string();
ASSERT_EQ(query_context->resource_ctx()->memory_context()->current_memory_bytes(),
2048);
ASSERT_EQ(query_context->resource_ctx()->memory_context()->reserved_consumption(),
2048);
ASSERT_LT(query_context->resource_ctx()->memory_context()->current_memory_bytes(),
query_context->resource_ctx()->memory_context()->mem_limit());
ASSERT_LT(query_context->resource_ctx()->memory_context()->current_memory_bytes()
+ 1024,
query_context->resource_ctx()->memory_context()->mem_limit());
bool exceed_low_watermark = false;
bool exceed_high_watermark = false;
wg->check_mem_used(&exceed_low_watermark, &exceed_high_watermark);
ASSERT_FALSE(exceed_low_watermark);
ASSERT_FALSE(exceed_high_watermark);
st = thread_context.thread_mem_tracker_mgr->try_reserve(1024);
ASSERT_TRUE(st.is<ErrorCode::WORKLOAD_GROUP_MEMORY_EXCEEDED>()) <<
st.to_string();
ASSERT_FALSE(st.is<ErrorCode::QUERY_MEMORY_EXCEEDED>()) <<
st.to_string();
ASSERT_EQ(query_context->resource_ctx()->memory_context()->current_memory_bytes(),
2048);
ASSERT_EQ(query_context->resource_ctx()->memory_context()->reserved_consumption(),
2048);
_wg_manager->add_paused_query(query_context->resource_ctx(), 1024,
st);
ASSERT_FALSE(query_context->get_memory_sufficient_dependency()->ready());
{
std::unique_lock<std::mutex>
lock(_wg_manager->_paused_queries_lock);
ASSERT_EQ(_wg_manager->_paused_queries_list[wg].size(), 1)
<< "paused queue should not be empty";
}
_run_checking_loop(wg, 3);
ASSERT_FALSE(query_context->get_memory_sufficient_dependency()->ready());
ASSERT_TRUE(
query_context->resource_ctx()->task_controller()->paused_reason().is<
ErrorCode::WORKLOAD_GROUP_MEMORY_EXCEEDED>());
ASSERT_FALSE(query_context->is_cancelled());
{
std::unique_lock<std::mutex>
lock(_wg_manager->_paused_queries_lock);
ASSERT_EQ(_wg_manager->_paused_queries_list[wg].size(), 1)
<< "paused queue should keep the query";
}
}
```
--
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]