Coverage Report

Created: 2026-04-22 12:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_rowset_builder.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 "cloud/cloud_rowset_builder.h"
19
20
#include "cloud/cloud_meta_mgr.h"
21
#include "cloud/cloud_storage_engine.h"
22
#include "cloud/cloud_tablet.h"
23
#include "cloud/cloud_tablet_mgr.h"
24
#include "storage/storage_policy.h"
25
26
namespace doris {
27
using namespace ErrorCode;
28
29
CloudRowsetBuilder::CloudRowsetBuilder(CloudStorageEngine& engine, const WriteRequest& req,
30
                                       RuntimeProfile* profile)
31
268k
        : BaseRowsetBuilder(req, profile), _engine(engine) {}
32
33
269k
CloudRowsetBuilder::~CloudRowsetBuilder() {
34
    // Clear file cache immediately when load fails
35
269k
    if (_is_init && _rowset != nullptr && _rowset->rowset_meta()->rowset_state() == PREPARED) {
36
0
        _rowset->clear_cache();
37
0
    }
38
269k
}
39
40
173k
Status CloudRowsetBuilder::init() {
41
173k
    _tablet = DORIS_TRY(_engine.get_tablet(_req.tablet_id));
42
43
173k
    std::shared_ptr<MowContext> mow_context;
44
173k
    if (_tablet->enable_unique_key_merge_on_write()) {
45
51.7k
        if (config::cloud_mow_sync_rowsets_when_load_txn_begin) {
46
0
            auto st = std::static_pointer_cast<CloudTablet>(_tablet)->sync_rowsets();
47
            // sync_rowsets will return INVALID_TABLET_STATE when tablet is under alter
48
0
            if (!st.ok() && !st.is<ErrorCode::INVALID_TABLET_STATE>()) {
49
0
                return st;
50
0
            }
51
0
        }
52
51.7k
        RETURN_IF_ERROR(init_mow_context(mow_context));
53
51.7k
    }
54
173k
    RETURN_IF_ERROR(check_tablet_version_count());
55
56
173k
    using namespace std::chrono;
57
173k
    std::static_pointer_cast<CloudTablet>(_tablet)->last_load_time_ms =
58
173k
            duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
59
60
    // build tablet schema in request level
61
173k
    RETURN_IF_ERROR(_build_current_tablet_schema(_req.index_id, _req.table_schema_param.get(),
62
173k
                                                 *_tablet->tablet_schema()));
63
64
173k
    RowsetWriterContext context;
65
173k
    context.txn_id = _req.txn_id;
66
173k
    context.txn_expiration = _req.txn_expiration;
67
173k
    context.load_id = _req.load_id;
68
173k
    context.rowset_state = PREPARED;
69
173k
    context.segments_overlap = OVERLAPPING;
70
173k
    context.tablet_schema = _tablet_schema;
71
173k
    context.newest_write_timestamp = UnixSeconds();
72
173k
    context.tablet_id = _req.tablet_id;
73
173k
    context.index_id = _req.index_id;
74
173k
    context.tablet = _tablet;
75
173k
    context.write_type = DataWriteType::TYPE_DIRECT;
76
173k
    context.mow_context = mow_context;
77
173k
    context.write_file_cache = _req.write_file_cache;
78
173k
    context.partial_update_info = _partial_update_info;
79
173k
    context.file_cache_ttl_sec = _tablet->ttl_seconds();
80
173k
    context.storage_resource = _engine.get_storage_resource(_req.storage_vault_id);
81
173k
    if (!context.storage_resource) {
82
0
        return Status::InternalError("vault id not found, maybe not sync, vault id {}",
83
0
                                     _req.storage_vault_id);
84
0
    }
85
86
173k
    _rowset_writer = DORIS_TRY(_tablet->create_rowset_writer(context, false));
87
88
173k
    _calc_delete_bitmap_token = _engine.calc_delete_bitmap_executor()->create_token();
89
90
173k
    if (!_skip_writing_rowset_metadata) {
91
53.2k
        RETURN_IF_ERROR(_engine.meta_mgr().prepare_rowset(*_rowset_writer->rowset_meta(), ""));
92
53.2k
    }
93
94
173k
    _is_init = true;
95
173k
    return Status::OK();
96
173k
}
97
98
173k
Status CloudRowsetBuilder::check_tablet_version_count() {
99
173k
    int64_t version_count = cloud_tablet()->fetch_add_approximate_num_rowsets(0);
100
173k
    DBUG_EXECUTE_IF("RowsetBuilder.check_tablet_version_count.too_many_version",
101
173k
                    { version_count = INT_MAX; });
102
    // TODO(plat1ko): load backoff algorithm
103
173k
    int32_t max_version_config = cloud_tablet()->max_version_config();
104
173k
    if (version_count > max_version_config) {
105
0
        return Status::Error<TOO_MANY_VERSION>(
106
0
                "failed to init rowset builder. version count: {}, exceed limit: {}, "
107
0
                "tablet: {}. Please reduce the frequency of loading data or adjust the "
108
0
                "max_tablet_version_num or time_series_max_tablet_version_numin be.conf to a "
109
0
                "larger value.",
110
0
                version_count, max_version_config, _tablet->tablet_id());
111
0
    }
112
173k
    return Status::OK();
113
173k
}
114
115
173k
void CloudRowsetBuilder::update_tablet_stats() {
116
173k
    auto* tablet = cloud_tablet();
117
173k
    DCHECK(tablet);
118
173k
    DCHECK(_rowset);
119
173k
    tablet->fetch_add_approximate_num_rowsets(1);
120
173k
    tablet->fetch_add_approximate_num_segments(_rowset->num_segments());
121
173k
    tablet->fetch_add_approximate_num_rows(_rowset->num_rows());
122
173k
    tablet->fetch_add_approximate_data_size(_rowset->total_disk_size());
123
173k
    tablet->fetch_add_approximate_cumu_num_rowsets(1);
124
173k
    tablet->fetch_add_approximate_cumu_num_deltas(_rowset->num_segments());
125
173k
    tablet->write_count.fetch_add(1, std::memory_order_relaxed);
126
173k
}
127
128
519k
CloudTablet* CloudRowsetBuilder::cloud_tablet() {
129
519k
    return static_cast<CloudTablet*>(_tablet.get());
130
519k
}
131
132
85.6k
const RowsetMetaSharedPtr& CloudRowsetBuilder::rowset_meta() {
133
85.6k
    return _rowset_writer->rowset_meta();
134
85.6k
}
135
136
173k
Status CloudRowsetBuilder::set_txn_related_info() {
137
173k
    if (_tablet->enable_unique_key_merge_on_write()) {
138
        // For empty rowsets when skip_writing_empty_rowset_metadata=true,
139
        // store only a lightweight marker instead of full rowset info.
140
        // This allows CalcDeleteBitmapTask to detect and skip gracefully,
141
        // while using minimal memory (~16 bytes per entry).
142
51.8k
        if (_skip_writing_rowset_metadata) {
143
31.0k
            _engine.txn_delete_bitmap_cache().mark_empty_rowset(_req.txn_id, _tablet->tablet_id(),
144
31.0k
                                                                _req.txn_expiration);
145
31.0k
            return Status::OK();
146
31.0k
        }
147
20.7k
        if (config::enable_merge_on_write_correctness_check && _rowset->num_rows() != 0) {
148
20.7k
            auto st = _tablet->check_delete_bitmap_correctness(
149
20.7k
                    _delete_bitmap, _rowset->end_version() - 1, _req.txn_id, *_rowset_ids);
150
20.7k
            if (!st.ok()) {
151
0
                LOG(WARNING) << fmt::format(
152
0
                        "[tablet_id:{}][txn_id:{}][load_id:{}][partition_id:{}] "
153
0
                        "delete bitmap correctness check failed in commit phase!",
154
0
                        _req.tablet_id, _req.txn_id, UniqueId(_req.load_id).to_string(),
155
0
                        _req.partition_id);
156
0
                return st;
157
0
            }
158
20.7k
        }
159
20.7k
        _engine.txn_delete_bitmap_cache().set_tablet_txn_info(
160
20.7k
                _req.txn_id, _tablet->tablet_id(), _delete_bitmap, *_rowset_ids, _rowset,
161
20.7k
                _req.txn_expiration, _partial_update_info);
162
121k
    } else {
163
121k
        if (config::enable_cloud_make_rs_visible_on_be) {
164
121k
            if (_skip_writing_rowset_metadata) {
165
89.5k
                _engine.committed_rs_mgr().mark_empty_rowset(_req.txn_id, _tablet->tablet_id(),
166
89.5k
                                                             _req.txn_expiration);
167
89.5k
            } else {
168
32.4k
                _engine.meta_mgr().cache_committed_rowset(rowset_meta(), _req.txn_expiration);
169
32.4k
            }
170
121k
        }
171
121k
    }
172
142k
    return Status::OK();
173
173k
}
174
} // namespace doris