be/src/runtime/workload_management/task_controller.h
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #pragma once |
19 | | |
20 | | #include <gen_cpp/PaloInternalService_types.h> |
21 | | #include <gen_cpp/Types_types.h> |
22 | | |
23 | | #include "common/factory_creator.h" |
24 | | #include "common/status.h" |
25 | | #include "util/time.h" |
26 | | |
27 | | namespace doris { |
28 | | class PipelineTask; |
29 | | |
30 | | class ResourceContext; |
31 | | class TaskController { |
32 | | ENABLE_FACTORY_CREATOR(TaskController); |
33 | | |
34 | | public: |
35 | 244k | TaskController() { task_id_ = TUniqueId(); }; |
36 | 244k | virtual ~TaskController() = default; |
37 | | |
38 | | /* common action |
39 | | */ |
40 | 548 | const TUniqueId& task_id() const { return task_id_; } |
41 | 122k | void set_task_id(TUniqueId task_id) { |
42 | 122k | task_id_ = task_id; |
43 | 122k | start_time_ = MonotonicMillis(); |
44 | 122k | } |
45 | 54 | TQueryType::type query_type() { return query_type_; } |
46 | 122k | void set_query_type(TQueryType::type query_type) { query_type_ = query_type; } |
47 | 16 | TNetworkAddress fe_addr() { return fe_addr_; } |
48 | 122k | void set_fe_addr(TNetworkAddress fe_addr) { fe_addr_ = fe_addr; } |
49 | | std::string debug_string(); |
50 | | |
51 | | /* finish action |
52 | | */ |
53 | 1 | bool is_finished() const { return is_finished_; } |
54 | 122k | void finish() { |
55 | 122k | if (!is_finished_) { |
56 | 122k | is_finished_ = true; |
57 | 122k | finish_time_ = MonotonicMillis(); |
58 | 122k | } |
59 | 122k | finish_impl(); |
60 | 122k | } |
61 | 122k | virtual void finish_impl() {} |
62 | 21 | int64_t start_time() const { return start_time_; } |
63 | 7 | int64_t finish_time() const { return finish_time_; } |
64 | 10 | int64_t running_time() const { |
65 | 10 | if (start_time() == 0) { |
66 | 1 | return 0; |
67 | 1 | } |
68 | 9 | if (is_finished_) { |
69 | 7 | return finish_time() - start_time(); |
70 | 7 | } else { |
71 | 2 | return MonotonicMillis() - start_time(); |
72 | 2 | } |
73 | 9 | } |
74 | | |
75 | | /* cancel action |
76 | | */ |
77 | 0 | virtual bool is_cancelled() const { return false; } |
78 | | |
79 | 23 | bool cancel(const Status& reason) { |
80 | 23 | if (cancelled_time_ == 0) { |
81 | 23 | cancelled_time_ = MonotonicMillis(); |
82 | 23 | } |
83 | 23 | return cancel_impl(reason); |
84 | 23 | } |
85 | | |
86 | 10 | int64_t cancel_elapsed_millis() const { return MonotonicMillis() - cancelled_time_; } |
87 | | |
88 | 0 | virtual bool cancel_impl(const Status& reason) { return false; } |
89 | | |
90 | 27 | int64_t cancelled_time() const { return cancelled_time_; } |
91 | | |
92 | | /* pause action & property |
93 | | */ |
94 | | void update_paused_reason(const Status& st); |
95 | 17 | void reset_paused_reason() { paused_reason_.reset(); } |
96 | 54 | Status paused_reason() { return paused_reason_.status(); } |
97 | 29 | void add_paused_count() { paused_count_.fetch_add(1); } |
98 | | |
99 | | /* memory status action |
100 | | */ |
101 | 0 | virtual int32_t get_slot_count() const { return 1; } |
102 | 0 | virtual bool is_pure_load_task() const { return false; } |
103 | 31 | void set_low_memory_mode(bool low_memory_mode) { low_memory_mode_ = low_memory_mode; } |
104 | 1.47M | bool low_memory_mode() { return low_memory_mode_; } |
105 | 5 | virtual void disable_reserve_memory() { enable_reserve_memory_ = false; } |
106 | 0 | virtual bool is_enable_reserve_memory() const { return enable_reserve_memory_; } |
107 | 0 | virtual void set_memory_sufficient(bool sufficient) {}; |
108 | 0 | virtual int64_t memory_sufficient_time() { return 0; }; |
109 | | |
110 | | /* memory revoke action |
111 | | */ |
112 | | virtual void get_revocable_info(size_t* revocable_size, size_t* memory_usage, |
113 | 0 | bool* has_running_task) {}; |
114 | 0 | virtual size_t get_revocable_size() { return 0; }; |
115 | 0 | virtual Status revoke_memory() { return Status::OK(); }; |
116 | 0 | virtual std::vector<PipelineTask*> get_revocable_tasks() { return {}; }; |
117 | 36 | void increase_revoking_tasks_count() { revoking_tasks_count_.fetch_add(1); } |
118 | 36 | void decrease_revoking_tasks_count() { revoking_tasks_count_.fetch_sub(1); } |
119 | 0 | int get_revoking_tasks_count() const { return revoking_tasks_count_.load(); } |
120 | | |
121 | | protected: |
122 | | friend class ResourceContext; |
123 | | |
124 | 244k | void set_resource_ctx(ResourceContext* resource_ctx) { resource_ctx_ = resource_ctx; } |
125 | | ResourceContext* resource_ctx_ {nullptr}; |
126 | | |
127 | | /* common property |
128 | | */ |
129 | | TUniqueId task_id_; |
130 | | TNetworkAddress fe_addr_; |
131 | | TQueryType::type query_type_; |
132 | | |
133 | | /* cancel property |
134 | | */ |
135 | | std::atomic<int64_t> cancelled_time_ = 0; |
136 | | |
137 | | /* finish property |
138 | | */ |
139 | | std::atomic<bool> is_finished_ = false; |
140 | | int64_t start_time_ = 0; |
141 | | std::atomic<int64_t> finish_time_ = 0; |
142 | | |
143 | | /* pause property |
144 | | */ |
145 | | AtomicStatus paused_reason_; |
146 | | std::atomic<int64_t> paused_count_ = 0; |
147 | | |
148 | | /* memory status property |
149 | | */ |
150 | | std::atomic<bool> low_memory_mode_ = false; |
151 | | std::atomic<bool> enable_reserve_memory_ = true; |
152 | | |
153 | | /* memory revoke property |
154 | | */ |
155 | | std::atomic<int> revoking_tasks_count_ = 0; |
156 | | }; |
157 | | |
158 | | } // namespace doris |