Skip to content

Commit cbcbb04

Browse files
authored
[fix](workload policy)query time based be policy not work due to wrong finish_time_ (#60456)
### What problem does this PR solve? Issue Number: close [#60454](#60454) Problem Summary: ### Release note None ### Check List (For Author) - Test <!-- At least one of them must be included. --> - [x] Regression test - [x] Unit Test - [x] Manual test (add detailed scripts or steps below) - [ ] No need to test or manual test. Explain why: - [ ] This is a refactor/code format and no logic has been changed. - [ ] Previous test can cover this change. - [ ] No code files have been changed. - [ ] Other reason <!-- Add your reason? --> - Behavior changed: - [x] No. - [ ] Yes. <!-- Explain the behavior change --> - Does this need documentation? - [x] No. - [ ] Yes. <!-- Add document PR link here. eg: apache/doris-website#1214 --> ### Check List (For Reviewer who merge this PR) - [ ] Confirm the release note - [ ] Confirm test cases - [ ] Confirm document - [ ] Add branch pick label <!-- Add branch pick label that this PR should merge into -->
1 parent d655b4b commit cbcbb04

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

be/src/runtime/workload_management/task_controller.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,16 @@ class TaskController {
6464
virtual void finish_impl() {}
6565
int64_t start_time() const { return start_time_; }
6666
int64_t finish_time() const { return finish_time_; }
67-
int64_t running_time() const { return finish_time() - start_time(); }
67+
int64_t running_time() const {
68+
if (start_time() == 0) {
69+
return 0;
70+
}
71+
if (is_finished_) {
72+
return finish_time() - start_time();
73+
} else {
74+
return MonotonicMillis() - start_time();
75+
}
76+
}
6877

6978
/* cancel action
7079
*/

be/test/runtime/workload_sched_policy_test.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ TEST_F(WorkloadSchedPolicyTest, one_policy_one_condition) {
101101
std::move(action_ptr_list));
102102

103103
WorkloadAction::RuntimeContext action_runtime_ctx = create_runtime_context();
104+
TUniqueId task_id;
105+
task_id.hi = 1;
106+
task_id.lo = 1;
107+
action_runtime_ctx.resource_ctx->task_controller()->set_task_id(task_id);
104108
std::this_thread::sleep_for(std::chrono::milliseconds(50));
105109
action_runtime_ctx.resource_ctx->task_controller()->finish();
106110
EXPECT_TRUE(policy->is_match(&action_runtime_ctx))
@@ -208,6 +212,10 @@ TEST_F(WorkloadSchedPolicyTest, one_policy_mutl_condition) {
208212

209213
// 2. is match
210214
WorkloadAction::RuntimeContext action_runtime_ctx = create_runtime_context();
215+
TUniqueId task_id;
216+
task_id.hi = 1;
217+
task_id.lo = 1;
218+
action_runtime_ctx.resource_ctx->task_controller()->set_task_id(task_id);
211219
std::shared_ptr<MemTrackerLimiter> mem_tracker =
212220
MemTrackerLimiter::create_shared(MemTrackerLimiter::Type::QUERY, "Test");
213221
action_runtime_ctx.resource_ctx->memory_context()->set_mem_tracker(mem_tracker);
@@ -283,4 +291,37 @@ TEST_F(WorkloadSchedPolicyTest, test_wg_id_set) {
283291
action_runtime_ctx.resource_ctx->set_workload_group(workload_group);
284292
EXPECT_TRUE(policy->is_match(&action_runtime_ctx));
285293
}
294+
295+
TEST_F(WorkloadSchedPolicyTest, test_task_controller_running_time) {
296+
std::shared_ptr<TaskController> task_controller = std::make_shared<TaskController>();
297+
TUniqueId task_id;
298+
task_id.hi = 1;
299+
task_id.lo = 1;
300+
301+
// 1. Before set_task_id, start_time is 0
302+
EXPECT_EQ(task_controller->start_time(), 0);
303+
304+
// 2. set_task_id will init start_time
305+
task_controller->set_task_id(task_id);
306+
EXPECT_GT(task_controller->start_time(), 0);
307+
308+
// 3. running_time should be positive when running
309+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
310+
int64_t running_time = task_controller->running_time();
311+
EXPECT_GT(running_time, 0);
312+
EXPECT_GE(running_time, 10);
313+
314+
// 4. finish task
315+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
316+
task_controller->finish();
317+
EXPECT_TRUE(task_controller->is_finished());
318+
319+
// 5. running_time should be fixed after finish
320+
int64_t finished_running_time = task_controller->running_time();
321+
EXPECT_GT(finished_running_time, running_time);
322+
323+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
324+
EXPECT_EQ(task_controller->running_time(), finished_running_time);
325+
}
326+
286327
} // namespace doris

0 commit comments

Comments
 (0)