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/06/27 14:54:54 UTC

[GitHub] [pulsar] freeznet commented on a change in pull request #11112: pulsar-function:Preload and release of external resources

freeznet commented on a change in pull request #11112:
URL: https://github.com/apache/pulsar/pull/11112#discussion_r659331813



##########
File path: pulsar-functions/api-java/src/main/java/org/apache/pulsar/functions/api/Hook.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 hook
+ * Preload and release of external resources
+ */
+@InterfaceAudience.Public
+public interface Hook {
+
+    /**
+     * Pre-Process Function Hook
+     *
+     * @throws Exception
+     */
+    void preProcess(Context context) throws RuntimeException;

Review comment:
       `preProcess` and `postProcess` is likely to be invoked with each message each time before and after `process`, so the naming is kind confuse from my understanding. 

##########
File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstance.java
##########
@@ -74,6 +75,24 @@ public JavaInstance(ContextImpl contextImpl, Object userClassObject, InstanceCon
         }
     }
 
+    public void setup() {
+        if (null != function && function instanceof Hook) {
+            try {
+                ((Hook) function).preProcess(context);
+            } catch (RuntimeException e) {
+                log.error("setup error:", e);
+                throw new RuntimeException("function preProcess occurred exception", e);
+            }
+        }
+        if (null != javaUtilFunction && javaUtilFunction instanceof Hook) {

Review comment:
       according to the [docs](http://pulsar.apache.org/docs/en/next/functions-package/#java), `java.util.function.Function` is not intend to interact with `Context`, so we should avoid to passing `Context` to `java.util.function.Function`.

##########
File path: pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/JavaInstance.java
##########
@@ -160,7 +179,22 @@ private void processAsyncResults(BiConsumer<Record, JavaExecutionResult> resultC
     @Override
     public void close() {
         context.close();
-        executor.shutdown();

Review comment:
       why remove this line?




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