Coverage Report

Created: 2026-11-02 16:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
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 <tuple>
25
#include <unordered_map>
26
#include <utility>
27
28
#include "core/block/block.h"
29
#include "core/types.h"
30
#include "format/jni/jni_data_bridge.h"
31
#include "runtime/descriptors.h"
32
#include "runtime/runtime_state.h"
33
#include "util/jni-util.h"
34
35
namespace doris {
36
class RuntimeProfile;
37
class RuntimeState;
38
39
class Block;
40
} // namespace doris
41
42
namespace doris {
43
44
const std::vector<SlotDescriptor*> JniReader::_s_empty_slot_descs;
45
46
// =========================================================================
47
// JniReader constructors
48
// =========================================================================
49
50
JniReader::JniReader(const std::vector<SlotDescriptor*>& file_slot_descs, RuntimeState* state,
51
                     RuntimeProfile* profile, Jni::PluginRef plugin_ref,
52
                     std::map<std::string, std::string> scanner_params,
53
                     std::vector<std::string> column_names, int64_t self_split_weight)
54
0
        : _file_slot_descs(file_slot_descs),
55
0
          _state(state),
56
0
          _profile(profile),
57
0
          _plugin_ref(plugin_ref),
58
0
          _connector_name(plugin_ref.plugin),
59
0
          _scanner_params(std::move(scanner_params)),
60
0
          _column_names(std::move(column_names)),
61
0
          _self_split_weight(static_cast<int32_t>(self_split_weight)) {}
62
63
JniReader::JniReader(Jni::PluginRef plugin_ref, std::map<std::string, std::string> scanner_params)
64
0
        : _file_slot_descs(_s_empty_slot_descs),
65
0
          _plugin_ref(plugin_ref),
66
0
          _connector_name(plugin_ref.plugin),
67
0
          _scanner_params(std::move(scanner_params)) {
68
0
    _is_table_schema = true;
69
0
}
70
71
// =========================================================================
72
// JniReader::open  (merged from JniConnector::open)
73
// =========================================================================
74
75
0
Status JniReader::open(RuntimeState* state, RuntimeProfile* profile) {
76
0
    _state = state;
77
0
    _profile = profile;
78
0
    if (_profile) {
79
0
        ADD_TIMER(_profile, _connector_name.c_str());
80
0
        _open_scanner_time = ADD_CHILD_TIMER(_profile, "OpenScannerTime", _connector_name.c_str());
81
0
        _java_scan_time = ADD_CHILD_TIMER(_profile, "JavaScanTime", _connector_name.c_str());
82
0
        _java_append_data_time =
83
0
                ADD_CHILD_TIMER(_profile, "JavaAppendDataTime", _connector_name.c_str());
84
0
        _java_create_vector_table_time =
85
0
                ADD_CHILD_TIMER(_profile, "JavaCreateVectorTableTime", _connector_name.c_str());
86
0
        _fill_block_time = ADD_CHILD_TIMER(_profile, "FillBlockTime", _connector_name.c_str());
87
0
        _max_time_split_weight_counter = _profile->add_conditition_counter(
88
0
                "MaxTimeSplitWeight", TUnit::UNIT, [](int64_t _c, int64_t c) { return c > _c; },
89
0
                _connector_name.c_str());
90
0
    }
91
0
    _java_scan_watcher = 0;
92
93
0
    JNIEnv* env = nullptr;
94
0
    int batch_size = 0;
95
0
    if (!_is_table_schema && _state) {
96
0
        batch_size = _state->batch_size();
97
0
    }
98
0
    _batch_size = batch_size;
99
0
    RETURN_IF_ERROR(Jni::Env::Get(&env));
100
0
    SCOPED_RAW_TIMER(&_jni_scanner_open_watcher);
101
0
    if (_state) {
102
0
        _scanner_params.emplace("time_zone", _state->timezone());
103
0
    }
104
0
    RETURN_IF_ERROR(_init_jni_scanner(env, batch_size));
105
    // Call org.apache.doris.jni.spi.JniScanner#open
106
0
    RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _scanner_api->open).call());
107
108
0
    RETURN_ERROR_IF_EXC(env);
109
0
    _scanner_opened = true;
110
0
    return Status::OK();
111
0
}
112
113
// =========================================================================
114
// JniReader::_do_get_next_block  (merged from JniConnector::get_next_block)
115
// =========================================================================
116
117
0
Status JniReader::_do_get_next_block(Block* block, size_t* read_rows, bool* eof) {
118
0
    JNIEnv* env = nullptr;
119
0
    RETURN_IF_ERROR(Jni::Env::Get(&env));
120
0
    long meta_address = 0;
121
0
    {
122
0
        SCOPED_RAW_TIMER(&_java_scan_watcher);
123
0
        RETURN_IF_ERROR(_jni_scanner_obj.call_long_method(env, _scanner_api->get_next_batch_meta)
124
0
                                .call(&meta_address));
125
0
    }
126
0
    if (meta_address == 0) {
127
0
        *read_rows = 0;
128
0
        *eof = true;
129
0
        return Status::OK();
130
0
    }
131
0
    _set_meta(meta_address);
132
0
    long num_rows = _table_meta.next_meta_as_long();
133
0
    if (num_rows == 0) {
134
0
        *read_rows = 0;
135
0
        *eof = true;
136
0
        return Status::OK();
137
0
    }
138
0
    RETURN_IF_ERROR(_fill_block(block, num_rows));
139
0
    *read_rows = num_rows;
140
0
    *eof = false;
141
0
    RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _scanner_api->release_table).call());
142
0
    _has_read += num_rows;
143
0
    return Status::OK();
144
0
}
145
146
// =========================================================================
147
// JniReader::close  (merged from JniConnector::close)
148
// =========================================================================
149
150
0
Status JniReader::close() {
151
0
    if (_closed) {
152
0
        return Status::OK();
153
0
    }
154
0
    if (!_scanner_opened) {
155
0
        _closed = true;
156
0
        return Status::OK();
157
0
    }
158
159
0
    JNIEnv* env = nullptr;
160
0
    RETURN_IF_ERROR(Jni::Env::Get(&env));
161
162
    // _fill_block may fail before releasing the current Java table. JniScanner::releaseTable()
163
    // is idempotent, so close always retries it. Java close must still run when that release
164
    // fails, otherwise connector resources such as Paimon's static table-cache lease can leak.
165
0
    auto close_status = _jni_scanner_obj.call_void_method(env, _scanner_api->release_table).call();
166
0
    auto java_close_status = _jni_scanner_obj.call_void_method(env, _scanner_api->close).call();
167
0
    if (close_status.ok() && !java_close_status.ok()) {
168
0
        close_status = std::move(java_close_status);
169
0
    }
170
0
    if (close_status.ok()) {
171
0
        _scanner_opened = false;
172
0
        _closed = true;
173
0
    }
174
0
    return close_status;
175
0
}
176
177
// =========================================================================
178
// JniReader::set_batch_size
179
// =========================================================================
180
181
0
void JniReader::set_batch_size(size_t batch_size) {
182
0
    DCHECK_GT(batch_size, 0);
183
0
    if (_batch_size == batch_size) {
184
0
        return;
185
0
    }
186
0
    _batch_size = batch_size;
187
0
    if (_scanner_opened) {
188
0
        JNIEnv* env = nullptr;
189
0
        Status st = Jni::Env::Get(&env);
190
0
        if (!st) {
191
0
            LOG(WARNING) << "failed to get jni env when set_batch_size: " << st;
192
0
            return;
193
0
        }
194
0
        st = _jni_scanner_obj.call_void_method(env, _scanner_api->set_batch_size)
195
0
                     .with_arg(static_cast<int>(_batch_size))
196
0
                     .call();
197
0
        if (!st) {
198
0
            LOG(WARNING) << "failed to call setBatchSize: " << st;
199
0
        }
200
0
    }
201
0
}
202
203
// =========================================================================
204
// JniReader::_init_jni_scanner  (merged from JniConnector::_init_jni_scanner)
205
// =========================================================================
206
207
0
Status JniReader::_init_jni_scanner(JNIEnv* env, int batch_size) {
208
0
    return Jni::PluginRegistry::create_scanner(env, _plugin_ref, batch_size, _scanner_params,
209
0
                                               &_jni_scanner_obj, &_scanner_api);
210
0
}
211
212
// =========================================================================
213
// JniReader::_fill_block  (merged from JniConnector::_fill_block)
214
// =========================================================================
215
216
0
Status JniReader::_fill_block(Block* block, size_t num_rows) {
217
0
    SCOPED_RAW_TIMER(&_fill_block_watcher);
218
0
    JNIEnv* env = nullptr;
219
0
    RETURN_IF_ERROR(Jni::Env::Get(&env));
220
0
    const auto local_name_to_idx = block->get_name_to_pos_map();
221
0
    const auto* col_map = &local_name_to_idx;
222
0
    for (int i = 0; i < _column_names.size(); ++i) {
223
0
        auto& column_with_type_and_name = block->get_by_position(col_map->at(_column_names[i]));
224
0
        auto& column_ptr = column_with_type_and_name.column;
225
0
        auto& column_type = column_with_type_and_name.type;
226
0
        RETURN_IF_ERROR(JniDataBridge::fill_column(_table_meta, column_ptr, column_type, num_rows));
227
        // Column is not released when fill_column failed. It will be released when releasing table.
228
0
        RETURN_IF_ERROR(_jni_scanner_obj.call_void_method(env, _scanner_api->release_column)
229
0
                                .with_arg(i)
230
0
                                .call());
231
0
        RETURN_ERROR_IF_EXC(env);
232
0
    }
233
0
    return Status::OK();
234
0
}
235
236
// =========================================================================
237
// JniReader::_get_statistics  (merged from JniConnector::get_statistics)
238
// =========================================================================
239
240
0
Status JniReader::_get_statistics(JNIEnv* env, std::map<std::string, std::string>* result) {
241
0
    result->clear();
242
0
    Jni::LocalObject metrics;
243
0
    RETURN_IF_ERROR(
244
0
            _jni_scanner_obj.call_object_method(env, _scanner_api->get_statistics).call(&metrics));
245
246
0
    RETURN_IF_ERROR(Jni::Util::convert_to_cpp_map(env, metrics, result));
247
0
    return Status::OK();
248
0
}
249
250
// =========================================================================
251
// JniReader::_collect_profile_before_close
252
// (merged from JniConnector::_collect_profile_before_close)
253
// =========================================================================
254
255
0
void JniReader::_collect_profile_before_close() {
256
0
    if (_scanner_opened && _profile != nullptr) {
257
0
        JNIEnv* env = nullptr;
258
0
        Status st = Jni::Env::Get(&env);
259
0
        if (!st) {
260
0
            LOG(WARNING) << "failed to get jni env when collect profile: " << st;
261
0
            return;
262
0
        }
263
0
        COUNTER_UPDATE(_open_scanner_time, _jni_scanner_open_watcher);
264
0
        COUNTER_UPDATE(_fill_block_time, _fill_block_watcher);
265
266
0
        jlong append_data_time = 0;
267
0
        auto append_time_status =
268
0
                _jni_scanner_obj.call_long_method(env, _scanner_api->get_append_data_time)
269
0
                        .call(&append_data_time);
270
0
        jlong create_vector_table_time = 0;
271
0
        auto create_table_time_status =
272
0
                _jni_scanner_obj.call_long_method(env, _scanner_api->get_create_vector_table_time)
273
0
                        .call(&create_vector_table_time);
274
0
        if (!append_time_status.ok()) {
275
0
            LOG(WARNING) << "failed to collect JNI append-data time before close: "
276
0
                         << append_time_status;
277
0
        }
278
0
        if (!create_table_time_status.ok()) {
279
0
            LOG(WARNING) << "failed to collect JNI vector-table time before close: "
280
0
                         << create_table_time_status;
281
0
        }
282
0
        if (append_time_status.ok() && create_table_time_status.ok()) {
283
0
            COUNTER_UPDATE(_java_append_data_time, append_data_time);
284
0
            COUNTER_UPDATE(_java_create_vector_table_time, create_vector_table_time);
285
0
            COUNTER_UPDATE(_java_scan_time,
286
0
                           _java_scan_watcher - append_data_time - create_vector_table_time);
287
0
            _max_time_split_weight_counter->conditional_update(
288
0
                    _jni_scanner_open_watcher + _fill_block_watcher + _java_scan_watcher,
289
0
                    _self_split_weight);
290
0
        }
291
292
        // update scanner metrics
293
0
        std::map<std::string, std::string> statistics_result;
294
0
        st = _get_statistics(env, &statistics_result);
295
0
        if (!st) {
296
0
            LOG(WARNING) << "failed to get_statistics when collect profile: " << st;
297
0
            return;
298
0
        }
299
300
0
        const auto update_peak = [](int64_t previous, int64_t current) {
301
0
            return current > previous;
302
0
        };
303
0
        for (const auto& metric : statistics_result) {
304
0
            std::vector<std::string> type_and_name = split(metric.first, ":");
305
0
            if (type_and_name.size() != 2) {
306
0
                LOG(WARNING) << "Name of JNI Scanner metric should be pattern like "
307
0
                             << "'metricType:metricName'";
308
0
                continue;
309
0
            }
310
0
            int64_t metric_value = std::stoll(metric.second);
311
0
            RuntimeProfile::Counter* scanner_counter;
312
0
            if (type_and_name[0] == "timer") {
313
0
                scanner_counter =
314
0
                        ADD_CHILD_TIMER(_profile, type_and_name[1], _connector_name.c_str());
315
0
                COUNTER_UPDATE(scanner_counter, metric_value);
316
0
            } else if (type_and_name[0] == "counter") {
317
0
                scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::UNIT,
318
0
                                                    _connector_name.c_str());
319
0
                COUNTER_UPDATE(scanner_counter, metric_value);
320
0
            } else if (type_and_name[0] == "bytes") {
321
0
                scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::BYTES,
322
0
                                                    _connector_name.c_str());
323
0
                COUNTER_UPDATE(scanner_counter, metric_value);
324
0
            } else if (type_and_name[0] == "timer_gauge") {
325
0
                scanner_counter =
326
0
                        ADD_CHILD_TIMER(_profile, type_and_name[1], _connector_name.c_str());
327
0
                COUNTER_SET(scanner_counter, metric_value);
328
0
            } else if (type_and_name[0] == "gauge") {
329
0
                scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::UNIT,
330
0
                                                    _connector_name.c_str());
331
0
                COUNTER_SET(scanner_counter, metric_value);
332
0
            } else if (type_and_name[0] == "bytes_gauge") {
333
0
                scanner_counter = ADD_CHILD_COUNTER(_profile, type_and_name[1], TUnit::BYTES,
334
0
                                                    _connector_name.c_str());
335
0
                COUNTER_SET(scanner_counter, metric_value);
336
0
            } else if (type_and_name[0] == "timer_peak") {
337
0
                auto* scanner_peak_counter = _profile->add_conditition_counter(
338
0
                        type_and_name[1], TUnit::TIME_NS, update_peak, _connector_name.c_str());
339
0
                scanner_peak_counter->conditional_update(metric_value, metric_value);
340
0
            } else if (type_and_name[0] == "peak") {
341
0
                auto* scanner_peak_counter = _profile->add_conditition_counter(
342
0
                        type_and_name[1], TUnit::UNIT, update_peak, _connector_name.c_str());
343
0
                scanner_peak_counter->conditional_update(metric_value, metric_value);
344
0
            } else if (type_and_name[0] == "bytes_peak") {
345
0
                auto* scanner_peak_counter = _profile->add_conditition_counter(
346
0
                        type_and_name[1], TUnit::BYTES, update_peak, _connector_name.c_str());
347
0
                scanner_peak_counter->conditional_update(metric_value, metric_value);
348
0
            } else {
349
                LOG(WARNING) << "Type of JNI Scanner metric should be timer, counter, bytes, "
350
0
                             << "timer_gauge, gauge, bytes_gauge, timer_peak, peak or bytes_peak";
351
0
                continue;
352
0
            }
353
0
        }
354
0
    }
355
0
}
356
357
} // namespace doris