Coverage Report

Created: 2024-11-21 17:33

/root/doris/be/src/pipeline/pipeline.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 <glog/logging.h>
21
22
#include <cstdint>
23
#include <memory>
24
#include <string_view>
25
#include <utility>
26
#include <vector>
27
28
#include "common/cast_set.h"
29
#include "common/status.h"
30
#include "pipeline/exec/operator.h"
31
#include "util/runtime_profile.h"
32
33
namespace doris::pipeline {
34
#include "common/compile_check_begin.h"
35
class PipelineFragmentContext;
36
class Pipeline;
37
38
using PipelinePtr = std::shared_ptr<Pipeline>;
39
using Pipelines = std::vector<PipelinePtr>;
40
using PipelineId = uint32_t;
41
42
class Pipeline : public std::enable_shared_from_this<Pipeline> {
43
    friend class PipelineTask;
44
    friend class PipelineFragmentContext;
45
46
public:
47
    explicit Pipeline(PipelineId pipeline_id, int num_tasks, int num_tasks_of_parent)
48
            : _pipeline_id(pipeline_id),
49
              _num_tasks(num_tasks),
50
0
              _num_tasks_of_parent(num_tasks_of_parent) {
51
0
        _init_profile();
52
0
        _tasks.resize(_num_tasks, nullptr);
53
0
    }
54
55
    // Add operators for pipelineX
56
    Status add_operator(OperatorPtr& op, const int parallelism);
57
    // prepare operators for pipelineX
58
    Status prepare(RuntimeState* state);
59
60
    Status set_sink(DataSinkOperatorPtr& sink_operator);
61
62
0
    DataSinkOperatorXBase* sink() { return _sink.get(); }
63
0
    Operators& operators() { return _operators; }
64
0
    DataSinkOperatorPtr sink_shared_pointer() { return _sink; }
65
66
0
    [[nodiscard]] const RowDescriptor& output_row_desc() const {
67
0
        return _operators.back()->row_desc();
68
0
    }
69
70
0
    [[nodiscard]] PipelineId id() const { return _pipeline_id; }
71
72
0
    static bool is_hash_exchange(ExchangeType idx) {
73
0
        return idx == ExchangeType::HASH_SHUFFLE || idx == ExchangeType::BUCKET_HASH_SHUFFLE;
74
0
    }
75
76
    bool need_to_local_exchange(const DataDistribution target_data_distribution,
77
                                const int idx) const;
78
0
    void init_data_distribution() {
79
0
        set_data_distribution(_operators.front()->required_data_distribution());
80
0
    }
81
0
    void set_data_distribution(const DataDistribution& data_distribution) {
82
0
        _data_distribution = data_distribution;
83
0
    }
84
0
    const DataDistribution& data_distribution() const { return _data_distribution; }
85
86
0
    std::vector<std::shared_ptr<Pipeline>>& children() { return _children; }
87
0
    void set_children(std::shared_ptr<Pipeline> child) { _children.push_back(child); }
88
0
    void set_children(std::vector<std::shared_ptr<Pipeline>> children) {
89
0
        _children = std::move(children);
90
0
    }
91
92
0
    void incr_created_tasks(int i, PipelineTask* task) {
93
0
        _num_tasks_created++;
94
0
        _num_tasks_running++;
95
0
        DCHECK_LT(i, _tasks.size());
96
0
        _tasks[i] = task;
97
0
    }
98
99
    void make_all_runnable();
100
101
0
    void set_num_tasks(int num_tasks) {
102
0
        _num_tasks = num_tasks;
103
0
        _tasks.resize(_num_tasks, nullptr);
104
0
        for (auto& op : _operators) {
105
0
            op->set_parallel_tasks(_num_tasks);
106
0
        }
107
108
0
#ifndef NDEBUG
109
0
        if (num_tasks > 1 &&
110
0
            std::any_of(_operators.begin(), _operators.end(),
111
0
                        [&](OperatorPtr op) -> bool { return op->is_serial_operator(); })) {
112
0
            DCHECK(false) << debug_string();
113
0
        }
114
0
#endif
115
0
    }
116
0
    int num_tasks() const { return _num_tasks; }
117
0
    bool close_task() { return _num_tasks_running.fetch_sub(1) == 1; }
118
119
0
    std::string debug_string() const {
120
0
        fmt::memory_buffer debug_string_buffer;
121
0
        fmt::format_to(debug_string_buffer,
122
0
                       "Pipeline [id: {}, _num_tasks: {}, _num_tasks_created: {}]", _pipeline_id,
123
0
                       _num_tasks, _num_tasks_created);
124
0
        for (int i = 0; i < _operators.size(); i++) {
125
0
            fmt::format_to(debug_string_buffer, "\n{}", _operators[i]->debug_string(i));
126
0
        }
127
0
        fmt::format_to(debug_string_buffer, "\n{}",
128
0
                       _sink->debug_string(cast_set<int>(_operators.size())));
129
0
        return fmt::to_string(debug_string_buffer);
130
0
    }
131
132
0
    int num_tasks_of_parent() const { return _num_tasks_of_parent; }
133
134
private:
135
    void _init_profile();
136
137
    std::vector<std::pair<int, std::weak_ptr<Pipeline>>> _parents;
138
    std::vector<std::pair<int, std::shared_ptr<Pipeline>>> _dependencies;
139
140
    std::vector<std::shared_ptr<Pipeline>> _children;
141
142
    PipelineId _pipeline_id;
143
    int _previous_schedule_id = -1;
144
145
    // pipline id + operator names. init when:
146
    //  build_operators(), if pipeline;
147
    //  _build_pipelines() and _create_tree_helper(), if pipelineX.
148
    std::string _name;
149
150
    std::unique_ptr<RuntimeProfile> _pipeline_profile;
151
152
    // Operators for pipelineX. All pipeline tasks share operators from this.
153
    // [SourceOperator -> ... -> SinkOperator]
154
    Operators _operators;
155
    DataSinkOperatorPtr _sink = nullptr;
156
157
    std::shared_ptr<ObjectPool> _obj_pool;
158
159
    // Input data distribution of this pipeline. We do local exchange when input data distribution
160
    // does not match the target data distribution.
161
    DataDistribution _data_distribution {ExchangeType::NOOP};
162
163
    // How many tasks should be created ?
164
    int _num_tasks = 1;
165
    // How many tasks are already created?
166
    std::atomic<int> _num_tasks_created = 0;
167
    // How many tasks are already created and not finished?
168
    std::atomic<int> _num_tasks_running = 0;
169
    // Tasks in this pipeline.
170
    std::vector<PipelineTask*> _tasks;
171
    // Parallelism of parent pipeline.
172
    const int _num_tasks_of_parent;
173
};
174
#include "common/compile_check_end.h"
175
} // namespace doris::pipeline