Coverage Report

Created: 2025-12-15 21:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/olap/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 <mutex>
31
#include <ostream>
32
#include <sstream>
33
#include <string>
34
#include <typeinfo>
35
#include <unordered_map>
36
#include <unordered_set>
37
#include <utility>
38
39
#include "common/cast_set.h"
40
#include "common/config.h"
41
#include "common/exception.h"
42
#include "io/io_common.h"
43
#include "olap/inverted_index_stats.h"
44
#include "olap/olap_define.h"
45
#include "olap/rowset/rowset_fwd.h"
46
#include "util/countdown_latch.h"
47
#include "util/hash_util.hpp"
48
#include "util/time.h"
49
#include "util/uid_util.h"
50
51
namespace doris {
52
#include "common/compile_check_begin.h"
53
static constexpr int64_t MAX_ROWSET_ID = 1L << 56;
54
static constexpr int64_t LOW_56_BITS = 0x00ffffffffffffff;
55
56
using SchemaHash = int32_t;
57
using int128_t = __int128;
58
using uint128_t = unsigned __int128;
59
60
using TabletUid = UniqueId;
61
62
enum CompactionType { BASE_COMPACTION = 1, CUMULATIVE_COMPACTION = 2, FULL_COMPACTION = 3 };
63
64
enum DataDirType {
65
    SPILL_DISK_DIR,
66
    OLAP_DATA_DIR,
67
    DATA_CACHE_DIR,
68
};
69
70
struct DataDirInfo {
71
    std::string path;
72
    size_t path_hash = 0;
73
    int64_t disk_capacity = 1; // actual disk capacity
74
    int64_t available = 0;     // available space, in bytes unit
75
    int64_t local_used_capacity = 0;
76
    int64_t remote_used_capacity = 0;
77
    int64_t trash_used_capacity = 0;
78
    bool is_used = false;                                      // whether available mark
79
    TStorageMedium::type storage_medium = TStorageMedium::HDD; // Storage medium type: SSD|HDD
80
    DataDirType data_dir_type = DataDirType::OLAP_DATA_DIR;
81
    std::string metric_name;
82
};
83
84
// Sort DataDirInfo by available space.
85
struct DataDirInfoLessAvailability {
86
7
    bool operator()(const DataDirInfo& left, const DataDirInfo& right) const {
87
7
        return left.available < right.available;
88
7
    }
89
};
90
91
struct TabletInfo {
92
    TabletInfo(TTabletId in_tablet_id, UniqueId in_uid)
93
1.25k
            : tablet_id(in_tablet_id), tablet_uid(in_uid) {}
94
95
8.98k
    bool operator<(const TabletInfo& right) const {
96
8.98k
        if (tablet_id != right.tablet_id) {
97
7.74k
            return tablet_id < right.tablet_id;
98
7.74k
        } else {
99
1.24k
            return tablet_uid < right.tablet_uid;
100
1.24k
        }
101
8.98k
    }
102
103
64
    std::string to_string() const {
104
64
        std::stringstream ss;
105
64
        ss << tablet_id << "." << tablet_uid.to_string();
106
64
        return ss.str();
107
64
    }
108
109
    TTabletId tablet_id;
110
    UniqueId tablet_uid;
111
};
112
113
struct TabletSize {
114
    TabletSize(TTabletId in_tablet_id, size_t in_tablet_size)
115
0
            : tablet_id(in_tablet_id), tablet_size(in_tablet_size) {}
116
117
    TTabletId tablet_id;
118
    size_t tablet_size;
119
};
120
121
// Define all data types supported by Field.
122
// If new filed_type is defined, not only new TypeInfo may need be defined,
123
// but also some functions like get_type_info in types.cpp need to be changed.
124
enum class FieldType {
125
    OLAP_FIELD_TYPE_TINYINT = 1, // MYSQL_TYPE_TINY
126
    OLAP_FIELD_TYPE_UNSIGNED_TINYINT = 2,
127
    OLAP_FIELD_TYPE_SMALLINT = 3, // MYSQL_TYPE_SHORT
128
    OLAP_FIELD_TYPE_UNSIGNED_SMALLINT = 4,
129
    OLAP_FIELD_TYPE_INT = 5, // MYSQL_TYPE_LONG
130
    OLAP_FIELD_TYPE_UNSIGNED_INT = 6,
131
    OLAP_FIELD_TYPE_BIGINT = 7, // MYSQL_TYPE_LONGLONG
132
    OLAP_FIELD_TYPE_UNSIGNED_BIGINT = 8,
133
    OLAP_FIELD_TYPE_LARGEINT = 9,
134
    OLAP_FIELD_TYPE_FLOAT = 10,  // MYSQL_TYPE_FLOAT
135
    OLAP_FIELD_TYPE_DOUBLE = 11, // MYSQL_TYPE_DOUBLE
136
    OLAP_FIELD_TYPE_DISCRETE_DOUBLE = 12,
137
    OLAP_FIELD_TYPE_CHAR = 13,     // MYSQL_TYPE_STRING
138
    OLAP_FIELD_TYPE_DATE = 14,     // MySQL_TYPE_NEWDATE
139
    OLAP_FIELD_TYPE_DATETIME = 15, // MySQL_TYPE_DATETIME
140
    OLAP_FIELD_TYPE_DECIMAL = 16,  // DECIMAL, using different store format against MySQL
141
    OLAP_FIELD_TYPE_VARCHAR = 17,
142
143
    OLAP_FIELD_TYPE_STRUCT = 18,  // Struct
144
    OLAP_FIELD_TYPE_ARRAY = 19,   // ARRAY
145
    OLAP_FIELD_TYPE_MAP = 20,     // Map
146
    OLAP_FIELD_TYPE_UNKNOWN = 21, // UNKNOW OLAP_FIELD_TYPE_STRING
147
    OLAP_FIELD_TYPE_NONE = 22,
148
    OLAP_FIELD_TYPE_HLL = 23,
149
    OLAP_FIELD_TYPE_BOOL = 24,
150
    OLAP_FIELD_TYPE_BITMAP = 25,
151
    OLAP_FIELD_TYPE_STRING = 26,
152
    OLAP_FIELD_TYPE_QUANTILE_STATE = 27,
153
    OLAP_FIELD_TYPE_DATEV2 = 28,
154
    OLAP_FIELD_TYPE_DATETIMEV2 = 29,
155
    OLAP_FIELD_TYPE_TIMEV2 = 30,
156
    OLAP_FIELD_TYPE_DECIMAL32 = 31,
157
    OLAP_FIELD_TYPE_DECIMAL64 = 32,
158
    OLAP_FIELD_TYPE_DECIMAL128I = 33,
159
    OLAP_FIELD_TYPE_JSONB = 34,
160
    OLAP_FIELD_TYPE_VARIANT = 35,
161
    OLAP_FIELD_TYPE_AGG_STATE = 36,
162
    OLAP_FIELD_TYPE_DECIMAL256 = 37,
163
    OLAP_FIELD_TYPE_IPV4 = 38,
164
    OLAP_FIELD_TYPE_IPV6 = 39,
165
    OLAP_FIELD_TYPE_TIMESTAMPTZ = 40,
166
};
167
168
// Define all aggregation methods supported by Field
169
// Note that in practice, not all types can use all the following aggregation methods
170
// For example, it is meaningless to use SUM for the string type (but it will not cause the program to crash)
171
// The implementation of the Field class does not perform such checks, and should be constrained when creating the table
172
enum class FieldAggregationMethod {
173
    OLAP_FIELD_AGGREGATION_NONE = 0,
174
    OLAP_FIELD_AGGREGATION_SUM = 1,
175
    OLAP_FIELD_AGGREGATION_MIN = 2,
176
    OLAP_FIELD_AGGREGATION_MAX = 3,
177
    OLAP_FIELD_AGGREGATION_REPLACE = 4,
178
    OLAP_FIELD_AGGREGATION_HLL_UNION = 5,
179
    OLAP_FIELD_AGGREGATION_UNKNOWN = 6,
180
    OLAP_FIELD_AGGREGATION_BITMAP_UNION = 7,
181
    // Replace if and only if added value is not null
182
    OLAP_FIELD_AGGREGATION_REPLACE_IF_NOT_NULL = 8,
183
    OLAP_FIELD_AGGREGATION_QUANTILE_UNION = 9,
184
    OLAP_FIELD_AGGREGATION_GENERIC = 10
185
};
186
187
enum class PushType {
188
    PUSH_NORMAL = 1,          // for broker/hadoop load, not used any more
189
    PUSH_FOR_DELETE = 2,      // for delete
190
    PUSH_FOR_LOAD_DELETE = 3, // not used any more
191
    PUSH_NORMAL_V2 = 4,       // for spark load
192
};
193
194
426
constexpr bool field_is_slice_type(const FieldType& field_type) {
195
426
    return field_type == FieldType::OLAP_FIELD_TYPE_VARCHAR ||
196
426
           field_type == FieldType::OLAP_FIELD_TYPE_CHAR ||
197
426
           field_type == FieldType::OLAP_FIELD_TYPE_STRING;
198
426
}
199
200
15
constexpr bool field_is_numeric_type(const FieldType& field_type) {
201
15
    return field_type == FieldType::OLAP_FIELD_TYPE_INT ||
202
15
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_INT ||
203
15
           field_type == FieldType::OLAP_FIELD_TYPE_BIGINT ||
204
15
           field_type == FieldType::OLAP_FIELD_TYPE_SMALLINT ||
205
15
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_TINYINT ||
206
15
           field_type == FieldType::OLAP_FIELD_TYPE_UNSIGNED_SMALLINT ||
207
15
           field_type == FieldType::OLAP_FIELD_TYPE_TINYINT ||
208
15
           field_type == FieldType::OLAP_FIELD_TYPE_DOUBLE ||
209
15
           field_type == FieldType::OLAP_FIELD_TYPE_FLOAT ||
210
15
           field_type == FieldType::OLAP_FIELD_TYPE_DATE ||
211
15
           field_type == FieldType::OLAP_FIELD_TYPE_DATEV2 ||
212
15
           field_type == FieldType::OLAP_FIELD_TYPE_DATETIME ||
213
15
           field_type == FieldType::OLAP_FIELD_TYPE_DATETIMEV2 ||
214
15
           field_type == FieldType::OLAP_FIELD_TYPE_TIMESTAMPTZ ||
215
15
           field_type == FieldType::OLAP_FIELD_TYPE_LARGEINT ||
216
15
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL ||
217
15
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL32 ||
218
15
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL64 ||
219
15
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL128I ||
220
15
           field_type == FieldType::OLAP_FIELD_TYPE_DECIMAL256 ||
221
15
           field_type == FieldType::OLAP_FIELD_TYPE_BOOL ||
222
15
           field_type == FieldType::OLAP_FIELD_TYPE_IPV4 ||
223
15
           field_type == FieldType::OLAP_FIELD_TYPE_IPV6;
224
15
}
225
226
// <start_version_id, end_version_id>, such as <100, 110>
227
//using Version = std::pair<TupleVersion, TupleVersion>;
228
229
struct Version {
230
    int64_t first;
231
    int64_t second;
232
233
1.87M
    Version(int64_t first_, int64_t second_) : first(first_), second(second_) {}
234
8.40k
    Version() : first(0), second(0) {}
235
236
0
    static Version mock() {
237
        // 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
238
        // The version number takes a BIG NUMBER plus the version number of the current SchemaChange
239
0
        return Version(1 << 28, 1 << 29);
240
0
    }
241
242
    friend std::ostream& operator<<(std::ostream& os, const Version& version);
243
244
447
    bool operator!=(const Version& rhs) const { return first != rhs.first || second != rhs.second; }
245
246
360k
    bool operator==(const Version& rhs) const { return first == rhs.first && second == rhs.second; }
247
248
357k
    bool contains(const Version& other) const {
249
357k
        return first <= other.first && second >= other.second;
250
357k
    }
251
252
1.51k
    std::string to_string() const { return fmt::format("[{}-{}]", first, second); }
253
};
254
255
using Versions = std::vector<Version>;
256
257
426
inline std::ostream& operator<<(std::ostream& os, const Version& version) {
258
426
    return os << version.to_string();
259
426
}
260
261
0
inline std::ostream& operator<<(std::ostream& os, const Versions& versions) {
262
0
    for (auto& version : versions) {
263
0
        os << version;
264
0
    }
265
0
    return os;
266
0
}
267
268
// used for hash-struct of hash_map<Version, Rowset*>.
269
struct HashOfVersion {
270
38.3k
    size_t operator()(const Version& version) const {
271
38.3k
        size_t seed = 0;
272
38.3k
        seed = HashUtil::hash64(&version.first, sizeof(version.first), seed);
273
38.3k
        seed = HashUtil::hash64(&version.second, sizeof(version.second), seed);
274
38.3k
        return seed;
275
38.3k
    }
276
};
277
278
// It is used to represent Graph vertex.
279
struct Vertex {
280
    int64_t value = 0;
281
    std::list<int64_t> edges;
282
283
12.4k
    Vertex(int64_t v) : value(v) {}
284
};
285
286
class Field;
287
class WrapperField;
288
using KeyRange = std::pair<WrapperField*, WrapperField*>;
289
290
// ReaderStatistics used to collect statistics when scan data from storage
291
struct OlapReaderStatistics {
292
    int64_t io_ns = 0;
293
    int64_t compressed_bytes_read = 0;
294
295
    int64_t decompress_ns = 0;
296
    int64_t uncompressed_bytes_read = 0;
297
298
    // total read bytes in memory
299
    int64_t bytes_read = 0;
300
301
    int64_t block_fetch_ns = 0; // time of rowset reader's `next_batch()` call
302
    int64_t block_load_ns = 0;
303
    int64_t blocks_load = 0;
304
    // Not used any more, will be removed after non-vectorized code is removed
305
    int64_t block_seek_num = 0;
306
    // Not used any more, will be removed after non-vectorized code is removed
307
    int64_t block_seek_ns = 0;
308
309
    // block_load_ns
310
    //      block_init_ns
311
    //          block_init_seek_ns
312
    //          generate_row_ranges_ns
313
    //      predicate_column_read_ns
314
    //          predicate_column_read_seek_ns
315
    //      lazy_read_ns
316
    //          block_lazy_read_seek_ns
317
    int64_t block_init_ns = 0;
318
    int64_t block_init_seek_num = 0;
319
    int64_t block_init_seek_ns = 0;
320
    int64_t predicate_column_read_ns = 0;
321
    int64_t non_predicate_read_ns = 0;
322
    int64_t predicate_column_read_seek_num = 0;
323
    int64_t predicate_column_read_seek_ns = 0;
324
    int64_t lazy_read_ns = 0;
325
    int64_t block_lazy_read_seek_num = 0;
326
    int64_t block_lazy_read_seek_ns = 0;
327
328
    int64_t raw_rows_read = 0;
329
330
    int64_t rows_vec_cond_filtered = 0;
331
    int64_t rows_short_circuit_cond_filtered = 0;
332
    int64_t rows_expr_cond_filtered = 0;
333
    int64_t vec_cond_input_rows = 0;
334
    int64_t short_circuit_cond_input_rows = 0;
335
    int64_t expr_cond_input_rows = 0;
336
    int64_t rows_vec_del_cond_filtered = 0;
337
    int64_t vec_cond_ns = 0;
338
    int64_t short_cond_ns = 0;
339
    int64_t expr_filter_ns = 0;
340
    int64_t output_col_ns = 0;
341
    int64_t rows_key_range_filtered = 0;
342
    int64_t rows_stats_filtered = 0;
343
    int64_t rows_stats_rp_filtered = 0;
344
    int64_t rows_bf_filtered = 0;
345
    int64_t segment_dict_filtered = 0;
346
    // Including the number of rows filtered out according to the Delete information in the Tablet,
347
    // and the number of rows filtered for marked deleted rows under the unique key model.
348
    // This metric is mainly used to record the number of rows filtered by the delete condition in Segment V1,
349
    // and it is also used to record the replaced rows in the Unique key model in the "Reader" class.
350
    // In segmentv2, if you want to get all filtered rows, you need the sum of "rows_del_filtered" and "rows_conditions_filtered".
351
    int64_t rows_del_filtered = 0;
352
    int64_t rows_del_by_bitmap = 0;
353
    // the number of rows filtered by various column indexes.
354
    int64_t rows_conditions_filtered = 0;
355
    int64_t generate_row_ranges_by_keys_ns = 0;
356
    int64_t generate_row_ranges_by_column_conditions_ns = 0;
357
    int64_t generate_row_ranges_by_bf_ns = 0;
358
    int64_t generate_row_ranges_by_zonemap_ns = 0;
359
    int64_t generate_row_ranges_by_dict_ns = 0;
360
361
    int64_t index_load_ns = 0;
362
363
    int64_t total_pages_num = 0;
364
    int64_t cached_pages_num = 0;
365
366
    int64_t rows_inverted_index_filtered = 0;
367
    int64_t inverted_index_filter_timer = 0;
368
    int64_t inverted_index_query_timer = 0;
369
    int64_t inverted_index_query_cache_hit = 0;
370
    int64_t inverted_index_query_cache_miss = 0;
371
    int64_t inverted_index_query_null_bitmap_timer = 0;
372
    int64_t inverted_index_query_bitmap_copy_timer = 0;
373
    int64_t inverted_index_searcher_open_timer = 0;
374
    int64_t inverted_index_searcher_search_timer = 0;
375
    int64_t inverted_index_searcher_search_init_timer = 0;
376
    int64_t inverted_index_searcher_search_exec_timer = 0;
377
    int64_t inverted_index_searcher_cache_hit = 0;
378
    int64_t inverted_index_searcher_cache_miss = 0;
379
    int64_t inverted_index_downgrade_count = 0;
380
    int64_t inverted_index_analyzer_timer = 0;
381
    int64_t inverted_index_lookup_timer = 0;
382
    InvertedIndexStatistics inverted_index_stats;
383
384
    int64_t ann_index_load_ns = 0;
385
    int64_t ann_topn_search_ns = 0;
386
    int64_t ann_index_topn_search_cnt = 0;
387
388
    // Detailed timing for ANN operations
389
    int64_t ann_index_topn_engine_search_ns = 0;  // time spent in engine for range search
390
    int64_t ann_index_topn_result_process_ns = 0; // time spent processing TopN results
391
    int64_t ann_index_topn_engine_convert_ns = 0; // time spent on FAISS-side conversions (TopN)
392
    int64_t ann_index_topn_engine_prepare_ns =
393
            0; // time spent preparing before engine search (TopN)
394
    int64_t rows_ann_index_topn_filtered = 0;
395
396
    int64_t ann_index_range_search_ns = 0;
397
    int64_t ann_index_range_search_cnt = 0;
398
    // Detailed timing for ANN Range search
399
    int64_t ann_range_engine_search_ns = 0; // time spent in engine for range search
400
    int64_t ann_range_pre_process_ns = 0;   // time spent preparing before engine search
401
402
    int64_t ann_range_result_convert_ns = 0; // time spent processing range results
403
    int64_t ann_range_engine_convert_ns = 0; // time spent on FAISS-side conversions (Range)
404
    int64_t rows_ann_index_range_filtered = 0;
405
406
    int64_t output_index_result_column_timer = 0;
407
    // number of segment filtered by column stat when creating seg iterator
408
    int64_t filtered_segment_number = 0;
409
    // number of segment with condition cache hit
410
    int64_t condition_cache_hit_seg_nums = 0;
411
    // number of rows filtered by condition cache hit
412
    int64_t condition_cache_filtered_rows = 0;
413
    // total number of segment
414
    int64_t total_segment_number = 0;
415
416
    io::FileCacheStatistics file_cache_stats;
417
    int64_t load_segments_timer = 0;
418
419
    int64_t collect_iterator_merge_next_timer = 0;
420
    int64_t collect_iterator_normal_next_timer = 0;
421
    int64_t delete_bitmap_get_agg_ns = 0;
422
423
    int64_t tablet_reader_init_timer_ns = 0;
424
    int64_t tablet_reader_capture_rs_readers_timer_ns = 0;
425
    int64_t tablet_reader_init_return_columns_timer_ns = 0;
426
    int64_t tablet_reader_init_keys_param_timer_ns = 0;
427
    int64_t tablet_reader_init_orderby_keys_param_timer_ns = 0;
428
    int64_t tablet_reader_init_conditions_param_timer_ns = 0;
429
    int64_t tablet_reader_init_delete_condition_param_timer_ns = 0;
430
    int64_t block_reader_vcollect_iter_init_timer_ns = 0;
431
    int64_t block_reader_rs_readers_init_timer_ns = 0;
432
    int64_t block_reader_build_heap_init_timer_ns = 0;
433
434
    int64_t rowset_reader_get_segment_iterators_timer_ns = 0;
435
    int64_t rowset_reader_create_iterators_timer_ns = 0;
436
    int64_t rowset_reader_init_iterators_timer_ns = 0;
437
    int64_t rowset_reader_load_segments_timer_ns = 0;
438
439
    int64_t segment_iterator_init_timer_ns = 0;
440
    int64_t segment_iterator_init_return_column_iterators_timer_ns = 0;
441
    int64_t segment_iterator_init_index_iterators_timer_ns = 0;
442
443
    int64_t segment_create_column_readers_timer_ns = 0;
444
    int64_t segment_load_index_timer_ns = 0;
445
446
    int64_t variant_scan_sparse_column_timer_ns = 0;
447
    int64_t variant_scan_sparse_column_bytes = 0;
448
    int64_t variant_fill_path_from_sparse_column_timer_ns = 0;
449
    int64_t variant_subtree_default_iter_count = 0;
450
    int64_t variant_subtree_leaf_iter_count = 0;
451
    int64_t variant_subtree_hierarchical_iter_count = 0;
452
    int64_t variant_subtree_sparse_iter_count = 0;
453
};
454
455
using ColumnId = uint32_t;
456
// Column unique id set
457
using UniqueIdSet = std::set<uint32_t>;
458
// Column unique Id -> column id map
459
using UniqueIdToColumnIdMap = std::map<ColumnId, ColumnId>;
460
461
// 8 bit rowset id version
462
// 56 bit, inc number from 1
463
// 128 bit backend uid, it is a uuid bit, id version
464
struct RowsetId {
465
    int8_t version = 0;
466
    int64_t hi = 0;
467
    int64_t mi = 0;
468
    int64_t lo = 0;
469
470
11.4k
    void init(std::string_view rowset_id_str) {
471
        // for new rowsetid its a 48 hex string
472
        // if the len < 48, then it is an old format rowset id
473
11.4k
        if (rowset_id_str.length() < 48) [[unlikely]] {
474
71
            int64_t high;
475
71
            auto [_, ec] = std::from_chars(rowset_id_str.data(),
476
71
                                           rowset_id_str.data() + rowset_id_str.length(), high);
477
71
            if (ec != std::errc {}) [[unlikely]] {
478
1
                if (config::force_regenerate_rowsetid_on_start_error) {
479
1
                    LOG(WARNING) << "failed to init rowset id: " << rowset_id_str;
480
1
                    high = MAX_ROWSET_ID - 1;
481
1
                } else {
482
0
                    throw Exception(
483
0
                            Status::FatalError("failed to init rowset id: {}", rowset_id_str));
484
0
                }
485
1
            }
486
71
            init(1, high, 0, 0);
487
11.3k
        } else {
488
11.3k
            int64_t high = 0;
489
11.3k
            int64_t middle = 0;
490
11.3k
            int64_t low = 0;
491
11.3k
            from_hex(&high, rowset_id_str.substr(0, 16));
492
11.3k
            from_hex(&middle, rowset_id_str.substr(16, 16));
493
11.3k
            from_hex(&low, rowset_id_str.substr(32, 16));
494
11.3k
            init(high >> 56, high & LOW_56_BITS, middle, low);
495
11.3k
        }
496
11.4k
    }
497
498
    // to compatible with old version
499
1.19k
    void init(int64_t rowset_id) { init(1, rowset_id, 0, 0); }
500
501
30.3k
    void init(int64_t id_version, int64_t high, int64_t middle, int64_t low) {
502
30.3k
        version = cast_set<int8_t>(id_version);
503
30.3k
        if (UNLIKELY(high >= MAX_ROWSET_ID)) {
504
0
            throw Exception(Status::FatalError("inc rowsetid is too large:{}", high));
505
0
        }
506
30.3k
        hi = (id_version << 56) + (high & LOW_56_BITS);
507
30.3k
        mi = middle;
508
30.3k
        lo = low;
509
30.3k
    }
510
511
50.3k
    std::string to_string() const {
512
50.3k
        if (version < 2) {
513
27.9k
            return std::to_string(hi & LOW_56_BITS);
514
27.9k
        } else {
515
22.3k
            char buf[48];
516
22.3k
            to_hex(hi, buf);
517
22.3k
            to_hex(mi, buf + 16);
518
22.3k
            to_hex(lo, buf + 32);
519
22.3k
            return {buf, 48};
520
22.3k
        }
521
50.3k
    }
522
523
    // std::unordered_map need this api
524
358k
    bool operator==(const RowsetId& rhs) const {
525
358k
        return hi == rhs.hi && mi == rhs.mi && lo == rhs.lo;
526
358k
    }
527
528
143
    bool operator!=(const RowsetId& rhs) const {
529
143
        return hi != rhs.hi || mi != rhs.mi || lo != rhs.lo;
530
143
    }
531
532
45.0M
    bool operator<(const RowsetId& rhs) const {
533
45.0M
        if (hi != rhs.hi) {
534
4.58M
            return hi < rhs.hi;
535
40.4M
        } else if (mi != rhs.mi) {
536
0
            return mi < rhs.mi;
537
40.4M
        } else {
538
40.4M
            return lo < rhs.lo;
539
40.4M
        }
540
45.0M
    }
541
542
1.70k
    friend std::ostream& operator<<(std::ostream& out, const RowsetId& rowset_id) {
543
1.70k
        out << rowset_id.to_string();
544
1.70k
        return out;
545
1.70k
    }
546
};
547
548
using RowsetIdUnorderedSet = std::unordered_set<RowsetId>;
549
550
// Extract rowset id from filename, return uninitialized rowset id if filename is invalid
551
483
inline RowsetId extract_rowset_id(std::string_view filename) {
552
483
    RowsetId rowset_id;
553
483
    if (filename.ends_with(".dat")) {
554
        // filename format: {rowset_id}_{segment_num}.dat
555
243
        auto end = filename.find('_');
556
243
        if (end == std::string::npos) {
557
0
            return rowset_id;
558
0
        }
559
243
        rowset_id.init(filename.substr(0, end));
560
243
        return rowset_id;
561
243
    }
562
240
    if (filename.ends_with(".idx")) {
563
        // filename format: {rowset_id}_{segment_num}_{index_id}.idx
564
240
        auto end = filename.find('_');
565
240
        if (end == std::string::npos) {
566
0
            return rowset_id;
567
0
        }
568
240
        rowset_id.init(filename.substr(0, end));
569
240
        return rowset_id;
570
240
    }
571
0
    return rowset_id;
572
240
}
573
574
class DeleteBitmap;
575
576
struct CalcDeleteBitmapTask {
577
    std::mutex m;
578
    Status status {Status::OK()};
579
    CountDownLatch latch {1};
580
581
123
    void set_status(Status st) {
582
123
        {
583
123
            std::unique_lock l(m);
584
123
            status = std::move(st);
585
123
        }
586
123
        latch.count_down(1);
587
123
    }
588
589
52
    Status get_status() {
590
52
        if (!latch.wait_for(
591
52
                    std::chrono::seconds(config::segcompaction_wait_for_dbm_task_timeout_s))) {
592
0
            return Status::InternalError<false>("wait for calc delete bitmap task timeout");
593
52
        };
594
52
        std::unique_lock l(m);
595
52
        return status;
596
52
    }
597
};
598
599
// merge on write context
600
struct MowContext {
601
    MowContext(int64_t version, int64_t txnid, std::shared_ptr<RowsetIdUnorderedSet> ids,
602
               std::vector<RowsetSharedPtr> rowset_ptrs, std::shared_ptr<DeleteBitmap> db)
603
8
            : max_version(version),
604
8
              txn_id(txnid),
605
8
              rowset_ids(std::move(ids)),
606
8
              rowset_ptrs(std::move(rowset_ptrs)),
607
8
              delete_bitmap(std::move(db)) {}
608
609
175
    CalcDeleteBitmapTask* get_calc_dbm_task(int32_t segment_id) {
610
175
        std::lock_guard l(m);
611
175
        return &calc_dbm_tasks[segment_id];
612
175
    }
613
614
    int64_t max_version;
615
    int64_t txn_id;
616
    std::shared_ptr<RowsetIdUnorderedSet> rowset_ids;
617
    std::vector<RowsetSharedPtr> rowset_ptrs;
618
    std::shared_ptr<DeleteBitmap> delete_bitmap;
619
620
    std::mutex m;
621
    // status of calc delete bitmap task in flush phase
622
    std::unordered_map<int32_t /* origin seg id*/, CalcDeleteBitmapTask> calc_dbm_tasks;
623
};
624
625
// used for controll compaction
626
struct VersionWithTime {
627
    std::atomic<int64_t> version;
628
    int64_t update_ts;
629
630
55
    VersionWithTime() : version(0), update_ts(MonotonicMillis()) {}
631
632
0
    void update_version_monoto(int64_t new_version) {
633
0
        int64_t cur_version = version.load(std::memory_order_relaxed);
634
0
        while (cur_version < new_version) {
635
0
            if (version.compare_exchange_strong(cur_version, new_version, std::memory_order_relaxed,
636
0
                                                std::memory_order_relaxed)) {
637
0
                update_ts = MonotonicMillis();
638
0
                break;
639
0
            }
640
0
        }
641
0
    }
642
};
643
#include "common/compile_check_end.h"
644
} // namespace doris
645
646
// This intended to be a "good" hash function.  It may change from time to time.
647
template <>
648
struct std::hash<doris::RowsetId> {
649
3.30k
    size_t operator()(const doris::RowsetId& rowset_id) const {
650
3.30k
        size_t seed = 0;
651
3.30k
        seed = doris::HashUtil::xxHash64WithSeed((const char*)&rowset_id.hi, sizeof(rowset_id.hi),
652
3.30k
                                                 seed);
653
3.30k
        seed = doris::HashUtil::xxHash64WithSeed((const char*)&rowset_id.mi, sizeof(rowset_id.mi),
654
3.30k
                                                 seed);
655
3.30k
        seed = doris::HashUtil::xxHash64WithSeed((const char*)&rowset_id.lo, sizeof(rowset_id.lo),
656
3.30k
                                                 seed);
657
3.30k
        return seed;
658
3.30k
    }
659
};