Coverage Report

Created: 2026-04-11 00:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/exec/sink/load_stream_map_pool.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/sink/load_stream_map_pool.h"
19
20
#include "util/debug_points.h"
21
22
namespace doris {
23
class TExpr;
24
25
LoadStreamMap::LoadStreamMap(UniqueId load_id, int64_t src_id, int num_streams, int num_use,
26
                             LoadStreamMapPool* pool)
27
15
        : _load_id(load_id),
28
15
          _src_id(src_id),
29
15
          _num_streams(num_streams),
30
15
          _use_cnt(num_use),
31
15
          _num_incremental_streams(0),
32
15
          _pool(pool),
33
15
          _tablet_schema_for_index(std::make_shared<IndexToTabletSchema>()),
34
15
          _enable_unique_mow_for_index(std::make_shared<IndexToEnableMoW>()) {
35
15
    DCHECK(num_streams > 0) << "stream num should be greater than 0";
36
15
    DCHECK(num_use > 0) << "use num should be greater than 0";
37
15
}
38
39
37
std::shared_ptr<LoadStreamStubs> LoadStreamMap::get_or_create(int64_t dst_id, bool incremental) {
40
37
    std::lock_guard<std::mutex> lock(_mutex);
41
37
    std::shared_ptr<LoadStreamStubs> streams = _streams_for_node[dst_id];
42
37
    if (streams != nullptr) {
43
1
        return streams;
44
1
    }
45
36
    if (incremental) {
46
0
        _num_incremental_streams.fetch_add(1);
47
0
    }
48
36
    streams = std::make_shared<LoadStreamStubs>(_num_streams, _load_id, _src_id,
49
36
                                                _tablet_schema_for_index,
50
36
                                                _enable_unique_mow_for_index, incremental);
51
36
    _streams_for_node[dst_id] = streams;
52
36
    return streams;
53
37
}
54
55
0
std::shared_ptr<LoadStreamStubs> LoadStreamMap::at(int64_t dst_id) {
56
0
    std::lock_guard<std::mutex> lock(_mutex);
57
0
    return _streams_for_node.at(dst_id);
58
0
}
59
60
0
bool LoadStreamMap::contains(int64_t dst_id) {
61
0
    std::lock_guard<std::mutex> lock(_mutex);
62
0
    return _streams_for_node.contains(dst_id);
63
0
}
64
65
13
void LoadStreamMap::for_each(std::function<void(int64_t, LoadStreamStubs&)> fn) {
66
13
    decltype(_streams_for_node) snapshot;
67
13
    {
68
13
        std::lock_guard<std::mutex> lock(_mutex);
69
13
        snapshot = _streams_for_node;
70
13
    }
71
33
    for (auto& [dst_id, streams] : snapshot) {
72
33
        fn(dst_id, *streams);
73
33
    }
74
13
}
75
76
0
Status LoadStreamMap::for_each_st(std::function<Status(int64_t, LoadStreamStubs&)> fn) {
77
0
    decltype(_streams_for_node) snapshot;
78
0
    {
79
0
        std::lock_guard<std::mutex> lock(_mutex);
80
0
        snapshot = _streams_for_node;
81
0
    }
82
0
    Status status = Status::OK();
83
0
    for (auto& [dst_id, streams] : snapshot) {
84
0
        auto st = fn(dst_id, *streams);
85
0
        if (!st.ok() && status.ok()) {
86
0
            status = st;
87
0
        }
88
0
    }
89
0
    return status;
90
0
}
91
92
void LoadStreamMap::save_tablets_to_commit(int64_t dst_id,
93
0
                                           const std::vector<PTabletID>& tablets_to_commit) {
94
0
    std::lock_guard<std::mutex> lock(_tablets_to_commit_mutex);
95
0
    auto& tablets = _tablets_to_commit[dst_id];
96
0
    for (const auto& tablet : tablets_to_commit) {
97
0
        tablets.emplace(tablet.tablet_id(), tablet);
98
0
    }
99
0
}
100
101
3
bool LoadStreamMap::release() {
102
3
    int num_use = --_use_cnt;
103
3
    if (num_use == 0) {
104
2
        LOG(INFO) << "releasing streams, load_id=" << _load_id;
105
2
        _pool->erase(_load_id);
106
2
        return true;
107
2
    }
108
3
    LOG(INFO) << "keeping streams, load_id=" << _load_id << ", use_cnt=" << num_use;
109
1
    return false;
110
3
}
111
112
0
void LoadStreamMap::close_load(bool incremental) {
113
0
    for (auto& [dst_id, streams] : _streams_for_node) {
114
0
        if (streams->is_incremental() != incremental) {
115
0
            continue;
116
0
        }
117
0
        std::vector<PTabletID> tablets_to_commit;
118
0
        const auto& tablets = _tablets_to_commit[dst_id];
119
0
        tablets_to_commit.reserve(tablets.size());
120
0
        for (const auto& [tablet_id, tablet] : tablets) {
121
0
            tablets_to_commit.push_back(tablet);
122
0
            tablets_to_commit.back().set_num_segments(_segments_for_tablet[tablet_id]);
123
0
        }
124
0
        auto st = streams->close_load(tablets_to_commit, _num_incremental_streams.load());
125
0
        if (!st.ok()) {
126
0
            LOG(WARNING) << "close_load for " << (incremental ? "incremental" : "non-incremental")
127
0
                         << " streams failed: " << st << ", load_id=" << _load_id;
128
0
        }
129
0
    }
130
0
}
131
132
1
LoadStreamMapPool::LoadStreamMapPool() = default;
133
134
1
LoadStreamMapPool::~LoadStreamMapPool() = default;
135
std::shared_ptr<LoadStreamMap> LoadStreamMapPool::get_or_create(UniqueId load_id, int64_t src_id,
136
3
                                                                int num_streams, int num_use) {
137
3
    std::lock_guard<std::mutex> lock(_mutex);
138
3
    std::shared_ptr<LoadStreamMap> streams = _pool[load_id];
139
3
    if (streams != nullptr) {
140
1
        return streams;
141
1
    }
142
2
    streams = std::make_shared<LoadStreamMap>(load_id, src_id, num_streams, num_use, this);
143
2
    _pool[load_id] = streams;
144
2
    return streams;
145
3
}
146
147
2
void LoadStreamMapPool::erase(UniqueId load_id) {
148
2
    std::lock_guard<std::mutex> lock(_mutex);
149
2
    _pool.erase(load_id);
150
2
}
151
152
} // namespace doris