Unverified Commit 375df0c7 authored by ZackYoung's avatar ZackYoung Committed by GitHub

[BugFix] [UDF] Fix the issue where UDF cannot find classes in Flink SQL tasks (#3523)

parent 695b78d4
...@@ -70,10 +70,10 @@ public class JobUDFBuilder extends JobBuilder { ...@@ -70,10 +70,10 @@ public class JobUDFBuilder extends JobBuilder {
List<File> jarFiles = List<File> jarFiles =
new ArrayList<>(jobManager.getUdfPathContextHolder().getAllFileSet()); new ArrayList<>(jobManager.getUdfPathContextHolder().getAllFileSet());
String[] userCustomUdfJarPath = UDFUtil.initJavaUDF(udfList, taskId);
String[] jarPaths = CollUtil.removeNull(jarFiles).stream() String[] jarPaths = CollUtil.removeNull(jarFiles).stream()
.map(File::getAbsolutePath) .map(File::getAbsolutePath)
.toArray(String[]::new); .toArray(String[]::new);
if (GATEWAY_TYPE_MAP.get(SESSION).contains(runMode)) { if (GATEWAY_TYPE_MAP.get(SESSION).contains(runMode)) {
config.setJarFiles(jarPaths); config.setJarFiles(jarPaths);
} }
...@@ -82,6 +82,7 @@ public class JobUDFBuilder extends JobBuilder { ...@@ -82,6 +82,7 @@ public class JobUDFBuilder extends JobBuilder {
String[] pyPaths = UDFUtil.initPythonUDF( String[] pyPaths = UDFUtil.initPythonUDF(
udfList, runMode, config.getTaskId(), executor.getTableConfig().getConfiguration()); udfList, runMode, config.getTaskId(), executor.getTableConfig().getConfiguration());
executor.initUDF(userCustomUdfJarPath);
executor.initUDF(jarPaths); executor.initUDF(jarPaths);
if (ArrayUtil.isNotEmpty(pyPaths)) { if (ArrayUtil.isNotEmpty(pyPaths)) {
...@@ -91,6 +92,13 @@ public class JobUDFBuilder extends JobBuilder { ...@@ -91,6 +92,13 @@ public class JobUDFBuilder extends JobBuilder {
} }
} }
} }
if (ArrayUtil.isNotEmpty(userCustomUdfJarPath)) {
for (String jarPath : userCustomUdfJarPath) {
if (StrUtil.isNotBlank(jarPath)) {
jobManager.getUdfPathContextHolder().addUdfPath(new File(jarPath));
}
}
}
Set<File> pyUdfFile = jobManager.getUdfPathContextHolder().getPyUdfFile(); Set<File> pyUdfFile = jobManager.getUdfPathContextHolder().getPyUdfFile();
executor.initPyUDF( executor.initPyUDF(
......
...@@ -25,13 +25,16 @@ import org.dinky.function.exception.UDFCompilerException; ...@@ -25,13 +25,16 @@ import org.dinky.function.exception.UDFCompilerException;
import org.apache.flink.configuration.ReadableConfig; import org.apache.flink.configuration.ReadableConfig;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import cn.hutool.core.lang.Singleton; import cn.hutool.core.lang.Singleton;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
/** @since 0.6.8 */ /** @since 0.6.8 */
public interface FunctionCompiler { public interface FunctionCompiler {
Set<String> COMPILER_CACHE = new HashSet<>();
/** /**
* 函数代码在线动态编译 * 函数代码在线动态编译
...@@ -54,6 +57,11 @@ public interface FunctionCompiler { ...@@ -54,6 +57,11 @@ public interface FunctionCompiler {
static boolean getCompiler(UDF udf, ReadableConfig conf, Integer missionId) { static boolean getCompiler(UDF udf, ReadableConfig conf, Integer missionId) {
Asserts.checkNull(udf, "udf为空"); Asserts.checkNull(udf, "udf为空");
Asserts.checkNull(udf.getCode(), "udf 代码为空"); Asserts.checkNull(udf.getCode(), "udf 代码为空");
String key = udf.getClassName() + udf.getFunctionLanguage();
if (COMPILER_CACHE.contains(key)) {
return true;
}
boolean success; boolean success;
switch (udf.getFunctionLanguage()) { switch (udf.getFunctionLanguage()) {
case JAVA: case JAVA:
...@@ -69,6 +77,9 @@ public interface FunctionCompiler { ...@@ -69,6 +77,9 @@ public interface FunctionCompiler {
throw UDFCompilerException.notSupportedException( throw UDFCompilerException.notSupportedException(
udf.getFunctionLanguage().name()); udf.getFunctionLanguage().name());
} }
if (success) {
COMPILER_CACHE.add(key);
}
return success; return success;
} }
......
...@@ -24,9 +24,6 @@ import org.dinky.function.data.model.UDF; ...@@ -24,9 +24,6 @@ import org.dinky.function.data.model.UDF;
import org.apache.flink.configuration.ReadableConfig; import org.apache.flink.configuration.ReadableConfig;
import java.util.HashSet;
import java.util.Set;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
/** /**
...@@ -34,7 +31,6 @@ import lombok.extern.slf4j.Slf4j; ...@@ -34,7 +31,6 @@ import lombok.extern.slf4j.Slf4j;
*/ */
@Slf4j @Slf4j
public class JavaCompiler implements FunctionCompiler { public class JavaCompiler implements FunctionCompiler {
private static final Set<String> COMPILER_CACHE = new HashSet<>();
/** /**
* 函数代码在线动态编译 * 函数代码在线动态编译
...@@ -46,22 +42,18 @@ public class JavaCompiler implements FunctionCompiler { ...@@ -46,22 +42,18 @@ public class JavaCompiler implements FunctionCompiler {
*/ */
@Override @Override
public synchronized boolean compiler(UDF udf, ReadableConfig conf, Integer missionId) { public synchronized boolean compiler(UDF udf, ReadableConfig conf, Integer missionId) {
String key = udf.getClassName() + udf.getFunctionLanguage();
if (COMPILER_CACHE.contains(key)) {
return true;
}
// TODO 改为ProcessStep注释 // TODO 改为ProcessStep注释
log.info("Compiling java code, class: {}", udf.getClassName()); log.info("Compiling java code, class: {}", udf.getClassName());
CustomStringJavaCompiler compiler = new CustomStringJavaCompiler(udf.getCode()); CustomStringJavaCompiler compiler = new CustomStringJavaCompiler(udf.getCode());
boolean res = compiler.compilerToTmpPath(PathConstant.getUdfCompilerJavaPath(missionId)); boolean res = compiler.compilerToTmpPath(PathConstant.getUdfCompilerJavaPath(missionId));
String className = compiler.getFullClassName(); String className = compiler.getFullClassName();
if (res) { if (res) {
log.info("class编译成功:{}", className); log.info("class compiled successfully:{}", className);
log.info("compiler take time:{}", compiler.getCompilerTakeTime()); log.info("compiler take time:{}", compiler.getCompilerTakeTime());
COMPILER_CACHE.add(key);
return true; return true;
} else { } else {
log.error("class编译失败:{}", className); log.error("class compilation failed:{}", className);
log.error(compiler.getCompilerMessage()); log.error(compiler.getCompilerMessage());
return false; return false;
} }
......
...@@ -154,7 +154,7 @@ public class UDFUtil { ...@@ -154,7 +154,7 @@ public class UDFUtil {
} }
} }
public static String[] initJavaUDF(List<UDF> udf, GatewayType gatewayType, Integer missionId) { public static String[] initJavaUDF(List<UDF> udf, Integer missionId) {
return FunctionFactory.initUDF( return FunctionFactory.initUDF(
CollUtil.newArrayList( CollUtil.newArrayList(
CollUtil.filterNew(udf, x -> x.getFunctionLanguage() != FunctionLanguage.PYTHON)), CollUtil.filterNew(udf, x -> x.getFunctionLanguage() != FunctionLanguage.PYTHON)),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment