be/src/storage/rowset/rowset_writer_context.h
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 | | #pragma once |
19 | | |
20 | | #include <gen_cpp/olap_file.pb.h> |
21 | | #include <glog/logging.h> |
22 | | |
23 | | #include <functional> |
24 | | #include <optional> |
25 | | #include <string_view> |
26 | | #include <unordered_map> |
27 | | |
28 | | #include "cloud/config.h" |
29 | | #include "common/status.h" |
30 | | #include "io/fs/encrypted_fs_factory.h" |
31 | | #include "io/fs/file_system.h" |
32 | | #include "io/fs/file_writer.h" |
33 | | #include "io/fs/local_file_system.h" |
34 | | #include "io/fs/packed_file_system.h" |
35 | | #include "runtime/exec_env.h" |
36 | | #include "storage/binlog.h" |
37 | | #include "storage/olap_define.h" |
38 | | #include "storage/partial_update_info.h" |
39 | | #include "storage/segment/historical_row_retriever.h" |
40 | | #include "storage/storage_policy.h" |
41 | | #include "storage/tablet/tablet.h" |
42 | | #include "storage/tablet/tablet_schema.h" |
43 | | |
44 | | namespace doris { |
45 | | |
46 | | class RowsetWriterContextBuilder; |
47 | | using RowsetWriterContextBuilderSharedPtr = std::shared_ptr<RowsetWriterContextBuilder>; |
48 | | class DataDir; |
49 | | class Tablet; |
50 | | class FileWriterCreator; |
51 | | class SegmentCollector; |
52 | | |
53 | | namespace segment_v2 { |
54 | | struct HistoricalRowRetrieverContext; |
55 | | } |
56 | | |
57 | | struct RowsetWriterContext { |
58 | 2.46k | RowsetWriterContext() : schema_lock(new std::mutex) { |
59 | 2.46k | load_id.set_hi(0); |
60 | 2.46k | load_id.set_lo(0); |
61 | 2.46k | } |
62 | | |
63 | | RowsetId rowset_id; |
64 | | int64_t db_id {0}; |
65 | | int64_t table_id {0}; |
66 | | int64_t tablet_id {0}; |
67 | | int32_t tablet_schema_hash {0}; |
68 | | int64_t index_id {0}; |
69 | | int64_t partition_id {0}; |
70 | | RowsetTypePB rowset_type {BETA_ROWSET}; |
71 | | |
72 | | TabletSchemaSPtr tablet_schema; |
73 | | |
74 | | // PREPARED/COMMITTED for pending rowset |
75 | | // VISIBLE for non-pending rowset |
76 | | RowsetStatePB rowset_state {PREPARED}; |
77 | | // properties for non-pending rowset |
78 | | Version version {0, 0}; |
79 | | |
80 | | // properties for pending rowset |
81 | | int64_t txn_id {0}; |
82 | | int64_t txn_expiration {0}; // For cloud mode |
83 | | PUniqueId load_id; |
84 | | TabletUid tablet_uid {0, 0}; |
85 | | // indicate whether the data among segments is overlapping. |
86 | | // default is OVERLAP_UNKNOWN. |
87 | | SegmentsOverlapPB segments_overlap {OVERLAP_UNKNOWN}; |
88 | | // segment file use uint32 to represent row number, therefore the maximum is UINT32_MAX. |
89 | | // the default is set to INT32_MAX to avoid overflow issue when casting from uint32_t to int. |
90 | | // test cases can change this value to control flush timing |
91 | | uint32_t max_rows_per_segment = INT32_MAX; |
92 | | // not owned, point to the data dir of this rowset |
93 | | // for checking disk capacity when write data to disk. |
94 | | // ATTN: not support for RowsetConvertor. |
95 | | // (because it hard to refactor, and RowsetConvertor will be deprecated in future) |
96 | | DataDir* data_dir = nullptr; |
97 | | |
98 | | int64_t newest_write_timestamp = -1; |
99 | | bool enable_unique_key_merge_on_write = false; |
100 | | // store column_unique_id to do index compaction |
101 | | std::set<int32_t> columns_to_do_index_compaction; |
102 | | DataWriteType write_type = DataWriteType::TYPE_DEFAULT; |
103 | | // need to figure out the sub type of compaction |
104 | | ReaderType compaction_type = ReaderType::UNKNOWN; |
105 | | BaseTabletSPtr tablet = nullptr; |
106 | | |
107 | | std::shared_ptr<MowContext> mow_context; |
108 | | std::shared_ptr<FileWriterCreator> file_writer_creator; |
109 | | std::shared_ptr<SegmentCollector> segment_collector; |
110 | | |
111 | | // memtable_on_sink_support_index_v2 = true, we will create SinkFileWriter to send inverted index file |
112 | | bool memtable_on_sink_support_index_v2 = false; |
113 | | |
114 | | /// begin file cache opts |
115 | | bool write_file_cache = false; |
116 | | bool is_hot_data = false; |
117 | | uint64_t file_cache_ttl_sec = 0; |
118 | | uint64_t approximate_bytes_to_write = 0; |
119 | | // If true, compaction output only writes index files to file cache, not data files |
120 | | bool compaction_output_write_index_only = false; |
121 | | /// end file cache opts |
122 | | |
123 | | // segcompaction for this RowsetWriter, only enabled when importing data |
124 | | bool enable_segcompaction = false; |
125 | | |
126 | | std::shared_ptr<PartialUpdateInfo> partial_update_info; |
127 | | |
128 | | bool is_transient_rowset_writer = false; |
129 | | |
130 | | segment_v2::HistoricalRowRetrieverContext make_historical_row_retriever_context(); |
131 | | |
132 | | // Intent flag: caller can actively turn merge-file feature on/off for this rowset. |
133 | | // This describes whether we *want* to try small-file merging. |
134 | | bool allow_packed_file = true; |
135 | | |
136 | | // Effective flag: whether this context actually ends up using MergeFileSystem for writes. |
137 | | // This is decided inside fs() based on enable_merge_file plus other conditions |
138 | | // (cloud mode, S3 filesystem, V1 inverted index, global config, etc.), and once |
139 | | // set to true it remains stable even if config::enable_merge_file changes later. |
140 | | mutable bool packed_file_active = false; |
141 | | |
142 | | // Cached FileSystem instance to ensure consistency across multiple fs() calls. |
143 | | // This prevents creating multiple MergeFileSystem instances and ensures |
144 | | // packed_file_active flag remains consistent. |
145 | | mutable io::FileSystemSPtr _cached_fs = nullptr; |
146 | | |
147 | | // For collect segment statistics for compaction |
148 | | std::vector<RowsetReaderSharedPtr> input_rs_readers; |
149 | | |
150 | | // TODO(lihangyu) remove this lock |
151 | | // In semi-structure senario tablet_schema will be updated concurrently, |
152 | | // this lock need to be held when update.Use shared_ptr to avoid delete copy contructor |
153 | | std::shared_ptr<std::mutex> schema_lock; |
154 | | |
155 | | int64_t compaction_level = 0; |
156 | | |
157 | | // For local rowset |
158 | | std::string tablet_path; |
159 | | |
160 | | // For remote rowset |
161 | | std::optional<StorageResource> storage_resource; |
162 | | |
163 | | std::optional<EncryptionAlgorithmPB> encrypt_algorithm; |
164 | | |
165 | | std::string job_id; |
166 | | |
167 | 7.52k | bool is_local_rowset() const { return !storage_resource; } |
168 | | |
169 | 6.24k | std::string segment_path(int seg_id) const { |
170 | 6.24k | if (is_local_rowset()) { |
171 | 6.21k | return local_segment_path(tablet_path, rowset_id.to_string(), seg_id); |
172 | 6.21k | } else { |
173 | 28 | return storage_resource->remote_segment_path(tablet_id, rowset_id.to_string(), seg_id); |
174 | 28 | } |
175 | 6.24k | } |
176 | | |
177 | 3.86k | io::FileSystemSPtr fs() const { |
178 | | // Return cached instance if available to ensure consistency across multiple calls |
179 | 3.86k | if (_cached_fs != nullptr) { |
180 | 3.02k | return _cached_fs; |
181 | 3.02k | } |
182 | | |
183 | 840 | auto fs = [this]() -> io::FileSystemSPtr { |
184 | 840 | if (is_local_rowset()) { |
185 | 837 | return io::global_local_filesystem(); |
186 | 837 | } else { |
187 | 3 | return storage_resource->fs; |
188 | 3 | } |
189 | 840 | }(); |
190 | | |
191 | 840 | bool is_s3_fs = fs->type() == io::FileSystemType::S3; |
192 | | |
193 | 840 | auto algorithm = encrypt_algorithm; |
194 | | |
195 | 840 | if (!algorithm.has_value()) { |
196 | | #ifndef BE_TEST |
197 | | constexpr std::string_view msg = |
198 | | "RowsetWriterContext::determine_encryption is not called when creating this " |
199 | | "RowsetWriterContext, it will result in encrypted rowsets left unencrypted"; |
200 | | auto st = Status::InternalError(msg); |
201 | | |
202 | | LOG(WARNING) << st; |
203 | | DCHECK(false) << st; |
204 | | #else |
205 | 737 | algorithm = EncryptionAlgorithmPB::PLAINTEXT; |
206 | 737 | #endif |
207 | 737 | } |
208 | | |
209 | | // Apply packed file system first for write path if enabled |
210 | | // Create empty index_map for write path |
211 | | // Index information will be populated after write completes |
212 | 840 | bool has_v1_inverted_index = tablet_schema != nullptr && |
213 | 840 | tablet_schema->has_inverted_index() && |
214 | 840 | tablet_schema->get_inverted_index_storage_format() == |
215 | 173 | InvertedIndexStorageFormatPB::V1; |
216 | | |
217 | 840 | if (has_v1_inverted_index && allow_packed_file && config::enable_packed_file) { |
218 | 8 | static constexpr std::string_view kMsg = |
219 | 8 | "Disable packed file for V1 inverted index tablet to avoid missing index " |
220 | 8 | "metadata (temporary workaround)"; |
221 | 8 | LOG(INFO) << kMsg << ", tablet_id=" << tablet_id << ", rowset_id=" << rowset_id; |
222 | 8 | } |
223 | | |
224 | | // Only enable merge file for S3 file system, not for HDFS or other remote file systems |
225 | 840 | packed_file_active = allow_packed_file && config::is_cloud_mode() && |
226 | 840 | config::enable_packed_file && !has_v1_inverted_index && is_s3_fs; |
227 | | |
228 | 840 | if (packed_file_active) { |
229 | 0 | io::PackedAppendContext append_info; |
230 | 0 | append_info.tablet_id = tablet_id; |
231 | 0 | append_info.rowset_id = rowset_id.to_string(); |
232 | 0 | append_info.txn_id = txn_id; |
233 | 0 | append_info.expiration_time = file_cache_ttl_sec > 0 && newest_write_timestamp > 0 |
234 | 0 | ? newest_write_timestamp + file_cache_ttl_sec |
235 | 0 | : 0; |
236 | 0 | fs = std::make_shared<io::PackedFileSystem>(fs, append_info); |
237 | 0 | } |
238 | | |
239 | | // Then apply encryption on top |
240 | 840 | if (algorithm.has_value()) { |
241 | 840 | fs = io::make_file_system(fs, algorithm.value()); |
242 | 840 | } |
243 | | |
244 | | // Cache the result to ensure consistency across multiple calls |
245 | 840 | _cached_fs = fs; |
246 | 840 | return fs; |
247 | 3.86k | } |
248 | | |
249 | 0 | io::FileSystem& fs_ref() const { return *fs(); } |
250 | | |
251 | 3.85k | io::FileWriterOptions get_file_writer_options(FileType file_type = FileType::SEGMENT_FILE) { |
252 | 3.85k | io::FileWriterOptions opts {.write_file_cache = write_file_cache, |
253 | 3.85k | .is_cold_data = is_hot_data, |
254 | 3.85k | .file_cache_expiration_time = file_cache_ttl_sec, |
255 | 3.85k | .approximate_bytes_to_write = approximate_bytes_to_write}; |
256 | | |
257 | 3.85k | if (config::enable_file_cache_write_index_file_only) { |
258 | 20 | opts.allow_adaptive_file_cache_write = false; |
259 | 20 | opts.approximate_bytes_to_write = 0; |
260 | 20 | opts.write_file_cache = file_type == FileType::INVERTED_INDEX_FILE; |
261 | 20 | return opts; |
262 | 20 | } |
263 | | |
264 | 3.83k | if (compaction_output_write_index_only && file_type == FileType::SEGMENT_FILE) { |
265 | 4 | opts.write_file_cache = false; |
266 | 4 | opts.allow_adaptive_file_cache_write = false; |
267 | 4 | opts.approximate_bytes_to_write = 0; |
268 | 4 | } |
269 | | |
270 | 3.83k | return opts; |
271 | 3.85k | } |
272 | | |
273 | | struct BinlogOptions { |
274 | | public: |
275 | | bool enable = false; |
276 | | |
277 | 32 | void set_need_before(bool need_before) { |
278 | 32 | this->_need_before = need_before; |
279 | 32 | _segment_write_binlog_opt.write_before = need_before; |
280 | 32 | } |
281 | | |
282 | 65 | segment_v2::SegmentWriteBinlogOptions& write_binlog_config() { |
283 | 65 | return _segment_write_binlog_opt; |
284 | 65 | } |
285 | | |
286 | 1 | const segment_v2::SegmentWriteBinlogOptions& write_binlog_config() const { |
287 | 1 | return _segment_write_binlog_opt; |
288 | 1 | } |
289 | | |
290 | | private: |
291 | | bool _need_before = false; |
292 | | segment_v2::SegmentWriteBinlogOptions _segment_write_binlog_opt; |
293 | | } _write_binlog_opt; |
294 | | |
295 | 3.83k | BinlogOptions& write_binlog_opt() { return _write_binlog_opt; } |
296 | | |
297 | 1.26k | const BinlogOptions& write_binlog_opt() const { return _write_binlog_opt; } |
298 | | }; |
299 | | |
300 | | inline segment_v2::HistoricalRowRetrieverContext |
301 | 37 | RowsetWriterContext::make_historical_row_retriever_context() { |
302 | 37 | return segment_v2::HistoricalRowRetrieverContext { |
303 | 37 | .tablet = tablet, |
304 | 37 | .tablet_schema = tablet_schema, |
305 | 37 | .rowset_writer_ctx = this, |
306 | 37 | .partial_update_info = partial_update_info, |
307 | 37 | .is_transient_rowset_writer = is_transient_rowset_writer, |
308 | 37 | .write_type = write_type}; |
309 | 37 | } |
310 | | |
311 | | } // namespace doris |