[ 
https://issues.apache.org/jira/browse/DRILL-7317?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16879861#comment-16879861
 ] 

ASF GitHub Bot commented on DRILL-7317:
---------------------------------------

vvysotskyi commented on pull request #1822: DRILL-7317: Close ClassLoaders used 
for udf jars uploading when closing FunctionImplementationRegistry
URL: https://github.com/apache/drill/pull/1822#discussion_r300867676
 
 

 ##########
 File path: 
exec/java-exec/src/main/java/org/apache/drill/exec/expr/fn/registry/FunctionRegistryHolder.java
 ##########
 @@ -376,28 +377,78 @@ private void removeAllByJar(String jarName) {
       return;
     }
 
+    boolean isClosed  = false;
     for (Map.Entry<String, Queue<String>> functionEntry : jar.entrySet()) {
       final String function = functionEntry.getKey();
       Map<String, DrillFuncHolder> functionHolders = functions.get(function);
       Queue<String> functionSignatures = functionEntry.getValue();
-      for (Map.Entry<String, DrillFuncHolder> entry : 
functionHolders.entrySet()) {
-        if (functionSignatures.contains(entry.getKey())) {
-          ClassLoader classLoader = entry.getValue().getClassLoader();
-          if (classLoader instanceof AutoCloseable) {
-            try {
-              ((AutoCloseable) classLoader).close();
-            } catch (Exception e) {
-              logger.warn("Problem during closing class loader", e);
-            }
-          }
-          break;
-        }
-      }
+      // closes class loader only one time
+      isClosed = !isClosed && closeClassLoader(function, functionSignatures);
       functionHolders.keySet().removeAll(functionSignatures);
 
       if (functionHolders.isEmpty()) {
         functions.remove(function);
       }
     }
   }
+
+  @Override
+  public void close() {
+    try (@SuppressWarnings("unused") Closeable lock = writeLock.open()) {
+      jars.forEach((jarName, jar) -> {
+        if (!LocalFunctionRegistry.BUILT_IN.equals(jarName)) {
+          for (Map.Entry<String, Queue<String>> functionEntry : 
jar.entrySet()) {
+            if (closeClassLoader(functionEntry.getKey(), 
functionEntry.getValue())) {
+              // class loader is closed, iterates to another jar
+              break;
+            }
+          }
+        }
+      });
+
+      jars.clear();
+      functions.clear();
+    }
+  }
+
+  /**
+   * Produces search of {@link DrillFuncHolder} which corresponds to specified 
{@code String functionName}
+   * with signature from {@code Queue<String> functionSignatures},
+   * closes its class loader if {@link DrillFuncHolder} is found and returns 
true. Otherwise faalse is returned.
+   *
+   * @param functionName       name of the function
+   * @param functionSignatures function signatures
+   * @return {@code true} if {@link DrillFuncHolder} is found and its class 
loader is closed
+   */
+  private boolean closeClassLoader(String functionName, Queue<String> 
functionSignatures) {
+    DrillFuncHolder drillFuncHolder = getDrillFuncHolder(functionName, 
functionSignatures);
+    if (drillFuncHolder != null) {
+      ClassLoader classLoader = drillFuncHolder.getClassLoader();
+      try {
+        ((AutoCloseable) classLoader).close();
+      } catch (Exception e) {
+        logger.warn("Problem during closing class loader", e);
+      }
+      return true;
+    }
+    return false;
+  }
+
+  /**
+   * Produces search of {@link DrillFuncHolder} which corresponds to specified 
{@code String functionName}
+   * with signature from {@code Queue<String> functionSignatures} and returns 
first found instance.
+   *
+   * @param functionName       name of the function
+   * @param functionSignatures function signatures
+   * @return returns first found {@link DrillFuncHolder} instance
+   */
+  private DrillFuncHolder getDrillFuncHolder(String functionName, 
Queue<String> functionSignatures) {
+    Map<String, DrillFuncHolder> functionHolders = functions.get(functionName);
+    Iterator<String> signaturesIterator = functionSignatures.iterator();
+    DrillFuncHolder drillFuncHolder = null;
+    while (signaturesIterator.hasNext() && drillFuncHolder == null) {
 
 Review comment:
   Thanks, it looks better with `findAny`.
 
----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


> Close ClassLoaders used for udf jars uploading when closing 
> FunctionImplementationRegistry
> ------------------------------------------------------------------------------------------
>
>                 Key: DRILL-7317
>                 URL: https://issues.apache.org/jira/browse/DRILL-7317
>             Project: Apache Drill
>          Issue Type: Task
>            Reporter: Volodymyr Vysotskyi
>            Assignee: Volodymyr Vysotskyi
>            Priority: Major
>             Fix For: 1.17.0
>
>
> For the case when {{FunctionImplementationRegistry}} is closed, class loaders 
> which were used for uploading jars are still open and don't close file 
> descriptors for these jars.
> The proposal is to unregister jars to close its {{ClassLoader}} when 
> {{FunctionImplementationRegistry}} is closed.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to