Coverage Report

Created: 2026-06-05 10:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/storage/tablet/tablet_meta.cpp
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
#include "storage/tablet/tablet_meta.h"
19
20
#include <bvar/bvar.h>
21
#include <gen_cpp/Descriptors_types.h>
22
#include <gen_cpp/FrontendService_types.h>
23
#include <gen_cpp/Types_types.h>
24
#include <gen_cpp/olap_common.pb.h>
25
#include <gen_cpp/olap_file.pb.h>
26
#include <gen_cpp/segment_v2.pb.h>
27
#include <gen_cpp/types.pb.h>
28
#include <json2pb/pb_to_json.h>
29
#include <time.h>
30
31
#include <cstdint>
32
#include <memory>
33
#include <random>
34
#include <set>
35
#include <utility>
36
37
#include "cloud/cloud_meta_mgr.h"
38
#include "cloud/cloud_storage_engine.h"
39
#include "cloud/config.h"
40
#include "common/config.h"
41
#include "io/fs/file_writer.h"
42
#include "io/fs/local_file_system.h"
43
#include "storage/data_dir.h"
44
#include "storage/file_header.h"
45
#include "storage/olap_common.h"
46
#include "storage/olap_define.h"
47
#include "storage/rowset/rowset.h"
48
#include "storage/rowset/rowset_meta_manager.h"
49
#include "storage/tablet/tablet_fwd.h"
50
#include "storage/tablet/tablet_meta_manager.h"
51
#include "storage/tablet/tablet_schema_cache.h"
52
#include "storage/utils.h"
53
#include "util/debug_points.h"
54
#include "util/lru_cache.h"
55
#include "util/mem_info.h"
56
#include "util/parse_util.h"
57
#include "util/string_util.h"
58
#include "util/time.h"
59
#include "util/uid_util.h"
60
61
using std::string;
62
using std::unordered_map;
63
using std::vector;
64
65
namespace doris {
66
using namespace ErrorCode;
67
68
bvar::Adder<uint64_t> g_contains_agg_with_cache_if_eligible_total(
69
        "g_contains_agg_with_cache_if_eligible_total");
70
bvar::Adder<uint64_t> g_contains_agg_with_cache_if_eligible_partial_hit(
71
        "g_contains_agg_with_cache_if_eligible_partial_hit");
72
bvar::Adder<uint64_t> g_contains_agg_with_cache_if_eligible_full_hit(
73
        "g_contains_agg_with_cache_if_eligible_full_hit");
74
bvar::Window<bvar::Adder<uint64_t>> g_contains_agg_with_cache_if_eligible_total_minute(
75
        "g_contains_agg_with_cache_if_eligible_total_1m",
76
        &g_contains_agg_with_cache_if_eligible_total, 60);
77
bvar::Window<bvar::Adder<uint64_t>> g_contains_agg_with_cache_if_eligible_partial_hit_minute(
78
        "g_contains_agg_with_cache_if_eligible_partial_hit_1m",
79
        &g_contains_agg_with_cache_if_eligible_partial_hit, 60);
80
bvar::Window<bvar::Adder<uint64_t>> g_contains_agg_with_cache_if_eligible_full_hit_minute(
81
        "g_contains_agg_with_cache_if_eligible_full_hit_1m",
82
        &g_contains_agg_with_cache_if_eligible_full_hit, 60);
83
84
TabletMetaSharedPtr TabletMeta::create(
85
        const TCreateTabletReq& request, const TabletUid& tablet_uid, uint64_t shard_id,
86
        uint32_t next_unique_id,
87
7.85k
        const unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id) {
88
7.85k
    std::optional<TBinlogConfig> binlog_config;
89
7.85k
    if (request.__isset.binlog_config) {
90
7.66k
        binlog_config = request.binlog_config;
91
7.66k
    }
92
7.85k
    TInvertedIndexFileStorageFormat::type inverted_index_file_storage_format =
93
7.85k
            request.inverted_index_file_storage_format;
94
95
    // We will discard this format. Don't make any further changes here.
96
7.85k
    if (request.__isset.inverted_index_storage_format) {
97
7.85k
        switch (request.inverted_index_storage_format) {
98
0
        case TInvertedIndexStorageFormat::V1:
99
0
            inverted_index_file_storage_format = TInvertedIndexFileStorageFormat::V1;
100
0
            break;
101
0
        case TInvertedIndexStorageFormat::V2:
102
0
            inverted_index_file_storage_format = TInvertedIndexFileStorageFormat::V2;
103
0
            break;
104
7.85k
        default:
105
7.85k
            break;
106
7.85k
        }
107
7.85k
    }
108
    // Decide storage format for this tablet. DEFAULT / not-set fall back to V2 on BE side.
109
7.85k
    TStorageFormat::type storage_format =
110
7.85k
            request.__isset.storage_format ? request.storage_format : TStorageFormat::V2;
111
7.85k
    return std::make_shared<TabletMeta>(
112
7.85k
            request.table_id, request.partition_id, request.tablet_id, request.replica_id,
113
7.85k
            request.tablet_schema.schema_hash, shard_id, request.tablet_schema, next_unique_id,
114
7.85k
            col_ordinal_to_unique_id, tablet_uid,
115
7.85k
            request.__isset.tablet_type ? request.tablet_type : TTabletType::TABLET_TYPE_DISK,
116
7.85k
            request.__isset.compression_type ? request.compression_type : TCompressionType::LZ4F,
117
7.85k
            request.__isset.storage_policy_id ? request.storage_policy_id : -1,
118
7.85k
            request.__isset.enable_unique_key_merge_on_write
119
7.85k
                    ? request.enable_unique_key_merge_on_write
120
7.85k
                    : false,
121
7.85k
            std::move(binlog_config), request.compaction_policy,
122
7.85k
            request.time_series_compaction_goal_size_mbytes,
123
7.85k
            request.time_series_compaction_file_count_threshold,
124
7.85k
            request.time_series_compaction_time_threshold_seconds,
125
7.85k
            request.time_series_compaction_empty_rowsets_threshold,
126
7.85k
            request.time_series_compaction_level_threshold, inverted_index_file_storage_format,
127
7.85k
            request.tde_algorithm, storage_format,
128
7.85k
            request.__isset.vertical_compaction_num_columns_per_group
129
7.85k
                    ? request.vertical_compaction_num_columns_per_group
130
7.85k
                    : 5,
131
7.85k
            request.__isset.row_binlog_schema ? &request.row_binlog_schema : nullptr);
132
7.85k
}
133
134
376k
TabletMeta::~TabletMeta() {
135
376k
    if (_handle) {
136
375k
        TabletSchemaCache::instance()->release(_handle);
137
375k
    }
138
376k
}
139
140
TabletMeta::TabletMeta()
141
609k
        : _tablet_uid(0, 0),
142
609k
          _schema(new TabletSchema),
143
609k
          _delete_bitmap(new DeleteBitmap(_tablet_id)),
144
609k
          _binlog_delvec(new DeleteBitmap(_tablet_id)) {}
145
146
TabletMeta::TabletMeta(int64_t table_id, int64_t partition_id, int64_t tablet_id,
147
                       int64_t replica_id, int32_t schema_hash, int32_t shard_id,
148
                       const TTabletSchema& tablet_schema, uint32_t next_unique_id,
149
                       const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id,
150
                       TabletUid tablet_uid, TTabletType::type tabletType,
151
                       TCompressionType::type compression_type, int64_t storage_policy_id,
152
                       bool enable_unique_key_merge_on_write,
153
                       std::optional<TBinlogConfig> binlog_config, std::string compaction_policy,
154
                       int64_t time_series_compaction_goal_size_mbytes,
155
                       int64_t time_series_compaction_file_count_threshold,
156
                       int64_t time_series_compaction_time_threshold_seconds,
157
                       int64_t time_series_compaction_empty_rowsets_threshold,
158
                       int64_t time_series_compaction_level_threshold,
159
                       TInvertedIndexFileStorageFormat::type inverted_index_file_storage_format,
160
                       TEncryptionAlgorithm::type tde_algorithm,
161
                       TStorageFormat::type storage_format,
162
                       int32_t vertical_compaction_num_columns_per_group,
163
                       const TTabletSchema* row_binlog_schema)
164
8.23k
        : _tablet_uid(0, 0),
165
8.23k
          _schema(new TabletSchema),
166
8.23k
          _delete_bitmap(new DeleteBitmap(tablet_id)),
167
8.23k
          _binlog_delvec(new DeleteBitmap(tablet_id)),
168
8.23k
          _storage_format(storage_format) {
169
8.23k
    TabletMetaPB tablet_meta_pb;
170
8.23k
    tablet_meta_pb.set_table_id(table_id);
171
8.23k
    tablet_meta_pb.set_partition_id(partition_id);
172
8.23k
    tablet_meta_pb.set_tablet_id(tablet_id);
173
8.23k
    tablet_meta_pb.set_replica_id(replica_id);
174
8.23k
    tablet_meta_pb.set_schema_hash(schema_hash);
175
8.23k
    tablet_meta_pb.set_shard_id(shard_id);
176
    // Persist the creation time, but it is not used
177
8.23k
    tablet_meta_pb.set_creation_time(time(nullptr));
178
8.23k
    tablet_meta_pb.set_cumulative_layer_point(-1);
179
8.23k
    tablet_meta_pb.set_tablet_state(PB_RUNNING);
180
8.23k
    *(tablet_meta_pb.mutable_tablet_uid()) = tablet_uid.to_proto();
181
8.23k
    tablet_meta_pb.set_tablet_type(tabletType == TTabletType::TABLET_TYPE_DISK
182
8.23k
                                           ? TabletTypePB::TABLET_TYPE_DISK
183
8.23k
                                           : TabletTypePB::TABLET_TYPE_MEMORY);
184
8.23k
    tablet_meta_pb.set_enable_unique_key_merge_on_write(enable_unique_key_merge_on_write);
185
8.23k
    tablet_meta_pb.set_storage_policy_id(storage_policy_id);
186
8.23k
    tablet_meta_pb.set_compaction_policy(compaction_policy);
187
8.23k
    tablet_meta_pb.set_time_series_compaction_goal_size_mbytes(
188
8.23k
            time_series_compaction_goal_size_mbytes);
189
8.23k
    tablet_meta_pb.set_time_series_compaction_file_count_threshold(
190
8.23k
            time_series_compaction_file_count_threshold);
191
8.23k
    tablet_meta_pb.set_time_series_compaction_time_threshold_seconds(
192
8.23k
            time_series_compaction_time_threshold_seconds);
193
8.23k
    tablet_meta_pb.set_time_series_compaction_empty_rowsets_threshold(
194
8.23k
            time_series_compaction_empty_rowsets_threshold);
195
8.23k
    tablet_meta_pb.set_time_series_compaction_level_threshold(
196
8.23k
            time_series_compaction_level_threshold);
197
8.23k
    tablet_meta_pb.set_vertical_compaction_num_columns_per_group(
198
8.23k
            vertical_compaction_num_columns_per_group);
199
8.23k
    SchemaCreateOptions schema_create_options_for_data = {
200
8.23k
            .col_ordinal_to_unique_id = col_ordinal_to_unique_id,
201
8.23k
            .compression_type = compression_type,
202
8.23k
            .inverted_index_file_storage_format = inverted_index_file_storage_format,
203
8.23k
            .next_unique_id = next_unique_id};
204
8.23k
    TabletSchemaPB* schema_pb_for_data = tablet_meta_pb.mutable_schema();
205
8.23k
    init_schema_from_thrift(tablet_schema, schema_create_options_for_data, schema_pb_for_data);
206
207
8.23k
    tablet_meta_pb.set_in_restore_mode(false);
208
209
8.23k
    TabletSchemaPB* schema_pb_for_row_binlog = nullptr;
210
8.23k
    if (row_binlog_schema != nullptr) {
211
3
        tablet_meta_pb.set_row_binlog_schema_hash(row_binlog_schema->schema_hash);
212
3
        DCHECK(binlog_config.has_value());
213
3
        DCHECK(binlog_config->enable && binlog_config->binlog_format == TBinlogFormat::ROW);
214
215
3
        std::unordered_map<uint32_t, uint32_t> row_binlog_col_ordinal_to_unique_id;
216
3
        uint32_t row_binlog_next_unique_id = 0;
217
21
        for (uint32_t col_ordinal = 0; col_ordinal < row_binlog_schema->columns.size();
218
18
             ++col_ordinal) {
219
18
            const auto& tcolumn = row_binlog_schema->columns[col_ordinal];
220
18
            uint32_t unique_id = 0;
221
18
            if (tcolumn.col_unique_id >= 0) {
222
0
                unique_id = tcolumn.col_unique_id;
223
18
            } else {
224
18
                unique_id = col_ordinal;
225
18
            }
226
18
            row_binlog_col_ordinal_to_unique_id[col_ordinal] = unique_id;
227
18
            if (row_binlog_next_unique_id <= unique_id) {
228
18
                row_binlog_next_unique_id = unique_id + 1;
229
18
            }
230
18
        }
231
232
3
        SchemaCreateOptions schema_create_options_for_row_binlog = {
233
3
                .col_ordinal_to_unique_id = row_binlog_col_ordinal_to_unique_id,
234
3
                .compression_type = compression_type,
235
3
                .inverted_index_file_storage_format = inverted_index_file_storage_format,
236
3
                .next_unique_id = row_binlog_next_unique_id};
237
3
        schema_pb_for_row_binlog = tablet_meta_pb.mutable_row_binlog_schema();
238
3
        init_schema_from_thrift(*row_binlog_schema, schema_create_options_for_row_binlog,
239
3
                                schema_pb_for_row_binlog);
240
3
    }
241
8.23k
    if (binlog_config.has_value()) {
242
7.64k
        BinlogConfig tmp_binlog_config;
243
7.64k
        tmp_binlog_config = binlog_config.value();
244
7.64k
        tmp_binlog_config.to_pb(tablet_meta_pb.mutable_binlog_config());
245
7.64k
    }
246
247
8.23k
    switch (tde_algorithm) {
248
0
    case doris::TEncryptionAlgorithm::AES256:
249
0
        tablet_meta_pb.set_encryption_algorithm(EncryptionAlgorithmPB::AES_256_CTR);
250
0
        break;
251
0
    case doris::TEncryptionAlgorithm::SM4:
252
0
        tablet_meta_pb.set_encryption_algorithm(EncryptionAlgorithmPB::SM4_128_CTR);
253
0
        break;
254
8.21k
    default:
255
8.21k
        tablet_meta_pb.set_encryption_algorithm(EncryptionAlgorithmPB::PLAINTEXT);
256
8.23k
    }
257
258
    // Initialize default external ColumnMeta usage according to storage format.
259
    // V2: legacy behavior, inline ColumnMetaPB only.
260
    // V3: V2 + external ColumnMetaPB (CMO) enabled by default.
261
8.21k
    switch (_storage_format) {
262
8.21k
    case TStorageFormat::V2:
263
8.21k
    case TStorageFormat::DEFAULT:
264
8.21k
    case TStorageFormat::V1:
265
8.21k
        break;
266
0
    case TStorageFormat::V3:
267
0
        schema_pb_for_data->set_storage_format(TabletStorageFormatPB::TABLET_STORAGE_FORMAT_V3);
268
0
        _schema->set_storage_format(TabletStorageFormatPB::TABLET_STORAGE_FORMAT_V3);
269
0
        if (schema_pb_for_row_binlog != nullptr) {
270
0
            schema_pb_for_row_binlog->set_storage_format(
271
0
                    TabletStorageFormatPB::TABLET_STORAGE_FORMAT_V3);
272
0
        }
273
0
        break;
274
0
    default:
275
0
        break;
276
8.21k
    }
277
278
8.21k
    init_from_pb(tablet_meta_pb);
279
8.21k
}
280
281
TabletMeta::TabletMeta(const TabletMeta& b)
282
1.27k
        : MetadataAdder(b),
283
1.27k
          _table_id(b._table_id),
284
1.27k
          _index_id(b._index_id),
285
1.27k
          _partition_id(b._partition_id),
286
1.27k
          _tablet_id(b._tablet_id),
287
1.27k
          _replica_id(b._replica_id),
288
1.27k
          _schema_hash(b._schema_hash),
289
1.27k
          _shard_id(b._shard_id),
290
1.27k
          _creation_time(b._creation_time),
291
1.27k
          _cumulative_layer_point(b._cumulative_layer_point),
292
1.27k
          _tablet_uid(b._tablet_uid),
293
1.27k
          _tablet_type(b._tablet_type),
294
1.27k
          _tablet_state(b._tablet_state),
295
1.27k
          _schema(b._schema),
296
1.27k
          _rs_metas(b._rs_metas),
297
1.27k
          _stale_rs_metas(b._stale_rs_metas),
298
1.27k
          _in_restore_mode(b._in_restore_mode),
299
1.27k
          _preferred_rowset_type(b._preferred_rowset_type),
300
1.27k
          _storage_policy_id(b._storage_policy_id),
301
1.27k
          _cooldown_meta_id(b._cooldown_meta_id),
302
1.27k
          _enable_unique_key_merge_on_write(b._enable_unique_key_merge_on_write),
303
1.27k
          _delete_bitmap(b._delete_bitmap),
304
1.27k
          _binlog_delvec(b._binlog_delvec),
305
1.27k
          _row_binlog_schema_hash(b._row_binlog_schema_hash),
306
1.27k
          _row_binlog_schema(b._row_binlog_schema),
307
1.27k
          _row_binlog_rs_metas(b._row_binlog_rs_metas),
308
1.27k
          _binlog_config(b._binlog_config),
309
1.27k
          _compaction_policy(b._compaction_policy),
310
1.27k
          _time_series_compaction_goal_size_mbytes(b._time_series_compaction_goal_size_mbytes),
311
          _time_series_compaction_file_count_threshold(
312
1.27k
                  b._time_series_compaction_file_count_threshold),
313
          _time_series_compaction_time_threshold_seconds(
314
1.27k
                  b._time_series_compaction_time_threshold_seconds),
315
          _time_series_compaction_empty_rowsets_threshold(
316
1.27k
                  b._time_series_compaction_empty_rowsets_threshold),
317
1.27k
          _time_series_compaction_level_threshold(b._time_series_compaction_level_threshold),
318
          _vertical_compaction_num_columns_per_group(
319
1.27k
                  b._vertical_compaction_num_columns_per_group) {};
320
321
void TabletMeta::init_column_from_tcolumn(uint32_t unique_id, const TColumn& tcolumn,
322
25.8M
                                          ColumnPB* column) {
323
25.8M
    column->set_unique_id(unique_id);
324
25.8M
    column->set_name(tcolumn.column_name);
325
25.8M
    column->set_is_auto_increment(tcolumn.is_auto_increment);
326
25.8M
    if (tcolumn.__isset.is_on_update_current_timestamp) {
327
25.8M
        column->set_is_on_update_current_timestamp(tcolumn.is_on_update_current_timestamp);
328
25.8M
    }
329
25.8M
    string data_type;
330
25.8M
    EnumToString(TPrimitiveType, tcolumn.column_type.type, data_type);
331
25.8M
    column->set_type(data_type);
332
333
25.8M
    uint32_t length = TabletColumn::get_field_length_by_type(tcolumn.column_type.type,
334
25.8M
                                                             tcolumn.column_type.len);
335
25.8M
    column->set_length(length);
336
25.8M
    column->set_index_length(length);
337
25.8M
    column->set_precision(tcolumn.column_type.precision);
338
25.8M
    column->set_frac(tcolumn.column_type.scale);
339
340
25.8M
    if (tcolumn.__isset.result_is_nullable) {
341
2.74k
        column->set_result_is_nullable(tcolumn.result_is_nullable);
342
2.74k
    }
343
344
25.8M
    if (tcolumn.__isset.be_exec_version) {
345
25.8M
        column->set_be_exec_version(tcolumn.be_exec_version);
346
25.8M
    }
347
348
25.8M
    if (tcolumn.column_type.type == TPrimitiveType::VARCHAR ||
349
25.8M
        tcolumn.column_type.type == TPrimitiveType::STRING) {
350
10.5M
        if (!tcolumn.column_type.__isset.index_len) {
351
109
            column->set_index_length(10);
352
10.5M
        } else {
353
10.5M
            column->set_index_length(tcolumn.column_type.index_len);
354
10.5M
        }
355
10.5M
    }
356
25.8M
    if (!tcolumn.is_key) {
357
18.2M
        column->set_is_key(false);
358
18.2M
        if (tcolumn.__isset.aggregation) {
359
2.74k
            column->set_aggregation(tcolumn.aggregation);
360
18.2M
        } else {
361
18.2M
            string aggregation_type;
362
18.2M
            EnumToString(TAggregationType, tcolumn.aggregation_type, aggregation_type);
363
18.2M
            column->set_aggregation(aggregation_type);
364
18.2M
        }
365
18.2M
    } else {
366
7.54M
        column->set_is_key(true);
367
7.54M
        column->set_aggregation("NONE");
368
7.54M
    }
369
25.8M
    column->set_is_nullable(tcolumn.is_allow_null);
370
25.8M
    if (tcolumn.__isset.default_value) {
371
2.43M
        column->set_default_value(tcolumn.default_value);
372
2.43M
    }
373
25.8M
    if (tcolumn.__isset.is_bloom_filter_column) {
374
11.1k
        column->set_is_bf_column(tcolumn.is_bloom_filter_column);
375
11.1k
    }
376
25.8M
    if (tcolumn.__isset.visible) {
377
25.8M
        column->set_visible(tcolumn.visible);
378
25.8M
    }
379
28.2M
    for (size_t i = 0; i < tcolumn.children_column.size(); i++) {
380
2.39M
        ColumnPB* children_column = column->add_children_columns();
381
2.39M
        init_column_from_tcolumn(tcolumn.children_column[i].col_unique_id,
382
2.39M
                                 tcolumn.children_column[i], children_column);
383
2.39M
    }
384
25.8M
    if (tcolumn.column_type.__isset.variant_max_subcolumns_count) {
385
25.8M
        column->set_variant_max_subcolumns_count(tcolumn.column_type.variant_max_subcolumns_count);
386
25.8M
    }
387
25.8M
    if (tcolumn.__isset.pattern_type) {
388
40.8k
        switch (tcolumn.pattern_type) {
389
2.00k
        case TPatternType::MATCH_NAME:
390
2.00k
            column->set_pattern_type(PatternTypePB::MATCH_NAME);
391
2.00k
            break;
392
38.8k
        case TPatternType::MATCH_NAME_GLOB:
393
38.8k
            column->set_pattern_type(PatternTypePB::MATCH_NAME_GLOB);
394
40.8k
        }
395
40.8k
    }
396
25.8M
    if (tcolumn.__isset.variant_enable_typed_paths_to_sparse) {
397
25.8M
        column->set_variant_enable_typed_paths_to_sparse(
398
25.8M
                tcolumn.variant_enable_typed_paths_to_sparse);
399
25.8M
    }
400
25.8M
    if (tcolumn.__isset.variant_max_sparse_column_statistics_size) {
401
25.8M
        column->set_variant_max_sparse_column_statistics_size(
402
25.8M
                tcolumn.variant_max_sparse_column_statistics_size);
403
25.8M
    }
404
25.8M
    if (tcolumn.__isset.variant_sparse_hash_shard_count) {
405
23.4M
        column->set_variant_sparse_hash_shard_count(tcolumn.variant_sparse_hash_shard_count);
406
23.4M
    }
407
25.8M
    if (tcolumn.column_type.__isset.variant_enable_doc_mode) {
408
25.8M
        column->set_variant_enable_doc_mode(tcolumn.column_type.variant_enable_doc_mode);
409
25.8M
    }
410
25.8M
    if (tcolumn.__isset.variant_doc_materialization_min_rows) {
411
23.4M
        column->set_variant_doc_materialization_min_rows(
412
23.4M
                tcolumn.variant_doc_materialization_min_rows);
413
23.4M
    }
414
25.8M
    if (tcolumn.__isset.variant_doc_hash_shard_count) {
415
23.4M
        column->set_variant_doc_hash_shard_count(tcolumn.variant_doc_hash_shard_count);
416
23.4M
    }
417
25.8M
    if (tcolumn.__isset.variant_enable_nested_group) {
418
23.4M
        column->set_variant_enable_nested_group(tcolumn.variant_enable_nested_group);
419
23.4M
    }
420
25.8M
}
421
422
void TabletMeta::init_schema_from_thrift(const TTabletSchema& tablet_schema,
423
                                         const SchemaCreateOptions& schema_create_options,
424
8.23k
                                         TabletSchemaPB* tablet_schema_pb) {
425
8.23k
    const std::unordered_map<uint32_t, uint32_t>& col_ordinal_to_unique_id =
426
8.23k
            schema_create_options.col_ordinal_to_unique_id;
427
8.23k
    TCompressionType::type compression_type = schema_create_options.compression_type;
428
8.23k
    TInvertedIndexFileStorageFormat::type inverted_index_file_storage_format =
429
8.23k
            schema_create_options.inverted_index_file_storage_format;
430
8.23k
    uint32_t next_unique_id = schema_create_options.next_unique_id;
431
432
8.23k
    tablet_schema_pb->set_num_short_key_columns(tablet_schema.short_key_column_count);
433
8.23k
    tablet_schema_pb->set_num_rows_per_row_block(config::default_num_rows_per_column_file_block);
434
8.23k
    tablet_schema_pb->set_sequence_col_idx(tablet_schema.sequence_col_idx);
435
8.23k
    auto p_seq_map = tablet_schema_pb->mutable_seq_map(); // ColumnGroupsPB
436
8.23k
    for (auto& it : tablet_schema.seq_map) {              // std::vector< ::doris::TColumnGroup>
437
0
        uint32_t key = it.sequence_column;
438
0
        ColumnGroupPB* cg_pb = p_seq_map->add_cg(); // ColumnGroupPB {key: {v1, v2, v3}}
439
0
        cg_pb->set_sequence_column(key);
440
0
        for (auto v : it.columns_in_group) {
441
0
            cg_pb->add_columns_in_group(v);
442
0
        }
443
0
    }
444
445
8.23k
    switch (tablet_schema.keys_type) {
446
4.95k
    case TKeysType::DUP_KEYS:
447
4.95k
        tablet_schema_pb->set_keys_type(KeysType::DUP_KEYS);
448
4.95k
        break;
449
2.27k
    case TKeysType::UNIQUE_KEYS:
450
2.27k
        tablet_schema_pb->set_keys_type(KeysType::UNIQUE_KEYS);
451
2.27k
        break;
452
745
    case TKeysType::AGG_KEYS:
453
745
        tablet_schema_pb->set_keys_type(KeysType::AGG_KEYS);
454
745
        break;
455
261
    default:
456
261
        LOG(WARNING) << "unknown tablet keys type";
457
261
        break;
458
8.23k
    }
459
460
    // compress_kind used to compress segment files
461
8.23k
    tablet_schema_pb->set_compress_kind(COMPRESS_LZ4);
462
463
    // compression_type used to compress segment page
464
8.23k
    switch (compression_type) {
465
0
    case TCompressionType::NO_COMPRESSION:
466
0
        tablet_schema_pb->set_compression_type(segment_v2::NO_COMPRESSION);
467
0
        break;
468
0
    case TCompressionType::SNAPPY:
469
0
        tablet_schema_pb->set_compression_type(segment_v2::SNAPPY);
470
0
        break;
471
0
    case TCompressionType::LZ4:
472
0
        tablet_schema_pb->set_compression_type(segment_v2::LZ4);
473
0
        break;
474
578
    case TCompressionType::LZ4F:
475
578
        tablet_schema_pb->set_compression_type(segment_v2::LZ4F);
476
578
        break;
477
0
    case TCompressionType::ZLIB:
478
0
        tablet_schema_pb->set_compression_type(segment_v2::ZLIB);
479
0
        break;
480
7.66k
    case TCompressionType::ZSTD:
481
7.66k
        tablet_schema_pb->set_compression_type(segment_v2::ZSTD);
482
7.66k
        break;
483
0
    default:
484
0
        tablet_schema_pb->set_compression_type(segment_v2::LZ4F);
485
0
        break;
486
8.23k
    }
487
488
8.22k
    switch (inverted_index_file_storage_format) {
489
0
    case TInvertedIndexFileStorageFormat::V1:
490
0
        tablet_schema_pb->set_inverted_index_storage_format(InvertedIndexStorageFormatPB::V1);
491
0
        break;
492
578
    case TInvertedIndexFileStorageFormat::V2:
493
578
        tablet_schema_pb->set_inverted_index_storage_format(InvertedIndexStorageFormatPB::V2);
494
578
        break;
495
7.64k
    case TInvertedIndexFileStorageFormat::V3:
496
7.64k
        tablet_schema_pb->set_inverted_index_storage_format(InvertedIndexStorageFormatPB::V3);
497
7.64k
        break;
498
0
    default:
499
0
        tablet_schema_pb->set_inverted_index_storage_format(InvertedIndexStorageFormatPB::V3);
500
0
        break;
501
8.22k
    }
502
503
8.22k
    switch (tablet_schema.sort_type) {
504
0
    case TSortType::type::ZORDER:
505
0
        tablet_schema_pb->set_sort_type(SortType::ZORDER);
506
0
        break;
507
8.23k
    default:
508
8.23k
        tablet_schema_pb->set_sort_type(SortType::LEXICAL);
509
8.22k
    }
510
8.22k
    tablet_schema_pb->set_sort_col_num(tablet_schema.sort_col_num);
511
8.22k
    for (const auto& i : tablet_schema.cluster_key_uids) {
512
2
        tablet_schema_pb->add_cluster_key_uids(i);
513
2
    }
514
515
    // set column information
516
8.22k
    uint32_t col_ordinal = 0;
517
8.22k
    bool has_bf_columns = false;
518
48.1k
    for (TColumn tcolumn : tablet_schema.columns) {
519
48.1k
        ColumnPB* column = tablet_schema_pb->add_column();
520
48.1k
        uint32_t unique_id = -1;
521
48.1k
        if (tcolumn.col_unique_id >= 0) {
522
46.3k
            unique_id = tcolumn.col_unique_id;
523
46.3k
        } else {
524
1.85k
            unique_id = col_ordinal_to_unique_id.at(col_ordinal);
525
1.85k
        }
526
48.1k
        col_ordinal++;
527
48.1k
        init_column_from_tcolumn(unique_id, tcolumn, column);
528
529
48.1k
        if (column->is_bf_column()) {
530
0
            has_bf_columns = true;
531
0
        }
532
533
48.1k
        if (tablet_schema.__isset.indexes) {
534
2
            for (auto& index : tablet_schema.indexes) {
535
2
                if (index.index_type == TIndexType::type::BLOOMFILTER ||
536
2
                    index.index_type == TIndexType::type::NGRAM_BF) {
537
0
                    DCHECK_EQ(index.columns.size(), 1);
538
0
                    if (iequal(tcolumn.column_name, index.columns[0])) {
539
0
                        column->set_is_bf_column(true);
540
0
                        break;
541
0
                    }
542
0
                }
543
2
            }
544
2
        }
545
48.1k
    }
546
547
    // copy index meta
548
8.22k
    if (tablet_schema.__isset.indexes) {
549
1
        for (auto& index : tablet_schema.indexes) {
550
1
            TabletIndexPB* index_pb = tablet_schema_pb->add_index();
551
1
            index_pb->set_index_id(index.index_id);
552
1
            index_pb->set_index_name(index.index_name);
553
            // init col_unique_id in index at be side, since col_unique_id may be -1 at fe side
554
            // get column unique id by name
555
1
            for (auto column_name : index.columns) {
556
2
                for (auto column : tablet_schema_pb->column()) {
557
2
                    if (iequal(column.name(), column_name)) {
558
1
                        index_pb->add_col_unique_id(column.unique_id());
559
1
                    }
560
2
                }
561
1
            }
562
1
            switch (index.index_type) {
563
1
            case TIndexType::BITMAP:
564
1
                index_pb->set_index_type(IndexType::BITMAP);
565
1
                break;
566
0
            case TIndexType::INVERTED:
567
0
                index_pb->set_index_type(IndexType::INVERTED);
568
0
                break;
569
0
            case TIndexType::ANN:
570
0
                index_pb->set_index_type(IndexType::ANN);
571
0
                break;
572
0
            case TIndexType::BLOOMFILTER:
573
0
                index_pb->set_index_type(IndexType::BLOOMFILTER);
574
0
                break;
575
0
            case TIndexType::NGRAM_BF:
576
0
                index_pb->set_index_type(IndexType::NGRAM_BF);
577
0
                break;
578
1
            }
579
580
1
            if (index.__isset.properties) {
581
0
                auto properties = index_pb->mutable_properties();
582
0
                for (auto kv : index.properties) {
583
0
                    (*properties)[kv.first] = kv.second;
584
0
                }
585
0
            }
586
1
        }
587
1
    }
588
589
8.22k
    tablet_schema_pb->set_next_column_unique_id(next_unique_id);
590
8.22k
    if (has_bf_columns && tablet_schema.__isset.bloom_filter_fpp) {
591
0
        tablet_schema_pb->set_bf_fpp(tablet_schema.bloom_filter_fpp);
592
0
    }
593
594
8.22k
    if (tablet_schema.__isset.is_in_memory) {
595
7.65k
        tablet_schema_pb->set_is_in_memory(tablet_schema.is_in_memory);
596
7.65k
    }
597
598
8.22k
    if (tablet_schema.__isset.disable_auto_compaction) {
599
7.66k
        tablet_schema_pb->set_disable_auto_compaction(tablet_schema.disable_auto_compaction);
600
7.66k
    }
601
602
    // Deprecated legacy flatten-nested switch. Distinct from variant_enable_nested_group.
603
8.22k
    if (tablet_schema.__isset.variant_enable_flatten_nested) {
604
8.22k
        tablet_schema_pb->set_enable_variant_flatten_nested(
605
8.22k
                tablet_schema.variant_enable_flatten_nested);
606
8.22k
    }
607
608
8.23k
    if (tablet_schema.__isset.delete_sign_idx) {
609
8.23k
        tablet_schema_pb->set_delete_sign_idx(tablet_schema.delete_sign_idx);
610
8.23k
    }
611
8.23k
    if (tablet_schema.__isset.store_row_column) {
612
8.23k
        tablet_schema_pb->set_store_row_column(tablet_schema.store_row_column);
613
8.23k
    }
614
8.23k
    if (tablet_schema.__isset.row_store_page_size) {
615
8.23k
        tablet_schema_pb->set_row_store_page_size(tablet_schema.row_store_page_size);
616
8.23k
    }
617
8.23k
    if (tablet_schema.__isset.storage_page_size) {
618
8.23k
        tablet_schema_pb->set_storage_page_size(tablet_schema.storage_page_size);
619
8.23k
    }
620
8.23k
    if (tablet_schema.__isset.storage_dict_page_size) {
621
8.23k
        tablet_schema_pb->set_storage_dict_page_size(tablet_schema.storage_dict_page_size);
622
8.23k
    }
623
8.22k
    if (tablet_schema.__isset.skip_write_index_on_load) {
624
8.22k
        tablet_schema_pb->set_skip_write_index_on_load(tablet_schema.skip_write_index_on_load);
625
8.22k
    }
626
8.22k
    if (tablet_schema.__isset.row_store_col_cids) {
627
7.64k
        tablet_schema_pb->mutable_row_store_column_unique_ids()->Add(
628
7.64k
                tablet_schema.row_store_col_cids.begin(), tablet_schema.row_store_col_cids.end());
629
7.64k
    }
630
8.22k
}
631
632
80.3k
void TabletMeta::remove_rowset_delete_bitmap(const RowsetId& rowset_id, const Version& version) {
633
80.3k
    if (_enable_unique_key_merge_on_write) {
634
56.2k
        delete_bitmap().remove({rowset_id, 0, 0}, {rowset_id, UINT32_MAX, 0});
635
56.2k
        if (config::enable_mow_verbose_log) {
636
0
            LOG_INFO("delete rowset delete bitmap. tablet={}, rowset={}, version={}", tablet_id(),
637
0
                     rowset_id.to_string(), version.to_string());
638
0
        }
639
56.2k
        size_t rowset_cache_version_size = delete_bitmap().remove_rowset_cache_version(rowset_id);
640
56.2k
        _check_mow_rowset_cache_version_size(rowset_cache_version_size);
641
56.2k
    }
642
80.3k
}
643
644
176
Status TabletMeta::create_from_file(const string& file_path) {
645
176
    TabletMetaPB tablet_meta_pb;
646
176
    RETURN_IF_ERROR(load_from_file(file_path, &tablet_meta_pb));
647
176
    init_from_pb(tablet_meta_pb);
648
176
    return Status::OK();
649
176
}
650
651
358
Status TabletMeta::load_from_file(const string& file_path, TabletMetaPB* tablet_meta_pb) {
652
358
    FileHeader<TabletMetaPB> file_header(file_path);
653
    // In file_header.deserialize(), it validates file length, signature, checksum of protobuf.
654
358
    RETURN_IF_ERROR(file_header.deserialize());
655
358
    try {
656
358
        tablet_meta_pb->CopyFrom(file_header.message());
657
358
    } catch (const std::exception& e) {
658
0
        LOG(WARNING) << "Failed to copy protocol buffer object: " << e.what()
659
0
                     << ", file=" << file_path;
660
0
        return Status::Error<PARSE_PROTOBUF_ERROR>(
661
0
                "fail to copy protocol buffer object. file={}, error={}", file_path, e.what());
662
0
    }
663
358
    return Status::OK();
664
358
}
665
666
6
Status TabletMeta::create_from_buffer(const uint8_t* buffer, size_t buffer_size) {
667
6
    FileHeader<TabletMetaPB> file_header(""); // empty file path
668
6
    RETURN_IF_ERROR(file_header.deserialize_from_memory(buffer, buffer_size));
669
670
2
    TabletMetaPB tablet_meta_pb;
671
2
    try {
672
2
        tablet_meta_pb.CopyFrom(file_header.message());
673
2
    } catch (const std::exception& e) {
674
0
        LOG(WARNING) << "Failed to copy protocol buffer object from buffer: " << e.what();
675
0
        return Status::Error<ErrorCode::PARSE_PROTOBUF_ERROR>(
676
0
                "fail to copy protocol buffer object from buffer. error={}", e.what());
677
0
    }
678
679
2
    init_from_pb(tablet_meta_pb);
680
2
    return Status::OK();
681
2
}
682
683
std::string TabletMeta::construct_header_file_path(const string& schema_hash_path,
684
175
                                                   int64_t tablet_id) {
685
175
    std::stringstream header_name_stream;
686
175
    header_name_stream << schema_hash_path << "/" << tablet_id << ".hdr";
687
175
    return header_name_stream.str();
688
175
}
689
690
0
Status TabletMeta::save_as_json(const string& file_path) {
691
0
    std::string json_meta;
692
0
    json2pb::Pb2JsonOptions json_options;
693
0
    json_options.pretty_json = true;
694
0
    json_options.bytes_to_base64 = true;
695
0
    to_json(&json_meta, json_options);
696
    // save to file
697
0
    io::FileWriterPtr file_writer;
698
0
    RETURN_IF_ERROR(io::global_local_filesystem()->create_file(file_path, &file_writer));
699
0
    RETURN_IF_ERROR(file_writer->append(json_meta));
700
0
    RETURN_IF_ERROR(file_writer->close());
701
0
    return Status::OK();
702
0
}
703
704
5.09k
Status TabletMeta::save(const string& file_path) {
705
5.09k
    TabletMetaPB tablet_meta_pb;
706
5.09k
    to_meta_pb(&tablet_meta_pb, false);
707
5.09k
    return TabletMeta::save(file_path, tablet_meta_pb);
708
5.09k
}
709
710
5.27k
Status TabletMeta::save(const string& file_path, const TabletMetaPB& tablet_meta_pb) {
711
5.27k
    DCHECK(!file_path.empty());
712
5.27k
    FileHeader<TabletMetaPB> file_header(file_path);
713
5.27k
    try {
714
5.27k
        file_header.mutable_message()->CopyFrom(tablet_meta_pb);
715
5.27k
    } catch (...) {
716
0
        LOG(WARNING) << "fail to copy protocol buffer object. file='" << file_path;
717
0
        return Status::Error<ErrorCode::INTERNAL_ERROR>(
718
0
                "fail to copy protocol buffer object. file={}", file_path);
719
0
    }
720
5.27k
    RETURN_IF_ERROR(file_header.prepare());
721
5.27k
    RETURN_IF_ERROR(file_header.serialize());
722
5.27k
    return Status::OK();
723
5.27k
}
724
725
24.9k
Status TabletMeta::save_meta(DataDir* data_dir) {
726
24.9k
    std::lock_guard<std::shared_mutex> wrlock(_meta_lock);
727
24.9k
    return _save_meta(data_dir);
728
24.9k
}
729
730
24.7k
Status TabletMeta::_save_meta(DataDir* data_dir) {
731
    // check if tablet uid is valid
732
24.7k
    if (_tablet_uid.hi == 0 && _tablet_uid.lo == 0) {
733
0
        LOG(FATAL) << "tablet_uid is invalid"
734
0
                   << " tablet=" << tablet_id() << " _tablet_uid=" << _tablet_uid.to_string();
735
0
    }
736
24.7k
    string meta_binary;
737
738
24.7k
    auto t1 = MonotonicMicros();
739
24.7k
    serialize(&meta_binary);
740
24.7k
    auto t2 = MonotonicMicros();
741
24.7k
    Status status = TabletMetaManager::save(data_dir, tablet_id(), schema_hash(), meta_binary);
742
24.7k
    if (!status.ok()) {
743
0
        LOG(FATAL) << "fail to save tablet_meta. status=" << status << ", tablet_id=" << tablet_id()
744
0
                   << ", schema_hash=" << schema_hash();
745
0
    }
746
24.7k
    auto t3 = MonotonicMicros();
747
24.7k
    auto cost = t3 - t1;
748
24.7k
    if (cost > 1 * 1000 * 1000) {
749
0
        LOG(INFO) << "save tablet(" << tablet_id() << ") meta too slow. serialize cost " << t2 - t1
750
0
                  << "(us), serialized binary size: " << meta_binary.length()
751
0
                  << "(bytes), write rocksdb cost " << t3 - t2 << "(us)";
752
0
    }
753
24.7k
    return status;
754
24.7k
}
755
756
25.0k
void TabletMeta::serialize(string* meta_binary) {
757
25.0k
    TabletMetaPB tablet_meta_pb;
758
25.0k
    to_meta_pb(&tablet_meta_pb, false);
759
25.0k
    if (tablet_meta_pb.partition_id() <= 0) {
760
244
        LOG(WARNING) << "invalid partition id " << tablet_meta_pb.partition_id() << " tablet "
761
244
                     << tablet_meta_pb.tablet_id();
762
244
    }
763
25.0k
    DBUG_EXECUTE_IF("TabletMeta::serialize::zero_partition_id", {
764
25.0k
        long partition_id = tablet_meta_pb.partition_id();
765
25.0k
        tablet_meta_pb.set_partition_id(0);
766
25.0k
        LOG(WARNING) << "set debug point TabletMeta::serialize::zero_partition_id old="
767
25.0k
                     << partition_id << " new=" << tablet_meta_pb.DebugString();
768
25.0k
    });
769
25.0k
    bool serialize_success = tablet_meta_pb.SerializeToString(meta_binary);
770
25.0k
    if (!_rs_metas.empty() || !_stale_rs_metas.empty()) {
771
24.9k
        _avg_rs_meta_serialize_size =
772
24.9k
                meta_binary->length() / (_rs_metas.size() + _stale_rs_metas.size());
773
24.9k
        if (meta_binary->length() > config::tablet_meta_serialize_size_limit ||
774
25.0k
            !serialize_success) {
775
0
            int64_t origin_meta_size = meta_binary->length();
776
0
            int64_t stale_rowsets_num = tablet_meta_pb.stale_rs_metas().size();
777
0
            tablet_meta_pb.clear_stale_rs_metas();
778
0
            meta_binary->clear();
779
0
            serialize_success = tablet_meta_pb.SerializeToString(meta_binary);
780
0
            LOG(WARNING) << "tablet meta serialization size exceeds limit: "
781
0
                         << config::tablet_meta_serialize_size_limit
782
0
                         << " clean up stale rowsets, tablet id: " << tablet_id()
783
0
                         << " stale rowset num: " << stale_rowsets_num
784
0
                         << " serialization size before clean " << origin_meta_size
785
0
                         << " serialization size after clean " << meta_binary->length();
786
0
        }
787
24.9k
    }
788
789
25.0k
    if (!serialize_success) {
790
0
        LOG(FATAL) << "failed to serialize meta " << tablet_id();
791
0
    }
792
25.0k
}
793
794
288k
Status TabletMeta::deserialize(std::string_view meta_binary) {
795
288k
    TabletMetaPB tablet_meta_pb;
796
288k
    bool parsed = tablet_meta_pb.ParseFromArray(meta_binary.data(),
797
288k
                                                static_cast<int32_t>(meta_binary.size()));
798
288k
    if (!parsed) {
799
0
        return Status::Error<INIT_FAILED>("parse tablet meta failed");
800
0
    }
801
288k
    init_from_pb(tablet_meta_pb);
802
288k
    return Status::OK();
803
288k
}
804
805
616k
void TabletMeta::init_from_pb(const TabletMetaPB& tablet_meta_pb) {
806
616k
    _table_id = tablet_meta_pb.table_id();
807
616k
    _index_id = tablet_meta_pb.index_id();
808
616k
    _partition_id = tablet_meta_pb.partition_id();
809
616k
    _tablet_id = tablet_meta_pb.tablet_id();
810
616k
    _replica_id = tablet_meta_pb.replica_id();
811
616k
    _schema_hash = tablet_meta_pb.schema_hash();
812
616k
    _shard_id = tablet_meta_pb.shard_id();
813
616k
    _creation_time = tablet_meta_pb.creation_time();
814
616k
    _cumulative_layer_point = tablet_meta_pb.cumulative_layer_point();
815
616k
    _tablet_uid = TabletUid(tablet_meta_pb.tablet_uid());
816
616k
    _ttl_seconds = tablet_meta_pb.ttl_seconds();
817
616k
    if (tablet_meta_pb.has_tablet_type()) {
818
616k
        _tablet_type = tablet_meta_pb.tablet_type();
819
18.4E
    } else {
820
18.4E
        _tablet_type = TabletTypePB::TABLET_TYPE_DISK;
821
18.4E
    }
822
823
    // init _tablet_state
824
616k
    switch (tablet_meta_pb.tablet_state()) {
825
19.4k
    case PB_NOTREADY:
826
19.4k
        _tablet_state = TabletState::TABLET_NOTREADY;
827
19.4k
        break;
828
590k
    case PB_RUNNING:
829
590k
        _tablet_state = TabletState::TABLET_RUNNING;
830
590k
        break;
831
0
    case PB_TOMBSTONED:
832
0
        _tablet_state = TabletState::TABLET_TOMBSTONED;
833
0
        break;
834
0
    case PB_STOPPED:
835
0
        _tablet_state = TabletState::TABLET_STOPPED;
836
0
        break;
837
5.78k
    case PB_SHUTDOWN:
838
5.78k
        _tablet_state = TabletState::TABLET_SHUTDOWN;
839
5.78k
        break;
840
0
    default:
841
0
        LOG(WARNING) << "tablet has no state. tablet=" << tablet_id()
842
0
                     << ", schema_hash=" << schema_hash();
843
616k
    }
844
845
    // init _schema
846
616k
    TabletSchemaSPtr schema = std::make_shared<TabletSchema>();
847
616k
    schema->init_from_pb(tablet_meta_pb.schema());
848
616k
    if (_handle) {
849
4
        TabletSchemaCache::instance()->release(_handle);
850
4
    }
851
616k
    auto pair = TabletSchemaCache::instance()->insert(schema->to_key());
852
616k
    _handle = pair.first;
853
616k
    _schema = pair.second;
854
855
616k
    if (tablet_meta_pb.has_row_binlog_schema()) {
856
47
        TabletSchemaSPtr row_binlog_schema = std::make_shared<TabletSchema>();
857
47
        row_binlog_schema->init_from_pb(tablet_meta_pb.row_binlog_schema());
858
47
        _row_binlog_schema = std::move(row_binlog_schema);
859
47
        _row_binlog_schema_hash = tablet_meta_pb.row_binlog_schema_hash();
860
47
    }
861
862
617k
    if (tablet_meta_pb.has_enable_unique_key_merge_on_write()) {
863
617k
        _enable_unique_key_merge_on_write = tablet_meta_pb.enable_unique_key_merge_on_write();
864
617k
        _delete_bitmap->set_tablet_id(_tablet_id);
865
617k
        _binlog_delvec->set_tablet_id(_tablet_id);
866
617k
    }
867
868
    // init _rs_metas
869
616k
    for (auto& it : tablet_meta_pb.rs_metas()) {
870
494k
        RowsetMetaSharedPtr rs_meta(new RowsetMeta());
871
494k
        rs_meta->init_from_pb(it);
872
494k
        _rs_metas.emplace(rs_meta->version(), rs_meta);
873
494k
    }
874
875
    // For mow table, delete bitmap of stale rowsets has not been persisted.
876
    // When be restart, query should not read the stale rowset, otherwise duplicate keys
877
    // will be read out. Therefore, we don't add them to _stale_rs_meta for mow table.
878
617k
    if (!config::skip_loading_stale_rowset_meta && !_enable_unique_key_merge_on_write) {
879
481k
        for (auto& it : tablet_meta_pb.stale_rs_metas()) {
880
7.25k
            RowsetMetaSharedPtr rs_meta(new RowsetMeta());
881
7.25k
            rs_meta->init_from_pb(it);
882
7.25k
            _stale_rs_metas.emplace(rs_meta->version(), rs_meta);
883
7.25k
        }
884
481k
    }
885
886
616k
    for (auto& it : tablet_meta_pb.row_binlog_rs_metas()) {
887
70
        RowsetMetaSharedPtr rs_meta(new RowsetMeta());
888
70
        rs_meta->init_from_pb(it);
889
70
        _row_binlog_rs_metas.emplace(rs_meta->version(), rs_meta);
890
70
    }
891
892
617k
    if (tablet_meta_pb.has_in_restore_mode()) {
893
617k
        _in_restore_mode = tablet_meta_pb.in_restore_mode();
894
617k
    }
895
896
616k
    if (tablet_meta_pb.has_preferred_rowset_type()) {
897
609k
        _preferred_rowset_type = tablet_meta_pb.preferred_rowset_type();
898
609k
    }
899
900
616k
    _storage_policy_id = tablet_meta_pb.storage_policy_id();
901
616k
    if (tablet_meta_pb.has_cooldown_meta_id()) {
902
319k
        _cooldown_meta_id = tablet_meta_pb.cooldown_meta_id();
903
319k
    }
904
905
616k
    if (tablet_meta_pb.has_delete_bitmap()) {
906
51.2k
        int rst_ids_size = tablet_meta_pb.delete_bitmap().rowset_ids_size();
907
51.2k
        int seg_ids_size = tablet_meta_pb.delete_bitmap().segment_ids_size();
908
51.2k
        int versions_size = tablet_meta_pb.delete_bitmap().versions_size();
909
51.2k
        int seg_maps_size = tablet_meta_pb.delete_bitmap().segment_delete_bitmaps_size();
910
51.2k
        int binlog_mark_size = tablet_meta_pb.delete_bitmap().is_binlog_delvec_size();
911
51.2k
        CHECK(rst_ids_size == seg_ids_size && seg_ids_size == seg_maps_size &&
912
51.2k
              seg_maps_size == versions_size);
913
51.2k
        CHECK(binlog_mark_size == 0 || binlog_mark_size == rst_ids_size);
914
55.2k
        for (int i = 0; i < rst_ids_size; ++i) {
915
3.94k
            RowsetId rst_id;
916
3.94k
            rst_id.init(tablet_meta_pb.delete_bitmap().rowset_ids(i));
917
3.94k
            auto seg_id = tablet_meta_pb.delete_bitmap().segment_ids(i);
918
3.94k
            auto ver = tablet_meta_pb.delete_bitmap().versions(i);
919
3.94k
            auto bitmap = tablet_meta_pb.delete_bitmap().segment_delete_bitmaps(i).data();
920
3.94k
            bool from_binlog = tablet_meta_pb.delete_bitmap().is_binlog_delvec_size() > 0
921
3.94k
                                       ? tablet_meta_pb.delete_bitmap().is_binlog_delvec(i)
922
3.94k
                                       : false;
923
3.94k
            if (!from_binlog) {
924
3.93k
                delete_bitmap().delete_bitmap[{rst_id, seg_id, ver}] =
925
3.93k
                        roaring::Roaring::read(bitmap);
926
3.93k
            } else {
927
12
                binlog_delvec().delete_bitmap[{rst_id, seg_id, ver}] =
928
12
                        roaring::Roaring::read(bitmap);
929
12
            }
930
3.94k
        }
931
51.2k
    }
932
933
616k
    if (tablet_meta_pb.has_binlog_config()) {
934
296k
        _binlog_config = tablet_meta_pb.binlog_config();
935
296k
    }
936
616k
    _compaction_policy = tablet_meta_pb.compaction_policy();
937
616k
    _time_series_compaction_goal_size_mbytes =
938
616k
            tablet_meta_pb.time_series_compaction_goal_size_mbytes();
939
616k
    _time_series_compaction_file_count_threshold =
940
616k
            tablet_meta_pb.time_series_compaction_file_count_threshold();
941
616k
    _time_series_compaction_time_threshold_seconds =
942
616k
            tablet_meta_pb.time_series_compaction_time_threshold_seconds();
943
616k
    _time_series_compaction_empty_rowsets_threshold =
944
616k
            tablet_meta_pb.time_series_compaction_empty_rowsets_threshold();
945
616k
    _time_series_compaction_level_threshold =
946
616k
            tablet_meta_pb.time_series_compaction_level_threshold();
947
616k
    _vertical_compaction_num_columns_per_group =
948
616k
            tablet_meta_pb.vertical_compaction_num_columns_per_group();
949
950
617k
    if (tablet_meta_pb.has_encryption_algorithm()) {
951
617k
        _encryption_algorithm = tablet_meta_pb.encryption_algorithm();
952
617k
    }
953
954
616k
    if (tablet_meta_pb.has_row_binlog_schema_hash()) {
955
47
        _row_binlog_schema_hash = tablet_meta_pb.row_binlog_schema_hash();
956
47
    }
957
616k
}
958
959
30.4k
void TabletMeta::to_meta_pb(TabletMetaPB* tablet_meta_pb, bool cloud_get_rowset_meta) {
960
30.4k
    tablet_meta_pb->set_table_id(table_id());
961
30.4k
    tablet_meta_pb->set_index_id(index_id());
962
30.4k
    tablet_meta_pb->set_partition_id(partition_id());
963
30.4k
    tablet_meta_pb->set_tablet_id(tablet_id());
964
30.4k
    tablet_meta_pb->set_replica_id(replica_id());
965
30.4k
    tablet_meta_pb->set_schema_hash(schema_hash());
966
30.4k
    tablet_meta_pb->set_shard_id(shard_id());
967
30.4k
    tablet_meta_pb->set_creation_time(creation_time());
968
30.4k
    tablet_meta_pb->set_cumulative_layer_point(cumulative_layer_point());
969
30.4k
    *(tablet_meta_pb->mutable_tablet_uid()) = tablet_uid().to_proto();
970
30.4k
    tablet_meta_pb->set_tablet_type(_tablet_type);
971
30.4k
    tablet_meta_pb->set_ttl_seconds(_ttl_seconds);
972
30.4k
    switch (tablet_state()) {
973
9
    case TABLET_NOTREADY:
974
9
        tablet_meta_pb->set_tablet_state(PB_NOTREADY);
975
9
        break;
976
20.7k
    case TABLET_RUNNING:
977
20.7k
        tablet_meta_pb->set_tablet_state(PB_RUNNING);
978
20.7k
        break;
979
0
    case TABLET_TOMBSTONED:
980
0
        tablet_meta_pb->set_tablet_state(PB_TOMBSTONED);
981
0
        break;
982
0
    case TABLET_STOPPED:
983
0
        tablet_meta_pb->set_tablet_state(PB_STOPPED);
984
0
        break;
985
9.60k
    case TABLET_SHUTDOWN:
986
9.60k
        tablet_meta_pb->set_tablet_state(PB_SHUTDOWN);
987
9.60k
        break;
988
30.4k
    }
989
990
    // RowsetMetaPB is separated from TabletMetaPB
991
30.4k
    if (!config::is_cloud_mode() || cloud_get_rowset_meta) {
992
67.9k
        for (const auto& [_, rs] : _rs_metas) {
993
67.9k
            rs->to_rowset_pb(tablet_meta_pb->add_rs_metas());
994
67.9k
        }
995
389k
        for (const auto& [_, rs] : _stale_rs_metas) {
996
389k
            rs->to_rowset_pb(tablet_meta_pb->add_stale_rs_metas());
997
389k
        }
998
30.4k
        for (const auto& [_, rs] : _row_binlog_rs_metas) {
999
6
            rs->to_rowset_pb(tablet_meta_pb->add_row_binlog_rs_metas());
1000
6
        }
1001
30.4k
    }
1002
1003
30.4k
    _schema->to_schema_pb(tablet_meta_pb->mutable_schema());
1004
1005
30.4k
    if (_row_binlog_schema != nullptr) {
1006
6
        _row_binlog_schema->to_schema_pb(tablet_meta_pb->mutable_row_binlog_schema());
1007
6
        tablet_meta_pb->set_row_binlog_schema_hash(_row_binlog_schema_hash);
1008
6
    }
1009
1010
30.4k
    tablet_meta_pb->set_in_restore_mode(in_restore_mode());
1011
1012
    // to avoid modify tablet meta to the greatest extend
1013
30.5k
    if (_preferred_rowset_type == BETA_ROWSET) {
1014
30.5k
        tablet_meta_pb->set_preferred_rowset_type(_preferred_rowset_type);
1015
30.5k
    }
1016
30.4k
    if (_storage_policy_id > 0) {
1017
5
        tablet_meta_pb->set_storage_policy_id(_storage_policy_id);
1018
5
    }
1019
30.4k
    if (_cooldown_meta_id.initialized()) {
1020
5
        tablet_meta_pb->mutable_cooldown_meta_id()->CopyFrom(_cooldown_meta_id.to_proto());
1021
5
    }
1022
1023
30.4k
    tablet_meta_pb->set_enable_unique_key_merge_on_write(_enable_unique_key_merge_on_write);
1024
1025
30.4k
    if (_enable_unique_key_merge_on_write) {
1026
9.69k
        std::set<RowsetId> stale_rs_ids;
1027
321k
        for (const auto& [_, rowset] : _stale_rs_metas) {
1028
321k
            stale_rs_ids.insert(rowset->rowset_id());
1029
321k
        }
1030
9.69k
        DeleteBitmapPB* delete_bitmap_pb = tablet_meta_pb->mutable_delete_bitmap();
1031
15.2k
        for (auto& [id, bitmap] : delete_bitmap().snapshot().delete_bitmap) {
1032
15.2k
            auto& [rowset_id, segment_id, ver] = id;
1033
15.2k
            if (stale_rs_ids.count(rowset_id) != 0) {
1034
13.8k
                continue;
1035
13.8k
            }
1036
1.41k
            delete_bitmap_pb->add_rowset_ids(rowset_id.to_string());
1037
1.41k
            delete_bitmap_pb->add_segment_ids(segment_id);
1038
1.41k
            delete_bitmap_pb->add_versions(ver);
1039
1.41k
            delete_bitmap_pb->add_is_binlog_delvec(false);
1040
1.41k
            std::string bitmap_data(bitmap.getSizeInBytes(), '\0');
1041
1.41k
            bitmap.write(bitmap_data.data());
1042
1.41k
            *(delete_bitmap_pb->add_segment_delete_bitmaps()) = std::move(bitmap_data);
1043
1.41k
        }
1044
1045
9.69k
        for (auto& [id, bitmap] : binlog_delvec().snapshot().delete_bitmap) {
1046
0
            auto& [rowset_id, segment_id, ver] = id;
1047
0
            delete_bitmap_pb->add_rowset_ids(rowset_id.to_string());
1048
0
            delete_bitmap_pb->add_segment_ids(segment_id);
1049
0
            delete_bitmap_pb->add_versions(ver);
1050
0
            delete_bitmap_pb->add_is_binlog_delvec(true);
1051
0
            std::string bitmap_data(bitmap.getSizeInBytes(), '\0');
1052
0
            bitmap.write(bitmap_data.data());
1053
0
            *(delete_bitmap_pb->add_segment_delete_bitmaps()) = std::move(bitmap_data);
1054
0
        }
1055
9.69k
    }
1056
30.4k
    _binlog_config.to_pb(tablet_meta_pb->mutable_binlog_config());
1057
30.4k
    tablet_meta_pb->set_compaction_policy(compaction_policy());
1058
30.4k
    tablet_meta_pb->set_time_series_compaction_goal_size_mbytes(
1059
30.4k
            time_series_compaction_goal_size_mbytes());
1060
30.4k
    tablet_meta_pb->set_time_series_compaction_file_count_threshold(
1061
30.4k
            time_series_compaction_file_count_threshold());
1062
30.4k
    tablet_meta_pb->set_time_series_compaction_time_threshold_seconds(
1063
30.4k
            time_series_compaction_time_threshold_seconds());
1064
30.4k
    tablet_meta_pb->set_time_series_compaction_empty_rowsets_threshold(
1065
30.4k
            time_series_compaction_empty_rowsets_threshold());
1066
30.4k
    tablet_meta_pb->set_time_series_compaction_level_threshold(
1067
30.4k
            time_series_compaction_level_threshold());
1068
30.4k
    tablet_meta_pb->set_vertical_compaction_num_columns_per_group(
1069
30.4k
            vertical_compaction_num_columns_per_group());
1070
1071
30.4k
    tablet_meta_pb->set_encryption_algorithm(_encryption_algorithm);
1072
30.4k
}
1073
1074
4
void TabletMeta::to_json(string* json_string, json2pb::Pb2JsonOptions& options) {
1075
4
    TabletMetaPB tablet_meta_pb;
1076
4
    to_meta_pb(&tablet_meta_pb, false);
1077
4
    json2pb::ProtoMessageToJson(tablet_meta_pb, json_string, options);
1078
4
}
1079
1080
1.66M
Version TabletMeta::max_version() const {
1081
1.66M
    Version max_version = {-1, 0};
1082
5.52M
    for (const auto& [_, rs_meta] : _rs_metas) {
1083
5.52M
        if (rs_meta->end_version() > max_version.second) {
1084
2.06M
            max_version = rs_meta->version();
1085
2.06M
        }
1086
5.52M
    }
1087
1.66M
    return max_version;
1088
1.66M
}
1089
1090
971k
size_t TabletMeta::version_count_cross_with_range(const Version& range) const {
1091
971k
    size_t count = 0;
1092
1.74M
    for (const auto& [_, rs_meta] : _rs_metas) {
1093
1.74M
        if (!(range.first > rs_meta->version().second || range.second < rs_meta->version().first)) {
1094
1.74M
            count++;
1095
1.74M
        }
1096
1.74M
    }
1097
971k
    return count;
1098
971k
}
1099
1100
58.7k
Status TabletMeta::add_rs_meta(const RowsetMetaSharedPtr& rs_meta) {
1101
    // check RowsetMeta is valid
1102
737k
    for (const auto& [_, rs] : _rs_metas) {
1103
737k
        if (rs->version() == rs_meta->version()) {
1104
0
            if (rs->rowset_id() != rs_meta->rowset_id()) {
1105
0
                return Status::Error<PUSH_VERSION_ALREADY_EXIST>(
1106
0
                        "version already exist. rowset_id={}, version={}, tablet={}",
1107
0
                        rs->rowset_id().to_string(), rs->version().to_string(), tablet_id());
1108
0
            } else {
1109
                // rowsetid,version is equal, it is a duplicate req, skip it
1110
0
                return Status::OK();
1111
0
            }
1112
0
        }
1113
737k
    }
1114
58.7k
    _rs_metas.emplace(rs_meta->version(), rs_meta);
1115
58.7k
    return Status::OK();
1116
58.7k
}
1117
1118
3
Status TabletMeta::add_row_binlog_rs_meta(const RowsetMetaSharedPtr& row_binlog_meta) {
1119
    // check RowsetMeta is valid
1120
3
    for (auto& [_, rs] : _row_binlog_rs_metas) {
1121
0
        if (rs->version() == row_binlog_meta->version()) {
1122
0
            if (rs->rowset_id() != row_binlog_meta->rowset_id()) {
1123
0
                return Status::Error<PUSH_VERSION_ALREADY_EXIST>(
1124
0
                        "binlog version already exist. binlog_rowset_id={}, version={}, tablet={}",
1125
0
                        rs->rowset_id().to_string(), rs->version().to_string(), tablet_id());
1126
0
            } else {
1127
                // rowsetid,version is equal, it is a duplicate req, skip it
1128
0
                return Status::OK();
1129
0
            }
1130
0
        }
1131
0
    }
1132
3
    _row_binlog_rs_metas.emplace(row_binlog_meta->version(), row_binlog_meta);
1133
3
    return Status::OK();
1134
3
}
1135
1136
310k
void TabletMeta::add_rowsets_unchecked(const std::vector<RowsetSharedPtr>& to_add) {
1137
324k
    for (const auto& rs : to_add) {
1138
324k
        _rs_metas.emplace(rs->rowset_meta()->version(), rs->rowset_meta());
1139
324k
    }
1140
310k
}
1141
1142
void TabletMeta::delete_rs_meta_by_version(const Version& version,
1143
836
                                           std::vector<RowsetMetaSharedPtr>* deleted_rs_metas) {
1144
836
    size_t rowset_cache_version_size = 0;
1145
836
    if (auto it = _rs_metas.find(version); it != _rs_metas.end()) {
1146
836
        if (deleted_rs_metas != nullptr) {
1147
0
            deleted_rs_metas->push_back(it->second);
1148
0
        }
1149
836
        auto rowset_id = it->second->rowset_id();
1150
836
        _rs_metas.erase(it);
1151
836
        if (_enable_unique_key_merge_on_write) {
1152
46
            rowset_cache_version_size = _delete_bitmap->remove_rowset_cache_version(rowset_id);
1153
46
        }
1154
836
        return;
1155
836
    }
1156
0
    _check_mow_rowset_cache_version_size(rowset_cache_version_size);
1157
0
}
1158
1159
void TabletMeta::modify_rs_metas(const std::vector<RowsetMetaSharedPtr>& to_add,
1160
                                 const std::vector<RowsetMetaSharedPtr>& to_delete,
1161
10.2k
                                 bool same_version) {
1162
10.2k
    size_t rowset_cache_version_size = 0;
1163
    // Remove to_delete rowsets from _rs_metas
1164
91.6k
    for (auto rs_to_del : to_delete) {
1165
91.6k
        if (auto it = _rs_metas.find(rs_to_del->version()); it != _rs_metas.end()) {
1166
91.5k
            auto rowset_id = it->second->rowset_id();
1167
91.5k
            _rs_metas.erase(it);
1168
91.5k
            if (_enable_unique_key_merge_on_write) {
1169
58.4k
                rowset_cache_version_size = _delete_bitmap->remove_rowset_cache_version(rowset_id);
1170
58.4k
            }
1171
91.5k
        }
1172
91.6k
    }
1173
10.2k
    if (!same_version) {
1174
        // put to_delete rowsets in _stale_rs_metas.
1175
91.6k
        for (auto rs_to_del : to_delete) {
1176
91.6k
            _stale_rs_metas.emplace(rs_to_del->version(), rs_to_del);
1177
91.6k
        }
1178
10.2k
    }
1179
1180
    // put to_add rowsets in _rs_metas.
1181
10.2k
    for (auto rs_to_add : to_add) {
1182
4.05k
        _rs_metas.emplace(rs_to_add->version(), rs_to_add);
1183
4.05k
    }
1184
10.2k
    _check_mow_rowset_cache_version_size(rowset_cache_version_size);
1185
10.2k
}
1186
1187
void TabletMeta::modify_row_binlog_rs_metas(const std::vector<RowsetMetaSharedPtr>& to_add,
1188
0
                                            const std::vector<RowsetMetaSharedPtr>& to_delete) {
1189
0
    for (const auto& rs_to_del : to_delete) {
1190
0
        _row_binlog_rs_metas.erase(rs_to_del->version());
1191
0
    }
1192
1193
0
    for (const auto& rs_to_add : to_add) {
1194
0
        _row_binlog_rs_metas.emplace(rs_to_add->version(), rs_to_add);
1195
0
    }
1196
0
}
1197
1198
// Use the passing "rs_metas" to replace the rs meta in this tablet meta
1199
// Also clear the _stale_rs_metas because this tablet meta maybe copyied from
1200
// an existing tablet before. Add after revise, only the passing "rs_metas"
1201
// is needed.
1202
321
void TabletMeta::revise_rs_metas(std::vector<RowsetMetaSharedPtr>&& rs_metas) {
1203
321
    {
1204
321
        std::lock_guard<std::shared_mutex> wrlock(_meta_lock);
1205
321
        _rs_metas.clear();
1206
478
        for (auto& rs_meta : rs_metas) {
1207
478
            _rs_metas.emplace(rs_meta->version(), rs_meta);
1208
478
        }
1209
321
        _stale_rs_metas.clear();
1210
321
    }
1211
321
    if (_enable_unique_key_merge_on_write) {
1212
40
        _delete_bitmap->clear_rowset_cache_version();
1213
40
    }
1214
321
}
1215
1216
0
void TabletMeta::revise_row_binlog_rs_metas(std::vector<RowsetMetaSharedPtr>&& rs_metas) {
1217
0
    std::lock_guard<std::shared_mutex> wrlock(_meta_lock);
1218
0
    _row_binlog_rs_metas.clear();
1219
0
    for (auto& rs_meta : rs_metas) {
1220
0
        _row_binlog_rs_metas.emplace(rs_meta->version(), rs_meta);
1221
0
    }
1222
0
}
1223
1224
// This method should call after revise_rs_metas, since new rs_metas might be a subset
1225
// of original tablet, we should revise the delete_bitmap according to current rowset.
1226
//
1227
// Delete bitmap is protected by Tablet::_meta_lock, we don't need to acquire the
1228
// TabletMeta's _meta_lock
1229
41
void TabletMeta::revise_delete_bitmap_unlocked(const DeleteBitmap& delete_bitmap) {
1230
41
    _delete_bitmap = std::make_unique<DeleteBitmap>(tablet_id());
1231
48
    for (const auto& [_, rs] : _rs_metas) {
1232
48
        DeleteBitmap rs_bm(tablet_id());
1233
48
        delete_bitmap.subset({rs->rowset_id(), 0, 0}, {rs->rowset_id(), UINT32_MAX, INT64_MAX},
1234
48
                             &rs_bm);
1235
48
        _delete_bitmap->merge(rs_bm);
1236
48
    }
1237
41
    for (const auto& [_, rs] : _stale_rs_metas) {
1238
0
        DeleteBitmap rs_bm(tablet_id());
1239
0
        delete_bitmap.subset({rs->rowset_id(), 0, 0}, {rs->rowset_id(), UINT32_MAX, INT64_MAX},
1240
0
                             &rs_bm);
1241
0
        _delete_bitmap->merge(rs_bm);
1242
0
    }
1243
41
}
1244
1245
0
void TabletMeta::revise_binlog_delvec_unlocked(const DeleteBitmap& binlog_delvec) {
1246
0
    _binlog_delvec = std::make_unique<DeleteBitmap>(tablet_id());
1247
0
    for (const auto& [_, rs] : _row_binlog_rs_metas) {
1248
0
        DeleteBitmap rs_bm(tablet_id());
1249
0
        binlog_delvec.subset({rs->rowset_id(), 0, 0}, {rs->rowset_id(), UINT32_MAX, INT64_MAX},
1250
0
                             &rs_bm);
1251
0
        _binlog_delvec->merge(rs_bm);
1252
0
    }
1253
0
}
1254
1255
88.7k
void TabletMeta::delete_stale_rs_meta_by_version(const Version& version) {
1256
88.7k
    _stale_rs_metas.erase(version);
1257
88.7k
}
1258
1259
0
RowsetMetaSharedPtr TabletMeta::acquire_rs_meta_by_version(const Version& version) const {
1260
0
    if (auto it = _rs_metas.find(version); it != _rs_metas.end()) {
1261
0
        return it->second;
1262
0
    }
1263
0
    return nullptr;
1264
0
}
1265
1266
35.7k
RowsetMetaSharedPtr TabletMeta::acquire_stale_rs_meta_by_version(const Version& version) const {
1267
35.7k
    if (auto it = _stale_rs_metas.find(version); it != _stale_rs_metas.end()) {
1268
35.7k
        return it->second;
1269
35.7k
    }
1270
8
    return nullptr;
1271
35.7k
}
1272
1273
RowsetMetaSharedPtr TabletMeta::acquire_row_binlog_rs_meta_by_version(
1274
0
        const Version& version) const {
1275
0
    if (auto it = _row_binlog_rs_metas.find(version); it != _row_binlog_rs_metas.end()) {
1276
0
        return it->second;
1277
0
    }
1278
0
    return nullptr;
1279
0
}
1280
1281
21
Status TabletMeta::set_partition_id(int64_t partition_id) {
1282
21
    if ((_partition_id > 0 && _partition_id != partition_id) || partition_id < 1) {
1283
0
        LOG(WARNING) << "cur partition id=" << _partition_id << " new partition id=" << partition_id
1284
0
                     << " not equal";
1285
0
    }
1286
21
    _partition_id = partition_id;
1287
21
    return Status::OK();
1288
21
}
1289
1290
0
void TabletMeta::clear_stale_rowset() {
1291
0
    _stale_rs_metas.clear();
1292
0
    if (_enable_unique_key_merge_on_write) {
1293
0
        _delete_bitmap->clear_rowset_cache_version();
1294
0
    }
1295
0
}
1296
1297
0
void TabletMeta::clear_rowsets() {
1298
0
    _rs_metas.clear();
1299
0
    if (_enable_unique_key_merge_on_write) {
1300
0
        _delete_bitmap->clear_rowset_cache_version();
1301
0
    }
1302
0
}
1303
1304
66.5k
void TabletMeta::_check_mow_rowset_cache_version_size(size_t rowset_cache_version_size) {
1305
66.5k
    if (_enable_unique_key_merge_on_write && config::enable_mow_verbose_log &&
1306
66.5k
        rowset_cache_version_size > _rs_metas.size() + _stale_rs_metas.size()) {
1307
0
        std::stringstream ss;
1308
0
        auto rowset_ids = _delete_bitmap->get_rowset_cache_version();
1309
0
        std::set<std::string> tablet_rowset_ids;
1310
0
        {
1311
0
            std::shared_lock rlock(_meta_lock);
1312
0
            for (const auto& [_, rs_meta] : _rs_metas) {
1313
0
                tablet_rowset_ids.emplace(rs_meta->rowset_id().to_string());
1314
0
            }
1315
0
            for (const auto& [_, rs_meta] : _stale_rs_metas) {
1316
0
                tablet_rowset_ids.emplace(rs_meta->rowset_id().to_string());
1317
0
            }
1318
0
        }
1319
0
        for (const auto& rowset_id : rowset_ids) {
1320
0
            if (tablet_rowset_ids.find(rowset_id) == tablet_rowset_ids.end()) {
1321
0
                ss << rowset_id << ", ";
1322
0
            }
1323
0
        }
1324
        // size(rowset_cache_version) <= size(_rs_metas) + size(_stale_rs_metas) + size(_unused_rs)
1325
0
        std::string msg = fmt::format(
1326
0
                "tablet: {}, rowset_cache_version size: {}, "
1327
0
                "_rs_metas size: {}, _stale_rs_metas size: {}, delta: {}. rowset only in cache: {}",
1328
0
                _tablet_id, rowset_cache_version_size, _rs_metas.size(), _stale_rs_metas.size(),
1329
0
                rowset_cache_version_size - _rs_metas.size() - _stale_rs_metas.size(), ss.str());
1330
0
        LOG(INFO) << msg;
1331
0
    }
1332
66.5k
}
1333
1334
3
bool operator==(const TabletMeta& a, const TabletMeta& b) {
1335
3
    if (a._table_id != b._table_id) return false;
1336
3
    if (a._index_id != b._index_id) return false;
1337
3
    if (a._partition_id != b._partition_id) return false;
1338
3
    if (a._tablet_id != b._tablet_id) return false;
1339
3
    if (a._replica_id != b._replica_id) return false;
1340
3
    if (a._schema_hash != b._schema_hash) return false;
1341
3
    if (a._shard_id != b._shard_id) return false;
1342
3
    if (a._creation_time != b._creation_time) return false;
1343
3
    if (a._cumulative_layer_point != b._cumulative_layer_point) return false;
1344
3
    if (a._tablet_uid != b._tablet_uid) return false;
1345
3
    if (a._tablet_type != b._tablet_type) return false;
1346
3
    if (a._tablet_state != b._tablet_state) return false;
1347
3
    if (*a._schema != *b._schema) return false;
1348
3
    if (a._rs_metas != b._rs_metas) return false;
1349
3
    if (a._in_restore_mode != b._in_restore_mode) return false;
1350
3
    if (a._preferred_rowset_type != b._preferred_rowset_type) return false;
1351
3
    if (a._storage_policy_id != b._storage_policy_id) return false;
1352
3
    if (a._compaction_policy != b._compaction_policy) return false;
1353
3
    if (a._time_series_compaction_goal_size_mbytes != b._time_series_compaction_goal_size_mbytes)
1354
0
        return false;
1355
3
    if (a._time_series_compaction_file_count_threshold !=
1356
3
        b._time_series_compaction_file_count_threshold)
1357
0
        return false;
1358
3
    if (a._time_series_compaction_time_threshold_seconds !=
1359
3
        b._time_series_compaction_time_threshold_seconds)
1360
0
        return false;
1361
3
    if (a._time_series_compaction_empty_rowsets_threshold !=
1362
3
        b._time_series_compaction_empty_rowsets_threshold)
1363
0
        return false;
1364
3
    if (a._time_series_compaction_level_threshold != b._time_series_compaction_level_threshold)
1365
0
        return false;
1366
3
    return true;
1367
3
}
1368
1369
0
bool operator!=(const TabletMeta& a, const TabletMeta& b) {
1370
0
    return !(a == b);
1371
0
}
1372
1373
// We cannot just copy the underlying memory to construct a string
1374
// due to equivalent objects may have different padding bytes.
1375
// Reading padding bytes is undefined behavior, neither copy nor
1376
// placement new will help simplify the code.
1377
// Refer to C11 standards §6.2.6.1/6 and §6.7.9/21 for more info.
1378
5.74M
static std::string agg_cache_key(int64_t tablet_id, const DeleteBitmap::BitmapKey& bmk) {
1379
5.74M
    std::string ret(sizeof(tablet_id) + sizeof(bmk), '\0');
1380
5.74M
    *reinterpret_cast<int64_t*>(ret.data()) = tablet_id;
1381
5.74M
    auto t = reinterpret_cast<DeleteBitmap::BitmapKey*>(ret.data() + sizeof(tablet_id));
1382
5.74M
    std::get<RowsetId>(*t).version = std::get<RowsetId>(bmk).version;
1383
5.74M
    std::get<RowsetId>(*t).hi = std::get<RowsetId>(bmk).hi;
1384
5.74M
    std::get<RowsetId>(*t).mi = std::get<RowsetId>(bmk).mi;
1385
5.74M
    std::get<RowsetId>(*t).lo = std::get<RowsetId>(bmk).lo;
1386
5.74M
    std::get<1>(*t) = std::get<1>(bmk);
1387
5.74M
    std::get<2>(*t) = std::get<2>(bmk);
1388
5.74M
    return ret;
1389
5.74M
}
1390
1391
// decode cache key info from a agg_cache_key
1392
static void decode_agg_cache_key(const std::string& key_str, int64_t& tablet_id,
1393
44.8k
                                 DeleteBitmap::BitmapKey& bmk) {
1394
44.8k
    const char* ptr = key_str.data();
1395
44.8k
    tablet_id = *reinterpret_cast<const int64_t*>(ptr);
1396
44.8k
    ptr += sizeof(tablet_id);
1397
44.8k
    const auto* t = reinterpret_cast<const DeleteBitmap::BitmapKey*>(ptr);
1398
44.8k
    std::get<RowsetId>(bmk).version = std::get<RowsetId>(*t).version;
1399
44.8k
    std::get<RowsetId>(bmk).hi = std::get<RowsetId>(*t).hi;
1400
44.8k
    std::get<RowsetId>(bmk).mi = std::get<RowsetId>(*t).mi;
1401
44.8k
    std::get<RowsetId>(bmk).lo = std::get<RowsetId>(*t).lo;
1402
44.8k
    std::get<1>(bmk) = std::get<1>(*t);
1403
44.8k
    std::get<2>(bmk) = std::get<2>(*t);
1404
44.8k
}
1405
1406
DeleteBitmapAggCache::DeleteBitmapAggCache(size_t capacity)
1407
8
        : LRUCachePolicy(CachePolicy::CacheType::DELETE_BITMAP_AGG_CACHE, capacity,
1408
8
                         LRUCacheType::SIZE, config::delete_bitmap_agg_cache_stale_sweep_time_sec,
1409
8
                         /*num_shards*/ 256,
1410
8
                         /*element_count_capacity*/ 0, /*enable_prune*/ true,
1411
8
                         /*is_lru_k*/ false) {}
1412
1413
12.5M
DeleteBitmapAggCache* DeleteBitmapAggCache::instance() {
1414
12.5M
    return ExecEnv::GetInstance()->delete_bitmap_agg_cache();
1415
12.5M
}
1416
1417
8
DeleteBitmapAggCache* DeleteBitmapAggCache::create_instance(size_t capacity) {
1418
8
    return new DeleteBitmapAggCache(capacity);
1419
8
}
1420
1421
2
DeleteBitmap DeleteBitmapAggCache::snapshot(int64_t tablet_id) {
1422
2
    DeleteBitmap ret(tablet_id);
1423
44.8k
    auto collector = [&](const LRUHandle* handle) {
1424
44.8k
        auto key = handle->key().to_string();
1425
44.8k
        int64_t key_tablet_id;
1426
44.8k
        DeleteBitmap::BitmapKey bmk;
1427
44.8k
        decode_agg_cache_key(key, key_tablet_id, bmk);
1428
44.8k
        if (key_tablet_id == tablet_id) {
1429
16
            const auto& dbm = reinterpret_cast<DeleteBitmapAggCache::Value*>(handle->value)->bitmap;
1430
16
            ret.set(bmk, dbm);
1431
16
        }
1432
44.8k
    };
1433
2
    DeleteBitmapAggCache::instance()->for_each_entry(collector);
1434
2
    return ret;
1435
2
}
1436
1437
1.50M
DeleteBitmap::DeleteBitmap(int64_t tablet_id) : _tablet_id(tablet_id) {}
1438
1439
23.1k
DeleteBitmap::DeleteBitmap(const DeleteBitmap& o) {
1440
23.1k
    std::shared_lock l1(o.lock);
1441
23.1k
    delete_bitmap = o.delete_bitmap;
1442
23.1k
    _tablet_id = o._tablet_id;
1443
23.1k
}
1444
1445
27.0k
DeleteBitmap& DeleteBitmap::operator=(const DeleteBitmap& o) {
1446
27.0k
    if (this == &o) return *this;
1447
25.9k
    if (this < &o) {
1448
25.9k
        std::unique_lock l1(lock);
1449
25.9k
        std::shared_lock l2(o.lock);
1450
25.9k
        delete_bitmap = o.delete_bitmap;
1451
25.9k
        _tablet_id = o._tablet_id;
1452
18.4E
    } else {
1453
18.4E
        std::shared_lock l2(o.lock);
1454
18.4E
        std::unique_lock l1(lock);
1455
18.4E
        delete_bitmap = o.delete_bitmap;
1456
18.4E
        _tablet_id = o._tablet_id;
1457
18.4E
    }
1458
25.8k
    return *this;
1459
27.0k
}
1460
1461
0
DeleteBitmap::DeleteBitmap(DeleteBitmap&& o) noexcept {
1462
0
    std::scoped_lock l(o.lock, o._rowset_cache_version_lock);
1463
0
    delete_bitmap = std::move(o.delete_bitmap);
1464
0
    _tablet_id = std::move(o._tablet_id);
1465
0
    o._rowset_cache_version.clear();
1466
0
}
1467
1468
38
DeleteBitmap& DeleteBitmap::operator=(DeleteBitmap&& o) noexcept {
1469
38
    if (this == &o) return *this;
1470
38
    std::scoped_lock l(lock, o.lock, o._rowset_cache_version_lock);
1471
38
    delete_bitmap = std::move(o.delete_bitmap);
1472
38
    _tablet_id = std::move(o._tablet_id);
1473
38
    o._rowset_cache_version.clear();
1474
38
    return *this;
1475
38
}
1476
1477
0
DeleteBitmap DeleteBitmap::from_pb(const DeleteBitmapPB& pb, int64_t tablet_id) {
1478
0
    size_t len = pb.rowset_ids().size();
1479
0
    DCHECK_EQ(len, pb.segment_ids().size());
1480
0
    DCHECK_EQ(len, pb.versions().size());
1481
0
    DeleteBitmap delete_bitmap(tablet_id);
1482
0
    for (int32_t i = 0; i < len; ++i) {
1483
0
        RowsetId rs_id;
1484
0
        rs_id.init(pb.rowset_ids(i));
1485
0
        BitmapKey key = {rs_id, pb.segment_ids(i), pb.versions(i)};
1486
0
        delete_bitmap.delete_bitmap[key] =
1487
0
                roaring::Roaring::read(pb.segment_delete_bitmaps(i).data());
1488
0
    }
1489
0
    return delete_bitmap;
1490
0
}
1491
1492
0
DeleteBitmapPB DeleteBitmap::to_pb() {
1493
0
    std::shared_lock l(lock);
1494
0
    DeleteBitmapPB ret;
1495
0
    for (const auto& [k, v] : delete_bitmap) {
1496
0
        ret.mutable_rowset_ids()->Add(std::get<0>(k).to_string());
1497
0
        ret.mutable_segment_ids()->Add(std::get<1>(k));
1498
0
        ret.mutable_versions()->Add(std::get<2>(k));
1499
0
        std::string bitmap_data(v.getSizeInBytes(), '\0');
1500
0
        v.write(bitmap_data.data());
1501
0
        ret.mutable_segment_delete_bitmaps()->Add(std::move(bitmap_data));
1502
0
    }
1503
0
    return ret;
1504
0
}
1505
1506
19.3k
DeleteBitmap DeleteBitmap::snapshot() const {
1507
19.3k
    std::shared_lock l(lock);
1508
19.3k
    return DeleteBitmap(*this);
1509
19.3k
}
1510
1511
43
DeleteBitmap DeleteBitmap::snapshot(Version version) const {
1512
    // Take snapshot first, then remove keys greater than given version.
1513
43
    DeleteBitmap snapshot = this->snapshot();
1514
43
    auto it = snapshot.delete_bitmap.begin();
1515
452
    while (it != snapshot.delete_bitmap.end()) {
1516
409
        if (std::get<2>(it->first) > version) {
1517
4
            it = snapshot.delete_bitmap.erase(it);
1518
405
        } else {
1519
405
            it++;
1520
405
        }
1521
409
    }
1522
43
    return snapshot;
1523
43
}
1524
1525
4.86M
void DeleteBitmap::add(const BitmapKey& bmk, uint32_t row_id) {
1526
4.86M
    std::lock_guard l(lock);
1527
4.86M
    delete_bitmap[bmk].add(row_id);
1528
4.86M
}
1529
1530
0
int DeleteBitmap::remove(const BitmapKey& bmk, uint32_t row_id) {
1531
0
    std::lock_guard l(lock);
1532
0
    auto it = delete_bitmap.find(bmk);
1533
0
    if (it == delete_bitmap.end()) return -1;
1534
0
    it->second.remove(row_id);
1535
0
    return 0;
1536
0
}
1537
1538
87.8k
void DeleteBitmap::remove(const BitmapKey& start, const BitmapKey& end) {
1539
87.8k
    std::lock_guard l(lock);
1540
103k
    for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end();) {
1541
63.8k
        auto& [k, _] = *it;
1542
63.8k
        if (k >= end) {
1543
48.3k
            break;
1544
48.3k
        }
1545
15.4k
        it = delete_bitmap.erase(it);
1546
15.4k
    }
1547
87.8k
}
1548
1549
930
void DeleteBitmap::remove(const std::vector<std::tuple<BitmapKey, BitmapKey>>& key_ranges) {
1550
930
    std::lock_guard l(lock);
1551
930
    for (auto& [start, end] : key_ranges) {
1552
1.89k
        for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end();) {
1553
1.89k
            auto& [k, _] = *it;
1554
1.89k
            if (k >= end) {
1555
930
                break;
1556
930
            }
1557
960
            it = delete_bitmap.erase(it);
1558
960
        }
1559
930
    }
1560
930
}
1561
1562
3.80M
bool DeleteBitmap::contains(const BitmapKey& bmk, uint32_t row_id) const {
1563
3.80M
    std::shared_lock l(lock);
1564
3.80M
    auto it = delete_bitmap.find(bmk);
1565
3.80M
    return it != delete_bitmap.end() && it->second.contains(row_id);
1566
3.80M
}
1567
1568
0
bool DeleteBitmap::contain_rowsets(const RowsetIdUnorderedSet& rowset_ids) const {
1569
0
    std::shared_lock l(lock);
1570
0
    return std::any_of(delete_bitmap.begin(), delete_bitmap.end(), [&](const auto& entry) {
1571
0
        return rowset_ids.contains(std::get<0>(entry.first));
1572
0
    });
1573
0
}
1574
1575
2
bool DeleteBitmap::contains_agg(const BitmapKey& bmk, uint32_t row_id) const {
1576
2
    return get_agg(bmk)->contains(row_id);
1577
2
}
1578
1579
0
bool DeleteBitmap::empty() const {
1580
0
    std::shared_lock l(lock);
1581
0
    return delete_bitmap.empty();
1582
0
}
1583
1584
285k
uint64_t DeleteBitmap::cardinality() const {
1585
285k
    std::shared_lock l(lock);
1586
285k
    uint64_t res = 0;
1587
1.35M
    for (auto entry : delete_bitmap) {
1588
1.35M
        if (std::get<1>(entry.first) != DeleteBitmap::INVALID_SEGMENT_ID) {
1589
49.8k
            res += entry.second.cardinality();
1590
49.8k
        }
1591
1.35M
    }
1592
285k
    return res;
1593
285k
}
1594
1595
6
uint64_t DeleteBitmap::get_size() const {
1596
6
    std::shared_lock l(lock);
1597
6
    uint64_t charge = 0;
1598
44
    for (auto& [k, v] : delete_bitmap) {
1599
44
        if (std::get<1>(k) != DeleteBitmap::INVALID_SEGMENT_ID) {
1600
44
            charge += v.getSizeInBytes();
1601
44
        }
1602
44
    }
1603
6
    return charge;
1604
6
}
1605
1606
bool DeleteBitmap::contains_agg_with_cache_if_eligible(const BitmapKey& bmk,
1607
3.79M
                                                       uint32_t row_id) const {
1608
3.79M
    g_contains_agg_with_cache_if_eligible_total << 1;
1609
3.79M
    int64_t start_version {0};
1610
3.81M
    if (config::enable_mow_get_agg_by_cache) {
1611
3.81M
        auto deleter = [&](Cache::Handle* handle) {
1612
1.47M
            DeleteBitmapAggCache::instance()->release(handle);
1613
1.47M
        };
1614
3.81M
        std::unique_ptr<Cache::Handle, decltype(deleter)> dbm_handle(nullptr, deleter);
1615
3.81M
        int64_t cached_version = 0;
1616
        // 1. try to lookup the desired key directly
1617
3.81M
        dbm_handle.reset(DeleteBitmapAggCache::instance()->lookup(agg_cache_key(_tablet_id, bmk)));
1618
3.81M
        if (dbm_handle != nullptr) {
1619
1.47M
            cached_version = std::get<2>(bmk);
1620
2.34M
        } else {
1621
            // 2. if not found, try to lookup with cached version
1622
2.34M
            cached_version = _get_rowset_cache_version(bmk);
1623
2.34M
            if (cached_version > 0) {
1624
296
                if (cached_version > std::get<2>(bmk)) {
1625
41
                    cached_version = 0;
1626
255
                } else {
1627
255
                    dbm_handle.reset(DeleteBitmapAggCache::instance()->lookup(agg_cache_key(
1628
255
                            _tablet_id, {std::get<0>(bmk), std::get<1>(bmk), cached_version})));
1629
255
                }
1630
296
            }
1631
2.34M
        }
1632
3.81M
        if (dbm_handle != nullptr) {
1633
1.46M
            const auto& cached_dbm =
1634
1.46M
                    reinterpret_cast<DeleteBitmapAggCache::Value*>(
1635
1.46M
                            DeleteBitmapAggCache::instance()->value(dbm_handle.get()))
1636
1.46M
                            ->bitmap;
1637
1.47M
            if (cached_version == std::get<2>(bmk)) {
1638
1.47M
                g_contains_agg_with_cache_if_eligible_full_hit << 1;
1639
18.4E
            } else {
1640
18.4E
                g_contains_agg_with_cache_if_eligible_partial_hit << 1;
1641
18.4E
            }
1642
1.46M
            if (cached_dbm.contains(row_id)) {
1643
340
                return true;
1644
340
            }
1645
1.47M
            if (cached_version == std::get<2>(bmk)) {
1646
1.47M
                return false;
1647
1.47M
            }
1648
18.4E
            start_version = cached_version + 1;
1649
18.4E
        }
1650
3.81M
    }
1651
2.32M
    DeleteBitmap::BitmapKey start {std::get<0>(bmk), std::get<1>(bmk), start_version};
1652
2.32M
    std::shared_lock l(lock);
1653
2.32M
    for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end(); ++it) {
1654
1.32M
        auto& [k, bm] = *it;
1655
1.32M
        if (std::get<0>(k) != std::get<0>(bmk) || std::get<1>(k) != std::get<1>(bmk) ||
1656
1.32M
            std::get<2>(k) > std::get<2>(bmk)) {
1657
1.32M
            break;
1658
1.32M
        }
1659
32
        if (bm.contains(row_id)) {
1660
20
            return true;
1661
20
        }
1662
32
    }
1663
2.32M
    return false;
1664
2.32M
}
1665
1666
7
void DeleteBitmap::remove_sentinel_marks() {
1667
7
    std::lock_guard l(lock);
1668
55
    for (auto it = delete_bitmap.begin(), end = delete_bitmap.end(); it != end;) {
1669
48
        if (std::get<1>(it->first) == DeleteBitmap::INVALID_SEGMENT_ID) {
1670
48
            it = delete_bitmap.erase(it);
1671
48
        } else {
1672
0
            ++it;
1673
0
        }
1674
48
    }
1675
7
}
1676
1677
9.04k
int DeleteBitmap::set(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap) {
1678
9.04k
    std::lock_guard l(lock);
1679
9.04k
    auto [_, inserted] = delete_bitmap.insert_or_assign(bmk, segment_delete_bitmap);
1680
9.04k
    return inserted;
1681
9.04k
}
1682
1683
7
int DeleteBitmap::get(const BitmapKey& bmk, roaring::Roaring* segment_delete_bitmap) const {
1684
7
    std::shared_lock l(lock);
1685
7
    auto it = delete_bitmap.find(bmk);
1686
7
    if (it == delete_bitmap.end()) return -1;
1687
7
    *segment_delete_bitmap = it->second; // copy
1688
7
    return 0;
1689
7
}
1690
1691
54
const roaring::Roaring* DeleteBitmap::get(const BitmapKey& bmk) const {
1692
54
    std::shared_lock l(lock);
1693
54
    auto it = delete_bitmap.find(bmk);
1694
54
    if (it == delete_bitmap.end()) return nullptr;
1695
41
    return &(it->second); // get address
1696
54
}
1697
1698
void DeleteBitmap::subset(const BitmapKey& start, const BitmapKey& end,
1699
59.2k
                          DeleteBitmap* subset_rowset_map) const {
1700
59.2k
    DCHECK(start < end);
1701
59.2k
    std::shared_lock l(lock);
1702
66.2k
    for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end(); ++it) {
1703
17.4k
        auto& [k, bm] = *it;
1704
17.4k
        if (k >= end) {
1705
10.3k
            break;
1706
10.3k
        }
1707
7.08k
        subset_rowset_map->set(k, bm);
1708
7.08k
    }
1709
59.2k
}
1710
1711
void DeleteBitmap::subset(std::vector<std::pair<RowsetId, int64_t>>& rowset_ids,
1712
                          int64_t start_version, int64_t end_version,
1713
0
                          DeleteBitmap* subset_delete_map) const {
1714
0
    DCHECK(start_version <= end_version);
1715
0
    for (auto& [rowset_id, _] : rowset_ids) {
1716
0
        BitmapKey start {rowset_id, 0, 0};
1717
0
        BitmapKey end {rowset_id, UINT32_MAX, end_version + 1};
1718
0
        std::shared_lock l(lock);
1719
0
        for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end(); ++it) {
1720
0
            auto& [k, bm] = *it;
1721
0
            if (k >= end) {
1722
0
                break;
1723
0
            }
1724
0
            auto version = std::get<2>(k);
1725
0
            if (version >= start_version && version <= end_version) {
1726
0
                subset_delete_map->merge(k, bm);
1727
0
                VLOG_DEBUG << "subset delete bitmap, tablet=" << _tablet_id << ", version=["
1728
0
                           << start_version << ", " << end_version
1729
0
                           << "]. rowset=" << std::get<0>(k).to_string()
1730
0
                           << ", segment=" << std::get<1>(k) << ", version=" << version
1731
0
                           << ", cardinality=" << bm.cardinality();
1732
0
            }
1733
0
        }
1734
0
    }
1735
0
}
1736
1737
void DeleteBitmap::subset_and_agg(std::vector<std::pair<RowsetId, int64_t>>& rowset_ids,
1738
                                  int64_t start_version, int64_t end_version,
1739
1
                                  DeleteBitmap* subset_delete_map) const {
1740
1
    DCHECK(start_version <= end_version);
1741
2
    for (auto& [rowset_id, segment_num] : rowset_ids) {
1742
6
        for (int64_t seg_id = 0; seg_id < segment_num; ++seg_id) {
1743
4
            BitmapKey end {rowset_id, seg_id, end_version};
1744
4
            auto bm = get_agg_without_cache(end, start_version);
1745
4
            VLOG_DEBUG << "subset delete bitmap, tablet=" << _tablet_id << ", rowset=" << rowset_id
1746
0
                       << ", segment=" << seg_id << ", version=[" << start_version << "-"
1747
0
                       << end_version << "], cardinality=" << bm->cardinality();
1748
4
            if (bm->isEmpty()) {
1749
0
                continue;
1750
0
            }
1751
4
            subset_delete_map->merge(end, *bm);
1752
4
        }
1753
2
    }
1754
1
}
1755
1756
680
size_t DeleteBitmap::get_count_with_range(const BitmapKey& start, const BitmapKey& end) const {
1757
680
    DCHECK(start < end);
1758
680
    size_t count = 0;
1759
680
    std::shared_lock l(lock);
1760
896
    for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end(); ++it) {
1761
671
        auto& [k, bm] = *it;
1762
671
        if (k >= end) {
1763
455
            break;
1764
455
        }
1765
216
        count++;
1766
216
    }
1767
680
    return count;
1768
680
}
1769
1770
17.3k
void DeleteBitmap::merge(const BitmapKey& bmk, const roaring::Roaring& segment_delete_bitmap) {
1771
17.3k
    std::lock_guard l(lock);
1772
17.3k
    auto [iter, succ] = delete_bitmap.emplace(bmk, segment_delete_bitmap);
1773
17.3k
    if (!succ) {
1774
0
        iter->second |= segment_delete_bitmap;
1775
0
    }
1776
17.3k
}
1777
1778
50.9k
void DeleteBitmap::merge(const DeleteBitmap& other) {
1779
50.9k
    std::lock_guard l(lock);
1780
50.9k
    for (auto& i : other.delete_bitmap) {
1781
2.34k
        auto [j, succ] = this->delete_bitmap.insert(i);
1782
2.34k
        if (!succ) j->second |= i.second;
1783
2.34k
    }
1784
50.9k
}
1785
1786
749k
uint64_t DeleteBitmap::get_delete_bitmap_count() {
1787
749k
    std::shared_lock l(lock);
1788
749k
    uint64_t count = 0;
1789
1.31M
    for (auto it = delete_bitmap.begin(); it != delete_bitmap.end(); it++) {
1790
566k
        if (std::get<1>(it->first) != DeleteBitmap::INVALID_SEGMENT_ID) {
1791
51.1k
            count++;
1792
51.1k
        }
1793
566k
    }
1794
749k
    return count;
1795
749k
}
1796
1797
void DeleteBitmap::traverse_rowset_and_version(
1798
0
        const std::function<int(const RowsetId& rowsetId, int64_t version)>& func) const {
1799
0
    std::shared_lock l(lock);
1800
0
    auto it = delete_bitmap.cbegin();
1801
0
    while (it != delete_bitmap.cend()) {
1802
0
        RowsetId rowset_id = std::get<0>(it->first);
1803
0
        int64_t version = std::get<2>(it->first);
1804
0
        int result = func(rowset_id, version);
1805
0
        if (result == -2) {
1806
            // find next <rowset, version>
1807
0
            it++;
1808
0
        } else {
1809
            // find next <rowset>
1810
0
            it = delete_bitmap.upper_bound({rowset_id, std::numeric_limits<SegmentId>::max(),
1811
0
                                            std::numeric_limits<Version>::max()});
1812
0
        }
1813
0
    }
1814
0
}
1815
1816
0
bool DeleteBitmap::has_calculated_for_multi_segments(const RowsetId& rowset_id) const {
1817
0
    return contains({rowset_id, INVALID_SEGMENT_ID, TEMP_VERSION_COMMON}, ROWSET_SENTINEL_MARK);
1818
0
}
1819
1820
114k
size_t DeleteBitmap::remove_rowset_cache_version(const RowsetId& rowset_id) {
1821
114k
    std::lock_guard l(_rowset_cache_version_lock);
1822
114k
    _rowset_cache_version.erase(rowset_id);
1823
18.4E
    VLOG_DEBUG << "remove agg cache version for tablet=" << _tablet_id
1824
18.4E
               << ", rowset=" << rowset_id.to_string();
1825
114k
    return _rowset_cache_version.size();
1826
114k
}
1827
1828
36
void DeleteBitmap::clear_rowset_cache_version() {
1829
36
    std::lock_guard l(_rowset_cache_version_lock);
1830
36
    _rowset_cache_version.clear();
1831
18.4E
    VLOG_DEBUG << "clear agg cache version for tablet=" << _tablet_id;
1832
36
}
1833
1834
0
std::set<std::string> DeleteBitmap::get_rowset_cache_version() {
1835
0
    std::set<std::string> set;
1836
0
    std::shared_lock l(_rowset_cache_version_lock);
1837
0
    for (auto& [k, _] : _rowset_cache_version) {
1838
0
        set.insert(k.to_string());
1839
0
    }
1840
0
    return set;
1841
0
}
1842
1843
2.44M
DeleteBitmap::Version DeleteBitmap::_get_rowset_cache_version(const BitmapKey& bmk) const {
1844
2.44M
    std::shared_lock l(_rowset_cache_version_lock);
1845
2.44M
    if (auto it = _rowset_cache_version.find(std::get<0>(bmk)); it != _rowset_cache_version.end()) {
1846
13.8k
        auto& segment_cache_version = it->second;
1847
13.8k
        if (auto it1 = segment_cache_version.find(std::get<1>(bmk));
1848
13.8k
            it1 != segment_cache_version.end()) {
1849
13.8k
            return it1->second;
1850
13.8k
        }
1851
13.8k
    }
1852
2.43M
    return 0;
1853
2.44M
}
1854
1855
2
DeleteBitmap DeleteBitmap::agg_cache_snapshot() {
1856
2
    return DeleteBitmapAggCache::instance()->snapshot(_tablet_id);
1857
2
}
1858
1859
1.23M
void DeleteBitmap::set_tablet_id(int64_t tablet_id) {
1860
1.23M
    _tablet_id = tablet_id;
1861
1.23M
}
1862
1863
1.92M
std::shared_ptr<roaring::Roaring> DeleteBitmap::get_agg(const BitmapKey& bmk) const {
1864
1.92M
    std::string key_str = agg_cache_key(_tablet_id, bmk); // Cache key container
1865
1.92M
    CacheKey key(key_str);
1866
1.92M
    Cache::Handle* handle = DeleteBitmapAggCache::instance()->lookup(key);
1867
1868
1.92M
    DeleteBitmapAggCache::Value* val =
1869
1.92M
            handle == nullptr ? nullptr
1870
1.92M
                              : reinterpret_cast<DeleteBitmapAggCache::Value*>(
1871
1.81M
                                        DeleteBitmapAggCache::instance()->value(handle));
1872
    // FIXME: do we need a mutex here to get rid of duplicated initializations
1873
    //        of cache entries in some cases?
1874
1.92M
    if (val == nullptr) { // Renew if needed, put a new Value to cache
1875
107k
        val = new DeleteBitmapAggCache::Value();
1876
107k
        Version start_version =
1877
107k
                config::enable_mow_get_agg_by_cache ? _get_rowset_cache_version(bmk) : 0;
1878
107k
        if (start_version > 0) {
1879
13.5k
            Cache::Handle* handle2 = DeleteBitmapAggCache::instance()->lookup(
1880
13.5k
                    agg_cache_key(_tablet_id, {std::get<0>(bmk), std::get<1>(bmk), start_version}));
1881
1882
13.5k
            DBUG_EXECUTE_IF("DeleteBitmap::get_agg.cache_miss", {
1883
13.5k
                if (handle2 != nullptr) {
1884
13.5k
                    auto p = dp->param("percent", 0.3);
1885
13.5k
                    std::mt19937 gen {std::random_device {}()};
1886
13.5k
                    std::bernoulli_distribution inject_fault {p};
1887
13.5k
                    if (inject_fault(gen)) {
1888
13.5k
                        LOG_INFO("injection DeleteBitmap::get_agg.cache_miss, tablet_id={}",
1889
13.5k
                                 _tablet_id);
1890
13.5k
                        handle2 = nullptr;
1891
13.5k
                    }
1892
13.5k
                }
1893
13.5k
            });
1894
13.5k
            if (handle2 == nullptr || start_version > std::get<2>(bmk)) {
1895
317
                start_version = 0;
1896
13.2k
            } else {
1897
13.2k
                val->bitmap |= reinterpret_cast<DeleteBitmapAggCache::Value*>(
1898
13.2k
                                       DeleteBitmapAggCache::instance()->value(handle2))
1899
13.2k
                                       ->bitmap;
1900
13.2k
                VLOG_DEBUG << "get agg cache version=" << start_version
1901
10
                           << " for tablet=" << _tablet_id
1902
10
                           << ", rowset=" << std::get<0>(bmk).to_string()
1903
10
                           << ", segment=" << std::get<1>(bmk);
1904
13.2k
                start_version += 1;
1905
13.2k
            }
1906
13.5k
            if (handle2 != nullptr) {
1907
13.5k
                DeleteBitmapAggCache::instance()->release(handle2);
1908
13.5k
            }
1909
13.5k
        }
1910
107k
        {
1911
107k
            std::shared_lock l(lock);
1912
107k
            DeleteBitmap::BitmapKey start {std::get<0>(bmk), std::get<1>(bmk), start_version};
1913
116k
            for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end(); ++it) {
1914
30.5k
                auto& [k, bm] = *it;
1915
30.5k
                if (std::get<0>(k) != std::get<0>(bmk) || std::get<1>(k) != std::get<1>(bmk) ||
1916
30.5k
                    std::get<2>(k) > std::get<2>(bmk)) {
1917
21.8k
                    break;
1918
21.8k
                }
1919
8.77k
                val->bitmap |= bm;
1920
8.77k
            }
1921
107k
        }
1922
107k
        size_t charge = val->bitmap.getSizeInBytes() + sizeof(DeleteBitmapAggCache::Value);
1923
107k
        handle = DeleteBitmapAggCache::instance()->insert(key, val, charge, charge,
1924
107k
                                                          CachePriority::NORMAL);
1925
107k
        if (config::enable_mow_get_agg_by_cache && !val->bitmap.isEmpty()) {
1926
19.9k
            std::lock_guard l(_rowset_cache_version_lock);
1927
            // this version is already agg
1928
19.9k
            _rowset_cache_version[std::get<0>(bmk)][std::get<1>(bmk)] = std::get<2>(bmk);
1929
19.9k
            VLOG_DEBUG << "set agg cache version=" << std::get<2>(bmk)
1930
5
                       << " for tablet=" << _tablet_id
1931
5
                       << ", rowset=" << std::get<0>(bmk).to_string()
1932
5
                       << ", segment=" << std::get<1>(bmk);
1933
19.9k
        }
1934
107k
        if (start_version > 0 && config::enable_mow_get_agg_correctness_check_core) {
1935
0
            std::shared_ptr<roaring::Roaring> bitmap = get_agg_without_cache(bmk);
1936
0
            if (val->bitmap != *bitmap) {
1937
0
                CHECK(false) << ". get agg correctness check failed for tablet=" << _tablet_id
1938
0
                             << ", rowset=" << std::get<0>(bmk).to_string()
1939
0
                             << ", segment=" << std::get<1>(bmk) << ", version=" << std::get<2>(bmk)
1940
0
                             << ". start_version from cache=" << start_version
1941
0
                             << ", delete_bitmap cardinality with cache="
1942
0
                             << val->bitmap.cardinality()
1943
0
                             << ", delete_bitmap cardinality without cache="
1944
0
                             << bitmap->cardinality();
1945
0
            }
1946
0
        }
1947
107k
    }
1948
1949
    // It is natural for the cache to reclaim the underlying memory
1950
1.92M
    return std::shared_ptr<roaring::Roaring>(
1951
1.92M
            &val->bitmap, [handle](...) { DeleteBitmapAggCache::instance()->release(handle); });
1952
1.92M
}
1953
1954
std::shared_ptr<roaring::Roaring> DeleteBitmap::get_agg_without_cache(
1955
6.40k
        const BitmapKey& bmk, const int64_t start_version) const {
1956
6.40k
    std::shared_ptr<roaring::Roaring> bitmap = std::make_shared<roaring::Roaring>();
1957
6.40k
    std::shared_lock l(lock);
1958
6.40k
    DeleteBitmap::BitmapKey start {std::get<0>(bmk), std::get<1>(bmk), start_version};
1959
32.3k
    for (auto it = delete_bitmap.lower_bound(start); it != delete_bitmap.end(); ++it) {
1960
30.7k
        auto& [k, bm] = *it;
1961
30.7k
        if (std::get<0>(k) != std::get<0>(bmk) || std::get<1>(k) != std::get<1>(bmk) ||
1962
30.7k
            std::get<2>(k) > std::get<2>(bmk)) {
1963
4.85k
            break;
1964
4.85k
        }
1965
25.9k
        *bitmap |= bm;
1966
25.9k
    }
1967
6.40k
    return bitmap;
1968
6.40k
}
1969
1970
0
DeleteBitmap DeleteBitmap::diffset(const std::set<BitmapKey>& key_set) const {
1971
0
    std::shared_lock l(lock);
1972
0
    auto diff_key_set_view =
1973
0
            delete_bitmap | std::ranges::views::transform([](const auto& kv) { return kv.first; }) |
1974
0
            std::ranges::views::filter(
1975
0
                    [&key_set](const auto& key) { return !key_set.contains(key); });
1976
1977
0
    DeleteBitmap dbm(_tablet_id);
1978
0
    for (const auto& key : diff_key_set_view) {
1979
0
        const auto* bitmap = get(key);
1980
0
        DCHECK_NE(bitmap, nullptr);
1981
0
        dbm.delete_bitmap[key] = *bitmap;
1982
0
    }
1983
0
    return dbm;
1984
0
}
1985
1986
0
std::string tablet_state_name(TabletState state) {
1987
0
    switch (state) {
1988
0
    case TABLET_NOTREADY:
1989
0
        return "TABLET_NOTREADY";
1990
1991
0
    case TABLET_RUNNING:
1992
0
        return "TABLET_RUNNING";
1993
1994
0
    case TABLET_TOMBSTONED:
1995
0
        return "TABLET_TOMBSTONED";
1996
1997
0
    case TABLET_STOPPED:
1998
0
        return "TABLET_STOPPED";
1999
2000
0
    case TABLET_SHUTDOWN:
2001
0
        return "TABLET_SHUTDOWN";
2002
2003
0
    default:
2004
0
        return "TabletState(" + std::to_string(state) + ")";
2005
0
    }
2006
0
}
2007
2008
} // namespace doris