Coverage Report

Created: 2025-10-15 07:26

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
1.09M
void Pipeline::_init_profile() {
31
1.09M
    auto s = fmt::format("Pipeline (pipeline id={})", _pipeline_id);
32
1.09M
    _pipeline_profile = std::make_unique<RuntimeProfile>(std::move(s));
33
1.09M
}
34
35
bool Pipeline::need_to_local_exchange(const DataDistribution target_data_distribution,
36
221k
                                      const int idx) const {
37
221k
    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
221k
    if (std::any_of(_operators.begin() + idx, _operators.end(),
42
221k
                    [&](OperatorPtr op) -> bool { return op->is_serial_operator(); })) {
43
86
        return false;
44
86
    }
45
    // If all operators are serial and sink is not serial, we should improve parallelism for sink.
46
221k
    if (std::all_of(_operators.begin(), _operators.end(),
47
304k
                    [&](OperatorPtr op) -> bool { return op->is_serial_operator(); })) {
48
25.5k
        if (!_sink->is_serial_operator()) {
49
18.1k
            return true;
50
18.1k
        }
51
195k
    } else if (std::any_of(_operators.begin(), _operators.end(),
52
293k
                           [&](OperatorPtr op) -> bool { return op->is_serial_operator(); })) {
53
        // If non-serial operators exist, we should improve parallelism for those.
54
79.6k
        return true;
55
79.6k
    }
56
57
123k
    if (target_data_distribution.distribution_type != ExchangeType::BUCKET_HASH_SHUFFLE &&
58
125k
        target_data_distribution.distribution_type != ExchangeType::HASH_SHUFFLE) {
59
        // Always do local exchange if non-hash-partition exchanger is required.
60
        // For example, `PASSTHROUGH` exchanger is always required to distribute data evenly.
61
37.0k
        return true;
62
86.5k
    } else if (_operators.front()->is_serial_operator()) {
63
0
        DCHECK(std::all_of(_operators.begin(), _operators.end(),
64
0
                           [&](OperatorPtr op) -> bool { return op->is_serial_operator(); }) &&
65
0
               _sink->is_serial_operator())
66
0
                << debug_string();
67
        // All operators and sink are serial in this path.
68
0
        return false;
69
86.5k
    } else {
70
86.5k
        return _data_distribution.distribution_type != target_data_distribution.distribution_type &&
71
86.5k
               !(is_hash_exchange(_data_distribution.distribution_type) &&
72
4.47k
                 is_hash_exchange(target_data_distribution.distribution_type));
73
86.5k
    }
74
123k
}
75
76
1.06M
Status Pipeline::add_operator(OperatorPtr& op, const int parallelism) {
77
1.06M
    if (parallelism > 0 && op->is_serial_operator()) {
78
116k
        set_num_tasks(parallelism);
79
116k
    }
80
1.06M
    op->set_parallel_tasks(num_tasks());
81
1.06M
    _operators.emplace_back(op);
82
1.06M
    if (op->is_source()) {
83
804k
        std::reverse(_operators.begin(), _operators.end());
84
804k
    }
85
1.06M
    return Status::OK();
86
1.06M
}
87
88
957k
Status Pipeline::prepare(RuntimeState* state) {
89
957k
    RETURN_IF_ERROR(_operators.back()->prepare(state));
90
948k
    RETURN_IF_ERROR(_sink->prepare(state));
91
948k
    _name.append(std::to_string(id()));
92
948k
    _name.push_back('-');
93
1.14M
    for (auto& op : _operators) {
94
1.14M
        _name.append(std::to_string(op->node_id()));
95
1.14M
        _name.append(op->get_name());
96
1.14M
    }
97
948k
    _name.push_back('-');
98
948k
    _name.append(std::to_string(_sink->node_id()));
99
948k
    _name.append(_sink->get_name());
100
948k
    return Status::OK();
101
948k
}
102
103
1.09M
Status Pipeline::set_sink(DataSinkOperatorPtr& sink) {
104
1.09M
    if (_sink) {
105
0
        return Status::InternalError("set sink twice");
106
0
    }
107
1.09M
    if (!sink->is_sink()) {
108
0
        return Status::InternalError("should set a sink operator but {}", typeid(sink).name());
109
0
    }
110
1.09M
    _sink = sink;
111
1.09M
    return Status::OK();
112
1.09M
}
113
114
563k
void Pipeline::make_all_runnable(PipelineId wake_by) {
115
563k
    DBUG_EXECUTE_IF("Pipeline::make_all_runnable.sleep", {
116
563k
        auto pipeline_id = DebugPoints::instance()->get_debug_param_or_default<int32_t>(
117
563k
                "Pipeline::make_all_runnable.sleep", "pipeline_id", -1);
118
563k
        if (pipeline_id == id()) {
119
563k
            LOG(WARNING) << "Pipeline::make_all_runnable.sleep sleep 10s";
120
563k
            sleep(10);
121
563k
        }
122
563k
    });
123
124
563k
    if (_sink->count_down_destination()) {
125
1.61M
        for (auto* task : _tasks) {
126
1.61M
            if (task) {
127
1.61M
                task->set_wake_up_early(wake_by);
128
1.61M
            }
129
1.61M
        }
130
1.61M
        for (auto* task : _tasks) {
131
1.61M
            if (task) {
132
1.61M
                task->terminate();
133
1.61M
            }
134
1.61M
        }
135
561k
    }
136
563k
}
137
138
} // namespace doris::pipeline