Coverage Report

Created: 2026-02-27 22:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/pipeline/pipeline.cpp
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
#include "pipeline.h"
19
20
#include <memory>
21
#include <string>
22
#include <utility>
23
24
#include "pipeline/exec/operator.h"
25
#include "pipeline/pipeline_fragment_context.h"
26
#include "pipeline/pipeline_task.h"
27
28
namespace doris::pipeline {
29
30
144k
void Pipeline::_init_profile() {
31
144k
    auto s = fmt::format("Pipeline (pipeline id={})", _pipeline_id);
32
144k
    _pipeline_profile = std::make_unique<RuntimeProfile>(std::move(s));
33
144k
}
34
35
bool Pipeline::need_to_local_exchange(const DataDistribution target_data_distribution,
36
5
                                      const int idx) const {
37
5
    if (!target_data_distribution.need_local_exchange()) {
38
2
        return false;
39
2
    }
40
    // If serial operator exists after `idx`-th operator, we should not improve parallelism.
41
3
    if (std::any_of(_operators.begin() + idx, _operators.end(),
42
3
                    [&](OperatorPtr op) -> bool { return op->is_serial_operator(); })) {
43
0
        return false;
44
0
    }
45
    // If all operators are serial and sink is not serial, we should improve parallelism for sink.
46
3
    if (std::all_of(_operators.begin(), _operators.end(),
47
3
                    [&](OperatorPtr op) -> bool { return op->is_serial_operator(); })) {
48
1
        if (!_sink->is_serial_operator()) {
49
0
            return true;
50
0
        }
51
2
    } else if (std::any_of(_operators.begin(), _operators.end(),
52
3
                           [&](OperatorPtr op) -> bool { return op->is_serial_operator(); })) {
53
        // If non-serial operators exist, we should improve parallelism for those.
54
0
        return true;
55
0
    }
56
57
3
    if (!is_shuffled_exchange(target_data_distribution.distribution_type)) {
58
        // Always do local exchange if non-hash-partition exchanger is required.
59
        // For example, `PASSTHROUGH` exchanger is always required to distribute data evenly.
60
1
        return true;
61
2
    } else if (_operators.front()->is_serial_operator()) {
62
0
        DCHECK(std::all_of(_operators.begin(), _operators.end(),
63
0
                           [&](OperatorPtr op) -> bool { return op->is_serial_operator(); }) &&
64
0
               _sink->is_serial_operator())
65
0
                << debug_string();
66
        // All operators and sink are serial in this path.
67
0
        return false;
68
2
    } else {
69
2
        return _data_distribution.distribution_type != target_data_distribution.distribution_type &&
70
2
               !(is_hash_exchange(_data_distribution.distribution_type) &&
71
0
                 is_hash_exchange(target_data_distribution.distribution_type));
72
2
    }
73
3
}
74
75
144k
Status Pipeline::add_operator(OperatorPtr& op, const int parallelism) {
76
144k
    if (parallelism > 0 && op->is_serial_operator()) {
77
0
        set_num_tasks(parallelism);
78
0
    }
79
144k
    op->set_parallel_tasks(num_tasks());
80
144k
    _operators.emplace_back(op);
81
144k
    if (op->is_source()) {
82
72.0k
        std::reverse(_operators.begin(), _operators.end());
83
72.0k
    }
84
144k
    return Status::OK();
85
144k
}
86
87
3
Status Pipeline::prepare(RuntimeState* state) {
88
3
    RETURN_IF_ERROR(_operators.back()->prepare(state));
89
3
    RETURN_IF_ERROR(_sink->prepare(state));
90
3
    _name.append(std::to_string(id()));
91
3
    _name.push_back('-');
92
4
    for (auto& op : _operators) {
93
4
        _name.append(std::to_string(op->node_id()));
94
4
        _name.append(op->get_name());
95
4
    }
96
3
    _name.push_back('-');
97
3
    _name.append(std::to_string(_sink->node_id()));
98
3
    _name.append(_sink->get_name());
99
3
    return Status::OK();
100
3
}
101
102
144k
Status Pipeline::set_sink(DataSinkOperatorPtr& sink) {
103
144k
    if (_sink) {
104
0
        return Status::InternalError("set sink twice");
105
0
    }
106
144k
    if (!sink->is_sink()) {
107
0
        return Status::InternalError("should set a sink operator but {}", typeid(sink).name());
108
0
    }
109
144k
    _sink = sink;
110
144k
    return Status::OK();
111
144k
}
112
113
0
void Pipeline::make_all_runnable(PipelineId wake_by) {
114
0
    DBUG_EXECUTE_IF("Pipeline::make_all_runnable.sleep", {
115
0
        auto pipeline_id = DebugPoints::instance()->get_debug_param_or_default<int32_t>(
116
0
                "Pipeline::make_all_runnable.sleep", "pipeline_id", -1);
117
0
        if (pipeline_id == id()) {
118
0
            LOG(WARNING) << "Pipeline::make_all_runnable.sleep sleep 10s";
119
0
            sleep(10);
120
0
        }
121
0
    });
122
123
0
    if (_sink->count_down_destination()) {
124
0
        for (auto* task : _tasks) {
125
0
            if (task) {
126
0
                task->set_wake_up_early(wake_by);
127
0
            }
128
0
        }
129
0
        for (auto* task : _tasks) {
130
0
            if (task) {
131
0
                task->terminate();
132
0
            }
133
0
        }
134
0
    }
135
0
}
136
137
} // namespace doris::pipeline