Coverage Report

Created: 2026-03-13 19:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_rowset_writer.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_writer.h"
19
20
#include "common/logging.h"
21
#include "common/status.h"
22
#include "io/cache/block_file_cache_factory.h"
23
#include "io/fs/packed_file_manager.h"
24
#include "io/fs/packed_file_writer.h"
25
#include "storage/rowset/rowset_factory.h"
26
27
namespace doris {
28
29
1
CloudRowsetWriter::CloudRowsetWriter(CloudStorageEngine& engine) : _engine(engine) {}
30
31
1
CloudRowsetWriter::~CloudRowsetWriter() {
32
    // Must cancel any pending delete bitmap tasks before destruction.
33
    // Otherwise, the lambda in _generate_delete_bitmap may execute after the
34
    // CloudRowsetWriter destructor runs but before BaseBetaRowsetWriter destructor,
35
    // causing virtual function calls to resolve to BaseBetaRowsetWriter::_build_rowset_meta
36
    // instead of CloudRowsetWriter::_build_rowset_meta (use-after-free on vtable).
37
1
    if (_calc_delete_bitmap_token != nullptr) {
38
0
        _calc_delete_bitmap_token->cancel();
39
0
    }
40
1
}
41
42
1
Status CloudRowsetWriter::init(const RowsetWriterContext& rowset_writer_context) {
43
1
    _context = rowset_writer_context;
44
1
    _rowset_meta = std::make_shared<RowsetMeta>();
45
46
1
    if (_context.is_local_rowset()) {
47
        // In cloud mode, this branch implies it is an intermediate rowset for external merge sort,
48
        // we use `global_local_filesystem` to write data to `tmp_file_dir`(see `local_segment_path`).
49
0
        _context.tablet_path = io::FileCacheFactory::instance()->pick_one_cache_path();
50
1
    } else {
51
1
        _rowset_meta->set_remote_storage_resource(*_context.storage_resource);
52
1
    }
53
54
1
    _rowset_meta->set_rowset_id(_context.rowset_id);
55
1
    _rowset_meta->set_partition_id(_context.partition_id);
56
1
    _rowset_meta->set_tablet_id(_context.tablet_id);
57
1
    _rowset_meta->set_index_id(_context.index_id);
58
1
    _rowset_meta->set_tablet_schema_hash(_context.tablet_schema_hash);
59
1
    _rowset_meta->set_rowset_type(_context.rowset_type);
60
1
    _rowset_meta->set_rowset_state(_context.rowset_state);
61
1
    _rowset_meta->set_segments_overlap(_context.segments_overlap);
62
1
    _rowset_meta->set_txn_id(_context.txn_id);
63
1
    _rowset_meta->set_txn_expiration(_context.txn_expiration);
64
1
    _rowset_meta->set_compaction_level(_context.compaction_level);
65
1
    if (_context.rowset_state == PREPARED || _context.rowset_state == COMMITTED) {
66
0
        _is_pending = true;
67
0
        _rowset_meta->set_load_id(_context.load_id);
68
1
    } else {
69
        // Rowset generated by compaction or schema change
70
1
        _rowset_meta->set_version(_context.version);
71
1
        DCHECK_NE(_context.newest_write_timestamp, -1);
72
1
        _rowset_meta->set_newest_write_timestamp(_context.newest_write_timestamp);
73
1
    }
74
1
    _rowset_meta->set_tablet_schema(_context.tablet_schema);
75
1
    _rowset_meta->set_job_id(_context.job_id);
76
1
    _context.segment_collector = std::make_shared<SegmentCollectorT<BaseBetaRowsetWriter>>(this);
77
1
    _context.file_writer_creator = std::make_shared<FileWriterCreatorT<BaseBetaRowsetWriter>>(this);
78
1
    if (_context.mow_context != nullptr) {
79
0
        _calc_delete_bitmap_token = _engine.calc_delete_bitmap_executor_for_load()->create_token();
80
0
    }
81
1
    return Status::OK();
82
1
}
83
84
0
Status CloudRowsetWriter::_build_rowset_meta(RowsetMeta* rowset_meta, bool check_segment_num) {
85
0
    VLOG_NOTICE << "start to build rowset meta. tablet_id=" << rowset_meta->tablet_id()
86
0
                << ", rowset_id=" << rowset_meta->rowset_id()
87
0
                << ", check_segment_num=" << check_segment_num;
88
    // Call base class implementation
89
0
    RETURN_IF_ERROR(BaseBetaRowsetWriter::_build_rowset_meta(rowset_meta, check_segment_num));
90
91
    // Collect packed file segment index information for interim rowsets as well.
92
0
    return _collect_all_packed_slice_locations(rowset_meta);
93
0
}
94
95
0
Status CloudRowsetWriter::build(RowsetSharedPtr& rowset) {
96
0
    if (_calc_delete_bitmap_token != nullptr) {
97
0
        RETURN_IF_ERROR(_calc_delete_bitmap_token->wait());
98
0
    }
99
0
    RETURN_IF_ERROR(_close_file_writers());
100
101
    // TODO(plat1ko): check_segment_footer
102
103
0
    RETURN_IF_ERROR(_build_rowset_meta(_rowset_meta.get()));
104
    // At this point all writers have been closed, so collecting packed file indices is safe.
105
0
    RETURN_IF_ERROR(_collect_all_packed_slice_locations(_rowset_meta.get()));
106
    // If the current load is a partial update, new segments may be appended to the tmp rowset after the tmp rowset
107
    // has been committed if conflicts occur due to concurrent partial updates. However, when the recycler do recycling,
108
    // it will generate the paths for the segments to be recycled on the object storage based on the number of segments
109
    // in the rowset meta. If these newly added segments are written to the object storage and the transaction is aborted
110
    // due to a failure before successfully updating the rowset meta of the corresponding tmp rowset, these newly added
111
    // segments cannot be recycled by the recycler on the object storage. Therefore, we need a new state `BEGIN_PARTIAL_UPDATE`
112
    // to indicate that the recycler should use list+delete to recycle segments. After the tmp rowset's rowset meta being
113
    // updated successfully, the `rowset_state` will be set to `COMMITTED` and the recycler can do recycling based on the
114
    // number of segments in the rowset meta safely.
115
    //
116
    // rowset_state's FSM:
117
    //
118
    //                      transfer 0
119
    //       PREPARED ---------------------------> COMMITTED
120
    //                 |                               ^
121
    //                 | transfer 1                    |
122
    //                 |                               | transfer 2
123
    //                 |--> BEGIN_PARTIAL_UPDATE ------|
124
    //
125
    // transfer 0 (PREPARED -> COMMITTED): finish writing a rowset and the rowset' meta will not be changed
126
    // transfer 1 (PREPARED -> BEGIN_PARTIAL_UPDATE): finish writing a rowset, but may append new segments later and the rowset's meta may be changed
127
    // transfer 2 (BEGIN_PARTIAL_UPDATE -> VISIBLE): finish adding new segments and the rowset' meta will not be changed, the rowset is visible to users
128
0
    if (_context.partial_update_info && _context.partial_update_info->is_partial_update()) {
129
0
        _rowset_meta->set_rowset_state(BEGIN_PARTIAL_UPDATE);
130
0
    } else {
131
0
        _rowset_meta->set_rowset_state(COMMITTED);
132
0
    }
133
134
0
    _rowset_meta->set_tablet_schema(_context.tablet_schema);
135
136
0
    if (_rowset_meta->newest_write_timestamp() == -1) {
137
0
        _rowset_meta->set_newest_write_timestamp(UnixSeconds());
138
0
    }
139
140
0
    if (auto seg_file_size = _seg_files.segments_file_size(_segment_start_id);
141
0
        !seg_file_size.has_value()) [[unlikely]] {
142
0
        LOG(ERROR) << "expected segment file sizes, but none presents: " << seg_file_size.error();
143
0
    } else {
144
0
        _rowset_meta->add_segments_file_size(seg_file_size.value());
145
0
    }
146
0
    if (_context.tablet_schema->has_inverted_index() || _context.tablet_schema->has_ann_index()) {
147
0
        if (auto idx_files_info = _idx_files.inverted_index_file_info(_segment_start_id);
148
0
            !idx_files_info.has_value()) [[unlikely]] {
149
0
            LOG(ERROR) << "expected inverted index files info, but none presents: "
150
0
                       << idx_files_info.error();
151
0
        } else {
152
0
            _rowset_meta->add_inverted_index_files_info(idx_files_info.value());
153
0
        }
154
0
    }
155
156
0
    RETURN_NOT_OK_STATUS_WITH_WARN(
157
0
            RowsetFactory::create_rowset(_context.tablet_schema, _context.tablet_path, _rowset_meta,
158
0
                                         &rowset),
159
0
            "rowset init failed when build new rowset");
160
0
    _already_built = true;
161
0
    return Status::OK();
162
0
}
163
164
0
Status CloudRowsetWriter::_collect_all_packed_slice_locations(RowsetMeta* rowset_meta) {
165
0
    VLOG_NOTICE << "start to collect packed slice locations for rowset meta. tablet_id="
166
0
                << rowset_meta->tablet_id() << ", rowset_id=" << rowset_meta->rowset_id();
167
0
    if (!_context.packed_file_active) {
168
0
        return Status::OK();
169
0
    }
170
171
    // Collect segment file packed indices
172
0
    const auto& file_writers = _seg_files.get_file_writers();
173
0
    for (const auto& [seg_id, writer_ptr] : file_writers) {
174
0
        auto segment_path = _context.segment_path(seg_id);
175
0
        RETURN_IF_ERROR(
176
0
                _collect_packed_slice_location(writer_ptr.get(), segment_path, rowset_meta));
177
0
    }
178
179
    // Collect inverted index file packed indices
180
0
    const auto& idx_file_writers = _idx_files.get_file_writers();
181
0
    for (const auto& [seg_id, idx_writer_ptr] : idx_file_writers) {
182
0
        if (idx_writer_ptr != nullptr && idx_writer_ptr->get_file_writer() != nullptr) {
183
0
            auto segment_path = _context.segment_path(seg_id);
184
0
            auto index_prefix_view =
185
0
                    InvertedIndexDescriptor::get_index_file_path_prefix(segment_path);
186
0
            std::string index_path =
187
0
                    InvertedIndexDescriptor::get_index_file_path_v2(std::string(index_prefix_view));
188
0
            RETURN_IF_ERROR(_collect_packed_slice_location(idx_writer_ptr->get_file_writer(),
189
0
                                                           index_path, rowset_meta));
190
0
        }
191
0
    }
192
193
0
    return Status::OK();
194
0
}
195
196
Status CloudRowsetWriter::_collect_packed_slice_location(io::FileWriter* file_writer,
197
                                                         const std::string& file_path,
198
0
                                                         RowsetMeta* rowset_meta) {
199
0
    VLOG_NOTICE << "collect packed slice location for file: " << file_path;
200
    // Check if file writer is closed
201
0
    if (file_writer->state() != io::FileWriter::State::CLOSED) {
202
        // Writer is still open; index will be collected after it is closed.
203
0
        return Status::OK();
204
0
    }
205
206
    // Check if file is actually in packed file (not direct write for large files)
207
0
    if (!file_writer->is_in_packed_file()) {
208
0
        return Status::OK();
209
0
    }
210
211
    // Get packed slice location directly from PackedFileManager
212
0
    io::PackedSliceLocation index;
213
0
    RETURN_IF_ERROR(
214
0
            io::PackedFileManager::instance()->get_packed_slice_location(file_path, &index));
215
0
    if (index.packed_file_path.empty()) {
216
0
        return Status::OK(); // File not in packed file, skip
217
0
    }
218
219
0
    rowset_meta->add_packed_slice_location(file_path, index.packed_file_path, index.offset,
220
0
                                           index.size, index.packed_file_size);
221
    LOG(INFO) << "collect packed file index: " << file_path << " -> " << index.packed_file_path
222
0
              << ", offset: " << index.offset << ", size: " << index.size;
223
0
    return Status::OK();
224
0
}
225
226
} // namespace doris