Coverage Report

Created: 2026-07-03 18:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/pipeline/task_queue.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
#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 "exec/pipeline/pipeline_task.h"
33
34
namespace doris {
35
36
class SubTaskQueue {
37
    friend class PriorityTaskQueue;
38
39
public:
40
33
    void push_back(PipelineTaskSPtr task) { _queue.emplace(task); }
41
42
    PipelineTaskSPtr try_take(bool is_steal);
43
44
270
    void set_level_factor(double level_factor) { _level_factor = level_factor; }
45
46
    // note:
47
    // runtime is the time consumed by the actual execution of the task
48
    // vruntime(means virtual runtime) = runtime / _level_factor
49
88
    double get_vruntime() { return double(_runtime) / _level_factor; }
50
51
1
    void inc_runtime(uint64_t delta_time) { _runtime += delta_time; }
52
53
0
    void adjust_runtime(uint64_t vruntime) {
54
0
        this->_runtime = uint64_t(double(vruntime) * _level_factor);
55
0
    }
56
57
105
    bool empty() { return _queue.empty(); }
58
59
    std::queue<PipelineTaskSPtr> take_all();
60
61
private:
62
    std::queue<PipelineTaskSPtr> _queue;
63
    // depends on LEVEL_QUEUE_TIME_FACTOR
64
    double _level_factor = 1;
65
66
    std::atomic<uint64_t> _runtime = 0;
67
};
68
69
// A Multilevel Feedback Queue
70
class PriorityTaskQueue {
71
public:
72
    PriorityTaskQueue();
73
74
    void close();
75
76
    PipelineTaskSPtr try_take(bool is_steal);
77
78
    PipelineTaskSPtr take(uint32_t timeout_ms = 0);
79
80
    Status push(PipelineTaskSPtr task);
81
82
1
    void inc_sub_queue_runtime(int level, uint64_t runtime) {
83
1
        _sub_queues[level].inc_runtime(runtime);
84
1
    }
85
86
private:
87
    PipelineTaskSPtr _try_take_unprotected(bool is_steal);
88
    static constexpr auto LEVEL_QUEUE_TIME_FACTOR = 2;
89
    static constexpr size_t SUB_QUEUE_LEVEL = 6;
90
    SubTaskQueue _sub_queues[SUB_QUEUE_LEVEL];
91
    // 1s, 3s, 10s, 60s, 300s
92
    uint64_t _queue_level_limit[SUB_QUEUE_LEVEL - 1] = {1000000000, 3000000000, 10000000000,
93
                                                        60000000000, 300000000000};
94
    std::mutex _work_size_mutex;
95
    std::condition_variable _wait_task;
96
    std::atomic<size_t> _total_task_size = 0;
97
    bool _closed;
98
99
    // used to adjust vruntime of a queue when it's not empty
100
    // protected by lock _work_size_mutex
101
    uint64_t _queue_level_min_vruntime = 0;
102
103
    int _compute_level(uint64_t real_runtime);
104
};
105
106
// Need consider NUMA architecture
107
class MultiCoreTaskQueue {
108
public:
109
    explicit MultiCoreTaskQueue(int core_size);
110
111
#ifndef BE_TEST
112
    ~MultiCoreTaskQueue();
113
    // Get the task by core id.
114
    PipelineTaskSPtr take(int core_id);
115
#else
116
    virtual ~MultiCoreTaskQueue();
117
    virtual PipelineTaskSPtr take(int core_id);
118
#endif
119
    void close();
120
121
    // TODO combine these methods to `push_back(task, core_id = -1)`
122
    Status push_back(PipelineTaskSPtr task);
123
124
    Status push_back(PipelineTaskSPtr task, int core_id);
125
126
    void update_statistics(PipelineTask* task, int64_t time_spent);
127
128
0
    int cores() const { return _core_size; }
129
130
private:
131
    PipelineTaskSPtr _steal_take(int core_id);
132
133
    std::vector<PriorityTaskQueue> _prio_task_queues;
134
    std::atomic<uint32_t> _next_core = 0;
135
    std::atomic<bool> _closed;
136
137
    int _core_size;
138
    static constexpr auto WAIT_CORE_TASK_TIMEOUT_MS = 100;
139
};
140
} // namespace doris