Coverage Report

Created: 2026-04-27 12:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/jni_native_method.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 "util/jni_native_method.h"
19
20
#include <gen_cpp/FrontendService.h>
21
#include <glog/logging.h>
22
23
#include <chrono>
24
#include <cstdlib>
25
#include <thread>
26
#include <vector>
27
28
#include "common/status.h"
29
#include "jni.h"
30
#include "runtime/exec_env.h"
31
#include "util/client_cache.h"
32
#include "util/defer_op.h"
33
#include "util/thrift_rpc_helper.h"
34
35
namespace doris {
36
37
namespace {
38
39
0
void throw_java_runtime_exception(JNIEnv* env, const std::string& message) {
40
0
    jclass exception_cl = env->FindClass("java/lang/IllegalStateException");
41
0
    if (exception_cl != nullptr) {
42
0
        env->ThrowNew(exception_cl, message.c_str());
43
0
        env->DeleteLocalRef(exception_cl);
44
0
    }
45
0
}
46
47
Result<int64_t> request_maxcompute_block_id_from_fe(int64_t txn_id,
48
0
                                                    const std::string& write_session_id) {
49
0
    if (txn_id <= 0) {
50
0
        return ResultError(Status::InvalidArgument(
51
0
                "invalid MaxCompute txn_id for block_id allocation: {}", txn_id));
52
0
    }
53
0
    if (write_session_id.empty()) {
54
0
        return ResultError(Status::InvalidArgument(
55
0
                "empty MaxCompute write_session_id for block_id allocation"));
56
0
    }
57
58
0
    constexpr uint32_t FETCH_BLOCK_ID_MAX_RETRY_TIMES = 3;
59
0
    TNetworkAddress master_addr = ExecEnv::GetInstance()->cluster_info()->master_fe_addr;
60
0
    for (uint32_t retry_times = 0; retry_times < FETCH_BLOCK_ID_MAX_RETRY_TIMES; retry_times++) {
61
0
        TMaxComputeBlockIdRequest request;
62
0
        TMaxComputeBlockIdResult result;
63
0
        request.__set_txn_id(txn_id);
64
0
        request.__set_write_session_id(write_session_id);
65
0
        request.__set_length(1);
66
67
0
        Status rpc_status = ThriftRpcHelper::rpc<FrontendServiceClient>(
68
0
                master_addr.hostname, master_addr.port,
69
0
                [&request, &result](FrontendServiceConnection& client) {
70
0
                    client->getMaxComputeBlockIdRange(result, request);
71
0
                });
72
73
0
        if (!rpc_status.ok()) {
74
0
            LOG(WARNING) << "Failed to allocate MaxCompute block_id, rpc failure, retry_time="
75
0
                         << retry_times << ", txn_id=" << txn_id
76
0
                         << ", write_session_id=" << write_session_id << ", status=" << rpc_status;
77
0
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
78
0
            continue;
79
0
        }
80
81
0
        if (!result.__isset.status) {
82
0
            return ResultError(Status::RpcError(
83
0
                    "failed to allocate MaxCompute block_id from FE, missing status in response, "
84
0
                    "txn_id={}, write_session_id={}",
85
0
                    txn_id, write_session_id));
86
0
        }
87
88
0
        Status fe_status = Status::create<false>(result.status);
89
0
        if (fe_status.is<ErrorCode::NOT_MASTER>()) {
90
0
            if (!result.__isset.master_address) {
91
0
                return ResultError(Status::RpcError(
92
0
                        "failed to allocate MaxCompute block_id from FE, missing master address "
93
0
                        "in NOT_MASTER response, txn_id={}, write_session_id={}",
94
0
                        txn_id, write_session_id));
95
0
            }
96
0
            LOG(WARNING) << "Failed to allocate MaxCompute block_id, requested non-master FE@"
97
0
                         << master_addr.hostname << ":" << master_addr.port << ", switch to FE@"
98
0
                         << result.master_address.hostname << ":" << result.master_address.port
99
0
                         << ", retry_time=" << retry_times << ", txn_id=" << txn_id
100
0
                         << ", write_session_id=" << write_session_id;
101
0
            master_addr = result.master_address;
102
0
            std::this_thread::sleep_for(std::chrono::milliseconds(10));
103
0
            continue;
104
0
        }
105
106
0
        if (!fe_status.ok()) {
107
0
            LOG(WARNING) << "Failed to allocate MaxCompute block_id, FE returned error, retry_time="
108
0
                         << retry_times << ", txn_id=" << txn_id
109
0
                         << ", write_session_id=" << write_session_id << ", status=" << fe_status;
110
0
            return ResultError(std::move(fe_status));
111
0
        }
112
113
0
        if (result.length != 1) {
114
0
            return ResultError(Status::RpcError(
115
0
                    "failed to allocate MaxCompute block_id from FE, expected length=1 but got "
116
0
                    "{}, txn_id={}, write_session_id={}",
117
0
                    result.length, txn_id, write_session_id));
118
0
        }
119
120
0
        LOG(INFO) << "Allocated MaxCompute block_id from FE@" << master_addr.hostname << ":"
121
0
                  << master_addr.port << ", txn_id=" << txn_id
122
0
                  << ", write_session_id=" << write_session_id << ", block_id=" << result.start;
123
0
        return result.start;
124
0
    }
125
126
0
    return ResultError(Status::RpcError(
127
0
            "failed to allocate MaxCompute block_id from FE, txn_id={}, write_session_id={}",
128
0
            txn_id, write_session_id));
129
0
}
130
131
} // namespace
132
133
0
jlong JavaNativeMethods::memoryMalloc(JNIEnv* env, jclass clazz, jlong bytes) {
134
0
    return reinterpret_cast<long>(malloc(bytes));
135
0
}
136
137
0
void JavaNativeMethods::memoryFree(JNIEnv* env, jclass clazz, jlong address) {
138
0
    free(reinterpret_cast<void*>(address));
139
0
}
140
141
0
jlongArray JavaNativeMethods::memoryMallocBatch(JNIEnv* env, jclass clazz, jintArray sizes) {
142
0
    DCHECK(sizes != nullptr);
143
0
    jsize n = env->GetArrayLength(sizes);
144
0
    DCHECK(n > 0);
145
0
    jint* elems = env->GetIntArrayElements(sizes, nullptr);
146
0
    if (elems == nullptr) {
147
0
        return nullptr;
148
0
    }
149
0
    DEFER({
150
0
        if (elems != nullptr) {
151
0
            env->ReleaseIntArrayElements(sizes, elems, JNI_ABORT);
152
0
        }
153
0
    });
154
155
0
    jlongArray result = env->NewLongArray(n);
156
0
    if (result == nullptr) {
157
0
        return nullptr;
158
0
    }
159
160
0
    std::vector<void*> allocated;
161
0
    allocated.reserve(n);
162
163
    // sizes are validated on Java side: n > 0 and each size > 0
164
0
    bool failed = false;
165
0
    for (jsize i = 0; i < n; ++i) {
166
0
        auto sz = static_cast<size_t>(elems[i]);
167
0
        void* p = malloc(sz);
168
0
        if (p == nullptr) {
169
0
            failed = true;
170
0
            break;
171
0
        }
172
0
        allocated.push_back(p);
173
0
    }
174
175
0
    if (failed) {
176
0
        for (void* p : allocated) {
177
0
            if (p != nullptr) {
178
0
                free(p);
179
0
            }
180
0
        }
181
0
        return nullptr;
182
0
    }
183
184
0
    std::vector<jlong> addrs(n);
185
0
    for (jsize i = 0; i < n; ++i) {
186
0
        addrs[i] = reinterpret_cast<jlong>(allocated[i]);
187
0
    }
188
0
    env->SetLongArrayRegion(result, 0, n, addrs.data());
189
0
    return result;
190
0
}
191
192
0
void JavaNativeMethods::memoryFreeBatch(JNIEnv* env, jclass clazz, jlongArray addrs) {
193
0
    if (addrs == nullptr) {
194
0
        return;
195
0
    }
196
0
    jsize n = env->GetArrayLength(addrs);
197
0
    if (n <= 0) {
198
0
        return;
199
0
    }
200
0
    jlong* elems = env->GetLongArrayElements(addrs, nullptr);
201
0
    if (elems == nullptr) {
202
0
        return;
203
0
    }
204
0
    for (jsize i = 0; i < n; ++i) {
205
0
        if (elems[i] != 0) {
206
0
            free(reinterpret_cast<void*>(elems[i]));
207
0
        }
208
0
    }
209
0
    env->ReleaseLongArrayElements(addrs, elems, JNI_ABORT);
210
0
}
211
212
jlong JavaNativeMethods::requestMaxComputeBlockId(JNIEnv* env, jclass clazz, jlong txn_id,
213
0
                                                  jstring write_session_id) {
214
0
    if (write_session_id == nullptr) {
215
0
        throw_java_runtime_exception(
216
0
                env, "MaxCompute write_session_id is null when requesting block_id");
217
0
        return 0;
218
0
    }
219
220
0
    const char* write_session_id_chars = env->GetStringUTFChars(write_session_id, nullptr);
221
0
    if (write_session_id_chars == nullptr) {
222
0
        throw_java_runtime_exception(env, "Failed to read MaxCompute write_session_id from Java");
223
0
        return 0;
224
0
    }
225
0
    std::string write_session_id_str(write_session_id_chars);
226
0
    env->ReleaseStringUTFChars(write_session_id, write_session_id_chars);
227
228
0
    auto block_id = request_maxcompute_block_id_from_fe(txn_id, write_session_id_str);
229
0
    if (!block_id.has_value()) {
230
0
        throw_java_runtime_exception(env, block_id.error().to_string());
231
0
        return 0;
232
0
    }
233
0
    return static_cast<jlong>(block_id.value());
234
0
}
235
236
} // namespace doris