This is an automated email from the ASF dual-hosted git repository. gabriellee pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/master by this push: new 2ee7ba79a8 [Improvement](javaudf) improve java loader usage (#13962) 2ee7ba79a8 is described below commit 2ee7ba79a87ba4daac9eadc7dec9e6677c300e05 Author: Gabriel <gabrielleeb...@gmail.com> AuthorDate: Sat Nov 5 13:20:04 2022 +0800 [Improvement](javaudf) improve java loader usage (#13962) --- be/src/runtime/user_function_cache.cpp | 1 + .../apache/doris/analysis/CreateFunctionStmt.java | 81 +++++++++++----------- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/be/src/runtime/user_function_cache.cpp b/be/src/runtime/user_function_cache.cpp index 47e688ed5f..b985fec75d 100644 --- a/be/src/runtime/user_function_cache.cpp +++ b/be/src/runtime/user_function_cache.cpp @@ -394,6 +394,7 @@ Status UserFunctionCache::_add_to_classpath(UserFunctionCacheEntry* entry) { jmethodID url_ctor = env->GetMethodID(class_url, "<init>", "(Ljava/lang/String;)V"); jobject urlInstance = env->NewObject(class_url, url_ctor, env->NewStringUTF(path.c_str())); env->CallVoidMethod(class_loader, method_add_url, urlInstance); + entry->is_loaded.store(true); return Status::OK(); #else return Status::InternalError("No libjvm is found!"); diff --git a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateFunctionStmt.java b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateFunctionStmt.java index 39fccef1dc..1629990a55 100644 --- a/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateFunctionStmt.java +++ b/fe/fe-core/src/main/java/org/apache/doris/analysis/CreateFunctionStmt.java @@ -464,52 +464,55 @@ public class CreateFunctionStmt extends DdlStmt { private void analyzeJavaUdf(String clazz) throws AnalysisException { try { URL[] urls = {new URL("jar:" + userFile + "!/")}; - URLClassLoader cl = URLClassLoader.newInstance(urls); - Class udfClass = cl.loadClass(clazz); - - Method eval = null; - for (Method m : udfClass.getMethods()) { - if (!m.getDeclaringClass().equals(udfClass)) { - continue; + try (URLClassLoader cl = URLClassLoader.newInstance(urls)) { + Class udfClass = cl.loadClass(clazz); + + Method eval = null; + for (Method m : udfClass.getMethods()) { + if (!m.getDeclaringClass().equals(udfClass)) { + continue; + } + String name = m.getName(); + if (EVAL_METHOD_KEY.equals(name) && eval == null) { + eval = m; + } else if (EVAL_METHOD_KEY.equals(name)) { + throw new AnalysisException(String.format( + "UDF class '%s' has multiple methods with name '%s' ", udfClass.getCanonicalName(), + EVAL_METHOD_KEY)); + } } - String name = m.getName(); - if (EVAL_METHOD_KEY.equals(name) && eval == null) { - eval = m; - } else if (EVAL_METHOD_KEY.equals(name)) { + if (eval == null) { throw new AnalysisException(String.format( - "UDF class '%s' has multiple methods with name '%s' ", udfClass.getCanonicalName(), - EVAL_METHOD_KEY)); + "No method '%s' in class '%s'!", EVAL_METHOD_KEY, udfClass.getCanonicalName())); + } + if (Modifier.isStatic(eval.getModifiers())) { + throw new AnalysisException( + String.format("Method '%s' in class '%s' should be non-static", eval.getName(), + udfClass.getCanonicalName())); + } + if (!Modifier.isPublic(eval.getModifiers())) { + throw new AnalysisException( + String.format("Method '%s' in class '%s' should be public", eval.getName(), + udfClass.getCanonicalName())); + } + if (eval.getParameters().length != argsDef.getArgTypes().length) { + throw new AnalysisException( + String.format("The number of parameters for method '%s' in class '%s' should be %d", + eval.getName(), udfClass.getCanonicalName(), argsDef.getArgTypes().length)); } - } - if (eval == null) { - throw new AnalysisException(String.format( - "No method '%s' in class '%s'!", EVAL_METHOD_KEY, udfClass.getCanonicalName())); - } - if (Modifier.isStatic(eval.getModifiers())) { - throw new AnalysisException( - String.format("Method '%s' in class '%s' should be non-static", eval.getName(), - udfClass.getCanonicalName())); - } - if (!Modifier.isPublic(eval.getModifiers())) { - throw new AnalysisException( - String.format("Method '%s' in class '%s' should be public", eval.getName(), - udfClass.getCanonicalName())); - } - if (eval.getParameters().length != argsDef.getArgTypes().length) { - throw new AnalysisException( - String.format("The number of parameters for method '%s' in class '%s' should be %d", - eval.getName(), udfClass.getCanonicalName(), argsDef.getArgTypes().length)); - } - checkUdfType(udfClass, eval, returnType.getType(), eval.getReturnType(), "return"); - for (int i = 0; i < eval.getParameters().length; i++) { - Parameter p = eval.getParameters()[i]; - checkUdfType(udfClass, eval, argsDef.getArgTypes()[i], p.getType(), p.getName()); + checkUdfType(udfClass, eval, returnType.getType(), eval.getReturnType(), "return"); + for (int i = 0; i < eval.getParameters().length; i++) { + Parameter p = eval.getParameters()[i]; + checkUdfType(udfClass, eval, argsDef.getArgTypes()[i], p.getType(), p.getName()); + } + } catch (ClassNotFoundException e) { + throw new AnalysisException("Class [" + clazz + "] not found in file :" + userFile); + } catch (IOException e) { + throw new AnalysisException("Failed to load file: " + userFile); } } catch (MalformedURLException e) { throw new AnalysisException("Failed to load file: " + userFile); - } catch (ClassNotFoundException e) { - throw new AnalysisException("Class [" + clazz + "] not found in file :" + userFile); } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org