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 | 0 | : BaseDeltaWriter(req, profile, load_id), _engine(engine) { |
37 | 0 | _rowset_builder = std::make_unique<CloudRowsetBuilder>(engine, req, profile); |
38 | 0 | _resource_ctx = thread_context()->resource_ctx(); |
39 | 0 | } |
40 | | |
41 | 0 | CloudDeltaWriter::~CloudDeltaWriter() = default; |
42 | | |
43 | 0 | Status CloudDeltaWriter::batch_init(std::vector<CloudDeltaWriter*> writers) { |
44 | 0 | if (writers.empty()) { |
45 | 0 | return Status::OK(); |
46 | 0 | } |
47 | | |
48 | 0 | std::vector<std::function<Status()>> tasks; |
49 | 0 | tasks.reserve(writers.size()); |
50 | 0 | for (auto* writer : writers) { |
51 | 0 | if (writer->_is_init || writer->_is_cancelled) { |
52 | 0 | continue; |
53 | 0 | } |
54 | | |
55 | 0 | tasks.emplace_back([writer] { |
56 | 0 | SCOPED_ATTACH_TASK(writer->resource_context()); |
57 | 0 | std::lock_guard<bthread::Mutex> lock(writer->_mtx); |
58 | 0 | if (writer->_is_init || writer->_is_cancelled) { |
59 | 0 | return Status::OK(); |
60 | 0 | } |
61 | 0 | Status st = writer->init(); // included in SCOPED_ATTACH_TASK |
62 | 0 | return st; |
63 | 0 | }); |
64 | 0 | } |
65 | |
|
66 | 0 | return cloud::bthread_fork_join(tasks, 10); |
67 | 0 | } |
68 | | |
69 | | Status CloudDeltaWriter::write(const Block* block, const DorisVector<uint32_t>& row_idxs, |
70 | 0 | bool* memtable_flushed) { |
71 | 0 | if (memtable_flushed != nullptr) { |
72 | 0 | *memtable_flushed = false; |
73 | 0 | } |
74 | 0 | if (row_idxs.empty()) [[unlikely]] { |
75 | 0 | return Status::OK(); |
76 | 0 | } |
77 | 0 | if (_req.enable_table_memtable_backpressure) { |
78 | 0 | ExecEnv::GetInstance()->memtable_memory_limiter()->handle_table_memtable_backpressure( |
79 | 0 | [this]() { |
80 | 0 | std::lock_guard lock(_mtx); |
81 | 0 | return _is_cancelled; |
82 | 0 | }, |
83 | 0 | table_id()); |
84 | 0 | } |
85 | 0 | std::lock_guard lock(_mtx); |
86 | 0 | CHECK(_is_init || _is_cancelled); |
87 | 0 | { |
88 | 0 | SCOPED_TIMER(_wait_flush_limit_timer); |
89 | 0 | while (_memtable_writer->flush_running_count() >= |
90 | 0 | config::memtable_flush_running_count_limit) { |
91 | 0 | std::this_thread::sleep_for(std::chrono::milliseconds(10)); |
92 | 0 | } |
93 | 0 | } |
94 | 0 | return _memtable_writer->write(block, row_idxs, memtable_flushed); |
95 | 0 | } |
96 | | |
97 | 0 | Status CloudDeltaWriter::close() { |
98 | 0 | std::lock_guard lock(_mtx); |
99 | 0 | CHECK(_is_init); |
100 | 0 | return _memtable_writer->close(); |
101 | 0 | } |
102 | | |
103 | 0 | Status CloudDeltaWriter::flush_memtable_async() { |
104 | 0 | std::lock_guard lock(_mtx); |
105 | 0 | return BaseDeltaWriter::flush_memtable_async(); |
106 | 0 | } |
107 | | |
108 | 0 | Status CloudDeltaWriter::cancel_with_status(const Status& st) { |
109 | 0 | std::lock_guard lock(_mtx); |
110 | 0 | return BaseDeltaWriter::cancel_with_status(st); |
111 | 0 | } |
112 | | |
113 | 0 | Status CloudDeltaWriter::build_rowset() { |
114 | 0 | std::lock_guard lock(_mtx); |
115 | 0 | CHECK(_is_init); |
116 | 0 | return BaseDeltaWriter::build_rowset(); |
117 | 0 | } |
118 | | |
119 | 0 | CloudRowsetBuilder* CloudDeltaWriter::rowset_builder() { |
120 | 0 | return static_cast<CloudRowsetBuilder*>(_rowset_builder.get()); |
121 | 0 | } |
122 | | |
123 | 0 | void CloudDeltaWriter::update_tablet_stats() { |
124 | 0 | rowset_builder()->update_tablet_stats(); |
125 | 0 | } |
126 | | |
127 | 0 | const RowsetMetaSharedPtr& CloudDeltaWriter::rowset_meta() { |
128 | 0 | return rowset_builder()->rowset_meta(); |
129 | 0 | } |
130 | | |
131 | 0 | Status CloudDeltaWriter::commit_rowset() { |
132 | 0 | g_cloud_commit_rowset_count << 1; |
133 | 0 | std::lock_guard<bthread::Mutex> lock(_mtx); |
134 | | |
135 | | // Handle empty rowset (no data written) |
136 | 0 | if (!_is_init) { |
137 | 0 | g_cloud_commit_empty_rowset_count << 1; |
138 | 0 | return _commit_empty_rowset(); |
139 | 0 | } |
140 | | |
141 | | // Handle normal rowset with data |
142 | 0 | return _engine.meta_mgr().commit_rowset(*rowset_meta(), "", |
143 | 0 | _rowset_builder->tablet()->table_id()); |
144 | 0 | } |
145 | | |
146 | 0 | Status CloudDeltaWriter::_commit_empty_rowset() { |
147 | | // If skip writing empty rowset metadata is enabled, |
148 | | // we do not prepare rowset to meta service. |
149 | 0 | if (config::skip_writing_empty_rowset_metadata) { |
150 | 0 | rowset_builder()->set_skip_writing_rowset_metadata(true); |
151 | 0 | } |
152 | |
|
153 | 0 | RETURN_IF_ERROR(_rowset_builder->init()); |
154 | 0 | RETURN_IF_ERROR(_rowset_builder->build_rowset()); |
155 | | |
156 | | // If skip writing empty rowset metadata is enabled, we do not commit rowset to meta service. |
157 | 0 | if (config::skip_writing_empty_rowset_metadata) { |
158 | 0 | return Status::OK(); |
159 | 0 | } |
160 | | // write a empty rowset kv to keep version continuous |
161 | 0 | return _engine.meta_mgr().commit_rowset(*rowset_meta(), "", |
162 | 0 | _rowset_builder->tablet()->table_id()); |
163 | 0 | } |
164 | | |
165 | 0 | Status CloudDeltaWriter::set_txn_related_info() { |
166 | 0 | return rowset_builder()->set_txn_related_info(); |
167 | 0 | } |
168 | | |
169 | | } // namespace doris |