Coverage Report

Created: 2026-08-13 04:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/load/delta_writer/delta_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 "load/delta_writer/delta_writer.h"
19
20
#include <fmt/format.h>
21
#include <gen_cpp/internal_service.pb.h>
22
#include <gen_cpp/olap_file.pb.h>
23
24
#include <ostream>
25
#include <string>
26
#include <utility>
27
28
// IWYU pragma: no_include <opentelemetry/common/threadlocal.h>
29
#include "common/compiler_util.h" // IWYU pragma: keep
30
#include "common/config.h"
31
#include "common/logging.h"
32
#include "common/status.h"
33
#include "core/block/block.h"
34
#include "io/fs/file_writer.h" // IWYU pragma: keep
35
#include "load/memtable/memtable_flush_executor.h"
36
#include "load/memtable/memtable_memory_limiter.h"
37
#include "runtime/exec_env.h"
38
#include "runtime/thread_context.h"
39
#include "runtime/workload_management/resource_context.h"
40
#include "storage/olap_define.h"
41
#include "storage/rowset/beta_rowset.h"
42
#include "storage/rowset/beta_rowset_writer.h"
43
#include "storage/rowset/rowset_meta.h"
44
#include "storage/rowset_builder.h"
45
#include "storage/schema_change/schema_change.h"
46
#include "storage/storage_engine.h"
47
#include "storage/tablet/tablet_manager.h"
48
#include "storage/tablet_info.h"
49
#include "util/mem_info.h"
50
#include "util/stopwatch.hpp"
51
#include "util/time.h"
52
53
namespace doris {
54
using namespace ErrorCode;
55
56
BaseDeltaWriter::BaseDeltaWriter(const WriteRequest& req, RuntimeProfile* profile,
57
                                 const UniqueId& load_id)
58
298k
        : _req(req), _memtable_writer(new MemTableWriter(req)) {
59
298k
    if (profile != nullptr) {
60
47
        _init_profile(profile);
61
47
    }
62
298k
}
63
64
DeltaWriter::DeltaWriter(StorageEngine& engine, const WriteRequest& req, RuntimeProfile* profile,
65
                         const UniqueId& load_id)
66
29.4k
        : BaseDeltaWriter(req, profile, load_id), _engine(engine) {
67
29.4k
    DCHECK(req.write_req_type == WriteRequestType::DATA);
68
29.4k
    _rowset_builder = std::make_unique<RowsetBuilder>(_engine, req, profile);
69
29.4k
}
70
71
DeltaWriter::DeltaWriter(StorageEngine& engine, const WriteRequest& group_build_req,
72
                         const WriteRequest& sub_data_req, const WriteRequest& sub_row_binlog_req,
73
                         RuntimeProfile* profile, const UniqueId& load_id)
74
0
        : BaseDeltaWriter(group_build_req, profile, load_id), _engine(engine) {
75
0
    DCHECK(group_build_req.write_req_type == WriteRequestType::GROUP &&
76
0
           sub_data_req.write_req_type == WriteRequestType::DATA &&
77
0
           sub_row_binlog_req.write_req_type == WriteRequestType::ROW_BINLOG);
78
0
    _rowset_builder = std::make_unique<GroupRowsetBuilder>(_engine, group_build_req, sub_data_req,
79
0
                                                           sub_row_binlog_req, profile);
80
0
}
81
82
47
void BaseDeltaWriter::_init_profile(RuntimeProfile* profile) {
83
47
    DCHECK(profile != nullptr);
84
47
    _profile = profile->create_child(fmt::format("DeltaWriter {}", _req.tablet_id), true, true);
85
47
    _close_wait_timer = ADD_TIMER(_profile, "CloseWaitTime");
86
47
    _wait_flush_limit_timer = ADD_TIMER(_profile, "WaitFlushLimitTime");
87
47
}
88
89
0
void DeltaWriter::_init_profile(RuntimeProfile* profile) {
90
0
    DCHECK(profile != nullptr);
91
0
    BaseDeltaWriter::_init_profile(profile);
92
0
    _commit_txn_timer = ADD_TIMER(_profile, "CommitTxnTime");
93
0
}
94
95
298k
BaseDeltaWriter::~BaseDeltaWriter() {
96
298k
    if (!_is_init) {
97
215k
        return;
98
215k
    }
99
100
    // cancel and wait all memtables in flush queue to be finished
101
82.4k
    static_cast<void>(_memtable_writer->cancel());
102
103
82.5k
    if (_rowset_builder->tablet() != nullptr) {
104
82.5k
        const FlushStatistic& stat = _memtable_writer->get_flush_token_stats();
105
82.5k
        _rowset_builder->tablet()->flush_bytes->increment(stat.flush_size_bytes);
106
82.5k
        _rowset_builder->tablet()->flush_finish_count->increment(stat.flush_finish_count);
107
82.5k
    }
108
82.4k
}
109
110
void BaseDeltaWriter::collect_tablet_load_rowset_num_info(
111
        BaseTablet* tablet,
112
158k
        google::protobuf::RepeatedPtrField<PTabletLoadRowsetInfo>* tablet_infos) {
113
158k
    if (tablet == nullptr) {
114
0
        return;
115
0
    }
116
158k
    auto max_version_config = tablet->max_version_config();
117
158k
    if (auto version_cnt = tablet->tablet_meta()->version_count();
118
158k
        UNLIKELY(version_cnt >
119
158k
                 (max_version_config * config::load_back_pressure_version_threshold / 100))) {
120
0
        auto* load_info = tablet_infos->Add();
121
0
        load_info->set_current_rowset_nums(static_cast<int32_t>(version_cnt));
122
0
        load_info->set_max_config_rowset_nums(max_version_config);
123
0
    }
124
158k
}
125
126
void BaseDeltaWriter::set_tablet_load_rowset_num_info(
127
130k
        google::protobuf::RepeatedPtrField<PTabletLoadRowsetInfo>* tablet_infos) {
128
130k
    auto* tablet = _rowset_builder->tablet().get();
129
130k
    collect_tablet_load_rowset_num_info(tablet, tablet_infos);
130
130k
}
131
132
4.37k
int64_t BaseDeltaWriter::table_id() const {
133
4.37k
    DORIS_CHECK(_req.table_schema_param != nullptr);
134
4.37k
    return _req.table_schema_param->table_id();
135
4.37k
}
136
137
DeltaWriter::~DeltaWriter() = default;
138
139
82.2k
Status BaseDeltaWriter::init() {
140
82.2k
    if (_is_init) {
141
0
        return Status::OK();
142
0
    }
143
82.2k
    std::shared_ptr<WorkloadGroup> wg_sptr = nullptr;
144
82.2k
    if (doris::thread_context()->is_attach_task()) {
145
82.2k
        wg_sptr = doris::thread_context()->resource_ctx()->workload_group();
146
82.2k
    }
147
82.2k
    RETURN_IF_ERROR(_rowset_builder->init());
148
82.2k
    RETURN_IF_ERROR(_memtable_writer->init(
149
82.2k
            _rowset_builder->rowset_writer(), _rowset_builder->tablet_schema(),
150
82.2k
            _rowset_builder->get_partial_update_info(), wg_sptr,
151
82.2k
            _rowset_builder->tablet_sptr()->enable_unique_key_merge_on_write()));
152
82.2k
    ExecEnv::GetInstance()->memtable_memory_limiter()->register_writer(_memtable_writer);
153
82.2k
    _is_init = true;
154
82.2k
    return Status::OK();
155
82.2k
}
156
157
Status DeltaWriter::write(const Block* block, const TabletAddRowsPayload& rows,
158
15.3k
                          bool* memtable_flushed) {
159
15.3k
    if (memtable_flushed != nullptr) {
160
15.2k
        *memtable_flushed = false;
161
15.2k
    }
162
15.3k
    if (UNLIKELY(rows.row_idxs.empty())) {
163
0
        return Status::OK();
164
0
    }
165
15.3k
    if (_req.enable_table_memtable_backpressure && !_req.is_high_priority) {
166
0
        ExecEnv::GetInstance()->memtable_memory_limiter()->handle_table_memtable_backpressure(
167
0
                [this]() {
168
0
                    std::lock_guard<std::mutex> l(_lock);
169
0
                    return _is_cancelled;
170
0
                },
171
0
                table_id());
172
0
    }
173
15.3k
    _lock_watch.start();
174
15.3k
    std::lock_guard<std::mutex> l(_lock);
175
15.3k
    _lock_watch.stop();
176
15.3k
    if (!_is_init && !_is_cancelled) {
177
15.3k
        RETURN_IF_ERROR(init());
178
15.3k
    }
179
15.3k
    {
180
15.3k
        SCOPED_TIMER(_wait_flush_limit_timer);
181
15.3k
        while (_memtable_writer->flush_running_count() >=
182
15.3k
               config::memtable_flush_running_count_limit) {
183
0
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
184
0
        }
185
15.3k
    }
186
15.3k
    return _memtable_writer->write(block, rows, memtable_flushed);
187
15.3k
}
188
189
27.9k
Status BaseDeltaWriter::wait_flush() {
190
27.9k
    return _memtable_writer->wait_flush();
191
27.9k
}
192
193
0
Status BaseDeltaWriter::flush_memtable_async() {
194
0
    return _memtable_writer->flush_async();
195
0
}
196
197
0
Status DeltaWriter::flush_memtable_async() {
198
0
    _lock_watch.start();
199
0
    std::lock_guard<std::mutex> l(_lock);
200
0
    _lock_watch.stop();
201
0
    return BaseDeltaWriter::flush_memtable_async();
202
0
}
203
204
27.9k
Status DeltaWriter::close() {
205
27.9k
    _lock_watch.start();
206
27.9k
    std::lock_guard<std::mutex> l(_lock);
207
27.9k
    _lock_watch.stop();
208
27.9k
    if (!_is_init && !_is_cancelled) {
209
        // if this delta writer is not initialized, but close() is called.
210
        // which means this tablet has no data loaded, but at least one tablet
211
        // in same partition has data loaded.
212
        // so we have to also init this DeltaWriter, so that it can create an empty rowset
213
        // for this tablet when being closed.
214
12.6k
        RETURN_IF_ERROR(init());
215
12.6k
    }
216
27.9k
    return _memtable_writer->close();
217
27.9k
}
218
219
82.5k
Status BaseDeltaWriter::build_rowset() {
220
82.5k
    SCOPED_TIMER(_close_wait_timer);
221
82.5k
    RETURN_IF_ERROR(_memtable_writer->close_wait(_profile));
222
82.4k
    return _rowset_builder->build_rowset();
223
82.5k
}
224
225
27.9k
Status DeltaWriter::build_rowset() {
226
27.9k
    std::lock_guard<std::mutex> l(_lock);
227
27.9k
    DCHECK(_is_init)
228
0
            << "delta writer is supposed be to initialized before build_rowset() being called";
229
27.9k
    return BaseDeltaWriter::build_rowset();
230
27.9k
}
231
232
202k
Status BaseDeltaWriter::submit_calc_delete_bitmap_task() {
233
202k
    return _rowset_builder->submit_calc_delete_bitmap_task();
234
202k
}
235
236
202k
Status BaseDeltaWriter::wait_calc_delete_bitmap() {
237
202k
    return _rowset_builder->wait_calc_delete_bitmap();
238
202k
}
239
240
15
Status DeltaWriter::commit_txn() {
241
15
    std::lock_guard<std::mutex> l(_lock);
242
15
    SCOPED_TIMER(_commit_txn_timer);
243
15
    return _rowset_builder->commit_txn();
244
15
}
245
246
95.4k
Status BaseDeltaWriter::cancel() {
247
95.4k
    return cancel_with_status(Status::Cancelled("already cancelled"));
248
95.4k
}
249
250
95.4k
Status BaseDeltaWriter::cancel_with_status(const Status& st) {
251
95.4k
    if (_is_cancelled) {
252
0
        return Status::OK();
253
0
    }
254
95.4k
    RETURN_IF_ERROR(_memtable_writer->cancel_with_status(st));
255
95.4k
    _is_cancelled = true;
256
95.4k
    return Status::OK();
257
95.4k
}
258
259
1.50k
Status DeltaWriter::cancel_with_status(const Status& st) {
260
1.50k
    std::lock_guard<std::mutex> l(_lock);
261
1.50k
    return BaseDeltaWriter::cancel_with_status(st);
262
1.50k
}
263
264
476
int64_t BaseDeltaWriter::mem_consumption(MemType mem) {
265
476
    return _memtable_writer->mem_consumption(mem);
266
476
}
267
268
230k
int64_t BaseDeltaWriter::num_rows_filtered() const {
269
230k
    auto rowset_writer = _rowset_builder->rowset_writer();
270
230k
    return rowset_writer == nullptr ? 0 : rowset_writer->num_rows_filtered();
271
230k
}
272
273
} // namespace doris