Coverage Report

Created: 2026-08-19 17:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/rowid_conversion.h
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
#pragma once
19
20
#include <map>
21
#include <vector>
22
23
#include "common/cast_set.h"
24
#include "common/check.h"
25
#include "runtime/thread_context.h"
26
#include "storage/olap_common.h"
27
#include "storage/utils.h"
28
29
namespace doris {
30
31
// For unique key merge on write table, we should update delete bitmap
32
// of destination rowset when compaction finished.
33
// Through the row id correspondence between the source rowset and the
34
// destination rowset, we can quickly update the delete bitmap of the
35
// destination rowset.
36
class RowIdConversion {
37
public:
38
268
    RowIdConversion() = default;
39
268
    ~RowIdConversion() { RELEASE_THREAD_MEM_TRACKER(_seg_rowid_map_mem_used); }
40
41
    // resize segment rowid map to its rows num
42
419
    Status init_segment_map(const RowsetId& src_rowset_id, const std::vector<uint32_t>& num_rows) {
43
1.15k
        for (size_t i = 0; i < num_rows.size(); i++) {
44
732
            auto src_segment = std::pair<RowsetId, uint32_t> {src_rowset_id, cast_set<uint32_t>(i)};
45
732
            auto iter = _segment_to_id_map.find(src_segment);
46
            // Each segment-group reader initializes all source segments, so reuse existing maps.
47
732
            if (iter != _segment_to_id_map.end()) {
48
20
                DORIS_CHECK_LT(iter->second, _segments_rowid_map.size());
49
20
                DORIS_CHECK_EQ(_segments_rowid_map[iter->second].size(), num_rows[i]);
50
20
                continue;
51
20
            }
52
53
712
            constexpr size_t RESERVED_MEMORY = 10 * 1024 * 1024; // 10M
54
712
            if (doris::GlobalMemoryArbitrator::is_exceed_hard_mem_limit(RESERVED_MEMORY)) {
55
0
                return Status::MemoryLimitExceeded(fmt::format(
56
0
                        "RowIdConversion init_segment_map failed, process memory exceed limit or "
57
0
                        "sys available memory less than low water mark , {}, "
58
0
                        "consuming "
59
0
                        "tracker:<{}>, peak used {}, current used {}.",
60
0
                        doris::GlobalMemoryArbitrator::process_mem_log_str(),
61
0
                        doris::thread_context()
62
0
                                ->thread_mem_tracker_mgr->limiter_mem_tracker()
63
0
                                ->label(),
64
0
                        doris::thread_context()
65
0
                                ->thread_mem_tracker_mgr->limiter_mem_tracker()
66
0
                                ->peak_consumption(),
67
0
                        doris::thread_context()
68
0
                                ->thread_mem_tracker_mgr->limiter_mem_tracker()
69
0
                                ->consumption()));
70
0
            }
71
72
712
            uint32_t id = cast_set<uint32_t>(_segments_rowid_map.size());
73
712
            auto insert_result = _segment_to_id_map.emplace(src_segment, id);
74
712
            DORIS_CHECK(insert_result.second);
75
712
            _id_to_segment_map.push_back(src_segment);
76
712
            std::vector<std::pair<uint32_t, uint32_t>> vec(
77
712
                    num_rows[i], std::pair<uint32_t, uint32_t>(UINT32_MAX, UINT32_MAX));
78
79
            //NOTE: manually count _segments_rowid_map's memory here, because _segments_rowid_map could be used by indexCompaction.
80
            // indexCompaction is a thridparty code, it's too complex to modify it.
81
            // refer compact_column.
82
712
            track_mem_usage(vec.capacity());
83
712
            _segments_rowid_map.emplace_back(std::move(vec));
84
712
        }
85
419
        return Status::OK();
86
419
    }
87
88
    // set dst rowset id
89
148
    void set_dst_rowset_id(const RowsetId& dst_rowset_id) { _dst_rowst_id = dst_rowset_id; }
90
25
    const RowsetId get_dst_rowset_id() { return _dst_rowst_id; }
91
92
    // add row id to the map
93
    void add(const std::vector<RowLocation>& rss_row_ids,
94
1.78k
             const std::vector<uint32_t>& dst_segments_num_row) {
95
4.75M
        for (auto& item : rss_row_ids) {
96
4.75M
            if (item.row_id == -1) {
97
0
                continue;
98
0
            }
99
4.75M
            uint32_t id = _segment_to_id_map.at(
100
4.75M
                    std::pair<RowsetId, uint32_t> {item.rowset_id, item.segment_id});
101
4.75M
            if (_cur_dst_segment_id < dst_segments_num_row.size() &&
102
4.75M
                _cur_dst_segment_rowid >= dst_segments_num_row[_cur_dst_segment_id]) {
103
1.15k
                _cur_dst_segment_id++;
104
1.15k
                _cur_dst_segment_rowid = 0;
105
1.15k
            }
106
4.75M
            _segments_rowid_map[id][item.row_id] =
107
4.75M
                    std::pair<uint32_t, uint32_t> {_cur_dst_segment_id, _cur_dst_segment_rowid++};
108
4.75M
        }
109
1.78k
    }
110
111
    // get destination RowLocation
112
    // return non-zero if the src RowLocation does not exist
113
1.77M
    int get(const RowLocation& src, RowLocation* dst) const {
114
1.77M
        auto iter = _segment_to_id_map.find({src.rowset_id, src.segment_id});
115
1.77M
        if (iter == _segment_to_id_map.end()) {
116
1
            return -1;
117
1
        }
118
1.77M
        const auto& rowid_map = _segments_rowid_map[iter->second];
119
1.77M
        if (src.row_id >= rowid_map.size()) {
120
1
            return -1;
121
1
        }
122
1.77M
        auto& [dst_segment_id, dst_rowid] = rowid_map[src.row_id];
123
1.77M
        if (dst_segment_id == UINT32_MAX && dst_rowid == UINT32_MAX) {
124
764k
            return -1;
125
764k
        }
126
127
1.00M
        dst->rowset_id = _dst_rowst_id;
128
1.00M
        dst->segment_id = dst_segment_id;
129
1.00M
        dst->row_id = dst_rowid;
130
1.00M
        return 0;
131
1.77M
    }
132
133
    const std::vector<std::vector<std::pair<uint32_t, uint32_t>>>& get_rowid_conversion_map()
134
29
            const {
135
29
        return _segments_rowid_map;
136
29
    }
137
138
29
    const std::map<std::pair<RowsetId, uint32_t>, uint32_t>& get_src_segment_to_id_map() {
139
29
        return _segment_to_id_map;
140
29
    }
141
142
0
    std::pair<RowsetId, uint32_t> get_segment_by_id(uint32_t id) const {
143
0
        DCHECK_GT(_id_to_segment_map.size(), id);
144
0
        return _id_to_segment_map.at(id);
145
0
    }
146
147
0
    uint32_t get_id_by_segment(const std::pair<RowsetId, uint32_t>& segment) const {
148
0
        return _segment_to_id_map.at(segment);
149
0
    }
150
151
private:
152
712
    void track_mem_usage(size_t delta_std_pair_cap) {
153
712
        _std_pair_cap += delta_std_pair_cap;
154
155
712
        size_t new_size =
156
712
                _std_pair_cap * sizeof(std::pair<uint32_t, uint32_t>) +
157
712
                _segments_rowid_map.capacity() * sizeof(std::vector<std::pair<uint32_t, uint32_t>>);
158
712
        CONSUME_THREAD_MEM_TRACKER(new_size - _seg_rowid_map_mem_used);
159
0
        _seg_rowid_map_mem_used = new_size;
160
712
    }
161
162
private:
163
    // the first level vector: index indicates src segment.
164
    // the second level vector: index indicates row id of source segment,
165
    // value indicates row id of destination segment.
166
    // <UINT32_MAX, UINT32_MAX> indicates current row not exist.
167
    std::vector<std::vector<std::pair<uint32_t, uint32_t>>> _segments_rowid_map;
168
    size_t _seg_rowid_map_mem_used {0};
169
    size_t _std_pair_cap {0};
170
171
    // Map source segment to 0 to n
172
    std::map<std::pair<RowsetId, uint32_t>, uint32_t> _segment_to_id_map;
173
174
    // Map 0 to n to source segment
175
    std::vector<std::pair<RowsetId, uint32_t>> _id_to_segment_map;
176
177
    // dst rowset id
178
    RowsetId _dst_rowst_id;
179
180
    // current dst segment id
181
    std::uint32_t _cur_dst_segment_id = 0;
182
183
    // current rowid of dst segment
184
    std::uint32_t _cur_dst_segment_rowid = 0;
185
};
186
187
} // namespace doris