Coverage Report

Created: 2026-10-09 05:44

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/jni_plugin_registry.h
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
#pragma once
19
20
#include <jni.h>
21
22
#include <map>
23
#include <string>
24
#include <string_view>
25
26
#include "common/status.h"
27
#include "util/jni-util.h"
28
29
namespace doris::Jni {
30
31
// How BE names one Java factory: the plugin's directory under plugins/jni/, and the
32
// factory's own name within that plugin. It replaces the class name BE used to reflect on -
33
// a concrete class is private to its plugin's classloader, so naming one from C++ stopped
34
// being possible the moment plugins were isolated.
35
//
36
// Both halves are compile-time constants; nothing builds a PluginRef out of a temporary.
37
struct PluginRef {
38
    std::string_view plugin;
39
    std::string_view factory;
40
};
41
42
// Every plugin and factory BE addresses, in one place because the plugin half is also a
43
// deployment directory name: it appears in plugins/jni/, in build.sh, in the layout check
44
// build.sh runs over the deployed tree, and in the "is not deployed" a user sees. It is named here
45
// once so renaming a plugin is one edit rather than a hunt through the readers - the same
46
// connector is reached from both the v1 and the v2 scan paths, and two independent string
47
// literals for one directory is how they would drift apart.
48
//
49
// The factory half must be unique within its plugin across all kinds of factory, because the
50
// pair below is everything BE sends: PluginRegistry receives a plugin name and a factory name
51
// and nothing that says which kind was wanted, so a name meaning two things would resolve by
52
// whichever list happens to be searched first.
53
//
54
// So a factory is named for what it does within its plugin, not for the connector - the plugin
55
// half already says which connector this is. A connector's ordinary read side is "reader" and
56
// its write side "writer"; anything else says what it is, as "connection-tester" does.
57
//
58
// The entry still repeating its plugin's name predates that rule and is renamed to "reader" by
59
// the change that turns it into a plugin, together with the getName() of the factory that change
60
// writes. Renaming it earlier would name a factory nothing implements.
61
namespace plugin {
62
63
inline constexpr PluginRef PAIMON_SCANNER {"paimon", "reader"};
64
inline constexpr PluginRef PAIMON_WRITER {"paimon", "writer"};
65
inline constexpr PluginRef FLUSS_SCANNER {"fluss", "reader"};
66
inline constexpr PluginRef HUDI_SCANNER {"hudi", "reader"};
67
// Only the Iceberg system tables come through Java; Iceberg data files are read natively - so
68
// this one keeps its own name when it is migrated. It is not the connector's reader.
69
inline constexpr PluginRef ICEBERG_SYS_TABLE_SCANNER {"iceberg", "sys-table"};
70
inline constexpr PluginRef MAX_COMPUTE_SCANNER {"max-compute", "reader"};
71
inline constexpr PluginRef MAX_COMPUTE_WRITER {"max-compute", "writer"};
72
inline constexpr PluginRef JDBC_SCANNER {"jdbc", "reader"};
73
inline constexpr PluginRef JDBC_WRITER {"jdbc", "writer"};
74
// Testing a JDBC connection is a scan of zero rows, so it is a scanner factory of its own
75
// rather than a second entry point.
76
inline constexpr PluginRef JDBC_CONNECTION_TESTER {"jdbc", "connection-tester"};
77
inline constexpr PluginRef TRINO_CONNECTOR_SCANNER {"trino-connector", "reader"};
78
// A table function's executor is the scalar one: the same evaluate() runs per row and the
79
// serialized parameters carry the flag that makes its return type an array. Hence two entries
80
// for three kinds of function.
81
inline constexpr PluginRef JAVA_UDF_SCALAR {"java-udf", "scalar"};
82
inline constexpr PluginRef JAVA_UDF_AGGREGATE {"java-udf", "aggregate"};
83
84
} // namespace plugin
85
86
// The methods BE calls on a scanner, resolved on the SPI's JniScanner rather than on the
87
// concrete class. Two reasons, and the first is what forces the design: the concrete class
88
// is loaded by its plugin's classloader, so its jclass cannot be shared between plugins,
89
// while the abstract base is loaded by the shared parent and is one class for the whole
90
// process. The second is that every method here is final on the base class, so a plugin
91
// cannot shadow one and change what BE calls.
92
//
93
// The names and descriptors are fixed by PROTOCOL.md section 1. Changing one means changing
94
// both sides in the same commit.
95
//
96
// The base class is kept alongside the ids resolved on it, for two reasons: a jmethodID is only
97
// valid while its class is reachable, and checking that an object really is a scanner before
98
// calling these on it needs that same class.
99
struct ScannerApi {
100
    GlobalClass cls;
101
    MethodId open;
102
    MethodId get_next_batch_meta;
103
    MethodId get_statistics;
104
    MethodId get_append_data_time;
105
    MethodId get_create_vector_table_time;
106
    MethodId set_batch_size;
107
    MethodId release_column;
108
    MethodId release_table;
109
    MethodId close;
110
};
111
112
// The writer half of the same contract, resolved on the SPI's JniWriter.
113
struct WriterApi {
114
    GlobalClass cls;
115
    MethodId open;
116
    MethodId write;
117
    MethodId get_statistics;
118
    MethodId close;
119
};
120
121
// The single Java entry point BE calls into, wrapping
122
// org.apache.doris.jni.bootstrap.PluginRegistry.
123
//
124
// There is deliberately no fallback anywhere below. If the registry cannot answer - the
125
// plugin is not deployed, its jars are broken, the factory name is wrong - that is the
126
// result, and the Java message says which of those it was. Nothing here retries against
127
// another classloader or degrades to a built-in reader: doing so would report a plugin's
128
// deployment problem as a query producing no rows.
129
class PluginRegistry {
130
public:
131
    // Creates a scanner and hands back the shared method ids for it. They come together
132
    // because an instance is useless without them and resolving them anywhere but on the
133
    // SPI base class is the mistake this API exists to prevent.
134
    static Status create_scanner(JNIEnv* env, const PluginRef& ref, int batch_size,
135
                                 const std::map<std::string, std::string>& params,
136
                                 GlobalObject* scanner, const ScannerApi** api);
137
138
    static Status create_writer(JNIEnv* env, const PluginRef& ref, int batch_size,
139
                                const std::map<std::string, std::string>& params,
140
                                GlobalObject* writer, const WriterApi** api);
141
142
    // Creates the executor behind one Java function. The parameters stay a serialized thrift
143
    // struct all the way into the plugin, which owns the only copy of thrift that can read
144
    // them; the executor comes back as a plain object because the three function kinds do
145
    // not share a method shape. See UdfExecutorFactory.
146
    //
147
    // Its class comes back with it, and it is the only way the caller can get one: the
148
    // executor's class lives in the plugin's classloader, so FindClass by name - which searches
149
    // BE's loader - would not find it. The caller resolves the methods of its own kind on this.
150
    static Status create_udf_executor(JNIEnv* env, const PluginRef& ref,
151
                                      const LocalArray& thrift_params, GlobalObject* executor,
152
                                      GlobalClass* executor_class);
153
154
    // Forwarded to every loaded plugin on DROP FUNCTION, so whichever one compiled that
155
    // function can drop what it cached for it. The id is the identity a plugin caches by; the
156
    // signature is for its logs, and for the case where FE sent no id.
157
    //
158
    // Returns without touching Java when no plugin has ever been loaded: dropping a
159
    // function must not be the thing that starts a JVM on a BE that runs no Java at all.
160
    static Status clean_udf_cache(int64_t function_id, const std::string& function_signature);
161
162
    // State of every plugin loaded so far, as JSON.
163
    static Status plugin_status_json(std::string* status);
164
165
    // Loads every deployed plugin now rather than on first use, so a broken deployment is
166
    // in the log before a user query finds it. Never fails for a single plugin's sake.
167
    //
168
    // Does nothing, and creates no JVM, when no plugin is deployed.
169
    static Status warmup();
170
171
    // Whether the plugin directory holds at least one plugin. Public because it is the
172
    // reason warmup is safe to turn on, and something has to be able to check it.
173
    static bool any_plugin_deployed();
174
175
    // Whether anything has reached the Java registry yet. The status endpoint asks first:
176
    // there is nothing to report before that, and asking Java would create the JVM.
177
0
    static bool registry_initialized() { return _registry_ready; }
178
179
private:
180
    // The bootstrap class and its static method ids, resolved once.
181
    struct Registry {
182
        GlobalClass cls;
183
        MethodId create_instance;
184
        MethodId create_udf_executor;
185
        MethodId clean_udf_cache;
186
        MethodId plugin_status_json;
187
        MethodId warmup;
188
    };
189
190
    static Status _ensure_registry(const Registry** registry);
191
    static Status _init_registry();
192
    static Status _init_scanner_api();
193
    static Status _init_writer_api();
194
195
    // createInstance() backs both scanners and writers: what a plugin hands back is decided
196
    // by which factory list the name was looked up in, not by a separate Java entry point.
197
    static Status _create_instance(JNIEnv* env, const PluginRef& ref, int batch_size,
198
                                   const std::map<std::string, std::string>& params,
199
                                   GlobalObject* instance);
200
201
    // Rejects an instance of the wrong kind before its method ids are handed out, because a
202
    // factory name can come from user SQL. See the comment at the definition.
203
    static Status _check_kind(JNIEnv* env, const PluginRef& ref, const GlobalObject& instance,
204
                              const GlobalClass& expected, const char* expected_kind);
205
206
    static Registry _registry;
207
    static ScannerApi _scanner_api;
208
    static WriterApi _writer_api;
209
210
    // Whether _init_registry() has ever succeeded, so that dropping a function can tell "no
211
    // plugin has ever been loaded" from "the registry is unreachable".
212
    static std::atomic<bool> _registry_ready;
213
};
214
215
} // namespace doris::Jni