Coverage Report

Created: 2025-07-26 00:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/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 "olap/olap_common.h"
25
#include "olap/utils.h"
26
#include "runtime/thread_context.h"
27
28
namespace doris {
29
#include "common/compile_check_begin.h"
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
167
    RowIdConversion() = default;
39
167
    ~RowIdConversion() { RELEASE_THREAD_MEM_TRACKER(_seg_rowid_map_mem_used); }
40
41
    // resize segment rowid map to its rows num
42
384
    Status init_segment_map(const RowsetId& src_rowset_id, const std::vector<uint32_t>& num_rows) {
43
1.00k
        for (size_t i = 0; i < num_rows.size(); i++) {
44
622
            constexpr size_t RESERVED_MEMORY = 10 * 1024 * 1024; // 10M
45
622
            if (doris::GlobalMemoryArbitrator::is_exceed_hard_mem_limit(RESERVED_MEMORY)) {
46
0
                return Status::MemoryLimitExceeded(fmt::format(
47
0
                        "RowIdConversion init_segment_map failed, process memory exceed limit or "
48
0
                        "sys available memory less than low water mark , {}, "
49
0
                        "consuming "
50
0
                        "tracker:<{}>, peak used {}, current used {}.",
51
0
                        doris::GlobalMemoryArbitrator::process_mem_log_str(),
52
0
                        doris::thread_context()
53
0
                                ->thread_mem_tracker_mgr->limiter_mem_tracker()
54
0
                                ->label(),
55
0
                        doris::thread_context()
56
0
                                ->thread_mem_tracker_mgr->limiter_mem_tracker()
57
0
                                ->peak_consumption(),
58
0
                        doris::thread_context()
59
0
                                ->thread_mem_tracker_mgr->limiter_mem_tracker()
60
0
                                ->consumption()));
61
0
            }
62
63
622
            uint32_t id = static_cast<uint32_t>(_segments_rowid_map.size());
64
622
            _segment_to_id_map.emplace(std::pair<RowsetId, uint32_t> {src_rowset_id, i}, id);
65
622
            _id_to_segment_map.emplace_back(src_rowset_id, i);
66
622
            std::vector<std::pair<uint32_t, uint32_t>> vec(
67
622
                    num_rows[i], std::pair<uint32_t, uint32_t>(UINT32_MAX, UINT32_MAX));
68
69
            //NOTE: manually count _segments_rowid_map's memory here, because _segments_rowid_map could be used by indexCompaction.
70
            // indexCompaction is a thridparty code, it's too complex to modify it.
71
            // refer compact_column.
72
622
            track_mem_usage(vec.capacity());
73
622
            _segments_rowid_map.emplace_back(std::move(vec));
74
622
        }
75
384
        return Status::OK();
76
384
    }
77
78
    // set dst rowset id
79
131
    void set_dst_rowset_id(const RowsetId& dst_rowset_id) { _dst_rowst_id = dst_rowset_id; }
80
20
    const RowsetId get_dst_rowset_id() { return _dst_rowst_id; }
81
82
    // add row id to the map
83
    void add(const std::vector<RowLocation>& rss_row_ids,
84
5.18k
             const std::vector<uint32_t>& dst_segments_num_row) {
85
4.71M
        for (auto& item : rss_row_ids) {
86
4.71M
            if (item.row_id == -1) {
87
0
                continue;
88
0
            }
89
4.71M
            uint32_t id = _segment_to_id_map.at(
90
4.71M
                    std::pair<RowsetId, uint32_t> {item.rowset_id, item.segment_id});
91
4.71M
            if (_cur_dst_segment_id < dst_segments_num_row.size() &&
92
4.71M
                _cur_dst_segment_rowid >= dst_segments_num_row[_cur_dst_segment_id]) {
93
3.64k
                _cur_dst_segment_id++;
94
3.64k
                _cur_dst_segment_rowid = 0;
95
3.64k
            }
96
4.71M
            _segments_rowid_map[id][item.row_id] =
97
4.71M
                    std::pair<uint32_t, uint32_t> {_cur_dst_segment_id, _cur_dst_segment_rowid++};
98
4.71M
        }
99
5.18k
    }
100
101
    // get destination RowLocation
102
    // return non-zero if the src RowLocation does not exist
103
1.75M
    int get(const RowLocation& src, RowLocation* dst) const {
104
1.75M
        auto iter = _segment_to_id_map.find({src.rowset_id, src.segment_id});
105
1.75M
        if (iter == _segment_to_id_map.end()) {
106
1
            return -1;
107
1
        }
108
1.75M
        const auto& rowid_map = _segments_rowid_map[iter->second];
109
1.75M
        if (src.row_id >= rowid_map.size()) {
110
1
            return -1;
111
1
        }
112
1.75M
        auto& [dst_segment_id, dst_rowid] = rowid_map[src.row_id];
113
1.75M
        if (dst_segment_id == UINT32_MAX && dst_rowid == UINT32_MAX) {
114
764k
            return -1;
115
764k
        }
116
117
993k
        dst->rowset_id = _dst_rowst_id;
118
993k
        dst->segment_id = dst_segment_id;
119
993k
        dst->row_id = dst_rowid;
120
993k
        return 0;
121
1.75M
    }
122
123
    const std::vector<std::vector<std::pair<uint32_t, uint32_t>>>& get_rowid_conversion_map()
124
20
            const {
125
20
        return _segments_rowid_map;
126
20
    }
127
128
20
    const std::map<std::pair<RowsetId, uint32_t>, uint32_t>& get_src_segment_to_id_map() {
129
20
        return _segment_to_id_map;
130
20
    }
131
132
0
    std::pair<RowsetId, uint32_t> get_segment_by_id(uint32_t id) const {
133
0
        DCHECK_GT(_id_to_segment_map.size(), id);
134
0
        return _id_to_segment_map.at(id);
135
0
    }
136
137
0
    uint32_t get_id_by_segment(const std::pair<RowsetId, uint32_t>& segment) const {
138
0
        return _segment_to_id_map.at(segment);
139
0
    }
140
141
private:
142
622
    void track_mem_usage(size_t delta_std_pair_cap) {
143
622
        _std_pair_cap += delta_std_pair_cap;
144
145
622
        size_t new_size =
146
622
                _std_pair_cap * sizeof(std::pair<uint32_t, uint32_t>) +
147
622
                _segments_rowid_map.capacity() * sizeof(std::vector<std::pair<uint32_t, uint32_t>>);
148
622
        CONSUME_THREAD_MEM_TRACKER(new_size - _seg_rowid_map_mem_used);
149
0
        _seg_rowid_map_mem_used = new_size;
150
622
    }
151
152
private:
153
    // the first level vector: index indicates src segment.
154
    // the second level vector: index indicates row id of source segment,
155
    // value indicates row id of destination segment.
156
    // <UINT32_MAX, UINT32_MAX> indicates current row not exist.
157
    std::vector<std::vector<std::pair<uint32_t, uint32_t>>> _segments_rowid_map;
158
    size_t _seg_rowid_map_mem_used {0};
159
    size_t _std_pair_cap {0};
160
161
    // Map source segment to 0 to n
162
    std::map<std::pair<RowsetId, uint32_t>, uint32_t> _segment_to_id_map;
163
164
    // Map 0 to n to source segment
165
    std::vector<std::pair<RowsetId, uint32_t>> _id_to_segment_map;
166
167
    // dst rowset id
168
    RowsetId _dst_rowst_id;
169
170
    // current dst segment id
171
    std::uint32_t _cur_dst_segment_id = 0;
172
173
    // current rowid of dst segment
174
    std::uint32_t _cur_dst_segment_rowid = 0;
175
};
176
177
#include "common/compile_check_end.h"
178
} // namespace doris