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 2021/12/09 07:01:33 UTC

[GitHub] [pulsar] wangjialing218 opened a new pull request #13205: [PIP 86][Function] add funtion api to support preload and release external resources

wangjialing218 opened a new pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205


   ### Motivation
   
   This patch is based on #11112, I have talked with @wuchenxi123 and I will continue the work for this feature.
   
   ### Modifications
   
   Introduce `RichFunction` interface extends `Function`,  which provide `setup` and `tearDown` API.
   `setup` is called only once when function instance started. `tearDown` is called when function instance closed.
   User could use these interface to initialize and release their external resources such as RedisClient.
   
   ### Does this pull request potentially affect one of the following parts:
   *If `yes` was chosen, please highlight the changes*
     - The public API: ( **yes**)
   
   ### Documentation
   
   Check the box below and label this PR (if you have committer privilege).
   
   Need to update docs? 
   
   - [x] `doc-required` 
   I think we need to add chapter to tell user how to use the API in `functions-develop.md`
     
   
   
   


-- 
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



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

Posted by GitBox <gi...@apache.org>.
wangjialing218 commented on pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#issuecomment-1021766448


   /pulsarbot run-failure-checks


-- 
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



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

Posted by GitBox <gi...@apache.org>.
wangjialing218 commented on pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#issuecomment-1031030419


   > The change looks good. But in your test your are not testing that the new methods are executed. I suggest to add some static variables in the function in order to be able to check that the function runs
   
   @eolivelli I tried to add static variables in function but failed to get correct value in test code, may be caused by different class loader, could you give some suggestion?
   Anyway, in the test funtion, `process()` will throw exception if `initialize()` did not run, and test code could check whether all messages received.


-- 
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



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

Posted by GitBox <gi...@apache.org>.
wangjialing218 commented on pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#issuecomment-1020996219


   /pulsarbot run-failure-checks


-- 
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



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

Posted by GitBox <gi...@apache.org>.
niclash commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r790131383



##########
File path: pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/RichFunction.java
##########
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.functions.api;
+
+import org.apache.pulsar.common.classification.InterfaceAudience;
+
+/**
+ * An interface for richFunction
+ * Extension for Function interface
+ * Provide setup and tearDown interface to initial and close any resource used in function
+ */
+@InterfaceAudience.Public
+public interface RichFunction extends Function{

Review comment:
       @merlimat Even better!




-- 
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



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

Posted by GitBox <gi...@apache.org>.
wangjialing218 commented on pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#issuecomment-1021804815


   @nlu90 @eolivelli please have a check again


-- 
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



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

Posted by GitBox <gi...@apache.org>.
niclash commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r789429183



##########
File path: pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/RichFunction.java
##########
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.functions.api;
+
+import org.apache.pulsar.common.classification.InterfaceAudience;
+
+/**
+ * An interface for richFunction
+ * Extension for Function interface
+ * Provide setup and tearDown interface to initial and close any resource used in function
+ */
+@InterfaceAudience.Public
+public interface RichFunction extends Function{

Review comment:
       I am not happy with the API naming. `InitializableFunction` is far more precise name. And `setup()` and `teardown()` sound like unit testing. Suggestion `initialize()` and `close()`.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
wangjialing218 commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r791534258



##########
File path: pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/RichFunction.java
##########
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.functions.api;
+
+import org.apache.pulsar.common.classification.InterfaceAudience;
+
+/**
+ * An interface for richFunction
+ * Extension for Function interface
+ * Provide setup and tearDown interface to initial and close any resource used in function
+ */
+@InterfaceAudience.Public
+public interface RichFunction extends Function{

Review comment:
       moved methods to `Funtion` 




-- 
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



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

Posted by GitBox <gi...@apache.org>.
wangjialing218 commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r791533767



##########
File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceRunnable.java
##########
@@ -229,7 +229,9 @@ synchronized private void setup() throws Exception {
         setupLogHandler();
 
         javaInstance = new JavaInstance(contextImpl, object, instanceConfig);
-
+        Thread.currentThread().setContextClassLoader(functionClassLoader);
+        javaInstance.setup();
+        Thread.currentThread().setContextClassLoader(instanceClassLoader);

Review comment:
       done




-- 
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



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

Posted by GitBox <gi...@apache.org>.
wangjialing218 commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r789439536



##########
File path: pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/RichFunction.java
##########
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.functions.api;
+
+import org.apache.pulsar.common.classification.InterfaceAudience;
+
+/**
+ * An interface for richFunction
+ * Extension for Function interface
+ * Provide setup and tearDown interface to initial and close any resource used in function
+ */
+@InterfaceAudience.Public
+public interface RichFunction extends Function{

Review comment:
       Sounds good. I will change the naming.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
eolivelli commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r789111569



##########
File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceRunnable.java
##########
@@ -229,7 +229,9 @@ synchronized private void setup() throws Exception {
         setupLogHandler();
 
         javaInstance = new JavaInstance(contextImpl, object, instanceConfig);
-
+        Thread.currentThread().setContextClassLoader(functionClassLoader);
+        javaInstance.setup();
+        Thread.currentThread().setContextClassLoader(instanceClassLoader);

Review comment:
       We should do this I  a finally block 




-- 
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



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

Posted by GitBox <gi...@apache.org>.
merlimat commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r790022297



##########
File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceRunnable.java
##########
@@ -229,7 +229,9 @@ synchronized private void setup() throws Exception {
         setupLogHandler();
 
         javaInstance = new JavaInstance(contextImpl, object, instanceConfig);
-
+        Thread.currentThread().setContextClassLoader(functionClassLoader);
+        javaInstance.setup();
+        Thread.currentThread().setContextClassLoader(instanceClassLoader);

Review comment:
       We need to make to reset the class loader to the `instanceClassLoader`.

##########
File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceRunnable.java
##########
@@ -229,7 +229,9 @@ synchronized private void setup() throws Exception {
         setupLogHandler();
 
         javaInstance = new JavaInstance(contextImpl, object, instanceConfig);
-
+        Thread.currentThread().setContextClassLoader(functionClassLoader);
+        javaInstance.setup();
+        Thread.currentThread().setContextClassLoader(instanceClassLoader);

Review comment:
       We need to make sure to reset the class loader to the `instanceClassLoader`.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
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



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

Posted by GitBox <gi...@apache.org>.
wangjialing218 commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r791533767



##########
File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceRunnable.java
##########
@@ -229,7 +229,9 @@ synchronized private void setup() throws Exception {
         setupLogHandler();
 
         javaInstance = new JavaInstance(contextImpl, object, instanceConfig);
-
+        Thread.currentThread().setContextClassLoader(functionClassLoader);
+        javaInstance.setup();
+        Thread.currentThread().setContextClassLoader(instanceClassLoader);

Review comment:
       done

##########
File path: pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/RichFunction.java
##########
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.functions.api;
+
+import org.apache.pulsar.common.classification.InterfaceAudience;
+
+/**
+ * An interface for richFunction
+ * Extension for Function interface
+ * Provide setup and tearDown interface to initial and close any resource used in function
+ */
+@InterfaceAudience.Public
+public interface RichFunction extends Function{

Review comment:
       moved methods to `Funtion` 

##########
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:
       done




-- 
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



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

Posted by GitBox <gi...@apache.org>.
wangjialing218 commented on pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#issuecomment-1020996219


   /pulsarbot run-failure-checks


-- 
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



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

Posted by GitBox <gi...@apache.org>.
merlimat commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r790021904



##########
File path: pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/RichFunction.java
##########
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.functions.api;
+
+import org.apache.pulsar.common.classification.InterfaceAudience;
+
+/**
+ * An interface for richFunction
+ * Extension for Function interface
+ * Provide setup and tearDown interface to initial and close any resource used in function
+ */
+@InterfaceAudience.Public
+public interface RichFunction extends Function{

Review comment:
       Actually, I'd suggest to instead add these methods in the `org.apache.pulsar.functions.api.Function`, with default implementations that doesn't do anything.




-- 
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



[GitHub] [pulsar] wangjialing218 edited a comment on pull request #13205: [PIP 86][Function] add funtion api to support preload and release external resources

Posted by GitBox <gi...@apache.org>.
wangjialing218 edited a comment on pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#issuecomment-1031030419


   > The change looks good. But in your test your are not testing that the new methods are executed. I suggest to add some static variables in the function in order to be able to check that the function runs
   
   @eolivelli I tried to add static variables in function but failed to get correct value in test code, may be caused by different class loader, could you give some suggestion?
   Anyway, in the test function, `process()` will throw exception if `initialize()` did not run, and test code could check whether all messages received.


-- 
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



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

Posted by GitBox <gi...@apache.org>.
wangjialing218 commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r791534708



##########
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:
       done




-- 
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



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

Posted by GitBox <gi...@apache.org>.
wangjialing218 commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r789278265



##########
File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceRunnable.java
##########
@@ -229,7 +229,9 @@ synchronized private void setup() throws Exception {
         setupLogHandler();
 
         javaInstance = new JavaInstance(contextImpl, object, instanceConfig);
-
+        Thread.currentThread().setContextClassLoader(functionClassLoader);
+        javaInstance.setup();
+        Thread.currentThread().setContextClassLoader(instanceClassLoader);

Review comment:
       Sorry I did not catch this.
   If exception happened in user's setup() code,  `JavaInstanceRunnable.run()` will catch the exception and close function instance. Why we need to do this in a finally block




-- 
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



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

Posted by GitBox <gi...@apache.org>.
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



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

Posted by GitBox <gi...@apache.org>.
merlimat commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r790021904



##########
File path: pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/RichFunction.java
##########
@@ -0,0 +1,44 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pulsar.functions.api;
+
+import org.apache.pulsar.common.classification.InterfaceAudience;
+
+/**
+ * An interface for richFunction
+ * Extension for Function interface
+ * Provide setup and tearDown interface to initial and close any resource used in function
+ */
+@InterfaceAudience.Public
+public interface RichFunction extends Function{

Review comment:
       Actually, I'd suggest to instead add these methods in the `org.apache.pulsar.functions.api.Function`, with default implementation that doesn't do anything.




-- 
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



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

Posted by GitBox <gi...@apache.org>.
mattisonchao commented on a change in pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#discussion_r790056646



##########
File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstanceRunnable.java
##########
@@ -229,7 +229,9 @@ synchronized private void setup() throws Exception {
         setupLogHandler();
 
         javaInstance = new JavaInstance(contextImpl, object, instanceConfig);
-
+        Thread.currentThread().setContextClassLoader(functionClassLoader);
+        javaInstance.setup();
+        Thread.currentThread().setContextClassLoader(instanceClassLoader);

Review comment:
       > We need to make sure to reset the class loader to the `instanceClassLoader`.
   
   +1




-- 
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



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

Posted by GitBox <gi...@apache.org>.
nlu90 commented on pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205#issuecomment-1035698895


   @eolivelli Could you help take a look again when you are available?


-- 
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



[GitHub] [pulsar] codelipenghui merged pull request #13205: [PIP 86][Function] add funtion api to support preload and release external resources

Posted by GitBox <gi...@apache.org>.
codelipenghui merged pull request #13205:
URL: https://github.com/apache/pulsar/pull/13205


   


-- 
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