Coverage Report

Created: 2026-08-10 11:21

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