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