be/src/format/jni/jni_reader.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 "jni_reader.h" |
19 | | |
20 | | #include <glog/logging.h> |
21 | | |
22 | | #include <map> |
23 | | #include <ostream> |
24 | | #include <sstream> |
25 | | #include <tuple> |
26 | | #include <unordered_map> |
27 | | #include <utility> |
28 | | |
29 | | #include "core/block/block.h" |
30 | | #include "core/types.h" |
31 | | #include "format/jni/jni_data_bridge.h" |
32 | | #include "format/table/partition_column_filler.h" |
33 | | #include "runtime/descriptors.h" |
34 | | #include "runtime/runtime_state.h" |
35 | | #include "util/jni-util.h" |
36 | | |
37 | | namespace doris { |
38 | | class RuntimeProfile; |
39 | | class RuntimeState; |
40 | | |
41 | | class Block; |
42 | | } // namespace doris |
43 | | |
44 | | namespace doris { |
45 | | |
46 | | const std::vector<SlotDescriptor*> JniReader::_s_empty_slot_descs; |
47 | | |
48 | | // ========================================================================= |
49 | | // JniReader constructors |
50 | | // ========================================================================= |
51 | | |
52 | | JniReader::JniReader(const std::vector<SlotDescriptor*>& file_slot_descs, RuntimeState* state, |
53 | | RuntimeProfile* profile, std::string connector_class, |
54 | | std::map<std::string, std::string> scanner_params, |
55 | | std::vector<std::string> column_names, int64_t self_split_weight) |
56 | 18 | : _file_slot_descs(file_slot_descs), |
57 | 18 | _state(state), |
58 | 18 | _profile(profile), |
59 | 18 | _connector_class(std::move(connector_class)), |
60 | 18 | _scanner_params(std::move(scanner_params)), |
61 | 18 | _column_names(std::move(column_names)), |
62 | 18 | _self_split_weight(static_cast<int32_t>(self_split_weight)) { |
63 | 18 | _connector_name = split(_connector_class, "/").back(); |
64 | 18 | } |
65 | | |
66 | | JniReader::JniReader(std::string connector_class, std::map<std::string, std::string> scanner_params) |
67 | 295 | : _file_slot_descs(_s_empty_slot_descs), |
68 | 295 | _connector_class(std::move(connector_class)), |
69 | 295 | _scanner_params(std::move(scanner_params)) { |
70 | 295 | _is_table_schema = true; |
71 | 295 | _connector_name = split(_connector_class, "/").back(); |
72 | 295 | } |
73 | | |
74 | 16 | Status JniReader::on_before_init_reader(ReaderInitContext* ctx) { |
75 | 16 | _column_descs = ctx->column_descs; |
76 | 16 | if (_col_name_to_block_idx == nullptr) { |
77 | 16 | _col_name_to_block_idx = ctx->col_name_to_block_idx; |
78 | 16 | } |
79 | 16 | _partition_values.clear(); |
80 | 16 | _partition_value_is_null.clear(); |
81 | 16 | if (ctx->range == nullptr || ctx->tuple_descriptor == nullptr || |
82 | 16 | !ctx->range->__isset.columns_from_path_keys) { |
83 | 16 | return Status::OK(); |
84 | 16 | } |
85 | | |
86 | 0 | DORIS_CHECK(ctx->range->__isset.columns_from_path); |
87 | 0 | DORIS_CHECK(ctx->range->columns_from_path.size() == ctx->range->columns_from_path_keys.size()); |
88 | 0 | const bool has_null_flags = ctx->range->__isset.columns_from_path_is_null; |
89 | 0 | if (has_null_flags) { |
90 | 0 | DORIS_CHECK(ctx->range->columns_from_path_is_null.size() == |
91 | 0 | ctx->range->columns_from_path_keys.size()); |
92 | 0 | } |
93 | |
|
94 | 0 | std::unordered_map<std::string, const SlotDescriptor*> name_to_slot; |
95 | 0 | for (auto* slot : ctx->tuple_descriptor->slots()) { |
96 | 0 | name_to_slot.emplace(slot->col_name(), slot); |
97 | 0 | } |
98 | 0 | for (size_t i = 0; i < ctx->range->columns_from_path_keys.size(); ++i) { |
99 | 0 | const auto& key = ctx->range->columns_from_path_keys[i]; |
100 | 0 | auto slot_it = name_to_slot.find(key); |
101 | 0 | if (slot_it == name_to_slot.end()) { |
102 | 0 | continue; |
103 | 0 | } |
104 | 0 | _partition_values.emplace( |
105 | 0 | key, std::make_tuple(ctx->range->columns_from_path[i], slot_it->second)); |
106 | 0 | _partition_value_is_null.emplace( |
107 | 0 | key, has_null_flags ? ctx->range->columns_from_path_is_null[i] : false); |
108 | 0 | } |
109 | 0 | return Status::OK(); |
110 | 16 | } |
111 | | |
112 | 32 | Status JniReader::on_after_read_block(Block* block, size_t* read_rows) { |
113 | 32 | if (_column_descs == nullptr || _partition_values.empty() || *read_rows == 0 || |
114 | 32 | _push_down_agg_type == TPushAggOp::type::COUNT) { |
115 | 32 | return Status::OK(); |
116 | 32 | } |
117 | 0 | return _fill_partition_columns(block, *read_rows); |
118 | 32 | } |
119 | | |
120 | | // ========================================================================= |
121 | | // JniReader::open (merged from JniConnector::open) |
122 | | // ========================================================================= |
123 | | |
124 | 311 | Status JniReader::open(RuntimeState* state, RuntimeProfile* profile) { |
125 | 311 | _state = state; |
126 | 311 | _profile = profile; |
127 | 311 | if (_profile) { |
128 | 16 | ADD_TIMER(_profile, _connector_name.c_str()); |
129 | 16 | _open_scanner_time = ADD_CHILD_TIMER(_profile, "OpenScannerTime", _connector_name.c_str()); |
130 | 16 | _java_scan_time = ADD_CHILD_TIMER(_profile, "JavaScanTime", _connector_name.c_str()); |
131 | 16 | _java_append_data_time = |
132 | 16 | ADD_CHILD_TIMER(_profile, "JavaAppendDataTime", _connector_name.c_str()); |
133 | 16 | _java_create_vector_table_time = |
134 | 16 | ADD_CHILD_TIMER(_profile, "JavaCreateVectorTableTime", _connector_name.c_str()); |
135 | 16 | _fill_block_time = ADD_CHILD_TIMER(_profile, "FillBlockTime", _connector_name.c_str()); |
136 | 16 | _max_time_split_weight_counter = _profile->add_conditition_counter( |
137 | 16 | "MaxTimeSplitWeight", TUnit::UNIT, [](int64_t _c, int64_t c) { return c > _c; }, |
138 | 16 | _connector_name.c_str()); |
139 | 16 | } |
140 | 311 | _java_scan_watcher = 0; |
141 | | |
142 | 311 | JNIEnv* env = nullptr; |
143 | 311 | int batch_size = 0; |
144 | 311 | if (!_is_table_schema && _state) { |
145 | 16 | batch_size = _state->batch_size(); |
146 | 16 | } |
147 | 311 | _batch_size = batch_size; |
148 | 311 | RETURN_IF_ERROR(Jni::Env::Get(&env)); |
149 | 311 | SCOPED_RAW_TIMER(&_jni_scanner_open_watcher); |
150 | 311 | if (_state) { |
151 | 16 | _scanner_params.emplace("time_zone", _state->timezone()); |
152 | 16 | } |
153 | 311 | RETURN_IF_ERROR(_init_jni_scanner(env, batch_size)); |
154 | | // Call org.apache.doris.common.jni.JniScanner#open |
155 | 311 | RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _jni_scanner_open).call()); |
156 | | |
157 | 311 | RETURN_ERROR_IF_EXC(env); |
158 | 311 | _scanner_opened = true; |
159 | 311 | return Status::OK(); |
160 | 311 | } |
161 | | |
162 | | // ========================================================================= |
163 | | // JniReader::_do_get_next_block (merged from JniConnector::get_next_block) |
164 | | // ========================================================================= |
165 | | |
166 | 32 | Status JniReader::_do_get_next_block(Block* block, size_t* read_rows, bool* eof) { |
167 | 32 | JNIEnv* env = nullptr; |
168 | 32 | RETURN_IF_ERROR(Jni::Env::Get(&env)); |
169 | 32 | long meta_address = 0; |
170 | 32 | { |
171 | 32 | SCOPED_RAW_TIMER(&_java_scan_watcher); |
172 | 32 | RETURN_IF_ERROR(_jni_scanner_obj.call_long_method(env, _jni_scanner_get_next_batch) |
173 | 32 | .call(&meta_address)); |
174 | 32 | } |
175 | 32 | if (meta_address == 0) { |
176 | 16 | *read_rows = 0; |
177 | 16 | *eof = true; |
178 | 16 | return Status::OK(); |
179 | 16 | } |
180 | 16 | _set_meta(meta_address); |
181 | 16 | long num_rows = _table_meta.next_meta_as_long(); |
182 | 16 | if (num_rows == 0) { |
183 | 0 | *read_rows = 0; |
184 | 0 | *eof = true; |
185 | 0 | return Status::OK(); |
186 | 0 | } |
187 | 16 | RETURN_IF_ERROR(_fill_block(block, num_rows)); |
188 | 16 | *read_rows = num_rows; |
189 | 16 | *eof = false; |
190 | 16 | RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _jni_scanner_release_table).call()); |
191 | 16 | _has_read += num_rows; |
192 | 16 | return Status::OK(); |
193 | 16 | } |
194 | | |
195 | | // ========================================================================= |
196 | | // JniReader::get_table_schema (merged from JniConnector::get_table_schema) |
197 | | // ========================================================================= |
198 | | |
199 | 0 | Status JniReader::get_table_schema(std::string& table_schema_str) { |
200 | 0 | JNIEnv* env = nullptr; |
201 | 0 | RETURN_IF_ERROR(Jni::Env::Get(&env)); |
202 | | |
203 | 0 | Jni::LocalString jstr; |
204 | 0 | RETURN_IF_ERROR( |
205 | 0 | _jni_scanner_obj.call_object_method(env, _jni_scanner_get_table_schema).call(&jstr)); |
206 | 0 | Jni::LocalStringBufferGuard cstr; |
207 | 0 | RETURN_IF_ERROR(jstr.get_string_chars(env, &cstr)); |
208 | 0 | table_schema_str = std::string {cstr.get()}; |
209 | 0 | return Status::OK(); |
210 | 0 | } |
211 | | |
212 | | // ========================================================================= |
213 | | // JniReader::close (merged from JniConnector::close) |
214 | | // ========================================================================= |
215 | | |
216 | 311 | Status JniReader::close() { |
217 | 311 | if (_closed) { |
218 | 0 | return Status::OK(); |
219 | 0 | } |
220 | 311 | if (!_scanner_opened) { |
221 | 0 | _closed = true; |
222 | 0 | return Status::OK(); |
223 | 0 | } |
224 | | |
225 | 311 | JNIEnv* env = nullptr; |
226 | 311 | RETURN_IF_ERROR(Jni::Env::Get(&env)); |
227 | | |
228 | | // _fill_block may fail before releasing the current Java table. JniScanner::releaseTable() |
229 | | // is idempotent, so close always retries it. Java close must still run when that release |
230 | | // fails, otherwise connector resources such as Paimon's static table-cache lease can leak. |
231 | 311 | auto close_status = _jni_scanner_obj.call_void_method(env, _jni_scanner_release_table).call(); |
232 | 311 | auto java_close_status = _jni_scanner_obj.call_void_method(env, _jni_scanner_close).call(); |
233 | 311 | if (close_status.ok() && !java_close_status.ok()) { |
234 | 0 | close_status = std::move(java_close_status); |
235 | 0 | } |
236 | 311 | if (close_status.ok()) { |
237 | 311 | _scanner_opened = false; |
238 | 311 | _closed = true; |
239 | 311 | } |
240 | 311 | return close_status; |
241 | 311 | } |
242 | | |
243 | | // ========================================================================= |
244 | | // JniReader::set_batch_size |
245 | | // ========================================================================= |
246 | | |
247 | 32 | void JniReader::set_batch_size(size_t batch_size) { |
248 | 32 | DCHECK_GT(batch_size, 0); |
249 | 32 | if (_batch_size == batch_size) { |
250 | 0 | return; |
251 | 0 | } |
252 | 32 | _batch_size = batch_size; |
253 | 32 | if (_scanner_opened) { |
254 | 32 | JNIEnv* env = nullptr; |
255 | 32 | Status st = Jni::Env::Get(&env); |
256 | 32 | if (!st) { |
257 | 0 | LOG(WARNING) << "failed to get jni env when set_batch_size: " << st; |
258 | 0 | return; |
259 | 0 | } |
260 | 32 | st = _jni_scanner_obj.call_void_method(env, _jni_scanner_set_batch_size) |
261 | 32 | .with_arg(static_cast<int>(_batch_size)) |
262 | 32 | .call(); |
263 | 32 | if (!st) { |
264 | 0 | LOG(WARNING) << "failed to call setBatchSize: " << st; |
265 | 0 | } |
266 | 32 | } |
267 | 32 | } |
268 | | |
269 | | // ========================================================================= |
270 | | // JniReader::_init_jni_scanner (merged from JniConnector::_init_jni_scanner) |
271 | | // ========================================================================= |
272 | | |
273 | 311 | Status JniReader::_init_jni_scanner(JNIEnv* env, int batch_size) { |
274 | 311 | RETURN_IF_ERROR( |
275 | 311 | Jni::Util::get_jni_scanner_class(env, _connector_class.c_str(), &_jni_scanner_cls)); |
276 | | |
277 | 311 | Jni::MethodId scanner_constructor; |
278 | 311 | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "<init>", "(ILjava/util/Map;)V", |
279 | 311 | &scanner_constructor)); |
280 | | |
281 | | // prepare constructor parameters |
282 | 311 | Jni::LocalObject hashmap_object; |
283 | 311 | RETURN_IF_ERROR(Jni::Util::convert_to_java_map(env, _scanner_params, &hashmap_object)); |
284 | 311 | RETURN_IF_ERROR(_jni_scanner_cls.new_object(env, scanner_constructor) |
285 | 311 | .with_arg(batch_size) |
286 | 311 | .with_arg(hashmap_object) |
287 | 311 | .call(&_jni_scanner_obj)); |
288 | | |
289 | 311 | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "open", "()V", &_jni_scanner_open)); |
290 | 311 | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getNextBatchMeta", "()J", |
291 | 311 | &_jni_scanner_get_next_batch)); |
292 | 311 | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getAppendDataTime", "()J", |
293 | 311 | &_jni_scanner_get_append_data_time)); |
294 | 311 | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getCreateVectorTableTime", "()J", |
295 | 311 | &_jni_scanner_get_create_vector_table_time)); |
296 | 311 | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getTableSchema", "()Ljava/lang/String;", |
297 | 311 | &_jni_scanner_get_table_schema)); |
298 | 311 | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "close", "()V", &_jni_scanner_close)); |
299 | 311 | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "releaseColumn", "(I)V", |
300 | 311 | &_jni_scanner_release_column)); |
301 | 311 | RETURN_IF_ERROR( |
302 | 311 | _jni_scanner_cls.get_method(env, "releaseTable", "()V", &_jni_scanner_release_table)); |
303 | 311 | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getStatistics", "()Ljava/util/Map;", |
304 | 311 | &_jni_scanner_get_statistics)); |
305 | 311 | RETURN_IF_ERROR( |
306 | 311 | _jni_scanner_cls.get_method(env, "setBatchSize", "(I)V", &_jni_scanner_set_batch_size)); |
307 | 311 | return Status::OK(); |
308 | 311 | } |
309 | | |
310 | | // ========================================================================= |
311 | | // JniReader::_fill_block (merged from JniConnector::_fill_block) |
312 | | // ========================================================================= |
313 | | |
314 | 16 | Status JniReader::_fill_block(Block* block, size_t num_rows) { |
315 | 16 | SCOPED_RAW_TIMER(&_fill_block_watcher); |
316 | 16 | JNIEnv* env = nullptr; |
317 | 16 | RETURN_IF_ERROR(Jni::Env::Get(&env)); |
318 | | // Fallback: if _col_name_to_block_idx was not set by the caller (e.g. JdbcScanner), |
319 | | // build the name-to-position map from the block itself. |
320 | 16 | std::unordered_map<std::string, uint32_t> local_name_to_idx; |
321 | 16 | const std::unordered_map<std::string, uint32_t>* col_map = _col_name_to_block_idx; |
322 | 16 | if (col_map == nullptr) { |
323 | 0 | local_name_to_idx = block->get_name_to_pos_map(); |
324 | 0 | col_map = &local_name_to_idx; |
325 | 0 | } |
326 | 62 | for (int i = 0; i < _column_names.size(); ++i) { |
327 | 46 | auto& column_with_type_and_name = block->get_by_position(col_map->at(_column_names[i])); |
328 | 46 | auto& column_ptr = column_with_type_and_name.column; |
329 | 46 | auto& column_type = column_with_type_and_name.type; |
330 | 46 | RETURN_IF_ERROR(JniDataBridge::fill_column(_table_meta, column_ptr, column_type, num_rows)); |
331 | | // Column is not released when fill_column failed. It will be released when releasing table. |
332 | 46 | RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _jni_scanner_release_column) |
333 | 46 | .with_arg(i) |
334 | 46 | .call()); |
335 | 46 | RETURN_ERROR_IF_EXC(env); |
336 | 46 | } |
337 | 16 | return Status::OK(); |
338 | 16 | } |
339 | | |
340 | 0 | Status JniReader::_fill_partition_columns(Block* block, size_t num_rows) { |
341 | 0 | std::unordered_map<std::string, uint32_t> local_name_to_idx; |
342 | 0 | const std::unordered_map<std::string, uint32_t>* col_map = _col_name_to_block_idx; |
343 | 0 | if (col_map == nullptr) { |
344 | 0 | local_name_to_idx = block->get_name_to_pos_map(); |
345 | 0 | col_map = &local_name_to_idx; |
346 | 0 | } |
347 | |
|
348 | 0 | for (const auto& desc : *_column_descs) { |
349 | 0 | if (desc.category != ColumnCategory::PARTITION_KEY) { |
350 | 0 | continue; |
351 | 0 | } |
352 | 0 | auto value_it = _partition_values.find(desc.name); |
353 | 0 | if (value_it == _partition_values.end()) { |
354 | 0 | continue; |
355 | 0 | } |
356 | 0 | auto col_it = col_map->find(desc.name); |
357 | 0 | if (col_it == col_map->end()) { |
358 | 0 | return Status::InternalError("Missing partition column {} in block {}", desc.name, |
359 | 0 | block->dump_structure()); |
360 | 0 | } |
361 | | |
362 | 0 | auto& column_with_type_and_name = block->get_by_position(col_it->second); |
363 | 0 | auto mutable_column = std::move(*column_with_type_and_name.column).mutate(); |
364 | 0 | const auto& [value, slot_desc] = value_it->second; |
365 | 0 | auto null_it = _partition_value_is_null.find(desc.name); |
366 | 0 | DORIS_CHECK(null_it != _partition_value_is_null.end()); |
367 | 0 | RETURN_IF_ERROR(fill_partition_column_from_path_value(*mutable_column, *slot_desc, value, |
368 | 0 | num_rows, null_it->second)); |
369 | 0 | column_with_type_and_name.column = std::move(mutable_column); |
370 | 0 | } |
371 | 0 | return Status::OK(); |
372 | 0 | } |
373 | | |
374 | | // ========================================================================= |
375 | | // JniReader::_get_statistics (merged from JniConnector::get_statistics) |
376 | | // ========================================================================= |
377 | | |
378 | 16 | Status JniReader::_get_statistics(JNIEnv* env, std::map<std::string, std::string>* result) { |
379 | 16 | result->clear(); |
380 | 16 | Jni::LocalObject metrics; |
381 | 16 | RETURN_IF_ERROR( |
382 | 16 | _jni_scanner_obj.call_object_method(env, _jni_scanner_get_statistics).call(&metrics)); |
383 | | |
384 | 16 | RETURN_IF_ERROR(Jni::Util::convert_to_cpp_map(env, metrics, result)); |
385 | 16 | return Status::OK(); |
386 | 16 | } |
387 | | |
388 | | // ========================================================================= |
389 | | // JniReader::_collect_profile_before_close |
390 | | // (merged from JniConnector::_collect_profile_before_close) |
391 | | // ========================================================================= |
392 | | |
393 | 16 | void JniReader::_collect_profile_before_close() { |
394 | 16 | if (_scanner_opened && _profile != nullptr) { |
395 | 16 | JNIEnv* env = nullptr; |
396 | 16 | Status st = Jni::Env::Get(&env); |
397 | 16 | if (!st) { |
398 | 0 | LOG(WARNING) << "failed to get jni env when collect profile: " << st; |
399 | 0 | return; |
400 | 0 | } |
401 | 16 | COUNTER_UPDATE(_open_scanner_time, _jni_scanner_open_watcher); |
402 | 16 | COUNTER_UPDATE(_fill_block_time, _fill_block_watcher); |
403 | | |
404 | 16 | jlong append_data_time = 0; |
405 | 16 | auto append_time_status = |
406 | 16 | _jni_scanner_obj.call_long_method(env, _jni_scanner_get_append_data_time) |
407 | 16 | .call(&append_data_time); |
408 | 16 | jlong create_vector_table_time = 0; |
409 | 16 | auto create_table_time_status = |
410 | 16 | _jni_scanner_obj.call_long_method(env, _jni_scanner_get_create_vector_table_time) |
411 | 16 | .call(&create_vector_table_time); |
412 | 16 | if (!append_time_status.ok()) { |
413 | 0 | LOG(WARNING) << "failed to collect JNI append-data time before close: " |
414 | 0 | << append_time_status; |
415 | 0 | } |
416 | 16 | if (!create_table_time_status.ok()) { |
417 | 0 | LOG(WARNING) << "failed to collect JNI vector-table time before close: " |
418 | 0 | << create_table_time_status; |
419 | 0 | } |
420 | 16 | if (append_time_status.ok() && create_table_time_status.ok()) { |
421 | 16 | COUNTER_UPDATE(_java_append_data_time, append_data_time); |
422 | 16 | COUNTER_UPDATE(_java_create_vector_table_time, create_vector_table_time); |
423 | 16 | COUNTER_UPDATE(_java_scan_time, |
424 | 16 | _java_scan_watcher - append_data_time - create_vector_table_time); |
425 | 16 | _max_time_split_weight_counter->conditional_update( |
426 | 16 | _jni_scanner_open_watcher + _fill_block_watcher + _java_scan_watcher, |
427 | 16 | _self_split_weight); |
428 | 16 | } |
429 | | |
430 | | // update scanner metrics |
431 | 16 | std::map<std::string, std::string> statistics_result; |
432 | 16 | st = _get_statistics(env, &statistics_result); |
433 | 16 | if (!st) { |
434 | 0 | LOG(WARNING) << "failed to get_statistics when collect profile: " << st; |
435 | 0 | return; |
436 | 0 | } |
437 | | |
438 | 16 | const auto update_peak = [](int64_t previous, int64_t current) { |
439 | 0 | return current > previous; |
440 | 0 | }; |
441 | 38 | for (const auto& metric : statistics_result) { |
442 | 38 | std::vector<std::string> type_and_name = split(metric.first, ":"); |
443 | 38 | if (type_and_name.size() != 2) { |
444 | 0 | LOG(WARNING) << "Name of JNI Scanner metric should be pattern like " |
445 | 0 | << "'metricType:metricName'"; |
446 | 0 | continue; |
447 | 0 | } |
448 | 38 | int64_t metric_value = std::stoll(metric.second); |
449 | 38 | RuntimeProfile::Counter* scanner_counter; |
450 | 38 | if (type_and_name[0] == "timer") { |
451 | 4 | scanner_counter = |
452 | 4 | ADD_CHILD_TIMER(_profile, type_and_name[1], _connector_name.c_str()); |
453 | 4 | COUNTER_UPDATE(scanner_counter, metric_value); |
454 | 34 | } else if (type_and_name[0] == "counter") { |
455 | 10 | scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::UNIT, |
456 | 10 | _connector_name.c_str()); |
457 | 10 | COUNTER_UPDATE(scanner_counter, metric_value); |
458 | 24 | } else if (type_and_name[0] == "bytes") { |
459 | 0 | scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::BYTES, |
460 | 0 | _connector_name.c_str()); |
461 | 0 | COUNTER_UPDATE(scanner_counter, metric_value); |
462 | 24 | } else if (type_and_name[0] == "timer_gauge") { |
463 | 0 | scanner_counter = |
464 | 0 | ADD_CHILD_TIMER(_profile, type_and_name[1], _connector_name.c_str()); |
465 | 0 | COUNTER_SET(scanner_counter, metric_value); |
466 | 24 | } else if (type_and_name[0] == "gauge") { |
467 | 10 | scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::UNIT, |
468 | 10 | _connector_name.c_str()); |
469 | 10 | COUNTER_SET(scanner_counter, metric_value); |
470 | 14 | } else if (type_and_name[0] == "bytes_gauge") { |
471 | 14 | scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::BYTES, |
472 | 14 | _connector_name.c_str()); |
473 | 14 | COUNTER_SET(scanner_counter, metric_value); |
474 | 14 | } else if (type_and_name[0] == "timer_peak") { |
475 | 0 | auto* scanner_peak_counter = _profile->add_conditition_counter( |
476 | 0 | type_and_name[1], TUnit::TIME_NS, update_peak, _connector_name.c_str()); |
477 | 0 | scanner_peak_counter->conditional_update(metric_value, metric_value); |
478 | 0 | } else if (type_and_name[0] == "peak") { |
479 | 0 | auto* scanner_peak_counter = _profile->add_conditition_counter( |
480 | 0 | type_and_name[1], TUnit::UNIT, update_peak, _connector_name.c_str()); |
481 | 0 | scanner_peak_counter->conditional_update(metric_value, metric_value); |
482 | 0 | } else if (type_and_name[0] == "bytes_peak") { |
483 | 0 | auto* scanner_peak_counter = _profile->add_conditition_counter( |
484 | 0 | type_and_name[1], TUnit::BYTES, update_peak, _connector_name.c_str()); |
485 | 0 | scanner_peak_counter->conditional_update(metric_value, metric_value); |
486 | 0 | } else { |
487 | 0 | LOG(WARNING) << "Type of JNI Scanner metric should be timer, counter, bytes, " |
488 | 0 | << "timer_gauge, gauge, bytes_gauge, timer_peak, peak or bytes_peak"; |
489 | 0 | continue; |
490 | 0 | } |
491 | 38 | } |
492 | 16 | } |
493 | 16 | } |
494 | | |
495 | | // ========================================================================= |
496 | | // MockJniReader |
497 | | // ========================================================================= |
498 | | |
499 | | MockJniReader::MockJniReader(const std::vector<SlotDescriptor*>& file_slot_descs, |
500 | | RuntimeState* state, RuntimeProfile* profile) |
501 | 0 | : JniReader( |
502 | 0 | file_slot_descs, state, profile, "org/apache/doris/common/jni/MockJniScanner", |
503 | 0 | [&]() { |
504 | 0 | std::ostringstream required_fields; |
505 | 0 | std::ostringstream columns_types; |
506 | 0 | int index = 0; |
507 | 0 | for (const auto& desc : file_slot_descs) { |
508 | 0 | std::string field = desc->col_name(); |
509 | 0 | std::string type = |
510 | 0 | JniDataBridge::get_jni_type_with_different_string(desc->type()); |
511 | 0 | if (index == 0) { |
512 | 0 | required_fields << field; |
513 | 0 | columns_types << type; |
514 | 0 | } else { |
515 | 0 | required_fields << "," << field; |
516 | 0 | columns_types << "#" << type; |
517 | 0 | } |
518 | 0 | index++; |
519 | 0 | } |
520 | 0 | return std::map<String, String> {{"mock_rows", "10240"}, |
521 | 0 | {"required_fields", required_fields.str()}, |
522 | 0 | {"columns_types", columns_types.str()}}; |
523 | 0 | }(), |
524 | 0 | [&]() { |
525 | 0 | std::vector<std::string> names; |
526 | 0 | for (const auto& desc : file_slot_descs) { |
527 | 0 | names.emplace_back(desc->col_name()); |
528 | 0 | } |
529 | 0 | return names; |
530 | 0 | }()) {} |
531 | | |
532 | 0 | Status MockJniReader::init_reader() { |
533 | 0 | return open(_state, _profile); |
534 | 0 | } |
535 | | |
536 | | } // namespace doris |