Coverage Report

Created: 2026-08-14 00:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/tablet_info.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/fast_rand.h>
21
#include <gen_cpp/Descriptors_types.h>
22
#include <gen_cpp/Types_types.h>
23
#include <gen_cpp/descriptors.pb.h>
24
#include <gen_cpp/olap_file.pb.h>
25
26
#include <cstdint>
27
#include <functional>
28
#include <iterator>
29
#include <map>
30
#include <memory>
31
#include <string>
32
#include <tuple>
33
#include <unordered_map>
34
#include <utility>
35
#include <vector>
36
37
#include "cloud/config.h"
38
#include "common/cast_set.h"
39
#include "common/logging.h"
40
#include "common/object_pool.h"
41
#include "common/status.h"
42
#include "core/block/block.h"
43
#include "core/block/column_with_type_and_name.h"
44
#include "core/column/column.h"
45
#include "exprs/vexpr.h"
46
#include "exprs/vexpr_fwd.h"
47
#include "runtime/descriptors.h"
48
#include "util/raw_value.h"
49
50
namespace doris {
51
class MemTracker;
52
class SlotDescriptor;
53
class TExprNode;
54
class TabletColumn;
55
class TabletIndex;
56
class TupleDescriptor;
57
58
struct OlapTableIndexSchema {
59
    int64_t index_id;
60
    int64_t row_binlog_id = 0;
61
    std::vector<SlotDescriptor*> slots;
62
    int32_t schema_hash;
63
    std::vector<TabletColumn*> columns;
64
    std::vector<TabletIndex*> indexes;
65
    VExprContextSPtr where_clause;
66
67
    void to_protobuf(POlapTableIndexSchema* pindex) const;
68
};
69
70
class OlapTableSchemaParam {
71
public:
72
63
    OlapTableSchemaParam() = default;
73
63
    ~OlapTableSchemaParam() noexcept = default;
74
75
    Status init(const TOlapTableSchemaParam& tschema);
76
    Status init(const POlapTableSchemaParam& pschema);
77
78
86
    int64_t db_id() const { return _db_id; }
79
86
    int64_t table_id() const { return _table_id; }
80
41
    int64_t version() const { return _version; }
81
82
29
    TupleDescriptor* tuple_desc() const { return _tuple_desc; }
83
144
    const std::vector<OlapTableIndexSchema*>& indexes() const { return _indexes; }
84
    const OlapTableIndexSchema* row_binlog_index_schema(int64_t index_id) const;
85
0
    const std::vector<OlapTableIndexSchema*>& row_binlog_index_schemas() const {
86
0
        return _row_binlog_index_schemas;
87
0
    }
88
89
    void to_protobuf(POlapTableSchemaParam* pschema) const;
90
91
    // NOTE: this function is not thread-safe.
92
16
    POlapTableSchemaParam* to_protobuf() const {
93
16
        if (_proto_schema == nullptr) {
94
16
            _proto_schema = _obj_pool.add(new POlapTableSchemaParam());
95
16
            to_protobuf(_proto_schema);
96
16
        }
97
16
        return _proto_schema;
98
16
    }
99
100
31
    UniqueKeyUpdateModePB unique_key_update_mode() const { return _unique_key_update_mode; }
101
102
50
    bool is_partial_update() const {
103
50
        return _unique_key_update_mode != UniqueKeyUpdateModePB::UPSERT;
104
50
    }
105
2
    bool is_fixed_partial_update() const {
106
2
        return _unique_key_update_mode == UniqueKeyUpdateModePB::UPDATE_FIXED_COLUMNS;
107
2
    }
108
0
    bool is_flexible_partial_update() const {
109
0
        return _unique_key_update_mode == UniqueKeyUpdateModePB::UPDATE_FLEXIBLE_COLUMNS;
110
0
    }
111
112
31
    const std::set<std::string>& partial_update_input_columns() const {
113
31
        return _partial_update_input_columns;
114
31
    }
115
31
    PartialUpdateNewRowPolicyPB partial_update_new_key_policy() const {
116
31
        return _partial_update_new_row_policy;
117
31
    }
118
31
    std::string auto_increment_coulumn() const { return _auto_increment_column; }
119
2
    int32_t auto_increment_column_unique_id() const { return _auto_increment_column_unique_id; }
120
2
    void set_timestamp_ms(int64_t timestamp_ms) { _timestamp_ms = timestamp_ms; }
121
31
    int64_t timestamp_ms() const { return _timestamp_ms; }
122
2
    void set_nano_seconds(int32_t nano_seconds) { _nano_seconds = nano_seconds; }
123
31
    int32_t nano_seconds() const { return _nano_seconds; }
124
2
    void set_timezone(std::string timezone) { _timezone = timezone; }
125
31
    std::string timezone() const { return _timezone; }
126
31
    bool is_strict_mode() const { return _is_strict_mode; }
127
30
    int32_t sequence_map_col_uid() const { return _sequence_map_col_uid; }
128
    std::string debug_string() const;
129
130
    Status init_unique_key_update_mode(const TOlapTableSchemaParam& tschema);
131
132
private:
133
    int64_t _db_id;
134
    int64_t _table_id;
135
    int64_t _version;
136
137
    TupleDescriptor* _tuple_desc = nullptr;
138
    mutable POlapTableSchemaParam* _proto_schema = nullptr;
139
    std::vector<OlapTableIndexSchema*> _indexes;
140
    std::vector<OlapTableIndexSchema*> _row_binlog_index_schemas;
141
    mutable ObjectPool _obj_pool;
142
    UniqueKeyUpdateModePB _unique_key_update_mode {UniqueKeyUpdateModePB::UPSERT};
143
    PartialUpdateNewRowPolicyPB _partial_update_new_row_policy {
144
            PartialUpdateNewRowPolicyPB::APPEND};
145
    std::set<std::string> _partial_update_input_columns;
146
    bool _is_strict_mode = false;
147
    std::string _auto_increment_column;
148
    int32_t _auto_increment_column_unique_id;
149
    int64_t _timestamp_ms = 0;
150
    int32_t _nano_seconds {0};
151
    std::string _timezone;
152
    int32_t _sequence_map_col_uid {-1};
153
};
154
155
using OlapTableIndexTablets = TOlapTableIndexTablets;
156
// struct TOlapTableIndexTablets {
157
//     1: required i64 index_id
158
//     2: required list<i64> tablets
159
//     3: optional i64 bucket_be_id
160
//     4: optional list<i32> local_bucket_seqs
161
// }
162
163
using BlockRow = std::pair<Block*, int32_t>;
164
using BlockRowWithIndicator = std::tuple<Block*, int32_t, bool>; // [block, row, is_transformed]
165
166
struct VOlapTablePartition {
167
    int64_t id = 0;
168
    BlockRow start_key;
169
    BlockRow end_key;
170
    std::vector<BlockRow> in_keys;
171
    int64_t num_buckets = 0;
172
    std::vector<OlapTableIndexTablets> indexes;
173
    bool is_mutable;
174
    // -1 indicates partition with hash distribution
175
    int64_t load_tablet_idx = -1;
176
    // Fallback FE-selected bucket owner BE for adaptive random bucket mode. New FE versions send
177
    // this per index in OlapTableIndexTablets.
178
    int64_t bucket_be_id = -1;
179
    // Fallback bucket indices (0-based) used by FIND_TABLET_RANDOM_BUCKET rotation. New FE versions
180
    // send this per index in OlapTableIndexTablets.
181
    std::vector<int32_t> local_bucket_seqs;
182
    int total_replica_num = 0;
183
    int load_required_replica_num = 0;
184
    // tablet_id -> set of backend_ids that have version gaps
185
    std::unordered_map<int64_t, std::unordered_set<int64_t>> tablet_version_gap_backends;
186
187
    VOlapTablePartition(Block* partition_block)
188
            // the default value of partition bound is -1.
189
25
            : start_key {partition_block, -1}, end_key {partition_block, -1} {}
190
};
191
192
// this is only used by tablet_sink. so we can assume it's inited by its' descriptor.
193
class VOlapTablePartKeyComparator {
194
public:
195
    VOlapTablePartKeyComparator(const std::vector<uint16_t>& slot_locs,
196
                                const std::vector<uint16_t>& params_locs)
197
41
            : _slot_locs(slot_locs), _param_locs(params_locs) {}
198
199
    // return true if lhs < rhs
200
    // 'row' is -1 mean maximal boundary
201
    bool operator()(const BlockRowWithIndicator& lhs, const BlockRowWithIndicator& rhs) const;
202
203
private:
204
    const std::vector<uint16_t>& _slot_locs;
205
    const std::vector<uint16_t>& _param_locs;
206
};
207
208
// store an olap table's tablet information
209
class VOlapTablePartitionParam {
210
public:
211
    VOlapTablePartitionParam(std::shared_ptr<OlapTableSchemaParam>& schema,
212
                             const TOlapTablePartitionParam& param);
213
214
    ~VOlapTablePartitionParam();
215
216
    Status init();
217
218
4
    int64_t db_id() const { return _t_param.db_id; }
219
4
    int64_t table_id() const { return _t_param.table_id; }
220
0
    int64_t version() const { return _t_param.version; }
221
222
    // return true if we found this block_row in partition
223
    ALWAYS_INLINE bool find_partition(Block* block, int row,
224
30
                                      VOlapTablePartition*& partition) const {
225
30
        auto it = _is_in_partition ? _partitions_map->find(std::tuple {block, row, true})
226
30
                                   : _partitions_map->upper_bound(std::tuple {block, row, true});
227
30
        VLOG_TRACE << "find row " << row << " of\n"
228
0
                   << block->dump_data() << "in:\n"
229
0
                   << _partition_block.dump_data() << "result line row: " << std::get<1>(it->first);
230
231
        // for list partition it might result in default partition
232
30
        if (_is_in_partition) {
233
0
            partition = (it != _partitions_map->end()) ? it->second : _default_partition;
234
0
            it = _partitions_map->end();
235
0
        }
236
30
        if (it != _partitions_map->end() &&
237
30
            _part_contains(it->second, std::tuple {block, row, true})) {
238
26
            partition = it->second;
239
26
        }
240
30
        return (partition != nullptr);
241
30
    }
242
243
    ALWAYS_INLINE void find_tablets(
244
            Block* block, const std::vector<uint32_t>& indexes,
245
            const std::vector<VOlapTablePartition*>& partitions,
246
            std::vector<uint32_t>& tablet_indexes /*result*/,
247
            /*TODO: check if flat hash map will be better*/
248
14
            std::map<VOlapTablePartition*, int64_t>* partition_tablets_buffer = nullptr) const {
249
14
        std::function<uint32_t(Block*, uint32_t, const VOlapTablePartition&)> compute_function;
250
14
        if (!_distributed_slot_locs.empty()) {
251
            //TODO: refactor by saving the hash values. then we can calculate in columnwise.
252
11
            compute_function = [this](Block* block, uint32_t row,
253
14
                                      const VOlapTablePartition& partition) -> uint32_t {
254
14
                uint32_t hash_val = 0;
255
14
                for (unsigned short _distributed_slot_loc : _distributed_slot_locs) {
256
14
                    auto* slot_desc = _slots[_distributed_slot_loc];
257
14
                    auto& column = block->get_by_position(_distributed_slot_loc).column;
258
14
                    auto val = column->get_data_at(row);
259
14
                    if (val.data != nullptr) {
260
14
                        hash_val = RawValue::zlib_crc32(val.data, val.size,
261
14
                                                        slot_desc->type()->get_primitive_type(),
262
14
                                                        hash_val);
263
14
                    } else {
264
0
                        hash_val = HashUtil::zlib_crc_hash_null(hash_val);
265
0
                    }
266
14
                }
267
14
                return cast_set<uint32_t>(hash_val % partition.num_buckets);
268
14
            };
269
11
        } else { // random distribution
270
3
            compute_function = [](Block* block, uint32_t row,
271
3
                                  const VOlapTablePartition& partition) -> uint32_t {
272
3
                if (partition.load_tablet_idx == -1) {
273
                    // for compatible with old version, just do random
274
0
                    return cast_set<uint32_t>(butil::fast_rand() % partition.num_buckets);
275
0
                }
276
3
                return cast_set<uint32_t>(partition.load_tablet_idx % partition.num_buckets);
277
3
            };
278
3
        }
279
280
14
        if (partition_tablets_buffer == nullptr) {
281
14
            for (auto index : indexes) {
282
14
                tablet_indexes[index] = compute_function(block, index, *partitions[index]);
283
14
            }
284
11
        } else { // use buffer
285
6
            for (auto index : indexes) {
286
6
                auto* partition = partitions[index];
287
6
                if (auto it = partition_tablets_buffer->find(partition);
288
6
                    it != partition_tablets_buffer->end()) {
289
3
                    tablet_indexes[index] = cast_set<uint32_t>(it->second); // tablet
290
3
                } else {
291
                    // compute and save in buffer
292
3
                    (*partition_tablets_buffer)[partition] = tablet_indexes[index] =
293
3
                            compute_function(block, index, *partitions[index]);
294
3
                }
295
6
            }
296
3
        }
297
14
    }
298
299
0
    const std::vector<VOlapTablePartition*>& get_partitions() const { return _partitions; }
300
301
    // it's same with auto now because we only support transformed partition in auto partition. may expand in future
302
9
    bool is_projection_partition() const { return _is_auto_partition; }
303
19
    bool is_auto_partition() const { return _is_auto_partition; }
304
305
9
    bool is_auto_detect_overwrite() const { return _is_auto_detect_overwrite; }
306
2
    int64_t get_overwrite_group_id() const { return _overwrite_group_id; }
307
308
0
    std::shared_ptr<TNetworkAddress> get_master_address() const { return _master_address; }
309
310
2
    std::vector<uint16_t> get_partition_keys() const { return _partition_slot_locs; }
311
312
    Status add_partitions(const std::vector<TOlapTablePartition>& partitions);
313
    // no need to del/reinsert partition keys, but change the link. reset the _partitions items
314
    Status replace_partitions(std::vector<int64_t>& old_partition_ids,
315
                              const std::vector<TOlapTablePartition>& new_partitions);
316
317
13
    VExprContextSPtrs get_part_func_ctx() { return _part_func_ctx; }
318
13
    VExprSPtrs get_partition_function() { return _partition_function; }
319
320
    // which will affect _partition_block
321
    Status generate_partition_from(const TOlapTablePartition& t_part,
322
                                   VOlapTablePartition*& part_result);
323
324
2
    void set_transformed_slots(const std::vector<uint16_t>& new_slots) {
325
2
        _transformed_slot_locs = new_slots;
326
2
    }
327
328
private:
329
    Status _create_partition_keys(const std::vector<TExprNode>& t_exprs, BlockRow* part_key);
330
331
    // check if this partition contain this key
332
    bool _part_contains(VOlapTablePartition* part, BlockRowWithIndicator key) const;
333
334
    // this partition only valid in this schema
335
    std::shared_ptr<OlapTableSchemaParam> _schema;
336
    TOlapTablePartitionParam _t_param;
337
338
    const std::vector<SlotDescriptor*>& _slots;
339
    std::vector<uint16_t> _partition_slot_locs;
340
    std::vector<uint16_t> _transformed_slot_locs;
341
    std::vector<uint16_t> _distributed_slot_locs;
342
343
    ObjectPool _obj_pool;
344
    Block _partition_block;
345
    std::unique_ptr<MemTracker> _mem_tracker;
346
    std::vector<VOlapTablePartition*> _partitions;
347
    // For all partition value rows saved in this map, indicator is false. whenever we use a value to find in it, the param is true.
348
    // so that we can distinguish which column index to use (origin slots or transformed slots).
349
    // For range partition we ONLY SAVE RIGHT ENDS. when we find a part's RIGHT by a value, check if part's left cover it then.
350
    std::unique_ptr<
351
            std::map<BlockRowWithIndicator, VOlapTablePartition*, VOlapTablePartKeyComparator>>
352
            _partitions_map;
353
354
    bool _is_in_partition = false;
355
    size_t _mem_usage = 0;
356
    // only works when using list partition, the resource is owned by _partitions
357
    VOlapTablePartition* _default_partition = nullptr;
358
359
    bool _is_auto_partition = false;
360
    VExprContextSPtrs _part_func_ctx = {nullptr};
361
    VExprSPtrs _partition_function = {nullptr};
362
    TPartitionType::type _part_type; // support list or range
363
    // "insert overwrite partition(*)", detect which partitions by BE
364
    bool _is_auto_detect_overwrite = false;
365
    int64_t _overwrite_group_id = 0;
366
    std::shared_ptr<TNetworkAddress> _master_address = nullptr;
367
};
368
369
// indicate where's the tablet and all its replications (node-wise)
370
using TabletLocation = TTabletLocation;
371
// struct TTabletLocation {
372
//     1: required i64 tablet_id
373
//     2: required list<i64> node_ids
374
// }
375
376
class OlapTableLocationParam {
377
public:
378
10
    OlapTableLocationParam(const TOlapTableLocationParam& t_param) : _t_param(t_param) {
379
20
        for (auto& location : _t_param.tablets) {
380
20
            _tablets.emplace(location.tablet_id, &location);
381
20
            _update_base_to_binlog_tablet(location);
382
20
        }
383
10
    }
384
385
0
    int64_t db_id() const { return _t_param.db_id; }
386
0
    int64_t table_id() const { return _t_param.table_id; }
387
0
    int64_t version() const { return _t_param.version; }
388
389
1
    TabletLocation* find_tablet(int64_t tablet_id) const {
390
1
        auto it = _tablets.find(tablet_id);
391
1
        if (it != std::end(_tablets)) {
392
1
            return it->second;
393
1
        }
394
0
        return nullptr;
395
1
    }
396
397
    // used by base tablet on node_id to mark whether to write binlog, 0 means no binlog.
398
0
    int64_t get_binlog_tablet_id(int64_t base_tablet_id, int64_t node_id) const {
399
0
        auto it = _base_to_binlog_tablet.find({base_tablet_id, node_id});
400
0
        if (it != _base_to_binlog_tablet.end()) {
401
0
            return it->second;
402
0
        }
403
0
        if (config::is_cloud_mode()) {
404
0
            auto cloud_it = _base_to_binlog_tablet.lower_bound({base_tablet_id, INT64_MIN});
405
0
            if (cloud_it != _base_to_binlog_tablet.end() &&
406
0
                cloud_it->first.first == base_tablet_id) {
407
0
                return cloud_it->second;
408
0
            }
409
0
        }
410
0
        return 0;
411
0
    }
412
413
0
    void add_locations(std::vector<TTabletLocation>& locations) {
414
0
        for (auto& location : locations) {
415
0
            if (_tablets.find(location.tablet_id) == _tablets.end()) {
416
0
                _tablets[location.tablet_id] = &location;
417
0
            }
418
0
            _update_base_to_binlog_tablet(location);
419
0
        }
420
0
    }
421
422
private:
423
20
    void _update_base_to_binlog_tablet(const TTabletLocation& location) {
424
20
        if (!location.__isset.base_tablet_id) {
425
20
            return;
426
20
        }
427
        // the binlog tablet is written by its base tablet on the nodes that own both.
428
0
        for (int64_t node_id : location.node_ids) {
429
0
            _base_to_binlog_tablet.emplace(std::make_pair(location.base_tablet_id, node_id),
430
0
                                           location.tablet_id);
431
0
        }
432
0
    }
433
434
    TOlapTableLocationParam _t_param;
435
    // [tablet_id, tablet]. tablet has id, also.
436
    std::unordered_map<int64_t, TabletLocation*> _tablets;
437
    // [(base_tablet_id, node_id), row_binlog_tablet_id]
438
    std::map<std::pair<int64_t, int64_t>, int64_t> _base_to_binlog_tablet;
439
};
440
441
struct NodeInfo {
442
    int64_t id;
443
    int64_t option;
444
    std::string host;
445
    int32_t brpc_port;
446
447
0
    NodeInfo() = default;
448
449
    NodeInfo(const TNodeInfo& tnode)
450
2
            : id(tnode.id),
451
2
              option(tnode.option),
452
2
              host(tnode.host),
453
2
              brpc_port(tnode.async_internal_port) {}
454
};
455
456
class DorisNodesInfo {
457
public:
458
0
    DorisNodesInfo() = default;
459
2
    DorisNodesInfo(const TPaloNodesInfo& t_nodes) {
460
2
        for (const auto& node : t_nodes.nodes) {
461
2
            _nodes.emplace(node.id, node);
462
2
        }
463
2
    }
464
0
    void setNodes(const TPaloNodesInfo& t_nodes) {
465
0
        _nodes.clear();
466
0
        for (const auto& node : t_nodes.nodes) {
467
0
            _nodes.emplace(node.id, node);
468
0
        }
469
0
    }
470
0
    const NodeInfo* find_node(int64_t id) const {
471
0
        auto it = _nodes.find(id);
472
0
        if (it != std::end(_nodes)) {
473
0
            return &it->second;
474
0
        }
475
0
        return nullptr;
476
0
    }
477
478
0
    void add_nodes(const std::vector<TNodeInfo>& t_nodes) {
479
0
        for (const auto& node : t_nodes) {
480
0
            const auto* node_info = find_node(node.id);
481
0
            if (node_info == nullptr) {
482
0
                _nodes.emplace(node.id, node);
483
0
            }
484
0
        }
485
0
    }
486
487
0
    const std::unordered_map<int64_t, NodeInfo>& nodes_info() { return _nodes; }
488
489
private:
490
    std::unordered_map<int64_t, NodeInfo> _nodes;
491
};
492
493
} // namespace doris