Coverage Report

Created: 2025-05-21 20:37

/root/doris/be/src/olap/tablet_meta.cpp
Line
Count
Source (jump to first uncovered line)
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
#include "olap/tablet_meta.h"
19
20
#include <gen_cpp/Descriptors_types.h>
21
#include <gen_cpp/Types_types.h>
22
#include <gen_cpp/olap_common.pb.h>
23
#include <gen_cpp/olap_file.pb.h>
24
#include <gen_cpp/segment_v2.pb.h>
25
#include <gen_cpp/types.pb.h>
26
#include <json2pb/pb_to_json.h>
27
#include <time.h>
28
29
#include <cstdint>
30
#include <memory>
31
#include <random>
32
#include <set>
33
#include <utility>
34
35
#include "cloud/cloud_meta_mgr.h"
36
#include "cloud/cloud_storage_engine.h"
37
#include "cloud/config.h"
38
#include "common/config.h"
39
#include "gutil/integral_types.h"
40
#include "io/fs/file_writer.h"
41
#include "io/fs/local_file_system.h"
42
#include "olap/data_dir.h"
43
#include "olap/file_header.h"
44
#include "olap/olap_common.h"
45
#include "olap/olap_define.h"
46
#include "olap/rowset/rowset.h"
47
#include "olap/rowset/rowset_meta_manager.h"
48
#include "olap/tablet_fwd.h"
49
#include "olap/tablet_meta_manager.h"
50
#include "olap/tablet_schema_cache.h"
51
#include "olap/utils.h"
52
#include "util/debug_points.h"
53
#include "util/mem_info.h"
54
#include "util/parse_util.h"
55
#include "util/string_util.h"
56
#include "util/time.h"
57
#include "util/uid_util.h"
58
59
using std::string;
60
using std::unordered_map;
61
using std::vector;
62
63
namespace doris {
64
#include "common/compile_check_begin.h"
65
using namespace ErrorCode;
66
67
TabletMetaSharedPtr TabletMeta::create(
68
        const TCreateTabletReq& request, const TabletUid& tablet_uid, uint64_t shard_id,
69
        uint32_t next_unique_id,
70
300
        const unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id) {
71
300
    std::optional<TBinlogConfig> binlog_config;
72
300
    if (request.__isset.binlog_config) {
73
0
        binlog_config = request.binlog_config;
74
0
    }
75
300
    TInvertedIndexFileStorageFormat::type inverted_index_file_storage_format =
76
300
            request.inverted_index_file_storage_format;
77
78
    // We will discard this format. Don't make any further changes here.
79
300
    if (request.__isset.inverted_index_storage_format) {
80
300
        switch (request.inverted_index_storage_format) {
81
0
        case TInvertedIndexStorageFormat::V1:
82
0
            inverted_index_file_storage_format = TInvertedIndexFileStorageFormat::V1;
83
0
            break;
84
0
        case TInvertedIndexStorageFormat::V2:
85
0
            inverted_index_file_storage_format = TInvertedIndexFileStorageFormat::V2;
86
0
            break;
87
300
        default:
88
300
            break;
89
300
        }
90
300
    }
91
300
    return std::make_shared<TabletMeta>(
92
300
            request.table_id, request.partition_id, request.tablet_id, request.replica_id,
93
300
            request.tablet_schema.schema_hash, shard_id, request.tablet_schema, next_unique_id,
94
300
            col_ordinal_to_unique_id, tablet_uid,
95
300
            request.__isset.tablet_type ? request.tablet_type : TTabletType::TABLET_TYPE_DISK,
96
300
            request.compression_type, request.storage_policy_id,
97
300
            request.__isset.enable_unique_key_merge_on_write
98
300
                    ? request.enable_unique_key_merge_on_write
99
300
                    : false,
100
300
            std::move(binlog_config), request.compaction_policy,
101
300
            request.time_series_compaction_goal_size_mbytes,
102
300
            request.time_series_compaction_file_count_threshold,
103
300
            request.time_series_compaction_time_threshold_seconds,
104
300
            request.time_series_compaction_empty_rowsets_threshold,
105
300
            request.time_series_compaction_level_threshold, inverted_index_file_storage_format);
106
300
}
107
108
1.02k
TabletMeta::~TabletMeta() {
109
1.02k
    if (_handle) {
110
946
        TabletSchemaCache::instance()->release(_handle);
111
946
    }
112
1.02k
}
113
114
TabletMeta::TabletMeta()
115
        : _tablet_uid(0, 0),
116
          _schema(new TabletSchema),
117
541
          _delete_bitmap(new DeleteBitmap(_tablet_id)) {}
118
119
TabletMeta::TabletMeta(int64_t table_id, int64_t partition_id, int64_t tablet_id,
120
                       int64_t replica_id, int32_t schema_hash, int32_t shard_id,
121
                       const TTabletSchema& tablet_schema, uint32_t next_unique_id,
122
                       const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id,
123
                       TabletUid tablet_uid, TTabletType::type tabletType,
124
                       TCompressionType::type compression_type, int64_t storage_policy_id,
125
                       bool enable_unique_key_merge_on_write,
126
                       std::optional<TBinlogConfig> binlog_config, std::string compaction_policy,
127
                       int64_t time_series_compaction_goal_size_mbytes,
128
                       int64_t time_series_compaction_file_count_threshold,
129
                       int64_t time_series_compaction_time_threshold_seconds,
130
                       int64_t time_series_compaction_empty_rowsets_threshold,
131
                       int64_t time_series_compaction_level_threshold,
132
                       TInvertedIndexFileStorageFormat::type inverted_index_file_storage_format)
133
        : _tablet_uid(0, 0),
134
          _schema(new TabletSchema),
135
456
          _delete_bitmap(new DeleteBitmap(tablet_id)) {
136
456
    TabletMetaPB tablet_meta_pb;
137
456
    tablet_meta_pb.set_table_id(table_id);
138
456
    tablet_meta_pb.set_partition_id(partition_id);
139
456
    tablet_meta_pb.set_tablet_id(tablet_id);
140
456
    tablet_meta_pb.set_replica_id(replica_id);
141
456
    tablet_meta_pb.set_schema_hash(schema_hash);
142
456
    tablet_meta_pb.set_shard_id(shard_id);
143
    // Persist the creation time, but it is not used
144
456
    tablet_meta_pb.set_creation_time(time(nullptr));
145
456
    tablet_meta_pb.set_cumulative_layer_point(-1);
146
456
    tablet_meta_pb.set_tablet_state(PB_RUNNING);
147
456
    *(tablet_meta_pb.mutable_tablet_uid()) = tablet_uid.to_proto();
148
456
    tablet_meta_pb.set_tablet_type(tabletType == TTabletType::TABLET_TYPE_DISK
149
456
                                           ? TabletTypePB::TABLET_TYPE_DISK
150
456
                                           : TabletTypePB::TABLET_TYPE_MEMORY);
151
456
    tablet_meta_pb.set_enable_unique_key_merge_on_write(enable_unique_key_merge_on_write);
152
456
    tablet_meta_pb.set_storage_policy_id(storage_policy_id);
153
456
    tablet_meta_pb.set_compaction_policy(compaction_policy);
154
456
    tablet_meta_pb.set_time_series_compaction_goal_size_mbytes(
155
456
            time_series_compaction_goal_size_mbytes);
156
456
    tablet_meta_pb.set_time_series_compaction_file_count_threshold(
157
456
            time_series_compaction_file_count_threshold);
158
456
    tablet_meta_pb.set_time_series_compaction_time_threshold_seconds(
159
456
            time_series_compaction_time_threshold_seconds);
160
456
    tablet_meta_pb.set_time_series_compaction_empty_rowsets_threshold(
161
456
            time_series_compaction_empty_rowsets_threshold);
162
456
    tablet_meta_pb.set_time_series_compaction_level_threshold(
163
456
            time_series_compaction_level_threshold);
164
456
    TabletSchemaPB* schema = tablet_meta_pb.mutable_schema();
165
456
    schema->set_num_short_key_columns(tablet_schema.short_key_column_count);
166
456
    schema->set_num_rows_per_row_block(config::default_num_rows_per_column_file_block);
167
456
    schema->set_sequence_col_idx(tablet_schema.sequence_col_idx);
168
456
    switch (tablet_schema.keys_type) {
169
31
    case TKeysType::DUP_KEYS:
170
31
        schema->set_keys_type(KeysType::DUP_KEYS);
171
31
        break;
172
301
    case TKeysType::UNIQUE_KEYS:
173
301
        schema->set_keys_type(KeysType::UNIQUE_KEYS);
174
301
        break;
175
64
    case TKeysType::AGG_KEYS:
176
64
        schema->set_keys_type(KeysType::AGG_KEYS);
177
64
        break;
178
60
    default:
179
60
        LOG(WARNING) << "unknown tablet keys type";
180
60
        break;
181
456
    }
182
    // compress_kind used to compress segment files
183
456
    schema->set_compress_kind(COMPRESS_LZ4);
184
185
    // compression_type used to compress segment page
186
456
    switch (compression_type) {
187
0
    case TCompressionType::NO_COMPRESSION:
188
0
        schema->set_compression_type(segment_v2::NO_COMPRESSION);
189
0
        break;
190
0
    case TCompressionType::SNAPPY:
191
0
        schema->set_compression_type(segment_v2::SNAPPY);
192
0
        break;
193
0
    case TCompressionType::LZ4:
194
0
        schema->set_compression_type(segment_v2::LZ4);
195
0
        break;
196
456
    case TCompressionType::LZ4F:
197
456
        schema->set_compression_type(segment_v2::LZ4F);
198
456
        break;
199
0
    case TCompressionType::ZLIB:
200
0
        schema->set_compression_type(segment_v2::ZLIB);
201
0
        break;
202
0
    case TCompressionType::ZSTD:
203
0
        schema->set_compression_type(segment_v2::ZSTD);
204
0
        break;
205
0
    default:
206
0
        schema->set_compression_type(segment_v2::LZ4F);
207
0
        break;
208
456
    }
209
210
456
    switch (inverted_index_file_storage_format) {
211
0
    case TInvertedIndexFileStorageFormat::V1:
212
0
        schema->set_inverted_index_storage_format(InvertedIndexStorageFormatPB::V1);
213
0
        break;
214
456
    case TInvertedIndexFileStorageFormat::V2:
215
456
        schema->set_inverted_index_storage_format(InvertedIndexStorageFormatPB::V2);
216
456
        break;
217
0
    default:
218
0
        schema->set_inverted_index_storage_format(InvertedIndexStorageFormatPB::V2);
219
0
        break;
220
456
    }
221
222
456
    switch (tablet_schema.sort_type) {
223
0
    case TSortType::type::ZORDER:
224
0
        schema->set_sort_type(SortType::ZORDER);
225
0
        break;
226
456
    default:
227
456
        schema->set_sort_type(SortType::LEXICAL);
228
456
    }
229
456
    schema->set_sort_col_num(tablet_schema.sort_col_num);
230
456
    for (const auto& i : tablet_schema.cluster_key_idxes) {
231
2
        schema->add_cluster_key_idxes(i);
232
2
    }
233
456
    tablet_meta_pb.set_in_restore_mode(false);
234
235
    // set column information
236
456
    uint32_t col_ordinal = 0;
237
456
    bool has_bf_columns = false;
238
2.11k
    for (TColumn tcolumn : tablet_schema.columns) {
239
2.11k
        ColumnPB* column = schema->add_column();
240
2.11k
        uint32_t unique_id = -1;
241
2.11k
        if (tcolumn.col_unique_id >= 0) {
242
1
            unique_id = tcolumn.col_unique_id;
243
2.10k
        } else {
244
2.10k
            unique_id = col_ordinal_to_unique_id.at(col_ordinal);
245
2.10k
        }
246
2.11k
        col_ordinal++;
247
2.11k
        init_column_from_tcolumn(unique_id, tcolumn, column);
248
249
2.11k
        if (column->is_bf_column()) {
250
0
            has_bf_columns = true;
251
0
        }
252
253
2.11k
        if (tablet_schema.__isset.indexes) {
254
0
            for (auto& index : tablet_schema.indexes) {
255
0
                if (index.index_type == TIndexType::type::BITMAP) {
256
0
                    DCHECK_EQ(index.columns.size(), 1);
257
0
                    if (iequal(tcolumn.column_name, index.columns[0])) {
258
0
                        column->set_has_bitmap_index(true);
259
0
                        break;
260
0
                    }
261
0
                } else if (index.index_type == TIndexType::type::BLOOMFILTER ||
262
0
                           index.index_type == TIndexType::type::NGRAM_BF) {
263
0
                    DCHECK_EQ(index.columns.size(), 1);
264
0
                    if (iequal(tcolumn.column_name, index.columns[0])) {
265
0
                        column->set_is_bf_column(true);
266
0
                        break;
267
0
                    }
268
0
                }
269
0
            }
270
0
        }
271
2.11k
    }
272
273
    // copy index meta
274
456
    if (tablet_schema.__isset.indexes) {
275
0
        for (auto& index : tablet_schema.indexes) {
276
0
            TabletIndexPB* index_pb = schema->add_index();
277
0
            index_pb->set_index_id(index.index_id);
278
0
            index_pb->set_index_name(index.index_name);
279
            // init col_unique_id in index at be side, since col_unique_id may be -1 at fe side
280
            // get column unique id by name
281
0
            for (auto column_name : index.columns) {
282
0
                for (auto column : schema->column()) {
283
0
                    if (iequal(column.name(), column_name)) {
284
0
                        index_pb->add_col_unique_id(column.unique_id());
285
0
                    }
286
0
                }
287
0
            }
288
0
            switch (index.index_type) {
289
0
            case TIndexType::BITMAP:
290
0
                index_pb->set_index_type(IndexType::BITMAP);
291
0
                break;
292
0
            case TIndexType::INVERTED:
293
0
                index_pb->set_index_type(IndexType::INVERTED);
294
0
                break;
295
0
            case TIndexType::BLOOMFILTER:
296
0
                index_pb->set_index_type(IndexType::BLOOMFILTER);
297
0
                break;
298
0
            case TIndexType::NGRAM_BF:
299
0
                index_pb->set_index_type(IndexType::NGRAM_BF);
300
0
                break;
301
0
            }
302
303
0
            if (index.__isset.properties) {
304
0
                auto properties = index_pb->mutable_properties();
305
0
                for (auto kv : index.properties) {
306
0
                    (*properties)[kv.first] = kv.second;
307
0
                }
308
0
            }
309
0
        }
310
0
    }
311
312
456
    schema->set_next_column_unique_id(next_unique_id);
313
456
    if (has_bf_columns && tablet_schema.__isset.bloom_filter_fpp) {
314
0
        schema->set_bf_fpp(tablet_schema.bloom_filter_fpp);
315
0
    }
316
317
456
    if (tablet_schema.__isset.is_in_memory) {
318
0
        schema->set_is_in_memory(tablet_schema.is_in_memory);
319
0
    }
320
321
456
    if (tablet_schema.__isset.disable_auto_compaction) {
322
0
        schema->set_disable_auto_compaction(tablet_schema.disable_auto_compaction);
323
0
    }
324
325
456
    if (tablet_schema.__isset.variant_enable_flatten_nested) {
326
456
        schema->set_variant_enable_flatten_nested(tablet_schema.variant_enable_flatten_nested);
327
456
    }
328
329
456
    if (tablet_schema.__isset.enable_single_replica_compaction) {
330
456
        schema->set_enable_single_replica_compaction(
331
456
                tablet_schema.enable_single_replica_compaction);
332
456
    }
333
334
456
    if (tablet_schema.__isset.delete_sign_idx) {
335
456
        schema->set_delete_sign_idx(tablet_schema.delete_sign_idx);
336
456
    }
337
456
    if (tablet_schema.__isset.store_row_column) {
338
456
        schema->set_store_row_column(tablet_schema.store_row_column);
339
456
    }
340
456
    if (tablet_schema.__isset.row_store_page_size) {
341
456
        schema->set_row_store_page_size(tablet_schema.row_store_page_size);
342
456
    }
343
456
    if (tablet_schema.__isset.storage_page_size) {
344
456
        schema->set_storage_page_size(tablet_schema.storage_page_size);
345
456
    }
346
456
    if (tablet_schema.__isset.skip_write_index_on_load) {
347
456
        schema->set_skip_write_index_on_load(tablet_schema.skip_write_index_on_load);
348
456
    }
349
456
    if (tablet_schema.__isset.row_store_col_cids) {
350
0
        schema->mutable_row_store_column_unique_ids()->Add(tablet_schema.row_store_col_cids.begin(),
351
0
                                                           tablet_schema.row_store_col_cids.end());
352
0
    }
353
456
    if (binlog_config.has_value()) {
354
0
        BinlogConfig tmp_binlog_config;
355
0
        tmp_binlog_config = binlog_config.value();
356
0
        tmp_binlog_config.to_pb(tablet_meta_pb.mutable_binlog_config());
357
0
    }
358
359
456
    init_from_pb(tablet_meta_pb);
360
456
}
361
362
TabletMeta::TabletMeta(const TabletMeta& b)
363
        : MetadataAdder(b),
364
          _table_id(b._table_id),
365
          _index_id(b._index_id),
366
          _partition_id(b._partition_id),
367
          _tablet_id(b._tablet_id),
368
          _replica_id(b._replica_id),
369
          _schema_hash(b._schema_hash),
370
          _shard_id(b._shard_id),
371
          _creation_time(b._creation_time),
372
          _cumulative_layer_point(b._cumulative_layer_point),
373
          _tablet_uid(b._tablet_uid),
374
          _tablet_type(b._tablet_type),
375
          _tablet_state(b._tablet_state),
376
          _schema(b._schema),
377
          _rs_metas(b._rs_metas),
378
          _stale_rs_metas(b._stale_rs_metas),
379
          _in_restore_mode(b._in_restore_mode),
380
          _preferred_rowset_type(b._preferred_rowset_type),
381
          _storage_policy_id(b._storage_policy_id),
382
          _cooldown_meta_id(b._cooldown_meta_id),
383
          _enable_unique_key_merge_on_write(b._enable_unique_key_merge_on_write),
384
          _delete_bitmap(b._delete_bitmap),
385
          _binlog_config(b._binlog_config),
386
          _compaction_policy(b._compaction_policy),
387
          _time_series_compaction_goal_size_mbytes(b._time_series_compaction_goal_size_mbytes),
388
          _time_series_compaction_file_count_threshold(
389
                  b._time_series_compaction_file_count_threshold),
390
          _time_series_compaction_time_threshold_seconds(
391
                  b._time_series_compaction_time_threshold_seconds),
392
          _time_series_compaction_empty_rowsets_threshold(
393
                  b._time_series_compaction_empty_rowsets_threshold),
394
0
          _time_series_compaction_level_threshold(b._time_series_compaction_level_threshold) {};
395
396
void TabletMeta::init_column_from_tcolumn(uint32_t unique_id, const TColumn& tcolumn,
397
2.11k
                                          ColumnPB* column) {
398
2.11k
    column->set_unique_id(unique_id);
399
2.11k
    column->set_name(tcolumn.column_name);
400
2.11k
    column->set_has_bitmap_index(tcolumn.has_bitmap_index);
401
2.11k
    column->set_is_auto_increment(tcolumn.is_auto_increment);
402
2.11k
    string data_type;
403
2.11k
    EnumToString(TPrimitiveType, tcolumn.column_type.type, data_type);
404
2.11k
    column->set_type(data_type);
405
406
2.11k
    uint32_t length = TabletColumn::get_field_length_by_type(tcolumn.column_type.type,
407
2.11k
                                                             tcolumn.column_type.len);
408
2.11k
    column->set_length(length);
409
2.11k
    column->set_index_length(length);
410
2.11k
    column->set_precision(tcolumn.column_type.precision);
411
2.11k
    column->set_frac(tcolumn.column_type.scale);
412
413
2.11k
    if (tcolumn.__isset.result_is_nullable) {
414
0
        column->set_result_is_nullable(tcolumn.result_is_nullable);
415
0
    }
416
417
2.11k
    if (tcolumn.__isset.be_exec_version) {
418
2.11k
        column->set_be_exec_version(tcolumn.be_exec_version);
419
2.11k
    }
420
421
2.11k
    if (tcolumn.column_type.type == TPrimitiveType::VARCHAR ||
422
2.11k
        tcolumn.column_type.type == TPrimitiveType::STRING) {
423
101
        if (!tcolumn.column_type.__isset.index_len) {
424
101
            column->set_index_length(10);
425
101
        } else {
426
0
            column->set_index_length(tcolumn.column_type.index_len);
427
0
        }
428
101
    }
429
2.11k
    if (!tcolumn.is_key) {
430
1.13k
        column->set_is_key(false);
431
1.13k
        if (tcolumn.__isset.aggregation) {
432
0
            column->set_aggregation(tcolumn.aggregation);
433
1.13k
        } else {
434
1.13k
            string aggregation_type;
435
1.13k
            EnumToString(TAggregationType, tcolumn.aggregation_type, aggregation_type);
436
1.13k
            column->set_aggregation(aggregation_type);
437
1.13k
        }
438
1.13k
    } else {
439
974
        column->set_is_key(true);
440
974
        column->set_aggregation("NONE");
441
974
    }
442
2.11k
    column->set_is_nullable(tcolumn.is_allow_null);
443
2.11k
    if (tcolumn.__isset.default_value) {
444
0
        column->set_default_value(tcolumn.default_value);
445
0
    }
446
2.11k
    if (tcolumn.__isset.is_bloom_filter_column) {
447
0
        column->set_is_bf_column(tcolumn.is_bloom_filter_column);
448
0
    }
449
2.11k
    for (size_t i = 0; i < tcolumn.children_column.size(); i++) {
450
0
        ColumnPB* children_column = column->add_children_columns();
451
0
        init_column_from_tcolumn(tcolumn.children_column[i].col_unique_id,
452
0
                                 tcolumn.children_column[i], children_column);
453
0
    }
454
2.11k
}
455
456
4
Status TabletMeta::create_from_file(const string& file_path) {
457
4
    TabletMetaPB tablet_meta_pb;
458
4
    RETURN_IF_ERROR(load_from_file(file_path, &tablet_meta_pb));
459
4
    init_from_pb(tablet_meta_pb);
460
4
    return Status::OK();
461
4
}
462
463
10
Status TabletMeta::load_from_file(const string& file_path, TabletMetaPB* tablet_meta_pb) {
464
10
    FileHeader<TabletMetaPB> file_header(file_path);
465
    // In file_header.deserialize(), it validates file length, signature, checksum of protobuf.
466
10
    RETURN_IF_ERROR(file_header.deserialize());
467
10
    try {
468
10
        tablet_meta_pb->CopyFrom(file_header.message());
469
10
    } catch (...) {
470
0
        return Status::Error<PARSE_PROTOBUF_ERROR>("fail to copy protocol buffer object. file={}",
471
0
                                                   file_path);
472
0
    }
473
10
    return Status::OK();
474
10
}
475
476
std::string TabletMeta::construct_header_file_path(const string& schema_hash_path,
477
3
                                                   int64_t tablet_id) {
478
3
    std::stringstream header_name_stream;
479
3
    header_name_stream << schema_hash_path << "/" << tablet_id << ".hdr";
480
3
    return header_name_stream.str();
481
3
}
482
483
0
Status TabletMeta::save_as_json(const string& file_path) {
484
0
    std::string json_meta;
485
0
    json2pb::Pb2JsonOptions json_options;
486
0
    json_options.pretty_json = true;
487
0
    json_options.bytes_to_base64 = true;
488
0
    to_json(&json_meta, json_options);
489
    // save to file
490
0
    io::FileWriterPtr file_writer;
491
0
    RETURN_IF_ERROR(io::global_local_filesystem()->create_file(file_path, &file_writer));
492
0
    RETURN_IF_ERROR(file_writer->append(json_meta));
493
0
    RETURN_IF_ERROR(file_writer->close());
494
0
    return Status::OK();
495
0
}
496
497
230
Status TabletMeta::save(const string& file_path) {
498
230
    TabletMetaPB tablet_meta_pb;
499
230
    to_meta_pb(&tablet_meta_pb);
500
230
    return TabletMeta::save(file_path, tablet_meta_pb);
501
230
}
502
503
234
Status TabletMeta::save(const string& file_path, const TabletMetaPB& tablet_meta_pb) {
504
234
    DCHECK(!file_path.empty());
505
234
    FileHeader<TabletMetaPB> file_header(file_path);
506
234
    try {
507
234
        file_header.mutable_message()->CopyFrom(tablet_meta_pb);
508
234
    } catch (...) {
509
0
        LOG(WARNING) << "fail to copy protocol buffer object. file='" << file_path;
510
0
        return Status::Error<ErrorCode::INTERNAL_ERROR>(
511
0
                "fail to copy protocol buffer object. file={}", file_path);
512
0
    }
513
234
    RETURN_IF_ERROR(file_header.prepare());
514
234
    RETURN_IF_ERROR(file_header.serialize());
515
234
    return Status::OK();
516
234
}
517
518
565
Status TabletMeta::save_meta(DataDir* data_dir) {
519
565
    std::lock_guard<std::shared_mutex> wrlock(_meta_lock);
520
565
    return _save_meta(data_dir);
521
565
}
522
523
565
Status TabletMeta::_save_meta(DataDir* data_dir) {
524
    // check if tablet uid is valid
525
565
    if (_tablet_uid.hi == 0 && _tablet_uid.lo == 0) {
526
0
        LOG(FATAL) << "tablet_uid is invalid"
527
0
                   << " tablet=" << tablet_id() << " _tablet_uid=" << _tablet_uid.to_string();
528
0
    }
529
565
    string meta_binary;
530
531
565
    auto t1 = MonotonicMicros();
532
565
    serialize(&meta_binary);
533
565
    auto t2 = MonotonicMicros();
534
565
    Status status = TabletMetaManager::save(data_dir, tablet_id(), schema_hash(), meta_binary);
535
565
    if (!status.ok()) {
536
0
        LOG(FATAL) << "fail to save tablet_meta. status=" << status << ", tablet_id=" << tablet_id()
537
0
                   << ", schema_hash=" << schema_hash();
538
0
    }
539
565
    auto t3 = MonotonicMicros();
540
565
    auto cost = t3 - t1;
541
565
    if (cost > 1 * 1000 * 1000) {
542
0
        LOG(INFO) << "save tablet(" << tablet_id() << ") meta too slow. serialize cost " << t2 - t1
543
0
                  << "(us), serialized binary size: " << meta_binary.length()
544
0
                  << "(bytes), write rocksdb cost " << t3 - t2 << "(us)";
545
0
    }
546
565
    return status;
547
565
}
548
549
570
void TabletMeta::serialize(string* meta_binary) {
550
570
    TabletMetaPB tablet_meta_pb;
551
570
    to_meta_pb(&tablet_meta_pb);
552
570
    if (tablet_meta_pb.partition_id() <= 0) {
553
468
        LOG(WARNING) << "invalid partition id " << tablet_meta_pb.partition_id() << " tablet "
554
468
                     << tablet_meta_pb.tablet_id();
555
468
    }
556
570
    DBUG_EXECUTE_IF("TabletMeta::serialize::zero_partition_id", {
557
570
        long partition_id = tablet_meta_pb.partition_id();
558
570
        tablet_meta_pb.set_partition_id(0);
559
570
        LOG(WARNING) << "set debug point TabletMeta::serialize::zero_partition_id old="
560
570
                     << partition_id << " new=" << tablet_meta_pb.DebugString();
561
570
    });
562
570
    bool serialize_success = tablet_meta_pb.SerializeToString(meta_binary);
563
570
    if (!_rs_metas.empty() || !_stale_rs_metas.empty()) {
564
570
        _avg_rs_meta_serialize_size =
565
570
                meta_binary->length() / (_rs_metas.size() + _stale_rs_metas.size());
566
570
        if (meta_binary->length() > config::tablet_meta_serialize_size_limit ||
567
570
            !serialize_success) {
568
0
            int64_t origin_meta_size = meta_binary->length();
569
0
            int64_t stale_rowsets_num = tablet_meta_pb.stale_rs_metas().size();
570
0
            tablet_meta_pb.clear_stale_rs_metas();
571
0
            meta_binary->clear();
572
0
            serialize_success = tablet_meta_pb.SerializeToString(meta_binary);
573
0
            LOG(WARNING) << "tablet meta serialization size exceeds limit: "
574
0
                         << config::tablet_meta_serialize_size_limit
575
0
                         << " clean up stale rowsets, tablet id: " << tablet_id()
576
0
                         << " stale rowset num: " << stale_rowsets_num
577
0
                         << " serialization size before clean " << origin_meta_size
578
0
                         << " serialization size after clean " << meta_binary->length();
579
0
        }
580
570
    }
581
582
570
    if (!serialize_success) {
583
0
        LOG(FATAL) << "failed to serialize meta " << tablet_id();
584
0
    }
585
570
}
586
587
461
Status TabletMeta::deserialize(std::string_view meta_binary) {
588
461
    TabletMetaPB tablet_meta_pb;
589
461
    bool parsed = tablet_meta_pb.ParseFromArray(meta_binary.data(),
590
461
                                                static_cast<int32_t>(meta_binary.size()));
591
461
    if (!parsed) {
592
0
        return Status::Error<INIT_FAILED>("parse tablet meta failed");
593
0
    }
594
461
    init_from_pb(tablet_meta_pb);
595
461
    return Status::OK();
596
461
}
597
598
949
void TabletMeta::init_from_pb(const TabletMetaPB& tablet_meta_pb) {
599
949
    _table_id = tablet_meta_pb.table_id();
600
949
    _index_id = tablet_meta_pb.index_id();
601
949
    _partition_id = tablet_meta_pb.partition_id();
602
949
    _tablet_id = tablet_meta_pb.tablet_id();
603
949
    _replica_id = tablet_meta_pb.replica_id();
604
949
    _schema_hash = tablet_meta_pb.schema_hash();
605
949
    _shard_id = tablet_meta_pb.shard_id();
606
949
    _creation_time = tablet_meta_pb.creation_time();
607
949
    _cumulative_layer_point = tablet_meta_pb.cumulative_layer_point();
608
949
    _tablet_uid = TabletUid(tablet_meta_pb.tablet_uid());
609
949
    _ttl_seconds = tablet_meta_pb.ttl_seconds();
610
949
    if (tablet_meta_pb.has_tablet_type()) {
611
928
        _tablet_type = tablet_meta_pb.tablet_type();
612
928
    } else {
613
21
        _tablet_type = TabletTypePB::TABLET_TYPE_DISK;
614
21
    }
615
616
    // init _tablet_state
617
949
    switch (tablet_meta_pb.tablet_state()) {
618
27
    case PB_NOTREADY:
619
27
        _tablet_state = TabletState::TABLET_NOTREADY;
620
27
        break;
621
697
    case PB_RUNNING:
622
697
        _tablet_state = TabletState::TABLET_RUNNING;
623
697
        break;
624
0
    case PB_TOMBSTONED:
625
0
        _tablet_state = TabletState::TABLET_TOMBSTONED;
626
0
        break;
627
0
    case PB_STOPPED:
628
0
        _tablet_state = TabletState::TABLET_STOPPED;
629
0
        break;
630
225
    case PB_SHUTDOWN:
631
225
        _tablet_state = TabletState::TABLET_SHUTDOWN;
632
225
        break;
633
0
    default:
634
0
        LOG(WARNING) << "tablet has no state. tablet=" << tablet_id()
635
0
                     << ", schema_hash=" << schema_hash();
636
949
    }
637
638
    // init _schema
639
949
    TabletSchemaSPtr schema = std::make_shared<TabletSchema>();
640
949
    schema->init_from_pb(tablet_meta_pb.schema());
641
949
    if (_handle) {
642
3
        TabletSchemaCache::instance()->release(_handle);
643
3
    }
644
949
    auto pair = TabletSchemaCache::instance()->insert(schema->to_key());
645
949
    _handle = pair.first;
646
949
    _schema = pair.second;
647
648
949
    if (tablet_meta_pb.has_enable_unique_key_merge_on_write()) {
649
928
        _enable_unique_key_merge_on_write = tablet_meta_pb.enable_unique_key_merge_on_write();
650
928
    }
651
652
    // init _rs_metas
653
10.8k
    for (auto& it : tablet_meta_pb.rs_metas()) {
654
10.8k
        RowsetMetaSharedPtr rs_meta(new RowsetMeta());
655
10.8k
        rs_meta->init_from_pb(it);
656
10.8k
        _rs_metas.push_back(std::move(rs_meta));
657
10.8k
    }
658
659
    // For mow table, delete bitmap of stale rowsets has not been persisted.
660
    // When be restart, query should not read the stale rowset, otherwise duplicate keys
661
    // will be read out. Therefore, we don't add them to _stale_rs_meta for mow table.
662
949
    if (!config::skip_loading_stale_rowset_meta && !_enable_unique_key_merge_on_write) {
663
914
        for (auto& it : tablet_meta_pb.stale_rs_metas()) {
664
0
            RowsetMetaSharedPtr rs_meta(new RowsetMeta());
665
0
            rs_meta->init_from_pb(it);
666
0
            _stale_rs_metas.push_back(std::move(rs_meta));
667
0
        }
668
914
    }
669
670
949
    if (tablet_meta_pb.has_in_restore_mode()) {
671
928
        _in_restore_mode = tablet_meta_pb.in_restore_mode();
672
928
    }
673
674
949
    if (tablet_meta_pb.has_preferred_rowset_type()) {
675
472
        _preferred_rowset_type = tablet_meta_pb.preferred_rowset_type();
676
472
    }
677
678
949
    _storage_policy_id = tablet_meta_pb.storage_policy_id();
679
949
    if (tablet_meta_pb.has_cooldown_meta_id()) {
680
0
        _cooldown_meta_id = tablet_meta_pb.cooldown_meta_id();
681
0
    }
682
683
949
    if (tablet_meta_pb.has_delete_bitmap()) {
684
0
        int rst_ids_size = tablet_meta_pb.delete_bitmap().rowset_ids_size();
685
0
        int seg_ids_size = tablet_meta_pb.delete_bitmap().segment_ids_size();
686
0
        int versions_size = tablet_meta_pb.delete_bitmap().versions_size();
687
0
        int seg_maps_size = tablet_meta_pb.delete_bitmap().segment_delete_bitmaps_size();
688
0
        CHECK(rst_ids_size == seg_ids_size && seg_ids_size == seg_maps_size &&
689
0
              seg_maps_size == versions_size);
690
0
        for (int i = 0; i < rst_ids_size; ++i) {
691
0
            RowsetId rst_id;
692
0
            rst_id.init(tablet_meta_pb.delete_bitmap().rowset_ids(i));
693
0
            auto seg_id = tablet_meta_pb.delete_bitmap().segment_ids(i);
694
0
            auto ver = tablet_meta_pb.delete_bitmap().versions(i);
695
0
            auto bitmap = tablet_meta_pb.delete_bitmap().segment_delete_bitmaps(i).data();
696
0
            delete_bitmap().delete_bitmap[{rst_id, seg_id, ver}] = roaring::Roaring::read(bitmap);
697
0
        }
698
0
    }
699
700
949
    if (tablet_meta_pb.has_binlog_config()) {
701
470
        _binlog_config = tablet_meta_pb.binlog_config();
702
470
    }
703
949
    _compaction_policy = tablet_meta_pb.compaction_policy();
704
949
    _time_series_compaction_goal_size_mbytes =
705
949
            tablet_meta_pb.time_series_compaction_goal_size_mbytes();
706
949
    _time_series_compaction_file_count_threshold =
707
949
            tablet_meta_pb.time_series_compaction_file_count_threshold();
708
949
    _time_series_compaction_time_threshold_seconds =
709
949
            tablet_meta_pb.time_series_compaction_time_threshold_seconds();
710
949
    _time_series_compaction_empty_rowsets_threshold =
711
949
            tablet_meta_pb.time_series_compaction_empty_rowsets_threshold();
712
949
    _time_series_compaction_level_threshold =
713
949
            tablet_meta_pb.time_series_compaction_level_threshold();
714
949
}
715
716
809
void TabletMeta::to_meta_pb(TabletMetaPB* tablet_meta_pb) {
717
809
    tablet_meta_pb->set_table_id(table_id());
718
809
    tablet_meta_pb->set_index_id(index_id());
719
809
    tablet_meta_pb->set_partition_id(partition_id());
720
809
    tablet_meta_pb->set_tablet_id(tablet_id());
721
809
    tablet_meta_pb->set_replica_id(replica_id());
722
809
    tablet_meta_pb->set_schema_hash(schema_hash());
723
809
    tablet_meta_pb->set_shard_id(shard_id());
724
809
    tablet_meta_pb->set_creation_time(creation_time());
725
809
    tablet_meta_pb->set_cumulative_layer_point(cumulative_layer_point());
726
809
    *(tablet_meta_pb->mutable_tablet_uid()) = tablet_uid().to_proto();
727
809
    tablet_meta_pb->set_tablet_type(_tablet_type);
728
809
    tablet_meta_pb->set_ttl_seconds(_ttl_seconds);
729
809
    switch (tablet_state()) {
730
8
    case TABLET_NOTREADY:
731
8
        tablet_meta_pb->set_tablet_state(PB_NOTREADY);
732
8
        break;
733
323
    case TABLET_RUNNING:
734
323
        tablet_meta_pb->set_tablet_state(PB_RUNNING);
735
323
        break;
736
0
    case TABLET_TOMBSTONED:
737
0
        tablet_meta_pb->set_tablet_state(PB_TOMBSTONED);
738
0
        break;
739
0
    case TABLET_STOPPED:
740
0
        tablet_meta_pb->set_tablet_state(PB_STOPPED);
741
0
        break;
742
478
    case TABLET_SHUTDOWN:
743
478
        tablet_meta_pb->set_tablet_state(PB_SHUTDOWN);
744
478
        break;
745
809
    }
746
747
    // RowsetMetaPB is separated from TabletMetaPB
748
809
    if (!config::is_cloud_mode()) {
749
21.6k
        for (auto& rs : _rs_metas) {
750
21.6k
            rs->to_rowset_pb(tablet_meta_pb->add_rs_metas());
751
21.6k
        }
752
809
        for (auto rs : _stale_rs_metas) {
753
0
            rs->to_rowset_pb(tablet_meta_pb->add_stale_rs_metas());
754
0
        }
755
809
    }
756
757
809
    _schema->to_schema_pb(tablet_meta_pb->mutable_schema());
758
759
809
    tablet_meta_pb->set_in_restore_mode(in_restore_mode());
760
761
    // to avoid modify tablet meta to the greatest extend
762
809
    if (_preferred_rowset_type == BETA_ROWSET) {
763
809
        tablet_meta_pb->set_preferred_rowset_type(_preferred_rowset_type);
764
809
    }
765
809
    if (_storage_policy_id > 0) {
766
5
        tablet_meta_pb->set_storage_policy_id(_storage_policy_id);
767
5
    }
768
809
    if (_cooldown_meta_id.initialized()) {
769
5
        tablet_meta_pb->mutable_cooldown_meta_id()->CopyFrom(_cooldown_meta_id.to_proto());
770
5
    }
771
772
809
    tablet_meta_pb->set_enable_unique_key_merge_on_write(_enable_unique_key_merge_on_write);
773
774
809
    if (_enable_unique_key_merge_on_write) {
775
4
        std::set<RowsetId> stale_rs_ids;
776
4
        for (const auto& rowset : _stale_rs_metas) {
777
0
            stale_rs_ids.insert(rowset->rowset_id());
778
0
        }
779
4
        DeleteBitmapPB* delete_bitmap_pb = tablet_meta_pb->mutable_delete_bitmap();
780
4
        for (auto& [id, bitmap] : delete_bitmap().snapshot().delete_bitmap) {
781
2
            auto& [rowset_id, segment_id, ver] = id;
782
2
            if (stale_rs_ids.count(rowset_id) != 0) {
783
0
                continue;
784
0
            }
785
2
            delete_bitmap_pb->add_rowset_ids(rowset_id.to_string());
786
2
            delete_bitmap_pb->add_segment_ids(segment_id);
787
2
            delete_bitmap_pb->add_versions(ver);
788
2
            std::string bitmap_data(bitmap.getSizeInBytes(), '\0');
789
2
            bitmap.write(bitmap_data.data());
790
2
            *(delete_bitmap_pb->add_segment_delete_bitmaps()) = std::move(bitmap_data);
791
2
        }
792
4
    }
793
809
    _binlog_config.to_pb(tablet_meta_pb->mutable_binlog_config());
794
809
    tablet_meta_pb->set_compaction_policy(compaction_policy());
795
809
    tablet_meta_pb->set_time_series_compaction_goal_size_mbytes(
796
809
            time_series_compaction_goal_size_mbytes());
797
809
    tablet_meta_pb->set_time_series_compaction_file_count_threshold(
798
809
            time_series_compaction_file_count_threshold());
799
809
    tablet_meta_pb->set_time_series_compaction_time_threshold_seconds(
800
809
            time_series_compaction_time_threshold_seconds());
801
809
    tablet_meta_pb->set_time_series_compaction_empty_rowsets_threshold(
802
809
            time_series_compaction_empty_rowsets_threshold());
803
809
    tablet_meta_pb->set_time_series_compaction_level_threshold(
804
809
            time_series_compaction_level_threshold());
805
809
}
806
807
2
void TabletMeta::to_json(string* json_string, json2pb::Pb2JsonOptions& options) {
808
2
    TabletMetaPB tablet_meta_pb;
809
2
    to_meta_pb(&tablet_meta_pb);
810
2
    json2pb::ProtoMessageToJson(tablet_meta_pb, json_string, options);
811
2
}
812
813
123
Version TabletMeta::max_version() const {
814
123
    Version max_version = {-1, 0};
815
187
    for (auto& rs_meta : _rs_metas) {
816
187
        if (rs_meta->end_version() > max_version.second) {
817
185
            max_version = rs_meta->version();
818
185
        }
819
187
    }
820
123
    return max_version;
821
123
}
822
823
0
size_t TabletMeta::version_count_cross_with_range(const Version& range) const {
824
0
    size_t count = 0;
825
0
    for (const auto& rs_meta : _rs_metas) {
826
0
        if (!(range.first > rs_meta->version().second || range.second < rs_meta->version().first)) {
827
0
            count++;
828
0
        }
829
0
    }
830
0
    return count;
831
0
}
832
833
11.1k
Status TabletMeta::add_rs_meta(const RowsetMetaSharedPtr& rs_meta) {
834
    // check RowsetMeta is valid
835
357k
    for (auto& rs : _rs_metas) {
836
357k
        if (rs->version() == rs_meta->version()) {
837
0
            if (rs->rowset_id() != rs_meta->rowset_id()) {
838
0
                return Status::Error<PUSH_VERSION_ALREADY_EXIST>(
839
0
                        "version already exist. rowset_id={}, version={}, tablet={}",
840
0
                        rs->rowset_id().to_string(), rs->version().to_string(), tablet_id());
841
0
            } else {
842
                // rowsetid,version is equal, it is a duplicate req, skip it
843
0
                return Status::OK();
844
0
            }
845
0
        }
846
357k
    }
847
11.1k
    _rs_metas.push_back(rs_meta);
848
11.1k
    return Status::OK();
849
11.1k
}
850
851
0
void TabletMeta::add_rowsets_unchecked(const std::vector<RowsetSharedPtr>& to_add) {
852
0
    for (const auto& rs : to_add) {
853
0
        _rs_metas.push_back(rs->rowset_meta());
854
0
    }
855
0
}
856
857
void TabletMeta::delete_rs_meta_by_version(const Version& version,
858
0
                                           std::vector<RowsetMetaSharedPtr>* deleted_rs_metas) {
859
0
    size_t rowset_cache_version_size = 0;
860
0
    auto it = _rs_metas.begin();
861
0
    while (it != _rs_metas.end()) {
862
0
        if ((*it)->version() == version) {
863
0
            if (deleted_rs_metas != nullptr) {
864
0
                deleted_rs_metas->push_back(*it);
865
0
            }
866
0
            _rs_metas.erase(it);
867
0
            if (_enable_unique_key_merge_on_write) {
868
0
                rowset_cache_version_size =
869
0
                        _delete_bitmap->remove_rowset_cache_version((*it)->rowset_id());
870
0
            }
871
0
            return;
872
0
        } else {
873
0
            ++it;
874
0
        }
875
0
    }
876
0
    _check_mow_rowset_cache_version_size(rowset_cache_version_size);
877
0
}
878
879
void TabletMeta::modify_rs_metas(const std::vector<RowsetMetaSharedPtr>& to_add,
880
                                 const std::vector<RowsetMetaSharedPtr>& to_delete,
881
24
                                 bool same_version) {
882
24
    size_t rowset_cache_version_size = 0;
883
    // Remove to_delete rowsets from _rs_metas
884
24
    for (auto rs_to_del : to_delete) {
885
18
        auto it = _rs_metas.begin();
886
20
        while (it != _rs_metas.end()) {
887
20
            if (rs_to_del->version() == (*it)->version()) {
888
18
                _rs_metas.erase(it);
889
18
                if (_enable_unique_key_merge_on_write) {
890
1
                    rowset_cache_version_size =
891
1
                            _delete_bitmap->remove_rowset_cache_version((*it)->rowset_id());
892
1
                }
893
                // there should be only one rowset match the version
894
18
                break;
895
18
            } else {
896
2
                ++it;
897
2
            }
898
20
        }
899
18
    }
900
24
    if (!same_version) {
901
        // put to_delete rowsets in _stale_rs_metas.
902
6
        _stale_rs_metas.insert(_stale_rs_metas.end(), to_delete.begin(), to_delete.end());
903
6
    }
904
    // put to_add rowsets in _rs_metas.
905
24
    _rs_metas.insert(_rs_metas.end(), to_add.begin(), to_add.end());
906
24
    _check_mow_rowset_cache_version_size(rowset_cache_version_size);
907
24
}
908
909
// Use the passing "rs_metas" to replace the rs meta in this tablet meta
910
// Also clear the _stale_rs_metas because this tablet meta maybe copyied from
911
// an existing tablet before. Add after revise, only the passing "rs_metas"
912
// is needed.
913
5
void TabletMeta::revise_rs_metas(std::vector<RowsetMetaSharedPtr>&& rs_metas) {
914
5
    {
915
5
        std::lock_guard<std::shared_mutex> wrlock(_meta_lock);
916
5
        _rs_metas = std::move(rs_metas);
917
5
        _stale_rs_metas.clear();
918
5
    }
919
5
    if (_enable_unique_key_merge_on_write) {
920
0
        _delete_bitmap->clear_rowset_cache_version();
921
0
    }
922
5
}
923
924
// This method should call after revise_rs_metas, since new rs_metas might be a subset
925
// of original tablet, we should revise the delete_bitmap according to current rowset.
926
//
927
// Delete bitmap is protected by Tablet::_meta_lock, we don't need to acquire the
928
// TabletMeta's _meta_lock
929
1
void TabletMeta::revise_delete_bitmap_unlocked(const DeleteBitmap& delete_bitmap) {
930
1
    _delete_bitmap = std::make_unique<DeleteBitmap>(tablet_id());
931
2
    for (auto rs : _rs_metas) {
932
2
        DeleteBitmap rs_bm(tablet_id());
933
2
        delete_bitmap.subset({rs->rowset_id(), 0, 0}, {rs->rowset_id(), UINT32_MAX, INT64_MAX},
934
2
                             &rs_bm);
935
2
        _delete_bitmap->merge(rs_bm);
936
2
    }
937
1
    for (auto rs : _stale_rs_metas) {
938
0
        DeleteBitmap rs_bm(tablet_id());
939
0
        delete_bitmap.subset({rs->rowset_id(), 0, 0}, {rs->rowset_id(), UINT32_MAX, INT64_MAX},
940
0
                             &rs_bm);
941
0
        _delete_bitmap->merge(rs_bm);
942
0
    }
943
1
}
944
945
0
void TabletMeta::delete_stale_rs_meta_by_version(const Version& version) {
946
0
    size_t rowset_cache_version_size = 0;
947
0
    auto it = _stale_rs_metas.begin();
948
0
    while (it != _stale_rs_metas.end()) {
949
0
        if ((*it)->version() == version) {
950
0
            if (_enable_unique_key_merge_on_write) {
951
                // remove rowset delete bitmap
952
0
                delete_bitmap().remove({(*it)->rowset_id(), 0, 0},
953
0
                                       {(*it)->rowset_id(), UINT32_MAX, 0});
954
0
                rowset_cache_version_size =
955
0
                        delete_bitmap().remove_rowset_cache_version((*it)->rowset_id());
956
0
                if (config::enable_mow_verbose_log) {
957
0
                    LOG_INFO(
958
0
                            "delete stale rowset's delete bitmap. tablet={}, version={}, rowset={}",
959
0
                            tablet_id(), version.to_string(), (*it)->rowset_id().to_string());
960
0
                }
961
0
            }
962
0
            it = _stale_rs_metas.erase(it);
963
0
        } else {
964
0
            it++;
965
0
        }
966
0
    }
967
0
    _check_mow_rowset_cache_version_size(rowset_cache_version_size);
968
0
}
969
970
0
RowsetMetaSharedPtr TabletMeta::acquire_rs_meta_by_version(const Version& version) const {
971
0
    for (auto it : _rs_metas) {
972
0
        if (it->version() == version) {
973
0
            return it;
974
0
        }
975
0
    }
976
0
    return nullptr;
977
0
}
978
979
8
RowsetMetaSharedPtr TabletMeta::acquire_stale_rs_meta_by_version(const Version& version) const {
980
8
    for (auto it : _stale_rs_metas) {
981
0
        if (it->version() == version) {
982
0
            return it;
983
0
        }
984
0
    }
985
8
    return nullptr;
986
8
}
987
988
23
Status TabletMeta::set_partition_id(int64_t partition_id) {
989
23
    if ((_partition_id > 0 && _partition_id != partition_id) || partition_id < 1) {
990
0
        LOG(WARNING) << "cur partition id=" << _partition_id << " new partition id=" << partition_id
991
0
                     << " not equal";
992
0
    }
993
23
    _partition_id = partition_id;
994
23
    return Status::OK();
995
23
}
996
997
0
void TabletMeta::clear_stale_rowset() {
998
0
    _stale_rs_metas.clear();
999
0
    if (_enable_unique_key_merge_on_write) {
1000
0
        _delete_bitmap->clear_rowset_cache_version();
1001
0
    }
1002
0
}
1003
1004
0
void TabletMeta::clear_rowsets() {
1005
0
    _rs_metas.clear();
1006
0
    if (_enable_unique_key_merge_on_write) {
1007
0
        _delete_bitmap->clear_rowset_cache_version();
1008
0
    }
1009
0
}
1010
1011
24
void TabletMeta::_check_mow_rowset_cache_version_size(size_t rowset_cache_version_size) {
1012
24
    if (_enable_unique_key_merge_on_write && config::enable_mow_verbose_log &&
1013
24
        rowset_cache_version_size > _rs_metas.size() + _stale_rs_metas.size()) {
1014
0
        std::stringstream ss;
1015
0
        auto rowset_ids = _delete_bitmap->get_rowset_cache_version();
1016
0
        for (const auto& rowset_id : rowset_ids) {
1017
0
            bool found = false;
1018
0
            for (auto& rs_meta : _rs_metas) {
1019
0
                if (rs_meta->rowset_id() == rowset_id) {
1020
0
                    found = true;
1021
0
                    break;
1022
0
                }
1023
0
            }
1024
0
            if (found) {
1025
0
                continue;
1026
0
            }
1027
0
            for (auto& rs_meta : _stale_rs_metas) {
1028
0
                if (rs_meta->rowset_id() == rowset_id) {
1029
0
                    found = true;
1030
0
                    break;
1031
0
                }
1032
0
            }
1033
0
            if (!found) {
1034
0
                ss << rowset_id.to_string() << ", ";
1035
0
            }
1036
0
        }
1037
        // size(rowset_cache_version) <= size(_rs_metas) + size(_stale_rs_metas) + size(_unused_rs)
1038
0
        std::string msg = fmt::format(
1039
0
                "tablet: {}, rowset_cache_version size: {}, "
1040
0
                "_rs_metas size: {}, _stale_rs_metas size: {}, delta: {}. rowset only in cache: {}",
1041
0
                _tablet_id, rowset_cache_version_size, _rs_metas.size(), _stale_rs_metas.size(),
1042
0
                rowset_cache_version_size - _rs_metas.size() - _stale_rs_metas.size(), ss.str());
1043
0
        LOG(INFO) << msg;
1044
0
    }
1045
24
}
1046
1047
1
bool operator==(const TabletMeta& a, const TabletMeta& b) {
1048
1
    if (a._table_id != b._table_id) return false;
1049
1
    if (a._index_id != b._index_id) return false;
1050
1
    if (a._partition_id != b._partition_id) return false;
1051
1
    if (a._tablet_id != b._tablet_id) return false;
1052
1
    if (a._replica_id != b._replica_id) return false;
1053
1
    if (a._schema_hash != b._schema_hash) return false;
1054
1
    if (a._shard_id != b._shard_id) return false;
1055
1
    if (a._creation_time != b._creation_time) return false;
1056
1
    if (a._cumulative_layer_point != b._cumulative_layer_point) return false;
1057
1
    if (a._tablet_uid != b._tablet_uid) return false;
1058
1
    if (a._tablet_type != b._tablet_type) return false;
1059
1
    if (a._tablet_state != b._tablet_state) return false;
1060
1
    if (*a._schema != *b._schema) return false;
1061
1
    if (a._rs_metas.size() != b._rs_metas.size()) return false;
1062
1
    for (int i = 0; i < a._rs_metas.size(); ++i) {
1063
0
        if (a._rs_metas[i] != b._rs_metas[i]) return false;
1064
0
    }
1065
1
    if (a._in_restore_mode != b._in_restore_mode) return false;
1066
1
    if (a._preferred_rowset_type != b._preferred_rowset_type) return false;
1067
1
    if (a._storage_policy_id != b._storage_policy_id) return false;
1068
1
    if (a._compaction_policy != b._compaction_policy) return false;
1069
1
    if (a._time_series_compaction_goal_size_mbytes != b._time_series_compaction_goal_size_mbytes)
1070
0
        return false;
1071
1
    if (a._time_series_compaction_file_count_threshold !=
1072
1
        b._time_series_compaction_file_count_threshold)
1073
0
        return false;
1074
1
    if (a._time_series_compaction_time_threshold_seconds !=
1075
1
        b._time_series_compaction_time_threshold_seconds)
1076
0
        return false;
1077
1
    if (a._time_series_compaction_empty_rowsets_threshold !=
1078
1
        b._time_series_compaction_empty_rowsets_threshold)
1079
0
        return false;
1080
1
    if (a._time_series_compaction_level_threshold != b._time_series_compaction_level_threshold)
1081
0
        return false;
1082
1
    return true;
1083
1
}
1084
1085
0
bool operator!=(const TabletMeta& a, const TabletMeta& b) {
1086
0
    return !(a == b);
1087
0
}
1088
1089
1.02k
DeleteBitmap::DeleteBitmap(int64_t tablet_id) : _tablet_id(tablet_id) {
1090
    // The default delete bitmap cache is set to 100MB,
1091
    // which can be insufficient and cause performance issues when the amount of user data is large.
1092
    // To mitigate the problem of an inadequate cache,
1093
    // we will take the larger of 0.5% of the total memory and 100MB as the delete bitmap cache size.
1094
1.02k
    bool is_percent = false;
1095
1.02k
    int64_t delete_bitmap_agg_cache_cache_limit =
1096
1.02k
            ParseUtil::parse_mem_spec(config::delete_bitmap_dynamic_agg_cache_limit,
1097
1.02k
                                      MemInfo::mem_limit(), MemInfo::physical_mem(), &is_percent);
1098
1.02k
    _agg_cache.reset(new AggCache(delete_bitmap_agg_cache_cache_limit >
1099
1.02k
                                                  config::delete_bitmap_agg_cache_capacity
1100
1.02k
                                          ? delete_bitmap_agg_cache_cache_limit
1101
1.02k
                                          : config::delete_bitmap_agg_cache_capacity));
1102
1.02k
}
1103
1104
7
DeleteBitmap::DeleteBitmap(const DeleteBitmap& o) {
1105
7
    delete_bitmap = o.delete_bitmap; // just copy data
1106
7
    _tablet_id = o._tablet_id;
1107
7
}
1108
1109
0
DeleteBitmap& DeleteBitmap::operator=(const DeleteBitmap& o) {
1110
0
    delete_bitmap = o.delete_bitmap; // just copy data
1111
0
    _tablet_id = o._tablet_id;
1112
0
    return *this;
1113
0
}
1114
1115
0
DeleteBitmap::DeleteBitmap(DeleteBitmap&& o) {
1116
0
    delete_bitmap = std::move(o.delete_bitmap);
1117
0
    _tablet_id = o._tablet_id;
1118
0
}
1119
1120
0
DeleteBitmap& DeleteBitmap::operator=(DeleteBitmap&& o) {
1121
0
    delete_bitmap = std::move(o.delete_bitmap);
1122
0
    _tablet_id = o._tablet_id;
1123
0
    return *this;
1124
0
}
1125
1126
7
DeleteBitmap DeleteBitmap::snapshot() const {
1127
7
    std::shared_lock l(lock);
1128
7
    return DeleteBitmap(*this);
1129
7
}
1130
1131
3
DeleteBitmap DeleteBitmap::snapshot(Version version) const {
1132
    // Take snapshot first, then remove keys greater than given version.
1133
3
    DeleteBitmap snapshot = this->snapshot();
1134
3
    auto it = snapshot.delete_bitmap.begin();
1135
412
    while (it != snapshot.delete_bitmap.end()) {
1136
409
        if (std::get<2>(it->first) > version) {
1137
4
            it = snapshot.delete_bitmap.erase(it);
1138
405
        } else {
1139
405
            it++;
1140
405
        }
1141
409
    }
1142
3
    return snapshot;
1143
3
}
1144
1145
459k
void DeleteBitmap::add(const BitmapKey& bmk, uint32_t row_id) {
1146
459k
    std::lock_guard l(lock);
1147
459k
    delete_bitmap[bmk].add(row_id);
1148
459k
}
1149
1150
0
int DeleteBitmap::remove(const BitmapKey& bmk, uint32_t row_id) {
1151
0
    std::lock_guard l(lock);
1152
0
    auto it = delete_bitmap.find(bmk);
1153
0
    if (it == delete_bitmap.end()) return -1;
1154
0
    it->second.remove(row_id);
1155
0
    return 0;
1156
0
}
1157
1158
8
void DeleteBitmap::remove(const BitmapKey& start, const BitmapKey& end) {
1159
8
    std::lock_guard l(lock);
1160
107
    for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end();) {
1161
101
        auto& [k, _] = *it;
1162
101
        if (k >= end) {
1163
2
            break;
1164
2
        }
1165
99
        it = delete_bitmap.erase(it);
1166
99
    }
1167
8
}
1168
1169
6
bool DeleteBitmap::contains(const BitmapKey& bmk, uint32_t row_id) const {
1170
6
    std::shared_lock l(lock);
1171
6
    auto it = delete_bitmap.find(bmk);
1172
6
    return it != delete_bitmap.end() && it->second.contains(row_id);
1173
6
}
1174
1175
2
bool DeleteBitmap::contains_agg(const BitmapKey& bmk, uint32_t row_id) const {
1176
2
    return get_agg(bmk)->contains(row_id);
1177
2
}
1178
1179
0
bool DeleteBitmap::empty() const {
1180
0
    std::shared_lock l(lock);
1181
0
    return delete_bitmap.empty();
1182
0
}
1183
1184
63
uint64_t DeleteBitmap::cardinality() const {
1185
63
    std::shared_lock l(lock);
1186
63
    uint64_t res = 0;
1187
314
    for (auto entry : delete_bitmap) {
1188
314
        res += entry.second.cardinality();
1189
314
    }
1190
63
    return res;
1191
63
}
1192
1193
0
uint64_t DeleteBitmap::get_size() const {
1194
0
    std::shared_lock l(lock);
1195
0
    uint64_t charge = 0;
1196
0
    for (auto& [k, v] : delete_bitmap) {
1197
0
        charge += v.getSizeInBytes();
1198
0
    }
1199
0
    return charge;
1200
0
}
1201
1202
1
bool DeleteBitmap::contains_agg_without_cache(const BitmapKey& bmk, uint32_t row_id) const {
1203
1
    std::shared_lock l(lock);
1204
1
    DeleteBitmap::BitmapKey start {std::get<0>(bmk), std::get<1>(bmk), 0};
1205
1
    for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end(); ++it) {
1206
0
        auto& [k, bm] = *it;
1207
0
        if (std::get<0>(k) != std::get<0>(bmk) || std::get<1>(k) != std::get<1>(bmk) ||
1208
0
            std::get<2>(k) > std::get<2>(bmk)) {
1209
0
            break;
1210
0
        }
1211
0
        if (bm.contains(row_id)) {
1212
0
            return true;
1213
0
        }
1214
0
    }
1215
1
    return false;
1216
1
}
1217
1218
0
void DeleteBitmap::remove_sentinel_marks() {
1219
0
    std::lock_guard l(lock);
1220
0
    for (auto it = delete_bitmap.begin(), end = delete_bitmap.end(); it != end;) {
1221
0
        if (std::get<1>(it->first) == DeleteBitmap::INVALID_SEGMENT_ID) {
1222
0
            it = delete_bitmap.erase(it);
1223
0
        } else {
1224
0
            ++it;
1225
0
        }
1226
0
    }
1227
0
}
1228
1229
38
int DeleteBitmap::set(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap) {
1230
38
    std::lock_guard l(lock);
1231
38
    auto [_, inserted] = delete_bitmap.insert_or_assign(bmk, segment_delete_bitmap);
1232
38
    return inserted;
1233
38
}
1234
1235
3
int DeleteBitmap::get(const BitmapKey& bmk, roaring::Roaring* segment_delete_bitmap) const {
1236
3
    std::shared_lock l(lock);
1237
3
    auto it = delete_bitmap.find(bmk);
1238
3
    if (it == delete_bitmap.end()) return -1;
1239
3
    *segment_delete_bitmap = it->second; // copy
1240
3
    return 0;
1241
3
}
1242
1243
54
const roaring::Roaring* DeleteBitmap::get(const BitmapKey& bmk) const {
1244
54
    std::shared_lock l(lock);
1245
54
    auto it = delete_bitmap.find(bmk);
1246
54
    if (it == delete_bitmap.end()) return nullptr;
1247
41
    return &(it->second); // get address
1248
54
}
1249
1250
void DeleteBitmap::subset(const BitmapKey& start, const BitmapKey& end,
1251
3
                          DeleteBitmap* subset_rowset_map) const {
1252
3
    roaring::Roaring roaring;
1253
3
    DCHECK(start < end);
1254
3
    std::shared_lock l(lock);
1255
26
    for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end(); ++it) {
1256
25
        auto& [k, bm] = *it;
1257
25
        if (k >= end) {
1258
2
            break;
1259
2
        }
1260
23
        subset_rowset_map->set(k, bm);
1261
23
    }
1262
3
}
1263
1264
0
size_t DeleteBitmap::get_count_with_range(const BitmapKey& start, const BitmapKey& end) const {
1265
0
    DCHECK(start < end);
1266
0
    size_t count = 0;
1267
0
    std::shared_lock l(lock);
1268
0
    for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end(); ++it) {
1269
0
        auto& [k, bm] = *it;
1270
0
        if (k >= end) {
1271
0
            break;
1272
0
        }
1273
0
        count++;
1274
0
    }
1275
0
    return count;
1276
0
}
1277
1278
2
void DeleteBitmap::merge(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap) {
1279
2
    std::lock_guard l(lock);
1280
2
    auto [iter, succ] = delete_bitmap.emplace(bmk, segment_delete_bitmap);
1281
2
    if (!succ) {
1282
0
        iter->second |= segment_delete_bitmap;
1283
0
    }
1284
2
}
1285
1286
9
void DeleteBitmap::merge(const DeleteBitmap& other) {
1287
9
    std::lock_guard l(lock);
1288
29
    for (auto& i : other.delete_bitmap) {
1289
29
        auto [j, succ] = this->delete_bitmap.insert(i);
1290
29
        if (!succ) j->second |= i.second;
1291
29
    }
1292
9
}
1293
1294
void DeleteBitmap::add_to_remove_queue(
1295
        const std::string& version_str,
1296
        const std::vector<std::tuple<int64_t, DeleteBitmap::BitmapKey, DeleteBitmap::BitmapKey>>&
1297
0
                vector) {
1298
0
    std::shared_lock l(stale_delete_bitmap_lock);
1299
0
    _stale_delete_bitmap.emplace(version_str, vector);
1300
0
}
1301
1302
0
void DeleteBitmap::remove_stale_delete_bitmap_from_queue(const std::vector<std::string>& vector) {
1303
0
    if (!config::enable_delete_bitmap_merge_on_compaction) {
1304
0
        return;
1305
0
    }
1306
0
    std::shared_lock l(stale_delete_bitmap_lock);
1307
    //<rowset_id, start_version, end_version>
1308
0
    std::vector<std::tuple<std::string, uint64_t, uint64_t>> to_delete;
1309
0
    int64_t tablet_id = -1;
1310
0
    for (auto& version_str : vector) {
1311
0
        auto it = _stale_delete_bitmap.find(version_str);
1312
0
        if (it != _stale_delete_bitmap.end()) {
1313
0
            auto delete_bitmap_vector = it->second;
1314
0
            for (auto& delete_bitmap_tuple : it->second) {
1315
0
                if (tablet_id < 0) {
1316
0
                    tablet_id = std::get<0>(delete_bitmap_tuple);
1317
0
                }
1318
0
                auto start_bmk = std::get<1>(delete_bitmap_tuple);
1319
0
                auto end_bmk = std::get<2>(delete_bitmap_tuple);
1320
                // the key range of to be removed is [start_bmk,end_bmk),
1321
                // due to the different definitions of the right boundary,
1322
                // so use end_bmk as right boundary when removing local delete bitmap,
1323
                // use (end_bmk - 1) as right boundary when removing ms delete bitmap
1324
0
                remove(start_bmk, end_bmk);
1325
0
                to_delete.emplace_back(std::make_tuple(std::get<0>(start_bmk).to_string(), 0,
1326
0
                                                       std::get<2>(end_bmk) - 1));
1327
0
            }
1328
0
            _stale_delete_bitmap.erase(version_str);
1329
0
        }
1330
0
    }
1331
0
    if (tablet_id == -1 || to_delete.empty()) {
1332
0
        return;
1333
0
    }
1334
0
    CloudStorageEngine& engine = ExecEnv::GetInstance()->storage_engine().to_cloud();
1335
0
    auto st = engine.meta_mgr().remove_old_version_delete_bitmap(tablet_id, to_delete);
1336
0
    if (!st.ok()) {
1337
0
        LOG(WARNING) << "fail to remove_stale_delete_bitmap_from_queue for tablet=" << tablet_id
1338
0
                     << ",st=" << st;
1339
0
    }
1340
0
}
1341
1342
63
uint64_t DeleteBitmap::get_delete_bitmap_count() {
1343
63
    std::shared_lock l(lock);
1344
63
    uint64_t count = 0;
1345
377
    for (auto it = delete_bitmap.begin(); it != delete_bitmap.end(); it++) {
1346
314
        if (std::get<1>(it->first) != DeleteBitmap::INVALID_SEGMENT_ID) {
1347
314
            count++;
1348
314
        }
1349
314
    }
1350
63
    return count;
1351
63
}
1352
1353
0
bool DeleteBitmap::has_calculated_for_multi_segments(const RowsetId& rowset_id) const {
1354
0
    return contains({rowset_id, INVALID_SEGMENT_ID, TEMP_VERSION_COMMON}, ROWSET_SENTINEL_MARK);
1355
0
}
1356
1357
1
size_t DeleteBitmap::remove_rowset_cache_version(const RowsetId& rowset_id) {
1358
1
    std::lock_guard l(_rowset_cache_version_lock);
1359
1
    _rowset_cache_version.erase(rowset_id);
1360
1
    VLOG_DEBUG << "remove agg cache version for tablet=" << _tablet_id
1361
0
               << ", rowset=" << rowset_id.to_string();
1362
1
    return _rowset_cache_version.size();
1363
1
}
1364
1365
0
void DeleteBitmap::clear_rowset_cache_version() {
1366
0
    std::lock_guard l(_rowset_cache_version_lock);
1367
0
    _rowset_cache_version.clear();
1368
0
    VLOG_DEBUG << "clear agg cache version for tablet=" << _tablet_id;
1369
0
}
1370
1371
0
std::set<RowsetId> DeleteBitmap::get_rowset_cache_version() {
1372
0
    std::set<RowsetId> set;
1373
0
    std::shared_lock l(_rowset_cache_version_lock);
1374
0
    for (auto& [k, _] : _rowset_cache_version) {
1375
0
        set.insert(k);
1376
0
    }
1377
0
    return set;
1378
0
}
1379
1380
38
DeleteBitmap::Version DeleteBitmap::_get_rowset_cache_version(const BitmapKey& bmk) const {
1381
38
    std::shared_lock l(_rowset_cache_version_lock);
1382
38
    if (auto it = _rowset_cache_version.find(std::get<0>(bmk)); it != _rowset_cache_version.end()) {
1383
30
        auto& segment_cache_version = it->second;
1384
30
        if (auto it1 = segment_cache_version.find(std::get<1>(bmk));
1385
30
            it1 != segment_cache_version.end()) {
1386
1
            return it1->second;
1387
1
        }
1388
30
    }
1389
37
    return 0;
1390
38
}
1391
1392
// We cannot just copy the underlying memory to construct a string
1393
// due to equivalent objects may have different padding bytes.
1394
// Reading padding bytes is undefined behavior, neither copy nor
1395
// placement new will help simplify the code.
1396
// Refer to C11 standards §6.2.6.1/6 and §6.7.9/21 for more info.
1397
45
static std::string agg_cache_key(int64_t tablet_id, const DeleteBitmap::BitmapKey& bmk) {
1398
45
    std::string ret(sizeof(tablet_id) + sizeof(bmk), '\0');
1399
45
    *reinterpret_cast<int64_t*>(ret.data()) = tablet_id;
1400
45
    auto t = reinterpret_cast<DeleteBitmap::BitmapKey*>(ret.data() + sizeof(tablet_id));
1401
45
    std::get<RowsetId>(*t).version = std::get<RowsetId>(bmk).version;
1402
45
    std::get<RowsetId>(*t).hi = std::get<RowsetId>(bmk).hi;
1403
45
    std::get<RowsetId>(*t).mi = std::get<RowsetId>(bmk).mi;
1404
45
    std::get<RowsetId>(*t).lo = std::get<RowsetId>(bmk).lo;
1405
45
    std::get<1>(*t) = std::get<1>(bmk);
1406
45
    std::get<2>(*t) = std::get<2>(bmk);
1407
45
    return ret;
1408
45
}
1409
1410
44
std::shared_ptr<roaring::Roaring> DeleteBitmap::get_agg(const BitmapKey& bmk) const {
1411
44
    std::string key_str = agg_cache_key(_tablet_id, bmk); // Cache key container
1412
44
    CacheKey key(key_str);
1413
44
    Cache::Handle* handle = _agg_cache->repr()->lookup(key);
1414
1415
44
    AggCache::Value* val =
1416
44
            handle == nullptr
1417
44
                    ? nullptr
1418
44
                    : reinterpret_cast<AggCache::Value*>(_agg_cache->repr()->value(handle));
1419
    // FIXME: do we need a mutex here to get rid of duplicated initializations
1420
    //        of cache entries in some cases?
1421
44
    if (val == nullptr) { // Renew if needed, put a new Value to cache
1422
38
        val = new AggCache::Value();
1423
38
        Version start_version =
1424
38
                config::enable_mow_get_agg_by_cache ? _get_rowset_cache_version(bmk) : 0;
1425
38
        if (start_version > 0) {
1426
1
            Cache::Handle* handle2 = _agg_cache->repr()->lookup(
1427
1
                    agg_cache_key(_tablet_id, {std::get<0>(bmk), std::get<1>(bmk), start_version}));
1428
1429
1
            DBUG_EXECUTE_IF("DeleteBitmap::get_agg.cache_miss", {
1430
1
                if (handle2 != nullptr) {
1431
1
                    auto p = dp->param("percent", 0.3);
1432
1
                    std::mt19937 gen {std::random_device {}()};
1433
1
                    std::bernoulli_distribution inject_fault {p};
1434
1
                    if (inject_fault(gen)) {
1435
1
                        LOG_INFO("injection DeleteBitmap::get_agg.cache_miss, tablet_id={}",
1436
1
                                 _tablet_id);
1437
1
                        handle2 = nullptr;
1438
1
                    }
1439
1
                }
1440
1
            });
1441
1
            if (handle2 == nullptr || start_version > std::get<2>(bmk)) {
1442
0
                start_version = 0;
1443
1
            } else {
1444
1
                val->bitmap |=
1445
1
                        reinterpret_cast<AggCache::Value*>(_agg_cache->repr()->value(handle2))
1446
1
                                ->bitmap;
1447
1
                _agg_cache->repr()->release(handle2);
1448
1
                VLOG_DEBUG << "get agg cache version=" << start_version
1449
0
                           << " for tablet=" << _tablet_id
1450
0
                           << ", rowset=" << std::get<0>(bmk).to_string()
1451
0
                           << ", segment=" << std::get<1>(bmk);
1452
1
                start_version += 1;
1453
1
            }
1454
1
        }
1455
38
        {
1456
38
            std::shared_lock l(lock);
1457
38
            DeleteBitmap::BitmapKey start {std::get<0>(bmk), std::get<1>(bmk), start_version};
1458
66
            for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end(); ++it) {
1459
63
                auto& [k, bm] = *it;
1460
63
                if (std::get<0>(k) != std::get<0>(bmk) || std::get<1>(k) != std::get<1>(bmk) ||
1461
63
                    std::get<2>(k) > std::get<2>(bmk)) {
1462
35
                    break;
1463
35
                }
1464
28
                val->bitmap |= bm;
1465
28
            }
1466
38
        }
1467
38
        size_t charge = val->bitmap.getSizeInBytes() + sizeof(AggCache::Value);
1468
38
        handle = _agg_cache->repr()->insert(key, val, charge, charge, CachePriority::NORMAL);
1469
38
        if (config::enable_mow_get_agg_by_cache && !val->bitmap.isEmpty()) {
1470
27
            std::lock_guard l(_rowset_cache_version_lock);
1471
            // this version is already agg
1472
27
            _rowset_cache_version[std::get<0>(bmk)][std::get<1>(bmk)] = std::get<2>(bmk);
1473
27
            VLOG_DEBUG << "set agg cache version=" << std::get<2>(bmk)
1474
0
                       << " for tablet=" << _tablet_id
1475
0
                       << ", rowset=" << std::get<0>(bmk).to_string()
1476
0
                       << ", segment=" << std::get<1>(bmk);
1477
27
        }
1478
38
        if (start_version > 0 && config::enable_mow_get_agg_correctness_check_core) {
1479
0
            std::shared_ptr<roaring::Roaring> bitmap = get_agg_without_cache(bmk);
1480
0
            if (val->bitmap != *bitmap) {
1481
0
                CHECK(false) << ". get agg correctness check failed for tablet=" << _tablet_id
1482
0
                             << ", rowset=" << std::get<0>(bmk).to_string()
1483
0
                             << ", segment=" << std::get<1>(bmk) << ", version=" << std::get<2>(bmk)
1484
0
                             << ". start_version from cache=" << start_version
1485
0
                             << ", delete_bitmap cardinality with cache="
1486
0
                             << val->bitmap.cardinality()
1487
0
                             << ", delete_bitmap cardinality without cache="
1488
0
                             << bitmap->cardinality();
1489
0
            }
1490
0
        }
1491
38
    }
1492
1493
    // It is natural for the cache to reclaim the underlying memory
1494
44
    return std::shared_ptr<roaring::Roaring>(
1495
44
            &val->bitmap, [this, handle](...) { _agg_cache->repr()->release(handle); });
1496
44
}
1497
1498
0
std::shared_ptr<roaring::Roaring> DeleteBitmap::get_agg_without_cache(const BitmapKey& bmk) const {
1499
0
    std::shared_ptr<roaring::Roaring> bitmap = std::make_shared<roaring::Roaring>();
1500
0
    std::shared_lock l(lock);
1501
0
    DeleteBitmap::BitmapKey start {std::get<0>(bmk), std::get<1>(bmk), 0};
1502
0
    for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end(); ++it) {
1503
0
        auto& [k, bm] = *it;
1504
0
        if (std::get<0>(k) != std::get<0>(bmk) || std::get<1>(k) != std::get<1>(bmk) ||
1505
0
            std::get<2>(k) > std::get<2>(bmk)) {
1506
0
            break;
1507
0
        }
1508
0
        *bitmap |= bm;
1509
0
    }
1510
0
    return bitmap;
1511
0
}
1512
1513
std::atomic<DeleteBitmap::AggCachePolicy*> DeleteBitmap::AggCache::s_repr {nullptr};
1514
1515
0
std::string tablet_state_name(TabletState state) {
1516
0
    switch (state) {
1517
0
    case TABLET_NOTREADY:
1518
0
        return "TABLET_NOTREADY";
1519
1520
0
    case TABLET_RUNNING:
1521
0
        return "TABLET_RUNNING";
1522
1523
0
    case TABLET_TOMBSTONED:
1524
0
        return "TABLET_TOMBSTONED";
1525
1526
0
    case TABLET_STOPPED:
1527
0
        return "TABLET_STOPPED";
1528
1529
0
    case TABLET_SHUTDOWN:
1530
0
        return "TABLET_SHUTDOWN";
1531
1532
0
    default:
1533
0
        return "TabletState(" + std::to_string(state) + ")";
1534
0
    }
1535
0
}
1536
1537
#include "common/compile_check_end.h"
1538
} // namespace doris