Coverage Report

Created: 2024-11-18 10:37

/root/doris/be/src/pipeline/task_scheduler.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
18
#pragma once
19
20
#include <stddef.h>
21
22
#include <atomic>
23
#include <condition_variable>
24
#include <list>
25
#include <memory>
26
#include <mutex>
27
#include <utility>
28
#include <vector>
29
30
#include "common/status.h"
31
#include "gutil/ref_counted.h"
32
#include "pipeline_task.h"
33
#include "runtime/task_group/task_group.h"
34
#include "util/thread.h"
35
36
namespace doris {
37
class ExecEnv;
38
class ThreadPool;
39
40
namespace pipeline {
41
class TaskQueue;
42
} // namespace pipeline
43
} // namespace doris
44
45
namespace doris::pipeline {
46
47
class BlockedTaskScheduler {
48
public:
49
    explicit BlockedTaskScheduler(std::shared_ptr<TaskQueue> task_queue);
50
51
0
    ~BlockedTaskScheduler() = default;
52
53
    Status start();
54
    void shutdown();
55
    Status add_blocked_task(PipelineTask* task);
56
57
private:
58
    std::shared_ptr<TaskQueue> _task_queue;
59
60
    std::mutex _task_mutex;
61
    std::condition_variable _task_cond;
62
    std::list<PipelineTask*> _blocked_tasks;
63
64
    scoped_refptr<Thread> _thread;
65
    std::atomic<bool> _started;
66
    std::atomic<bool> _shutdown;
67
68
    static constexpr auto EMPTY_TIMES_TO_YIELD = 64;
69
70
private:
71
    void _schedule();
72
    void _make_task_run(std::list<PipelineTask*>& local_tasks,
73
                        std::list<PipelineTask*>::iterator& task_itr,
74
                        PipelineTaskState state = PipelineTaskState::RUNNABLE);
75
};
76
77
class TaskScheduler {
78
public:
79
    TaskScheduler(ExecEnv* exec_env, std::shared_ptr<BlockedTaskScheduler> b_scheduler,
80
                  std::shared_ptr<TaskQueue> task_queue, std::string name)
81
            : _task_queue(std::move(task_queue)),
82
              _blocked_task_scheduler(std::move(b_scheduler)),
83
              _shutdown(false),
84
0
              _name(name) {}
85
86
    ~TaskScheduler();
87
88
    Status schedule_task(PipelineTask* task);
89
90
    Status start();
91
92
    void shutdown();
93
94
0
    TaskQueue* task_queue() const { return _task_queue.get(); }
95
96
private:
97
    std::unique_ptr<ThreadPool> _fix_thread_pool;
98
    std::shared_ptr<TaskQueue> _task_queue;
99
    std::vector<std::unique_ptr<std::atomic<bool>>> _markers;
100
    std::shared_ptr<BlockedTaskScheduler> _blocked_task_scheduler;
101
    std::atomic<bool> _shutdown;
102
    std::string _name;
103
104
    void _do_work(size_t index);
105
    // after _try_close_task, task maybe destructed.
106
    void _try_close_task(PipelineTask* task, PipelineTaskState state);
107
};
108
} // namespace doris::pipeline