Coverage Report

Created: 2026-08-19 01:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/olap_common.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 <gen_cpp/Types_types.h>
21
#include <netinet/in.h>
22
23
#include <atomic>
24
#include <charconv>
25
#include <cstdint>
26
#include <functional>
27
#include <list>
28
#include <map>
29
#include <memory>
30
#include <ostream>
31
#include <sstream>
32
#include <string>
33
#include <typeinfo>
34
#include <unordered_map>
35
#include <unordered_set>
36
#include <utility>
37
38
#include "common/cast_set.h"
39
#include "common/config.h"
40
#include "common/exception.h"
41
#include "core/extended_types.h"
42
#include "io/io_common.h"
43
#include "storage/field_type.h"
44
#include "storage/index/inverted/inverted_index_stats.h"
45
#include "storage/index/snii/snii_query_stats.h"
46
#include "storage/olap_define.h"
47
#include "storage/rowset/rowset_fwd.h"
48
#include "storage/rowset_id.h"
49
#include "util/hash_util.hpp"
50
#include "util/time.h"
51
#include "util/uid_util.h"
52
53
namespace doris {
54
using SchemaHash = int32_t;
55
56
using TabletUid = UniqueId;
57
58
enum CompactionType {
59
    BASE_COMPACTION = 1,
60
    CUMULATIVE_COMPACTION = 2,
61
    FULL_COMPACTION = 3,
62
    // Only used by scheduler to route row-binlog tablets to the binlog thread pool.
63
    CUMU_BINLOG_COMPACTION = 4
64
};
65
66
struct CompactionScoreStats {
67
    int64_t max_score = 0;
68
    int64_t size_based_max_score = 0;
69
    int64_t time_series_max_score = 0;
70
    bool scanned = false;
71
};
72
73
enum DataDirType {
74
    SPILL_DISK_DIR,
75
    OLAP_DATA_DIR,
76
    DATA_CACHE_DIR,
77
};
78
79
struct DataDirInfo {
80
    std::string path;
81
    size_t path_hash = 0;
82
    int64_t disk_capacity = 1; // actual disk capacity
83
    int64_t available = 0;     // available space, in bytes unit
84
    int64_t local_used_capacity = 0;
85
    int64_t remote_used_capacity = 0;
86
    int64_t trash_used_capacity = 0;
87
    bool is_used = false;                                      // whether available mark
88
    TStorageMedium::type storage_medium = TStorageMedium::HDD; // Storage medium type: SSD|HDD
89
    DataDirType data_dir_type = DataDirType::OLAP_DATA_DIR;
90
    std::string metric_name;
91
};
92
93
// Sort DataDirInfo by available space.
94
struct DataDirInfoLessAvailability {
95
7
    bool operator()(const DataDirInfo& left, const DataDirInfo& right) const {
96
7
        return left.available < right.available;
97
7
    }
98
};
99
100
struct TabletInfo {
101
    TabletInfo(TTabletId in_tablet_id, UniqueId in_uid)
102
939
            : tablet_id(in_tablet_id), tablet_uid(in_uid) {}
103
104
5.21k
    bool operator<(const TabletInfo& right) const {
105
5.21k
        if (tablet_id != right.tablet_id) {
106
4.33k
            return tablet_id < right.tablet_id;
107
4.33k
        } else {
108
884
            return tablet_uid < right.tablet_uid;
109
884
        }
110
5.21k
    }
111
112
44
    std::string to_string() const {
113
44
        std::stringstream ss;
114
44
        ss << tablet_id << "." << tablet_uid.to_string();
115
44
        return ss.str();
116
44
    }
117
118
    TTabletId tablet_id;
119
    UniqueId tablet_uid;
120
};
121
122
struct TabletSize {
123
    TabletSize(TTabletId in_tablet_id, size_t in_tablet_size)
124
0
            : tablet_id(in_tablet_id), tablet_size(in_tablet_size) {}
125
126
    TTabletId tablet_id;
127
    size_t tablet_size;
128
};
129
130
// FieldType moved to storage/field_type.h (included above) so that
131
// data-type headers can name storage cell types without pulling in the
132
// whole of olap_common.h.
133
134
// Define all aggregation methods supported by TabletColumn
135
// Note that in practice, not all types can use all the following aggregation methods
136
// For example, it is meaningless to use SUM for the string type (but it will not cause the program to crash)
137
// The implementation of the TabletColumn class does not perform such checks, and should be constrained when creating the table
138
enum class FieldAggregationMethod {
139
    OLAP_FIELD_AGGREGATION_NONE = 0,
140
    OLAP_FIELD_AGGREGATION_SUM = 1,
141
    OLAP_FIELD_AGGREGATION_MIN = 2,
142
    OLAP_FIELD_AGGREGATION_MAX = 3,
143
    OLAP_FIELD_AGGREGATION_REPLACE = 4,
144
    OLAP_FIELD_AGGREGATION_HLL_UNION = 5,
145
    OLAP_FIELD_AGGREGATION_UNKNOWN = 6,
146
    OLAP_FIELD_AGGREGATION_BITMAP_UNION = 7,
147
    // Replace if and only if added value is not null
148
    OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL = 8,
149
    OLAP_FIELD_AGGREGATION_QUANTILE_UNION = 9,
150
    OLAP_FIELD_AGGREGATION_GENERIC = 10
151
};
152
153
enum class PushType {
154
    PUSH_NORMAL = 1,          // for broker/hadoop load, not used any more
155
    PUSH_FOR_DELETE = 2,      // for delete
156
    PUSH_FOR_LOAD_DELETE = 3, // not used any more
157
    PUSH_NORMAL_V2 = 4,       // for spark load
158
};
159
160
// <start_version_id, end_version_id>, such as <100, 110>
161
//using Version = std::pair<TupleVersion, TupleVersion>;
162
163
struct Version {
164
    int64_t first;
165
    int64_t second;
166
167
2.24M
    Version(int64_t first_, int64_t second_) : first(first_), second(second_) {}
168
7.60k
    Version() : first(0), second(0) {}
169
170
0
    static Version mock() {
171
        // Every time SchemaChange is used for external rowing, some temporary versions (such as 999, 1000, 1001) will be written, in order to avoid Cache conflicts, temporary
172
        // The version number takes a BIG NUMBER plus the version number of the current SchemaChange
173
0
        return Version(1 << 28, 1 << 29);
174
0
    }
175
176
    friend std::ostream& operator<<(std::ostream& os, const Version& version);
177
178
570
    bool operator!=(const Version& rhs) const { return first != rhs.first || second != rhs.second; }
179
180
507k
    bool operator==(const Version& rhs) const { return first == rhs.first && second == rhs.second; }
181
182
171k
    bool contains(const Version& other) const {
183
171k
        return first <= other.first && second >= other.second;
184
171k
    }
185
186
2.09k
    std::string to_string() const { return fmt::format("[{}-{}]", first, second); }
187
};
188
189
struct TsoRange : public Version {
190
7.43k
    TsoRange() : Version(-1, -1) {}
191
1.71k
    TsoRange(int64_t start_tso, int64_t end_tso) : Version(start_tso, end_tso) {}
192
193
171
    int64_t start_tso() const { return first; }
194
8.14k
    int64_t end_tso() const { return second; }
195
196
0
    bool contains(const TsoRange& other) const { return Version::contains(other); }
197
};
198
199
using Versions = std::vector<Version>;
200
201
717
inline std::ostream& operator<<(std::ostream& os, const Version& version) {
202
717
    return os << version.to_string();
203
717
}
204
205
0
inline std::ostream& operator<<(std::ostream& os, const Versions& versions) {
206
0
    for (auto& version : versions) {
207
0
        os << version;
208
0
    }
209
0
    return os;
210
0
}
211
212
// used for hash-struct of hash_map<Version, Rowset*>.
213
struct HashOfVersion {
214
30.7k
    size_t operator()(const Version& version) const {
215
30.7k
        size_t seed = 0;
216
30.7k
        seed = HashUtil::hash64(&version.first, sizeof(version.first), seed);
217
30.7k
        seed = HashUtil::hash64(&version.second, sizeof(version.second), seed);
218
30.7k
        return seed;
219
30.7k
    }
220
};
221
222
// It is used to represent Graph vertex.
223
struct Vertex {
224
    int64_t value = 0;
225
    std::list<int64_t> edges;
226
227
10.8k
    Vertex(int64_t v) : value(v) {}
228
};
229
230
// ReaderStatistics used to collect statistics when scan data from storage
231
struct OlapReaderStatistics {
232
    int64_t io_ns = 0;
233
    int64_t compressed_bytes_read = 0;
234
235
    int64_t decompress_ns = 0;
236
    int64_t uncompressed_bytes_read = 0;
237
238
    // total read bytes in memory
239
    int64_t bytes_read = 0;
240
241
    int64_t block_fetch_ns = 0; // time of rowset reader's `next_batch()` call
242
    int64_t block_load_ns = 0;
243
    int64_t blocks_load = 0;
244
    // Not used any more, will be removed after non-vectorized code is removed
245
    int64_t block_seek_num = 0;
246
    // Not used any more, will be removed after non-vectorized code is removed
247
    int64_t block_seek_ns = 0;
248
249
    // block_load_ns
250
    //      block_init_ns
251
    //          block_init_seek_ns
252
    //          generate_row_ranges_ns
253
    //      predicate_column_read_ns
254
    //          predicate_column_read_seek_ns
255
    //      lazy_read_ns
256
    //          block_lazy_read_seek_ns
257
    int64_t block_init_ns = 0;
258
    int64_t block_init_seek_num = 0;
259
    int64_t block_init_seek_ns = 0;
260
    int64_t predicate_column_read_ns = 0;
261
    int64_t non_predicate_read_ns = 0;
262
    int64_t predicate_column_read_seek_num = 0;
263
    int64_t predicate_column_read_seek_ns = 0;
264
    int64_t lazy_read_ns = 0;
265
    int64_t block_lazy_read_seek_num = 0;
266
    int64_t block_lazy_read_seek_ns = 0;
267
    int64_t lazy_read_pruned_ns = 0;
268
269
    int64_t raw_rows_read = 0;
270
271
    int64_t rows_vec_cond_filtered = 0;
272
    int64_t rows_short_circuit_cond_filtered = 0;
273
    int64_t rows_expr_cond_filtered = 0;
274
    int64_t vec_cond_input_rows = 0;
275
    int64_t short_circuit_cond_input_rows = 0;
276
    int64_t expr_cond_input_rows = 0;
277
    int64_t rows_vec_del_cond_filtered = 0;
278
    int64_t vec_cond_ns = 0;
279
    int64_t short_cond_ns = 0;
280
    int64_t expr_filter_ns = 0;
281
    int64_t output_col_ns = 0;
282
    int64_t rows_key_range_filtered = 0;
283
    int64_t rows_stats_filtered = 0;
284
    int64_t rows_stats_rp_filtered = 0;
285
    int64_t expr_zonemap_filtered_segments = 0;
286
    int64_t expr_zonemap_filtered_pages = 0;
287
    int64_t expr_zonemap_unusable_evals = 0;
288
    int64_t in_zonemap_point_check_count = 0;
289
    int64_t in_zonemap_range_only_count = 0;
290
    int64_t rows_bf_filtered = 0;
291
    int64_t segment_dict_filtered = 0;
292
    // Including the number of rows filtered out according to the Delete information in the Tablet,
293
    // and the number of rows filtered for marked deleted rows under the unique key model.
294
    // This metric is mainly used to record the number of rows filtered by the delete condition in Segment V1,
295
    // and it is also used to record the replaced rows in the Unique key model in the "Reader" class.
296
    // In segmentv2, if you want to get all filtered rows, you need the sum of "rows_del_filtered" and "rows_conditions_filtered".
297
    int64_t rows_del_filtered = 0;
298
    int64_t rows_del_by_bitmap = 0;
299
    // the number of rows filtered by various column indexes.
300
    int64_t rows_conditions_filtered = 0;
301
    int64_t generate_row_ranges_by_keys_ns = 0;
302
    int64_t generate_row_ranges_by_column_conditions_ns = 0;
303
    int64_t generate_row_ranges_by_bf_ns = 0;
304
    int64_t generate_row_ranges_by_zonemap_ns = 0;
305
    int64_t generate_row_ranges_by_dict_ns = 0;
306
307
    int64_t index_load_ns = 0;
308
309
    int64_t total_pages_num = 0;
310
    int64_t cached_pages_num = 0;
311
312
    int64_t rows_inverted_index_filtered = 0;
313
    int64_t inverted_index_filter_timer = 0;
314
    int64_t inverted_index_query_timer = 0;
315
    int64_t inverted_index_query_cache_hit = 0;
316
    int64_t inverted_index_query_cache_miss = 0;
317
    int64_t inverted_index_query_cache_lookup = 0;
318
    int64_t inverted_index_query_cache_insert = 0;
319
    int64_t inverted_index_query_null_bitmap_timer = 0;
320
    int64_t inverted_index_query_bitmap_copy_timer = 0;
321
    int64_t inverted_index_searcher_open_timer = 0;
322
    int64_t inverted_index_searcher_search_timer = 0;
323
    int64_t inverted_index_searcher_search_init_timer = 0;
324
    int64_t inverted_index_searcher_search_exec_timer = 0;
325
    int64_t inverted_index_searcher_cache_hit = 0;
326
    int64_t inverted_index_searcher_cache_miss = 0;
327
    int64_t inverted_index_downgrade_count = 0;
328
    int64_t inverted_index_analyzer_timer = 0;
329
    int64_t inverted_index_lookup_timer = 0;
330
    // See snii_query_stats.h: one field here instead of one per SNII counter.
331
    snii::SniiQueryStats snii_stats;
332
    InvertedIndexStatistics inverted_index_stats;
333
334
    int64_t ann_index_load_ns = 0;
335
    int64_t ann_topn_search_ns = 0;
336
    int64_t ann_index_topn_search_cnt = 0;
337
    int64_t ann_ivf_on_disk_load_ns = 0;
338
    int64_t ann_ivf_on_disk_cache_hit_cnt = 0;
339
    int64_t ann_ivf_on_disk_cache_miss_cnt = 0;
340
    int64_t ann_index_cache_hits = 0;
341
342
    // Detailed timing for ANN operations
343
    int64_t ann_index_topn_engine_search_ns = 0;  // time spent in engine for range search
344
    int64_t ann_index_topn_result_process_ns = 0; // time spent processing TopN results
345
    int64_t ann_index_topn_engine_convert_ns = 0; // time spent on FAISS-side conversions (TopN)
346
    int64_t ann_index_topn_engine_prepare_ns =
347
            0; // time spent preparing before engine search (TopN)
348
    int64_t rows_ann_index_topn_filtered = 0;
349
350
    int64_t ann_index_range_search_ns = 0;
351
    int64_t ann_index_range_search_cnt = 0;
352
    // Detailed timing for ANN Range search
353
    int64_t ann_range_engine_search_ns = 0; // time spent in engine for range search
354
    int64_t ann_range_pre_process_ns = 0;   // time spent preparing before engine search
355
356
    int64_t ann_range_result_convert_ns = 0; // time spent processing range results
357
    int64_t ann_range_engine_convert_ns = 0; // time spent on FAISS-side conversions (Range)
358
    int64_t rows_ann_index_range_filtered = 0;
359
    int64_t ann_index_range_cache_hits = 0;
360
    int64_t ann_fall_back_brute_force_cnt = 0;
361
    int64_t ann_topn_fallback_by_small_candidate_cnt = 0;
362
    int64_t ann_topn_fallback_small_candidate_rows = 0;
363
    int64_t ann_range_fallback_by_small_candidate_cnt = 0;
364
    int64_t ann_range_fallback_small_candidate_rows = 0;
365
366
    int64_t output_index_result_column_timer = 0;
367
    // number of segment filtered by column stat when creating seg iterator
368
    int64_t filtered_segment_number = 0;
369
    // number of segment with condition cache hit
370
    int64_t condition_cache_hit_seg_nums = 0;
371
    // number of rows filtered by condition cache hit
372
    int64_t condition_cache_filtered_rows = 0;
373
    // total number of segment
374
    int64_t total_segment_number = 0;
375
376
    io::FileCacheStatistics file_cache_stats;
377
    int64_t load_segments_timer = 0;
378
379
    int64_t collect_iterator_merge_next_timer = 0;
380
    int64_t collect_iterator_normal_next_timer = 0;
381
    int64_t delete_bitmap_get_agg_ns = 0;
382
383
    int64_t tablet_reader_init_timer_ns = 0;
384
    int64_t tablet_reader_capture_rs_readers_timer_ns = 0;
385
    int64_t tablet_reader_init_return_columns_timer_ns = 0;
386
    int64_t tablet_reader_init_keys_param_timer_ns = 0;
387
    int64_t tablet_reader_init_orderby_keys_param_timer_ns = 0;
388
    int64_t tablet_reader_init_conditions_param_timer_ns = 0;
389
    int64_t tablet_reader_init_delete_condition_param_timer_ns = 0;
390
    int64_t block_reader_vcollect_iter_init_timer_ns = 0;
391
    int64_t block_reader_rs_readers_init_timer_ns = 0;
392
    int64_t block_reader_build_heap_init_timer_ns = 0;
393
394
    int64_t rowset_reader_get_segment_iterators_timer_ns = 0;
395
    int64_t rowset_reader_create_iterators_timer_ns = 0;
396
    int64_t rowset_reader_init_iterators_timer_ns = 0;
397
    int64_t rowset_reader_load_segments_timer_ns = 0;
398
399
    int64_t segment_iterator_init_timer_ns = 0;
400
    int64_t segment_iterator_init_return_column_iterators_timer_ns = 0;
401
    int64_t segment_iterator_init_index_iterators_timer_ns = 0;
402
    int64_t segment_iterator_init_segment_prefetchers_timer_ns = 0;
403
404
    int64_t segment_create_column_readers_timer_ns = 0;
405
    int64_t segment_load_index_timer_ns = 0;
406
407
    int64_t adaptive_batch_size_predict_min_rows = INT64_MAX;
408
    int64_t adaptive_batch_size_predict_max_rows = 0;
409
410
    int64_t variant_scan_sparse_column_timer_ns = 0;
411
    int64_t variant_scan_sparse_column_bytes = 0;
412
    int64_t variant_fill_path_from_sparse_column_timer_ns = 0;
413
    int64_t variant_subtree_default_iter_count = 0;
414
    int64_t variant_subtree_leaf_iter_count = 0;
415
    int64_t variant_subtree_hierarchical_iter_count = 0;
416
    int64_t variant_subtree_sparse_iter_count = 0;
417
    int64_t variant_doc_value_column_iter_count = 0;
418
};
419
420
using ColumnId = uint32_t;
421
// Column unique id set
422
using UniqueIdSet = std::set<uint32_t>;
423
// Column unique Id -> column id map
424
using UniqueIdToColumnIdMap = std::map<ColumnId, ColumnId>;
425
426
// RowsetId moved to storage/rowset_id.h (included above): core/column/column.h
427
// needs the complete type, and this way it gets it without the rest of
428
// olap_common.h.
429
430
using RowsetIdUnorderedSet = std::unordered_set<RowsetId>;
431
432
// Extract rowset id from filename, return uninitialized rowset id if filename is invalid
433
483
inline RowsetId extract_rowset_id(std::string_view filename) {
434
483
    RowsetId rowset_id;
435
483
    if (filename.ends_with(".dat")) {
436
        // filename format: {rowset_id}_{segment_num}.dat
437
243
        auto end = filename.find('_');
438
243
        if (end == std::string::npos) {
439
0
            return rowset_id;
440
0
        }
441
243
        rowset_id.init(filename.substr(0, end));
442
243
        return rowset_id;
443
243
    }
444
240
    if (filename.ends_with(".idx")) {
445
        // filename format: {rowset_id}_{segment_num}_{index_id}.idx
446
240
        auto end = filename.find('_');
447
240
        if (end == std::string::npos) {
448
0
            return rowset_id;
449
0
        }
450
240
        rowset_id.init(filename.substr(0, end));
451
240
        return rowset_id;
452
240
    }
453
0
    return rowset_id;
454
240
}
455
456
class DeleteBitmap;
457
// merge on write context
458
struct MowContext {
459
    MowContext(int64_t version, int64_t txnid, std::shared_ptr<RowsetIdUnorderedSet> ids,
460
               std::vector<RowsetSharedPtr> rowset_ptrs, std::shared_ptr<DeleteBitmap> db)
461
106
            : max_version(version),
462
106
              txn_id(txnid),
463
106
              rowset_ids(std::move(ids)),
464
106
              rowset_ptrs(std::move(rowset_ptrs)),
465
106
              delete_bitmap(std::move(db)) {}
466
    int64_t max_version;
467
    int64_t txn_id;
468
    std::shared_ptr<RowsetIdUnorderedSet> rowset_ids;
469
    std::vector<RowsetSharedPtr> rowset_ptrs;
470
    std::shared_ptr<DeleteBitmap> delete_bitmap;
471
};
472
473
// used for controll compaction
474
struct VersionWithTime {
475
    std::atomic<int64_t> version;
476
    int64_t update_ts;
477
478
69
    VersionWithTime() : version(0), update_ts(MonotonicMillis()) {}
479
480
0
    void update_version_monoto(int64_t new_version) {
481
0
        int64_t cur_version = version.load(std::memory_order_relaxed);
482
0
        while (cur_version < new_version) {
483
0
            if (version.compare_exchange_strong(cur_version, new_version, std::memory_order_relaxed,
484
0
                                                std::memory_order_relaxed)) {
485
0
                update_ts = MonotonicMillis();
486
0
                break;
487
0
            }
488
0
        }
489
0
    }
490
};
491
} // namespace doris