Coverage Report

Created: 2026-08-07 14:29

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/cloud/cloud_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 "cloud/cloud_delta_writer.h"
19
20
#include "cloud/cloud_meta_mgr.h"
21
#include "cloud/cloud_rowset_builder.h"
22
#include "cloud/cloud_storage_engine.h"
23
#include "cloud/config.h"
24
#include "load/delta_writer/delta_writer.h"
25
#include "load/memtable/memtable_memory_limiter.h"
26
#include "runtime/exec_env.h"
27
#include "runtime/thread_context.h"
28
29
namespace doris {
30
31
bvar::Adder<int64_t> g_cloud_commit_rowset_count("cloud_commit_rowset_count");
32
bvar::Adder<int64_t> g_cloud_commit_empty_rowset_count("cloud_commit_empty_rowset_count");
33
34
CloudDeltaWriter::CloudDeltaWriter(CloudStorageEngine& engine, const WriteRequest& req,
35
                                   RuntimeProfile* profile, const UniqueId& load_id)
36
266k
        : BaseDeltaWriter(req, profile, load_id) {
37
266k
    _rowset_builder = std::make_unique<CloudRowsetBuilder>(engine, req, profile);
38
266k
    _resource_ctx = thread_context()->resource_ctx();
39
266k
}
40
41
CloudDeltaWriter::CloudDeltaWriter(CloudStorageEngine& engine, const WriteRequest& group_build_req,
42
                                   const WriteRequest& sub_data_req,
43
                                   const WriteRequest& sub_row_binlog_req, RuntimeProfile* profile,
44
                                   const UniqueId& load_id)
45
0
        : BaseDeltaWriter(group_build_req, profile, load_id) {
46
0
    DCHECK(group_build_req.write_req_type == WriteRequestType::GROUP &&
47
0
           sub_data_req.write_req_type == WriteRequestType::DATA &&
48
0
           sub_row_binlog_req.write_req_type == WriteRequestType::ROW_BINLOG);
49
0
    _rowset_builder = std::make_unique<CloudGroupRowsetBuilder>(
50
0
            engine, group_build_req, sub_data_req, sub_row_binlog_req, profile);
51
0
    _resource_ctx = thread_context()->resource_ctx();
52
0
}
53
54
266k
CloudDeltaWriter::~CloudDeltaWriter() = default;
55
56
31.4k
Status CloudDeltaWriter::batch_init(std::vector<CloudDeltaWriter*> writers) {
57
31.4k
    if (writers.empty()) {
58
0
        return Status::OK();
59
0
    }
60
61
31.4k
    std::vector<std::function<Status()>> tasks;
62
31.4k
    tasks.reserve(writers.size());
63
112k
    for (auto* writer : writers) {
64
112k
        if (writer->_is_init || writer->_is_cancelled) {
65
58.3k
            continue;
66
58.3k
        }
67
68
54.6k
        tasks.emplace_back([writer] {
69
54.4k
            SCOPED_ATTACH_TASK(writer->resource_context());
70
54.4k
            std::lock_guard<bthread::Mutex> lock(writer->_mtx);
71
54.5k
            if (writer->_is_init || writer->_is_cancelled) {
72
0
                return Status::OK();
73
0
            }
74
54.4k
            Status st = writer->init(); // included in SCOPED_ATTACH_TASK
75
54.4k
            return st;
76
54.4k
        });
77
54.6k
    }
78
79
31.4k
    return cloud::bthread_fork_join(tasks, 10);
80
31.4k
}
81
82
Status CloudDeltaWriter::write(const Block* block, const TabletAddRowsPayload& rows,
83
114k
                               bool* memtable_flushed) {
84
114k
    if (memtable_flushed != nullptr) {
85
114k
        *memtable_flushed = false;
86
114k
    }
87
114k
    if (rows.row_idxs.empty()) [[unlikely]] {
88
0
        return Status::OK();
89
0
    }
90
114k
    if (_req.enable_table_memtable_backpressure && !_req.is_high_priority) {
91
4.45k
        ExecEnv::GetInstance()->memtable_memory_limiter()->handle_table_memtable_backpressure(
92
4.45k
                [this]() {
93
0
                    std::lock_guard lock(_mtx);
94
0
                    return _is_cancelled;
95
0
                },
96
4.45k
                table_id());
97
4.45k
    }
98
114k
    std::lock_guard lock(_mtx);
99
114k
    CHECK(_is_init || _is_cancelled);
100
114k
    {
101
114k
        SCOPED_TIMER(_wait_flush_limit_timer);
102
114k
        while (_memtable_writer->flush_running_count() >=
103
114k
               config::memtable_flush_running_count_limit) {
104
0
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
105
0
        }
106
114k
    }
107
114k
    return _memtable_writer->write(block, rows, memtable_flushed);
108
114k
}
109
110
54.6k
Status CloudDeltaWriter::close() {
111
54.6k
    std::lock_guard lock(_mtx);
112
54.6k
    CHECK(_is_init);
113
54.6k
    return _memtable_writer->close();
114
54.6k
}
115
116
0
Status CloudDeltaWriter::flush_memtable_async() {
117
0
    std::lock_guard lock(_mtx);
118
0
    return BaseDeltaWriter::flush_memtable_async();
119
0
}
120
121
91.6k
Status CloudDeltaWriter::cancel_with_status(const Status& st) {
122
91.6k
    std::lock_guard lock(_mtx);
123
91.6k
    return BaseDeltaWriter::cancel_with_status(st);
124
91.6k
}
125
126
54.6k
Status CloudDeltaWriter::build_rowset() {
127
54.6k
    std::lock_guard lock(_mtx);
128
54.6k
    CHECK(_is_init);
129
54.6k
    return BaseDeltaWriter::build_rowset();
130
54.6k
}
131
132
523k
CloudRowsetBuilder* CloudDeltaWriter::rowset_builder() {
133
523k
    return static_cast<CloudRowsetBuilder*>(_rowset_builder.get());
134
523k
}
135
136
174k
void CloudDeltaWriter::update_tablet_stats() {
137
174k
    rowset_builder()->update_tablet_stats();
138
174k
}
139
140
173k
Status CloudDeltaWriter::commit_rowset() {
141
173k
    g_cloud_commit_rowset_count << 1;
142
173k
    std::lock_guard<bthread::Mutex> lock(_mtx);
143
144
    // Handle empty rowset (no data written)
145
173k
    if (!_is_init) {
146
119k
        g_cloud_commit_empty_rowset_count << 1;
147
119k
        return _commit_empty_rowset();
148
119k
    }
149
150
53.4k
    return rowset_builder()->commit_rowset("", _rowset_builder->tablet()->table_id());
151
173k
}
152
153
119k
Status CloudDeltaWriter::_commit_empty_rowset() {
154
    // If skip writing empty rowset metadata is enabled,
155
    // we do not prepare rowset to meta service.
156
119k
    if (config::skip_writing_empty_rowset_metadata) {
157
119k
        rowset_builder()->set_skip_writing_rowset_metadata(true);
158
119k
    }
159
160
119k
    RETURN_IF_ERROR(_rowset_builder->init());
161
119k
    RETURN_IF_ERROR(_rowset_builder->build_rowset());
162
163
    // If skip writing empty rowset metadata is enabled, we do not commit rowset to meta service.
164
119k
    if (config::skip_writing_empty_rowset_metadata) {
165
119k
        return Status::OK();
166
119k
    }
167
    // write a empty rowset kv to keep version continuous
168
145
    return rowset_builder()->commit_rowset("", _rowset_builder->tablet()->table_id());
169
119k
}
170
171
174k
Status CloudDeltaWriter::set_txn_related_info() {
172
174k
    return rowset_builder()->set_txn_related_info();
173
174k
}
174
175
} // namespace doris