Coverage Report

Created: 2026-04-13 08:21

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