Coverage Report

Created: 2025-04-30 19:30

/root/doris/be/src/pipeline/task_queue.h
Line
Count
Source (jump to first uncovered line)
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
#pragma once
18
19
#include <glog/logging.h>
20
#include <stddef.h>
21
#include <stdint.h>
22
23
#include <atomic>
24
#include <condition_variable>
25
#include <memory>
26
#include <mutex>
27
#include <ostream>
28
#include <queue>
29
#include <set>
30
31
#include "common/status.h"
32
#include "pipeline_task.h"
33
34
namespace doris::pipeline {
35
36
class TaskQueue {
37
public:
38
0
    TaskQueue(int core_size) : _core_size(core_size) {}
39
    virtual ~TaskQueue();
40
    virtual void close() = 0;
41
    // Get the task by core id.
42
    // TODO: To think the logic is useful?
43
    virtual PipelineTask* take(int core_id) = 0;
44
45
    // push from scheduler
46
    virtual Status push_back(PipelineTask* task) = 0;
47
48
    // push from worker
49
    virtual Status push_back(PipelineTask* task, int core_id) = 0;
50
51
0
    virtual void update_statistics(PipelineTask* task, int64_t time_spent) {}
52
53
0
    int cores() const { return _core_size; }
54
55
protected:
56
    int _core_size;
57
    static constexpr auto WAIT_CORE_TASK_TIMEOUT_MS = 100;
58
};
59
60
class SubTaskQueue {
61
    friend class PriorityTaskQueue;
62
63
public:
64
0
    void push_back(PipelineTask* task) { _queue.emplace(task); }
65
66
    PipelineTask* try_take(bool is_steal);
67
68
0
    void set_level_factor(double level_factor) { _level_factor = level_factor; }
69
70
    // note:
71
    // runtime is the time consumed by the actual execution of the task
72
    // vruntime(means virtual runtime) = runtime / _level_factor
73
0
    double get_vruntime() { return _runtime / _level_factor; }
74
75
0
    void inc_runtime(uint64_t delta_time) { _runtime += delta_time; }
76
77
0
    void adjust_runtime(uint64_t vruntime) { this->_runtime = uint64_t(vruntime * _level_factor); }
78
79
0
    bool empty() { return _queue.empty(); }
80
81
private:
82
    std::queue<PipelineTask*> _queue;
83
    // depends on LEVEL_QUEUE_TIME_FACTOR
84
    double _level_factor = 1;
85
86
    std::atomic<uint64_t> _runtime = 0;
87
};
88
89
// A Multilevel Feedback Queue
90
class PriorityTaskQueue {
91
public:
92
    PriorityTaskQueue();
93
94
    void close();
95
96
    PipelineTask* try_take(bool is_steal);
97
98
    PipelineTask* take(uint32_t timeout_ms = 0);
99
100
    Status push(PipelineTask* task);
101
102
0
    void inc_sub_queue_runtime(int level, uint64_t runtime) {
103
0
        _sub_queues[level].inc_runtime(runtime);
104
0
    }
105
106
private:
107
    PipelineTask* _try_take_unprotected(bool is_steal);
108
    static constexpr auto LEVEL_QUEUE_TIME_FACTOR = 2;
109
    static constexpr size_t SUB_QUEUE_LEVEL = 6;
110
    SubTaskQueue _sub_queues[SUB_QUEUE_LEVEL];
111
    // 1s, 3s, 10s, 60s, 300s
112
    uint64_t _queue_level_limit[SUB_QUEUE_LEVEL - 1] = {1000000000, 3000000000, 10000000000,
113
                                                        60000000000, 300000000000};
114
    std::mutex _work_size_mutex;
115
    std::condition_variable _wait_task;
116
    std::atomic<size_t> _total_task_size = 0;
117
    bool _closed;
118
119
    // used to adjust vruntime of a queue when it's not empty
120
    // protected by lock _work_size_mutex
121
    uint64_t _queue_level_min_vruntime = 0;
122
123
    int _compute_level(uint64_t real_runtime);
124
};
125
126
// Need consider NUMA architecture
127
class MultiCoreTaskQueue : public TaskQueue {
128
public:
129
    explicit MultiCoreTaskQueue(int core_size);
130
131
    ~MultiCoreTaskQueue() override;
132
133
    void close() override;
134
135
    // Get the task by core id.
136
    PipelineTask* take(int core_id) override;
137
138
    // TODO combine these methods to `push_back(task, core_id = -1)`
139
    Status push_back(PipelineTask* task) override;
140
141
    Status push_back(PipelineTask* task, int core_id) override;
142
143
    void update_statistics(PipelineTask* task, int64_t time_spent) override;
144
145
private:
146
    PipelineTask* _steal_take(
147
            int core_id, std::vector<std::unique_ptr<PriorityTaskQueue>>& prio_task_queue_list);
148
149
    std::shared_ptr<std::vector<std::unique_ptr<PriorityTaskQueue>>> _prio_task_queue_list;
150
    std::atomic<uint32_t> _next_core = 0;
151
    std::atomic<bool> _closed;
152
};
153
154
} // namespace doris::pipeline