You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2022/01/24 22:26:31 UTC

[GitHub] [pulsar] nlu90 commented on a change in pull request #13205: [PIP 86][Function] add funtion api to support preload and release external resources

nlu90 commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r791200163



##########
File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstance.java
##########
@@ -155,10 +155,24 @@ private void processAsyncResults(JavaInstanceRunnable.AsyncResultConsumer result
 
     }
 
+    public void initialize() throws Exception {
+        if (function != null) {
+            function.initialize(context);
+        }
+    }
+
     @Override
     public void close() {
         context.close();
         executor.shutdown();
+
+        if (function != null) {

Review comment:
       I think we should first close the function resources before close context and executor.

##########
File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceRunnable.java
##########
@@ -453,7 +458,9 @@ synchronized public void close() {
         }
 
         if (null != javaInstance) {
+            Thread.currentThread().setContextClassLoader(functionClassLoader);
             javaInstance.close();
+            Thread.currentThread().setContextClassLoader(instanceClassLoader);

Review comment:
       need `try ... finally ...`  to make sure the classloader is reset correctly

##########
File path: pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/Function.java
##########
@@ -38,4 +38,20 @@
      * @return the output
      */
     O process(I input, Context context) throws Exception;
+
+    /**
+     * Called when function instance start

Review comment:
       `Called once to initialize resources when function instance is started.`

##########
File path: pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/Function.java
##########
@@ -38,4 +38,20 @@
      * @return the output
      */
     O process(I input, Context context) throws Exception;
+
+    /**
+     * Called when function instance start
+     *
+     * @param context The Function context
+     *
+     * @throws Exception
+     */
+    default void initialize(Context context) throws Exception {}
+
+    /**
+     * Called when function instance close

Review comment:
       Called once to properly close resources when function instance is stopped.




-- 
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: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org