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 | | |
26 | | #include "core/block/block.h" |
27 | | #include "core/types.h" |
28 | | #include "format/jni/jni_data_bridge.h" |
29 | | #include "runtime/descriptors.h" |
30 | | #include "runtime/runtime_state.h" |
31 | | #include "util/jni-util.h" |
32 | | |
33 | | namespace doris { |
34 | | #include "common/compile_check_begin.h" |
35 | | class RuntimeProfile; |
36 | | class RuntimeState; |
37 | | |
38 | | class Block; |
39 | | } // namespace doris |
40 | | |
41 | | namespace doris { |
42 | | |
43 | | const std::vector<SlotDescriptor*> JniReader::_s_empty_slot_descs; |
44 | | |
45 | | // ========================================================================= |
46 | | // JniReader constructors |
47 | | // ========================================================================= |
48 | | |
49 | | JniReader::JniReader(const std::vector<SlotDescriptor*>& file_slot_descs, RuntimeState* state, |
50 | | RuntimeProfile* profile, std::string connector_class, |
51 | | std::map<std::string, std::string> scanner_params, |
52 | | std::vector<std::string> column_names, int64_t self_split_weight) |
53 | 5.82k | : _file_slot_descs(file_slot_descs), |
54 | 5.82k | _state(state), |
55 | 5.82k | _profile(profile), |
56 | 5.82k | _connector_class(std::move(connector_class)), |
57 | 5.82k | _scanner_params(std::move(scanner_params)), |
58 | 5.82k | _column_names(std::move(column_names)), |
59 | 5.82k | _self_split_weight(static_cast<int32_t>(self_split_weight)) { |
60 | 5.82k | _connector_name = split(_connector_class, "/").back(); |
61 | 5.82k | } |
62 | | |
63 | | JniReader::JniReader(std::string connector_class, std::map<std::string, std::string> scanner_params) |
64 | 307 | : _file_slot_descs(_s_empty_slot_descs), |
65 | 307 | _connector_class(std::move(connector_class)), |
66 | 307 | _scanner_params(std::move(scanner_params)) { |
67 | 307 | _is_table_schema = true; |
68 | 307 | _connector_name = split(_connector_class, "/").back(); |
69 | 307 | } |
70 | | |
71 | | // ========================================================================= |
72 | | // JniReader::open (merged from JniConnector::open) |
73 | | // ========================================================================= |
74 | | |
75 | 6.12k | Status JniReader::open(RuntimeState* state, RuntimeProfile* profile) { |
76 | 6.12k | _state = state; |
77 | 6.12k | _profile = profile; |
78 | 6.12k | if (_profile) { |
79 | 5.81k | ADD_TIMER(_profile, _connector_name.c_str()); |
80 | 5.81k | _open_scanner_time = ADD_CHILD_TIMER(_profile, "OpenScannerTime", _connector_name.c_str()); |
81 | 5.81k | _java_scan_time = ADD_CHILD_TIMER(_profile, "JavaScanTime", _connector_name.c_str()); |
82 | 5.81k | _java_append_data_time = |
83 | 5.81k | ADD_CHILD_TIMER(_profile, "JavaAppendDataTime", _connector_name.c_str()); |
84 | 5.81k | _java_create_vector_table_time = |
85 | 5.81k | ADD_CHILD_TIMER(_profile, "JavaCreateVectorTableTime", _connector_name.c_str()); |
86 | 5.81k | _fill_block_time = ADD_CHILD_TIMER(_profile, "FillBlockTime", _connector_name.c_str()); |
87 | 5.81k | _max_time_split_weight_counter = _profile->add_conditition_counter( |
88 | 5.82k | "MaxTimeSplitWeight", TUnit::UNIT, [](int64_t _c, int64_t c) { return c > _c; }, |
89 | 5.81k | _connector_name.c_str()); |
90 | 5.81k | } |
91 | 6.12k | _java_scan_watcher = 0; |
92 | | |
93 | 6.12k | JNIEnv* env = nullptr; |
94 | 6.12k | int batch_size = 0; |
95 | 6.12k | if (!_is_table_schema && _state) { |
96 | 5.82k | batch_size = _state->batch_size(); |
97 | 5.82k | } |
98 | 6.12k | RETURN_IF_ERROR(Jni::Env::Get(&env)); |
99 | 6.12k | SCOPED_RAW_TIMER(&_jni_scanner_open_watcher); |
100 | 6.12k | if (_state) { |
101 | 5.82k | _scanner_params.emplace("time_zone", _state->timezone()); |
102 | 5.82k | } |
103 | 6.12k | RETURN_IF_ERROR(_init_jni_scanner(env, batch_size)); |
104 | | // Call org.apache.doris.common.jni.JniScanner#open |
105 | 6.12k | RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _jni_scanner_open).call()); |
106 | | |
107 | 6.12k | RETURN_ERROR_IF_EXC(env); |
108 | 6.12k | _scanner_opened = true; |
109 | 6.12k | return Status::OK(); |
110 | 6.12k | } |
111 | | |
112 | | // ========================================================================= |
113 | | // JniReader::get_next_block (merged from JniConnector::get_next_block) |
114 | | // ========================================================================= |
115 | | |
116 | 12.2k | Status JniReader::get_next_block(Block* block, size_t* read_rows, bool* eof) { |
117 | 12.2k | JNIEnv* env = nullptr; |
118 | 12.2k | RETURN_IF_ERROR(Jni::Env::Get(&env)); |
119 | 12.2k | long meta_address = 0; |
120 | 12.2k | { |
121 | 12.2k | SCOPED_RAW_TIMER(&_java_scan_watcher); |
122 | 12.2k | RETURN_IF_ERROR(_jni_scanner_obj.call_long_method(env, _jni_scanner_get_next_batch) |
123 | 12.2k | .call(&meta_address)); |
124 | 12.2k | } |
125 | 12.2k | if (meta_address == 0) { |
126 | 5.73k | *read_rows = 0; |
127 | 5.73k | *eof = true; |
128 | 5.73k | return Status::OK(); |
129 | 5.73k | } |
130 | 6.55k | _set_meta(meta_address); |
131 | 6.55k | long num_rows = _table_meta.next_meta_as_long(); |
132 | 6.55k | if (num_rows == 0) { |
133 | 0 | *read_rows = 0; |
134 | 0 | *eof = true; |
135 | 0 | return Status::OK(); |
136 | 0 | } |
137 | 6.55k | RETURN_IF_ERROR(_fill_block(block, num_rows)); |
138 | 6.55k | *read_rows = num_rows; |
139 | 6.55k | *eof = false; |
140 | 6.55k | RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _jni_scanner_release_table).call()); |
141 | 6.55k | _has_read += num_rows; |
142 | 6.55k | return Status::OK(); |
143 | 6.55k | } |
144 | | |
145 | | // ========================================================================= |
146 | | // JniReader::get_table_schema (merged from JniConnector::get_table_schema) |
147 | | // ========================================================================= |
148 | | |
149 | 0 | Status JniReader::get_table_schema(std::string& table_schema_str) { |
150 | 0 | JNIEnv* env = nullptr; |
151 | 0 | RETURN_IF_ERROR(Jni::Env::Get(&env)); |
152 | | |
153 | 0 | Jni::LocalString jstr; |
154 | 0 | RETURN_IF_ERROR( |
155 | 0 | _jni_scanner_obj.call_object_method(env, _jni_scanner_get_table_schema).call(&jstr)); |
156 | 0 | Jni::LocalStringBufferGuard cstr; |
157 | 0 | RETURN_IF_ERROR(jstr.get_string_chars(env, &cstr)); |
158 | 0 | table_schema_str = std::string {cstr.get()}; |
159 | 0 | return Status::OK(); |
160 | 0 | } |
161 | | |
162 | | // ========================================================================= |
163 | | // JniReader::close (merged from JniConnector::close) |
164 | | // ========================================================================= |
165 | | |
166 | 6.13k | Status JniReader::close() { |
167 | 6.13k | if (!_closed) { |
168 | 6.13k | _closed = true; |
169 | 6.13k | JNIEnv* env = nullptr; |
170 | 6.13k | RETURN_IF_ERROR(Jni::Env::Get(&env)); |
171 | 6.13k | if (_scanner_opened) { |
172 | 6.13k | if (_profile) { |
173 | 5.82k | COUNTER_UPDATE(_open_scanner_time, _jni_scanner_open_watcher); |
174 | 5.82k | COUNTER_UPDATE(_fill_block_time, _fill_block_watcher); |
175 | 5.82k | } |
176 | | |
177 | 6.13k | RETURN_ERROR_IF_EXC(env); |
178 | 6.13k | jlong _append = 0; |
179 | 6.13k | RETURN_IF_ERROR( |
180 | 6.13k | _jni_scanner_obj.call_long_method(env, _jni_scanner_get_append_data_time) |
181 | 6.13k | .call(&_append)); |
182 | | |
183 | 6.13k | if (_profile) { |
184 | 5.82k | COUNTER_UPDATE(_java_append_data_time, _append); |
185 | 5.82k | } |
186 | | |
187 | 6.13k | jlong _create = 0; |
188 | 6.13k | RETURN_IF_ERROR( |
189 | 6.13k | _jni_scanner_obj |
190 | 6.13k | .call_long_method(env, _jni_scanner_get_create_vector_table_time) |
191 | 6.13k | .call(&_create)); |
192 | | |
193 | 6.13k | if (_profile) { |
194 | 5.82k | COUNTER_UPDATE(_java_create_vector_table_time, _create); |
195 | 5.82k | COUNTER_UPDATE(_java_scan_time, _java_scan_watcher - _append - _create); |
196 | 5.82k | _max_time_split_weight_counter->conditional_update( |
197 | 5.82k | _jni_scanner_open_watcher + _fill_block_watcher + _java_scan_watcher, |
198 | 5.82k | _self_split_weight); |
199 | 5.82k | } |
200 | | |
201 | | // _fill_block may be failed and returned, we should release table in close. |
202 | | // org.apache.doris.common.jni.JniScanner#releaseTable is idempotent |
203 | 6.13k | RETURN_IF_ERROR( |
204 | 6.13k | _jni_scanner_obj.call_void_method(env, _jni_scanner_release_table).call()); |
205 | 6.13k | RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _jni_scanner_close).call()); |
206 | 6.13k | } |
207 | 6.13k | } |
208 | 6.13k | return Status::OK(); |
209 | 6.13k | } |
210 | | |
211 | | // ========================================================================= |
212 | | // JniReader::_init_jni_scanner (merged from JniConnector::_init_jni_scanner) |
213 | | // ========================================================================= |
214 | | |
215 | 6.12k | Status JniReader::_init_jni_scanner(JNIEnv* env, int batch_size) { |
216 | 6.12k | RETURN_IF_ERROR( |
217 | 6.12k | Jni::Util::get_jni_scanner_class(env, _connector_class.c_str(), &_jni_scanner_cls)); |
218 | | |
219 | 6.12k | Jni::MethodId scanner_constructor; |
220 | 6.12k | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "<init>", "(ILjava/util/Map;)V", |
221 | 6.12k | &scanner_constructor)); |
222 | | |
223 | | // prepare constructor parameters |
224 | 6.12k | Jni::LocalObject hashmap_object; |
225 | 6.12k | RETURN_IF_ERROR(Jni::Util::convert_to_java_map(env, _scanner_params, &hashmap_object)); |
226 | 6.12k | RETURN_IF_ERROR(_jni_scanner_cls.new_object(env, scanner_constructor) |
227 | 6.12k | .with_arg(batch_size) |
228 | 6.12k | .with_arg(hashmap_object) |
229 | 6.12k | .call(&_jni_scanner_obj)); |
230 | | |
231 | 6.12k | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "open", "()V", &_jni_scanner_open)); |
232 | 6.12k | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getNextBatchMeta", "()J", |
233 | 6.12k | &_jni_scanner_get_next_batch)); |
234 | 6.12k | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getAppendDataTime", "()J", |
235 | 6.12k | &_jni_scanner_get_append_data_time)); |
236 | 6.12k | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getCreateVectorTableTime", "()J", |
237 | 6.12k | &_jni_scanner_get_create_vector_table_time)); |
238 | 6.12k | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getTableSchema", "()Ljava/lang/String;", |
239 | 6.12k | &_jni_scanner_get_table_schema)); |
240 | 6.12k | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "close", "()V", &_jni_scanner_close)); |
241 | 6.12k | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "releaseColumn", "(I)V", |
242 | 6.12k | &_jni_scanner_release_column)); |
243 | 6.12k | RETURN_IF_ERROR( |
244 | 6.12k | _jni_scanner_cls.get_method(env, "releaseTable", "()V", &_jni_scanner_release_table)); |
245 | 6.12k | RETURN_IF_ERROR(_jni_scanner_cls.get_method(env, "getStatistics", "()Ljava/util/Map;", |
246 | 6.12k | &_jni_scanner_get_statistics)); |
247 | 6.12k | return Status::OK(); |
248 | 6.12k | } |
249 | | |
250 | | // ========================================================================= |
251 | | // JniReader::_fill_block (merged from JniConnector::_fill_block) |
252 | | // ========================================================================= |
253 | | |
254 | 6.57k | Status JniReader::_fill_block(Block* block, size_t num_rows) { |
255 | 6.57k | SCOPED_RAW_TIMER(&_fill_block_watcher); |
256 | 6.57k | JNIEnv* env = nullptr; |
257 | 6.57k | RETURN_IF_ERROR(Jni::Env::Get(&env)); |
258 | | // Fallback: if _col_name_to_block_idx was not set by the caller (e.g. JdbcScanner), |
259 | | // build the name-to-position map from the block itself. |
260 | 6.57k | std::unordered_map<std::string, uint32_t> local_name_to_idx; |
261 | 6.57k | const std::unordered_map<std::string, uint32_t>* col_map = _col_name_to_block_idx; |
262 | 6.57k | if (col_map == nullptr) { |
263 | 0 | local_name_to_idx = block->get_name_to_pos_map(); |
264 | 0 | col_map = &local_name_to_idx; |
265 | 0 | } |
266 | 35.6k | for (int i = 0; i < _column_names.size(); ++i) { |
267 | 29.0k | auto& column_with_type_and_name = block->get_by_position(col_map->at(_column_names[i])); |
268 | 29.0k | auto& column_ptr = column_with_type_and_name.column; |
269 | 29.0k | auto& column_type = column_with_type_and_name.type; |
270 | 29.0k | RETURN_IF_ERROR(JniDataBridge::fill_column(_table_meta, column_ptr, column_type, num_rows)); |
271 | | // Column is not released when fill_column failed. It will be released when releasing table. |
272 | 29.0k | RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _jni_scanner_release_column) |
273 | 29.0k | .with_arg(i) |
274 | 29.0k | .call()); |
275 | 29.0k | RETURN_ERROR_IF_EXC(env); |
276 | 29.0k | } |
277 | 6.57k | return Status::OK(); |
278 | 6.57k | } |
279 | | |
280 | | // ========================================================================= |
281 | | // JniReader::_get_statistics (merged from JniConnector::get_statistics) |
282 | | // ========================================================================= |
283 | | |
284 | 5.00k | Status JniReader::_get_statistics(JNIEnv* env, std::map<std::string, std::string>* result) { |
285 | 5.00k | result->clear(); |
286 | 5.00k | Jni::LocalObject metrics; |
287 | 5.00k | RETURN_IF_ERROR( |
288 | 5.00k | _jni_scanner_obj.call_object_method(env, _jni_scanner_get_statistics).call(&metrics)); |
289 | | |
290 | 5.00k | RETURN_IF_ERROR(Jni::Util::convert_to_cpp_map(env, metrics, result)); |
291 | 5.00k | return Status::OK(); |
292 | 5.00k | } |
293 | | |
294 | | // ========================================================================= |
295 | | // JniReader::_collect_profile_before_close |
296 | | // (merged from JniConnector::_collect_profile_before_close) |
297 | | // ========================================================================= |
298 | | |
299 | 5.00k | void JniReader::_collect_profile_before_close() { |
300 | 5.00k | if (_scanner_opened && _profile != nullptr) { |
301 | 5.00k | JNIEnv* env = nullptr; |
302 | 5.00k | Status st = Jni::Env::Get(&env); |
303 | 5.00k | if (!st) { |
304 | 0 | LOG(WARNING) << "failed to get jni env when collect profile: " << st; |
305 | 0 | return; |
306 | 0 | } |
307 | | // update scanner metrics |
308 | 5.00k | std::map<std::string, std::string> statistics_result; |
309 | 5.00k | st = _get_statistics(env, &statistics_result); |
310 | 5.00k | if (!st) { |
311 | 0 | LOG(WARNING) << "failed to get_statistics when collect profile: " << st; |
312 | 0 | return; |
313 | 0 | } |
314 | | |
315 | 9.19k | for (const auto& metric : statistics_result) { |
316 | 9.19k | std::vector<std::string> type_and_name = split(metric.first, ":"); |
317 | 9.19k | if (type_and_name.size() != 2) { |
318 | 0 | LOG(WARNING) << "Name of JNI Scanner metric should be pattern like " |
319 | 0 | << "'metricType:metricName'"; |
320 | 0 | continue; |
321 | 0 | } |
322 | 9.19k | long metric_value = std::stol(metric.second); |
323 | 9.19k | RuntimeProfile::Counter* scanner_counter; |
324 | 9.19k | if (type_and_name[0] == "timer") { |
325 | 6.86k | scanner_counter = |
326 | 6.86k | ADD_CHILD_TIMER(_profile, type_and_name[1], _connector_name.c_str()); |
327 | 6.86k | } else if (type_and_name[0] == "counter") { |
328 | 2.32k | scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::UNIT, |
329 | 2.32k | _connector_name.c_str()); |
330 | 2.32k | } else if (type_and_name[0] == "bytes") { |
331 | 0 | scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::BYTES, |
332 | 0 | _connector_name.c_str()); |
333 | 0 | } else { |
334 | 0 | LOG(WARNING) << "Type of JNI Scanner metric should be timer, counter or bytes"; |
335 | 0 | continue; |
336 | 0 | } |
337 | 9.19k | COUNTER_UPDATE(scanner_counter, metric_value); |
338 | 9.19k | } |
339 | 5.00k | } |
340 | 5.00k | } |
341 | | |
342 | | // ========================================================================= |
343 | | // MockJniReader |
344 | | // ========================================================================= |
345 | | |
346 | | MockJniReader::MockJniReader(const std::vector<SlotDescriptor*>& file_slot_descs, |
347 | | RuntimeState* state, RuntimeProfile* profile) |
348 | 0 | : JniReader( |
349 | 0 | file_slot_descs, state, profile, "org/apache/doris/common/jni/MockJniScanner", |
350 | 0 | [&]() { |
351 | 0 | std::ostringstream required_fields; |
352 | 0 | std::ostringstream columns_types; |
353 | 0 | int index = 0; |
354 | 0 | for (const auto& desc : file_slot_descs) { |
355 | 0 | std::string field = desc->col_name(); |
356 | 0 | std::string type = |
357 | 0 | JniDataBridge::get_jni_type_with_different_string(desc->type()); |
358 | 0 | if (index == 0) { |
359 | 0 | required_fields << field; |
360 | 0 | columns_types << type; |
361 | 0 | } else { |
362 | 0 | required_fields << "," << field; |
363 | 0 | columns_types << "#" << type; |
364 | 0 | } |
365 | 0 | index++; |
366 | 0 | } |
367 | 0 | return std::map<String, String> {{"mock_rows", "10240"}, |
368 | 0 | {"required_fields", required_fields.str()}, |
369 | 0 | {"columns_types", columns_types.str()}}; |
370 | 0 | }(), |
371 | 0 | [&]() { |
372 | 0 | std::vector<std::string> names; |
373 | 0 | for (const auto& desc : file_slot_descs) { |
374 | 0 | names.emplace_back(desc->col_name()); |
375 | 0 | } |
376 | 0 | return names; |
377 | 0 | }()) {} |
378 | | |
379 | 0 | Status MockJniReader::init_reader() { |
380 | 0 | return open(_state, _profile); |
381 | 0 | } |
382 | | |
383 | | #include "common/compile_check_end.h" |
384 | | } // namespace doris |