fsk119 commented on code in PR #22938:
URL: https://github.com/apache/flink/pull/22938#discussion_r1350236277
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/resource/ResourceManager.java:
##########
@@ -65,9 +69,14 @@ public class ResourceManager implements Closeable {
private static final String FILE_SCHEME = "file";
private final Path localResourceDir;
+ /** Resource infos for functions. */
+ private final Map<ResourceUri, ResourceCounter> functionResourceInfos;
+
protected final Map<ResourceUri, URL> resourceInfos;
protected final MutableURLClassLoader userClassLoader;
+ private final List<URLClassLoader> createdClassLoaderList;
Review Comment:
Why we need this list here? I find the only usage is here.
```
URLClassLoader classLoader =
resourceManager.createUserClassLoader(function.getFunctionResources());
try {
UserDefinedFunctionHelper.validateClass(
(Class<? extends UserDefinedFunction>)
classLoader.loadClass(function.getClassName()));
} finally {
resourceManager.closeUserClassLoader(classLoader);
}
```
If we close the created classloder immediately, can we move the management
outside?
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/resource/ResourceManager.java:
##########
@@ -124,15 +136,79 @@ public String registerFileResource(ResourceUri
resourceUri) throws IOException {
Collections.singletonList(resourceUri),
ResourceType.FILE,
false,
- url -> {});
+ url -> {},
+ false);
registerResources(stagingResources, false);
return resourceInfos.get(new
ArrayList<>(stagingResources.keySet()).get(0)).getPath();
}
+ /**
+ * Register a resource for function and add it to the function resource
infos. If the file is
+ * remote, it will be copied to a local file.
+ *
+ * @param resourceUris the resource uri for function.
+ */
+ public void registerFunctionResources(Set<ResourceUri> resourceUris)
throws IOException {
+ prepareStagingResources(
+ resourceUris,
+ ResourceType.JAR,
+ true,
+ url -> {
+ try {
+ JarUtils.checkJarFile(url);
+ } catch (IOException e) {
+ throw new ValidationException(
+ String.format("Failed to register jar resource
[%s]", url), e);
+ }
+ },
+ true);
+ }
+
+ /**
+ * Unregister the resource uri in function resources, when the reference
count of the resource
+ * is 0, the resource will be removed from the function resources.
+ *
+ * @param resourceUris the uris to unregister in function resources.
+ */
+ public void unregisterFunctionResources(List<ResourceUri> resourceUris) {
+ if (!resourceUris.isEmpty()) {
+ resourceUris.forEach(
+ uri -> {
+ ResourceCounter counter =
functionResourceInfos.get(uri);
+ if (counter != null && counter.decreaseCounter()) {
+ functionResourceInfos.remove(uri);
+ }
Review Comment:
No. We always assume table env is used by one thread at any moement.
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/resource/ResourceManager.java:
##########
@@ -80,8 +89,10 @@ public ResourceManager(ReadableConfig config,
MutableURLClassLoader userClassLoa
new Path(
config.get(TableConfigOptions.RESOURCES_DOWNLOAD_DIR),
String.format("flink-table-%s", UUID.randomUUID()));
+ this.functionResourceInfos = new HashMap<>();
this.resourceInfos = new HashMap<>();
this.userClassLoader = userClassLoader;
+ this.createdClassLoaderList = new ArrayList<>();
Review Comment:
use a better name?
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/catalog/FunctionCatalog.java:
##########
@@ -44,7 +44,11 @@
import org.apache.flink.table.resource.ResourceUri;
import org.apache.flink.util.Preconditions;
+import javax.annotation.Nullable;
Review Comment:
+1
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/catalog/FunctionCatalog.java:
##########
@@ -569,6 +581,8 @@ private Optional<ContextResolvedFunction>
resolvePreciseFunctionReference(Object
CatalogFunction potentialResult =
tempCatalogFunctions.get(normalizedIdentifier);
if (potentialResult != null) {
+ registerFunctionJarResources(
+ oi.asSummaryString(),
potentialResult.getFunctionResources());
Review Comment:
for temporary function, Function Catalog always eagerly validate function.
##########
flink-table/flink-table-api-java/src/main/java/org/apache/flink/table/catalog/FunctionCatalog.java:
##########
@@ -569,6 +581,8 @@ private Optional<ContextResolvedFunction>
resolvePreciseFunctionReference(Object
CatalogFunction potentialResult =
tempCatalogFunctions.get(normalizedIdentifier);
if (potentialResult != null) {
+ registerFunctionJarResources(
Review Comment:
Line 727 should be registerFunctionJarResources?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]