Coverage Report

Created: 2026-06-23 19:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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
245k
    TaskController() { task_id_ = TUniqueId(); };
36
245k
    virtual ~TaskController() = default;
37
38
    /* common action
39
    */
40
562
    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
    // Distinguish missing user metadata from an empty username.
51
0
    virtual bool get_user(std::string* user) { return false; }
52
53
    /* finish action
54
    */
55
1
    bool is_finished() const { return is_finished_; }
56
122k
    void finish() {
57
122k
        if (!is_finished_) {
58
122k
            is_finished_ = true;
59
122k
            finish_time_ = MonotonicMillis();
60
122k
        }
61
122k
        finish_impl();
62
122k
    }
63
122k
    virtual void finish_impl() {}
64
25
    int64_t start_time() const { return start_time_; }
65
8
    int64_t finish_time() const { return finish_time_; }
66
12
    int64_t running_time() const {
67
12
        if (start_time() == 0) {
68
1
            return 0;
69
1
        }
70
11
        if (is_finished_) {
71
8
            return finish_time() - start_time();
72
8
        } else {
73
3
            return MonotonicMillis() - start_time();
74
3
        }
75
11
    }
76
77
    /* cancel action
78
    */
79
0
    virtual bool is_cancelled() const { return false; }
80
81
23
    bool cancel(const Status& reason) {
82
23
        if (cancelled_time_ == 0) {
83
23
            cancelled_time_ = MonotonicMillis();
84
23
        }
85
23
        return cancel_impl(reason);
86
23
    }
87
88
10
    int64_t cancel_elapsed_millis() const { return MonotonicMillis() - cancelled_time_; }
89
90
0
    virtual bool cancel_impl(const Status& reason) { return false; }
91
92
27
    int64_t cancelled_time() const { return cancelled_time_; }
93
94
    /* pause action & property
95
    */
96
    void update_paused_reason(const Status& st);
97
17
    void reset_paused_reason() { paused_reason_.reset(); }
98
54
    Status paused_reason() { return paused_reason_.status(); }
99
29
    void add_paused_count() { paused_count_.fetch_add(1); }
100
101
    /* memory status action
102
    */
103
0
    virtual int32_t get_slot_count() const { return 1; }
104
0
    virtual bool is_pure_load_task() const { return false; }
105
31
    void set_low_memory_mode(bool low_memory_mode) { low_memory_mode_ = low_memory_mode; }
106
1.59M
    bool low_memory_mode() { return low_memory_mode_; }
107
5
    virtual void disable_reserve_memory() { enable_reserve_memory_ = false; }
108
0
    virtual bool is_enable_reserve_memory() const { return enable_reserve_memory_; }
109
0
    virtual void set_memory_sufficient(bool sufficient) {};
110
0
    virtual int64_t memory_sufficient_time() { return 0; };
111
112
    /* memory revoke action
113
    */
114
    virtual void get_revocable_info(size_t* revocable_size, size_t* memory_usage,
115
0
                                    bool* has_running_task) {};
116
0
    virtual size_t get_revocable_size() { return 0; };
117
0
    virtual Status revoke_memory() { return Status::OK(); };
118
0
    virtual std::vector<PipelineTask*> get_revocable_tasks() { return {}; };
119
36
    void increase_revoking_tasks_count() { revoking_tasks_count_.fetch_add(1); }
120
36
    void decrease_revoking_tasks_count() { revoking_tasks_count_.fetch_sub(1); }
121
0
    int get_revoking_tasks_count() const { return revoking_tasks_count_.load(); }
122
123
protected:
124
    friend class ResourceContext;
125
126
245k
    void set_resource_ctx(ResourceContext* resource_ctx) { resource_ctx_ = resource_ctx; }
127
    ResourceContext* resource_ctx_ {nullptr};
128
129
    /* common property
130
    */
131
    TUniqueId task_id_;
132
    TNetworkAddress fe_addr_;
133
    TQueryType::type query_type_;
134
135
    /* cancel property
136
    */
137
    std::atomic<int64_t> cancelled_time_ = 0;
138
139
    /* finish property
140
    */
141
    std::atomic<bool> is_finished_ = false;
142
    int64_t start_time_ = 0;
143
    std::atomic<int64_t> finish_time_ = 0;
144
145
    /* pause property
146
    */
147
    AtomicStatus paused_reason_;
148
    std::atomic<int64_t> paused_count_ = 0;
149
150
    /* memory status property
151
    */
152
    std::atomic<bool> low_memory_mode_ = false;
153
    std::atomic<bool> enable_reserve_memory_ = true;
154
155
    /* memory revoke property
156
    */
157
    std::atomic<int> revoking_tasks_count_ = 0;
158
};
159
160
} // namespace doris