Coverage Report

Created: 2026-06-01 15:57

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