Coverage Report

Created: 2026-11-04 13:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/id_manager.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 <butil/macros.h>
21
#include <gen_cpp/Types_types.h>
22
#include <stddef.h>
23
#include <stdint.h>
24
25
#include <functional>
26
#include <map>
27
#include <memory>
28
#include <mutex>
29
#include <set>
30
#include <shared_mutex>
31
#include <string>
32
#include <string_view>
33
#include <unordered_map>
34
#include <unordered_set>
35
#include <utility>
36
#include <vector>
37
38
#include "common/status.h"
39
#include "runtime/query_context.h"
40
#include "storage/olap_common.h"
41
#include "storage/tablet/tablet.h"
42
#include "storage/tablet/tablet_meta.h"
43
44
namespace doris {
45
46
enum class FileMappingType {
47
    INTERNAL, // for doris format file {tablet_id}{rowset_id}{segment_id}
48
    EXTERNAL, // for external table.
49
};
50
51
struct InternalFileMappingInfo {
52
    int64_t tablet_id;
53
    RowsetId rowset_id;
54
    uint32_t segment_id;
55
56
2.98k
    std::string to_string() const {
57
2.98k
        std::string value;
58
2.98k
        value.resize(sizeof(tablet_id) + sizeof(rowset_id) + sizeof(segment_id));
59
2.98k
        auto* ptr = value.data();
60
61
2.98k
        memcpy(ptr, &tablet_id, sizeof(tablet_id));
62
2.98k
        ptr += sizeof(tablet_id);
63
2.98k
        memcpy(ptr, &rowset_id, sizeof(rowset_id));
64
2.98k
        ptr += sizeof(rowset_id);
65
2.98k
        memcpy(ptr, &segment_id, sizeof(segment_id));
66
2.98k
        return value;
67
2.98k
    }
68
};
69
70
struct ExternalFileMappingInfo {
71
    /* By recording the plan_node_id in fileMapping, the TFileScanRangeParams used in the scan phase can be found
72
    * from QueryContext according to the plan_node_id. Because there are some important information in
73
    * TFileScanRangeParams (needed when creating hdfs/s3 reader):
74
    *      8: optional THdfsParams hdfs_params;
75
    *      9: optional map<string, string> properties;
76
    */
77
    int plan_node_id;
78
79
    /*
80
     * Record TFileRangeDesc external_scan_range_desc in fileMapping, usage:
81
     * 1. If the file belongs to a partition, columns_from_path_keys and columns_from_path in TFileRangeDesc are needed when materializing the partition column
82
     * 2. path, file_type, modification_time,compress_type .... used to read the file
83
     * 3. TFileFormatType can distinguish whether it is iceberg/hive/hudi/paimon
84
     */
85
    TFileRangeDesc scan_range_desc;
86
    bool enable_file_meta_cache;
87
88
    ExternalFileMappingInfo(int plan_node_id, const TFileRangeDesc& scan_range,
89
                            bool file_meta_cache)
90
1
            : plan_node_id(plan_node_id),
91
1
              scan_range_desc(scan_range),
92
1
              enable_file_meta_cache(file_meta_cache) {}
93
94
2
    std::string to_string() const {
95
2
        std::string value;
96
2
        value.resize(scan_range_desc.path.size() + sizeof(plan_node_id) +
97
2
                     sizeof(scan_range_desc.start_offset));
98
2
        auto* ptr = value.data();
99
100
2
        memcpy(ptr, &plan_node_id, sizeof(plan_node_id));
101
2
        ptr += sizeof(plan_node_id);
102
2
        memcpy(ptr, &scan_range_desc.start_offset, sizeof(scan_range_desc.start_offset));
103
2
        ptr += sizeof(scan_range_desc.start_offset);
104
2
        memcpy(ptr, scan_range_desc.path.data(), scan_range_desc.path.size());
105
2
        return value;
106
2
    }
107
};
108
109
struct FileMapping {
110
    ENABLE_FACTORY_CREATOR(FileMapping);
111
112
    FileMappingType type;
113
    std::variant<InternalFileMappingInfo, ExternalFileMappingInfo> value;
114
115
    FileMapping(int64_t tablet_id, RowsetId rowset_id, uint32_t segment_id)
116
992
            : type(FileMappingType::INTERNAL),
117
992
              value(std::in_place_type<InternalFileMappingInfo>, tablet_id, rowset_id, segment_id) {
118
992
    }
119
120
    FileMapping(int plan_node_id, const TFileRangeDesc& scan_range, bool enable_file_meta_cache)
121
1
            : type(FileMappingType::EXTERNAL),
122
1
              value(std::in_place_type<ExternalFileMappingInfo>, plan_node_id, scan_range,
123
1
                    enable_file_meta_cache) {}
124
125
0
    std::tuple<int64_t, RowsetId, uint32_t> get_doris_format_info() const {
126
0
        DCHECK(type == FileMappingType::INTERNAL);
127
0
        auto info = std::get<InternalFileMappingInfo>(value);
128
0
        return std::make_tuple(info.tablet_id, info.rowset_id, info.segment_id);
129
0
    }
130
131
0
    ExternalFileMappingInfo& get_external_file_info() {
132
0
        DCHECK(type == FileMappingType::EXTERNAL);
133
0
        return std::get<ExternalFileMappingInfo>(value);
134
0
    }
135
136
    static std::string file_mapping_info_to_string(
137
2.97k
            const std::variant<InternalFileMappingInfo, ExternalFileMappingInfo>& info) {
138
2.97k
        return std::visit(
139
2.99k
                [](const auto& info) -> std::string {
140
2.99k
                    using T = std::decay_t<decltype(info)>;
141
142
2.99k
                    if constexpr (std::is_same_v<T, InternalFileMappingInfo>) {
143
2.98k
                        return info.to_string();
144
145
2.98k
                    } else if constexpr (std::is_same_v<T, ExternalFileMappingInfo>) {
146
2
                        return info.to_string();
147
2
                    }
148
2.99k
                },
_ZZN5doris11FileMapping27file_mapping_info_to_stringB5cxx11ERKSt7variantIJNS_23InternalFileMappingInfoENS_23ExternalFileMappingInfoEEEENKUlRKT_E_clIS2_EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES9_
Line
Count
Source
139
2.98k
                [](const auto& info) -> std::string {
140
2.98k
                    using T = std::decay_t<decltype(info)>;
141
142
2.98k
                    if constexpr (std::is_same_v<T, InternalFileMappingInfo>) {
143
2.98k
                        return info.to_string();
144
145
                    } else if constexpr (std::is_same_v<T, ExternalFileMappingInfo>) {
146
                        return info.to_string();
147
                    }
148
2.98k
                },
_ZZN5doris11FileMapping27file_mapping_info_to_stringB5cxx11ERKSt7variantIJNS_23InternalFileMappingInfoENS_23ExternalFileMappingInfoEEEENKUlRKT_E_clIS3_EENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES9_
Line
Count
Source
139
2
                [](const auto& info) -> std::string {
140
2
                    using T = std::decay_t<decltype(info)>;
141
142
                    if constexpr (std::is_same_v<T, InternalFileMappingInfo>) {
143
                        return info.to_string();
144
145
2
                    } else if constexpr (std::is_same_v<T, ExternalFileMappingInfo>) {
146
2
                        return info.to_string();
147
2
                    }
148
2
                },
149
2.97k
                info);
150
2.97k
    }
151
152
2.98k
    std::string file_mapping_info_to_string() { return file_mapping_info_to_string(value); }
153
};
154
155
class IdFileMap {
156
public:
157
206
    IdFileMap(uint64_t expired_timestamp) : delayed_expired_timestamp(expired_timestamp) {}
158
159
1.00k
    std::shared_ptr<FileMapping> get_file_mapping(uint32_t id) {
160
1.00k
        std::shared_lock lock(_mtx);
161
1.00k
        auto it = _id_map.find(id);
162
1.00k
        if (it == _id_map.end()) {
163
1
            return nullptr;
164
1
        }
165
1.00k
        return it->second;
166
1.00k
    }
167
168
997
    uint32_t get_file_mapping_id(const std::shared_ptr<FileMapping>& mapping) {
169
997
        DCHECK(mapping.get() != nullptr);
170
997
        auto value = mapping->file_mapping_info_to_string();
171
172
997
        std::unique_lock lock(_mtx);
173
997
        auto it = _mapping_to_id.find(value);
174
997
        if (it != _mapping_to_id.end()) {
175
0
            return it->second;
176
0
        }
177
997
        _id_map[_init_id++] = mapping;
178
997
        _mapping_to_id[value] = _init_id - 1;
179
180
997
        return _init_id - 1;
181
997
    }
182
183
0
    void add_temp_rowset(const RowsetSharedPtr& rowset) {
184
0
        std::unique_lock lock(_mtx);
185
0
        _temp_rowset_maps[{rowset->rowset_meta()->tablet_id(), rowset->rowset_id()}] = rowset;
186
0
    }
187
188
0
    RowsetSharedPtr get_temp_rowset(const int64_t tablet_id, const RowsetId& rowset_id) {
189
0
        std::shared_lock lock(_mtx);
190
0
        auto it = _temp_rowset_maps.find({tablet_id, rowset_id});
191
0
        if (it == _temp_rowset_maps.end()) {
192
0
            return nullptr;
193
0
        }
194
0
        return it->second;
195
0
    }
196
197
0
    int64_t get_delayed_expired_timestamp() { return delayed_expired_timestamp; }
198
199
0
    void set_external_scan_params(QueryContext* query_ctx, int max_file_scanners) {
200
0
        std::call_once(once_flag_for_external, [&] {
201
0
            DCHECK(query_ctx != nullptr);
202
0
            _query_global = query_ctx->get_query_globals();
203
0
            _query_options = query_ctx->get_query_options();
204
0
            _file_scan_range_params_map = query_ctx->file_scan_range_params_map;
205
0
            _max_file_scanners = max_file_scanners;
206
0
        });
207
0
    }
208
209
0
    const TQueryGlobals& get_query_globals() const { return _query_global; }
210
211
0
    const TQueryOptions& get_query_options() const { return _query_options; }
212
213
0
    const std::map<int, TFileScanRangeParams>& get_external_scan_params() const {
214
0
        return _file_scan_range_params_map;
215
0
    }
216
217
0
    int get_max_file_scanners() const { return _max_file_scanners; }
218
219
private:
220
    std::shared_mutex _mtx;
221
    uint32_t _init_id = 0;
222
    std::unordered_map<std::string, uint32_t> _mapping_to_id;
223
    std::unordered_map<uint32_t, std::shared_ptr<FileMapping>> _id_map;
224
225
    // use in scan external table
226
    TQueryGlobals _query_global;
227
    TQueryOptions _query_options;
228
    std::map<int, TFileScanRangeParams> _file_scan_range_params_map;
229
    std::once_flag once_flag_for_external;
230
    int _max_file_scanners = 10;
231
232
    // use in Doris Format to keep temp rowsets, preventing them from being deleted by compaction
233
    std::unordered_map<std::pair<int64_t, RowsetId>, RowsetSharedPtr> _temp_rowset_maps;
234
    uint64_t delayed_expired_timestamp = 0;
235
};
236
237
class IdManager {
238
public:
239
    static constexpr uint8_t ID_VERSION = 0;
240
241
3
    IdManager() = default;
242
243
3
    ~IdManager() {
244
3
        std::unique_lock lock(_query_to_id_file_map_mtx);
245
3
        _query_to_id_file_map.clear();
246
3
    }
247
248
205
    std::shared_ptr<IdFileMap> add_id_file_map(const UniqueId& query_id, int timeout) {
249
205
        std::unique_lock lock(_query_to_id_file_map_mtx);
250
205
        auto it = _query_to_id_file_map.find(query_id);
251
205
        if (it == _query_to_id_file_map.end()) {
252
204
            auto id_file_map = std::make_shared<IdFileMap>(UnixSeconds() + timeout);
253
204
            _query_to_id_file_map[query_id] = id_file_map;
254
204
            return id_file_map;
255
204
        }
256
1
        return it->second;
257
205
    }
258
259
0
    void gc_expired_id_file_map(int64_t now) {
260
0
        std::unique_lock lock(_query_to_id_file_map_mtx);
261
0
        for (auto it = _query_to_id_file_map.begin(); it != _query_to_id_file_map.end();) {
262
0
            if (it->second->get_delayed_expired_timestamp() <= now) {
263
0
                it = _query_to_id_file_map.erase(it);
264
0
            } else {
265
0
                ++it;
266
0
            }
267
0
        }
268
0
    }
269
270
101
    void remove_id_file_map(const UniqueId& query_id) {
271
101
        std::unique_lock lock(_query_to_id_file_map_mtx);
272
101
        _query_to_id_file_map.erase(query_id);
273
101
    }
274
275
1
    std::shared_ptr<IdFileMap> get_id_file_map(const UniqueId& query_id) {
276
1
        std::shared_lock lock(_query_to_id_file_map_mtx);
277
1
        auto it = _query_to_id_file_map.find(query_id);
278
1
        if (it == _query_to_id_file_map.end()) {
279
0
            return nullptr;
280
0
        }
281
1
        return it->second;
282
1
    }
283
284
private:
285
    DISALLOW_COPY_AND_ASSIGN(IdManager);
286
287
    phmap::flat_hash_map<UniqueId, std::shared_ptr<IdFileMap>> _query_to_id_file_map;
288
    std::shared_mutex _query_to_id_file_map_mtx;
289
};
290
291
} // namespace doris