Coverage Report

Created: 2026-04-11 00:05

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
0
        : BaseRowsetBuilder(req, profile), _engine(engine) {}
32
33
0
CloudRowsetBuilder::~CloudRowsetBuilder() {
34
    // Clear file cache immediately when load fails
35
0
    if (_is_init && _rowset != nullptr && _rowset->rowset_meta()->rowset_state() == PREPARED) {
36
0
        _rowset->clear_cache();
37
0
    }
38
0
}
39
40
0
Status CloudRowsetBuilder::init() {
41
0
    _tablet = DORIS_TRY(_engine.get_tablet(_req.tablet_id));
42
43
0
    std::shared_ptr<MowContext> mow_context;
44
0
    if (_tablet->enable_unique_key_merge_on_write()) {
45
0
        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
0
        RETURN_IF_ERROR(init_mow_context(mow_context));
53
0
    }
54
0
    RETURN_IF_ERROR(check_tablet_version_count());
55
56
0
    using namespace std::chrono;
57
0
    std::static_pointer_cast<CloudTablet>(_tablet)->last_load_time_ms =
58
0
            duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
59
60
    // build tablet schema in request level
61
0
    RETURN_IF_ERROR(_build_current_tablet_schema(_req.index_id, _req.table_schema_param.get(),
62
0
                                                 *_tablet->tablet_schema()));
63
64
0
    RowsetWriterContext context;
65
0
    context.txn_id = _req.txn_id;
66
0
    context.txn_expiration = _req.txn_expiration;
67
0
    context.load_id = _req.load_id;
68
0
    context.rowset_state = PREPARED;
69
0
    context.segments_overlap = OVERLAPPING;
70
0
    context.tablet_schema = _tablet_schema;
71
0
    context.newest_write_timestamp = UnixSeconds();
72
0
    context.tablet_id = _req.tablet_id;
73
0
    context.index_id = _req.index_id;
74
0
    context.tablet = _tablet;
75
0
    context.write_type = DataWriteType::TYPE_DIRECT;
76
0
    context.mow_context = mow_context;
77
0
    context.write_file_cache = _req.write_file_cache;
78
0
    context.partial_update_info = _partial_update_info;
79
0
    context.file_cache_ttl_sec = _tablet->ttl_seconds();
80
0
    context.storage_resource = _engine.get_storage_resource(_req.storage_vault_id);
81
0
    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
0
    _rowset_writer = DORIS_TRY(_tablet->create_rowset_writer(context, false));
87
88
0
    _calc_delete_bitmap_token = _engine.calc_delete_bitmap_executor()->create_token();
89
90
0
    if (!_skip_writing_rowset_metadata) {
91
0
        RETURN_IF_ERROR(_engine.meta_mgr().prepare_rowset(*_rowset_writer->rowset_meta(), ""));
92
0
    }
93
94
0
    _is_init = true;
95
0
    return Status::OK();
96
0
}
97
98
0
Status CloudRowsetBuilder::check_tablet_version_count() {
99
0
    int64_t version_count = cloud_tablet()->fetch_add_approximate_num_rowsets(0);
100
0
    DBUG_EXECUTE_IF("RowsetBuilder.check_tablet_version_count.too_many_version",
101
0
                    { version_count = INT_MAX; });
102
    // TODO(plat1ko): load backoff algorithm
103
0
    int32_t max_version_config = cloud_tablet()->max_version_config();
104
0
    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
0
    return Status::OK();
113
0
}
114
115
0
void CloudRowsetBuilder::update_tablet_stats() {
116
0
    auto* tablet = cloud_tablet();
117
0
    DCHECK(tablet);
118
0
    DCHECK(_rowset);
119
0
    tablet->fetch_add_approximate_num_rowsets(1);
120
0
    tablet->fetch_add_approximate_num_segments(_rowset->num_segments());
121
0
    tablet->fetch_add_approximate_num_rows(_rowset->num_rows());
122
0
    tablet->fetch_add_approximate_data_size(_rowset->total_disk_size());
123
0
    tablet->fetch_add_approximate_cumu_num_rowsets(1);
124
0
    tablet->fetch_add_approximate_cumu_num_deltas(_rowset->num_segments());
125
0
    tablet->write_count.fetch_add(1, std::memory_order_relaxed);
126
0
}
127
128
0
CloudTablet* CloudRowsetBuilder::cloud_tablet() {
129
0
    return static_cast<CloudTablet*>(_tablet.get());
130
0
}
131
132
0
const RowsetMetaSharedPtr& CloudRowsetBuilder::rowset_meta() {
133
0
    return _rowset_writer->rowset_meta();
134
0
}
135
136
0
Status CloudRowsetBuilder::set_txn_related_info() {
137
0
    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
0
        if (_skip_writing_rowset_metadata) {
143
0
            _engine.txn_delete_bitmap_cache().mark_empty_rowset(_req.txn_id, _tablet->tablet_id(),
144
0
                                                                _req.txn_expiration);
145
0
            return Status::OK();
146
0
        }
147
0
        if (config::enable_merge_on_write_correctness_check && _rowset->num_rows() != 0) {
148
0
            auto st = _tablet->check_delete_bitmap_correctness(
149
0
                    _delete_bitmap, _rowset->end_version() - 1, _req.txn_id, *_rowset_ids);
150
0
            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
0
        }
159
0
        _engine.txn_delete_bitmap_cache().set_tablet_txn_info(
160
0
                _req.txn_id, _tablet->tablet_id(), _delete_bitmap, *_rowset_ids, _rowset,
161
0
                _req.txn_expiration, _partial_update_info);
162
0
    } else {
163
0
        if (config::enable_cloud_make_rs_visible_on_be) {
164
0
            if (_skip_writing_rowset_metadata) {
165
0
                _engine.committed_rs_mgr().mark_empty_rowset(_req.txn_id, _tablet->tablet_id(),
166
0
                                                             _req.txn_expiration);
167
0
            } else {
168
0
                _engine.meta_mgr().cache_committed_rowset(rowset_meta(), _req.txn_expiration);
169
0
            }
170
0
        }
171
0
    }
172
0
    return Status::OK();
173
0
}
174
} // namespace doris