Coverage Report

Created: 2026-08-10 05:10

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
267k
        : BaseDeltaWriter(req, profile, load_id) {
37
267k
    _rowset_builder = std::make_unique<CloudRowsetBuilder>(engine, req, profile);
38
267k
    _resource_ctx = thread_context()->resource_ctx();
39
267k
}
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
267k
CloudDeltaWriter::~CloudDeltaWriter() = default;
55
56
30.9k
Status CloudDeltaWriter::batch_init(std::vector<CloudDeltaWriter*> writers) {
57
30.9k
    if (writers.empty()) {
58
0
        return Status::OK();
59
0
    }
60
61
30.9k
    std::vector<std::function<Status()>> tasks;
62
30.9k
    tasks.reserve(writers.size());
63
111k
    for (auto* writer : writers) {
64
111k
        if (writer->_is_init || writer->_is_cancelled) {
65
58.0k
            continue;
66
58.0k
        }
67
68
53.9k
        tasks.emplace_back([writer] {
69
53.7k
            SCOPED_ATTACH_TASK(writer->resource_context());
70
53.7k
            std::lock_guard<bthread::Mutex> lock(writer->_mtx);
71
53.8k
            if (writer->_is_init || writer->_is_cancelled) {
72
0
                return Status::OK();
73
0
            }
74
53.7k
            Status st = writer->init(); // included in SCOPED_ATTACH_TASK
75
53.7k
            return st;
76
53.7k
        });
77
53.9k
    }
78
79
30.9k
    return cloud::bthread_fork_join(tasks, 10);
80
30.9k
}
81
82
Status CloudDeltaWriter::write(const Block* block, const TabletAddRowsPayload& rows,
83
113k
                               bool* memtable_flushed) {
84
113k
    if (memtable_flushed != nullptr) {
85
113k
        *memtable_flushed = false;
86
113k
    }
87
113k
    if (rows.row_idxs.empty()) [[unlikely]] {
88
0
        return Status::OK();
89
0
    }
90
113k
    if (_req.enable_table_memtable_backpressure && !_req.is_high_priority) {
91
4.34k
        ExecEnv::GetInstance()->memtable_memory_limiter()->handle_table_memtable_backpressure(
92
4.34k
                [this]() {
93
0
                    std::lock_guard lock(_mtx);
94
0
                    return _is_cancelled;
95
0
                },
96
4.34k
                table_id());
97
4.34k
    }
98
113k
    std::lock_guard lock(_mtx);
99
113k
    CHECK(_is_init || _is_cancelled);
100
113k
    {
101
113k
        SCOPED_TIMER(_wait_flush_limit_timer);
102
113k
        while (_memtable_writer->flush_running_count() >=
103
113k
               config::memtable_flush_running_count_limit) {
104
0
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
105
0
        }
106
113k
    }
107
113k
    return _memtable_writer->write(block, rows, memtable_flushed);
108
113k
}
109
110
53.9k
Status CloudDeltaWriter::close() {
111
53.9k
    std::lock_guard lock(_mtx);
112
53.9k
    CHECK(_is_init);
113
53.9k
    return _memtable_writer->close();
114
53.9k
}
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
93.1k
Status CloudDeltaWriter::cancel_with_status(const Status& st) {
122
93.1k
    std::lock_guard lock(_mtx);
123
93.1k
    return BaseDeltaWriter::cancel_with_status(st);
124
93.1k
}
125
126
53.8k
Status CloudDeltaWriter::build_rowset() {
127
53.8k
    std::lock_guard lock(_mtx);
128
53.8k
    CHECK(_is_init);
129
53.8k
    return BaseDeltaWriter::build_rowset();
130
53.8k
}
131
132
522k
CloudRowsetBuilder* CloudDeltaWriter::rowset_builder() {
133
522k
    return static_cast<CloudRowsetBuilder*>(_rowset_builder.get());
134
522k
}
135
136
174k
void CloudDeltaWriter::update_tablet_stats() {
137
174k
    rowset_builder()->update_tablet_stats();
138
174k
}
139
140
172k
Status CloudDeltaWriter::commit_rowset() {
141
172k
    g_cloud_commit_rowset_count << 1;
142
172k
    std::lock_guard<bthread::Mutex> lock(_mtx);
143
144
    // Handle empty rowset (no data written)
145
172k
    if (!_is_init) {
146
120k
        g_cloud_commit_empty_rowset_count << 1;
147
120k
        return _commit_empty_rowset();
148
120k
    }
149
150
52.6k
    return rowset_builder()->commit_rowset("", _rowset_builder->tablet()->table_id());
151
172k
}
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
120k
    if (config::skip_writing_empty_rowset_metadata) {
157
120k
        rowset_builder()->set_skip_writing_rowset_metadata(true);
158
120k
    }
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
120k
    if (config::skip_writing_empty_rowset_metadata) {
165
120k
        return Status::OK();
166
120k
    }
167
    // write a empty rowset kv to keep version continuous
168
18.4E
    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