-
Notifications
You must be signed in to change notification settings - Fork 3.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PIP 86][Function] Preload and release of external resources #11112
Changes from 1 commit
f51c76a
ff26202
c5e6055
b631bfb
93c4300
e30457f
b655851
f355667
c6b2d92
c1b2c51
c7556d4
9d003df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 Exception; | ||
|
||
|
||
/** | ||
* Post-Process Function Hook | ||
* | ||
* @throws Exception | ||
*/ | ||
void postProcess() throws Exception; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.pulsar.functions.api.Function; | ||
import org.apache.pulsar.functions.api.Hook; | ||
import org.apache.pulsar.functions.api.Record; | ||
|
||
/** | ||
|
@@ -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 (Exception e) { | ||
log.error("setup error:", e); | ||
throw new RuntimeException("function preProcess occurred exception", e); | ||
} | ||
} | ||
if (null != javaUtilFunction && javaUtilFunction instanceof Hook) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. according to the docs, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have corrected it |
||
try { | ||
((Hook) javaUtilFunction).preProcess(context); | ||
} catch (Exception e) { | ||
log.error("setup error:", e); | ||
throw new RuntimeException("javaUtilFunction preProcess occurred exception", e); | ||
} | ||
} | ||
} | ||
@VisibleForTesting | ||
public JavaExecutionResult handleMessage(Record<?> record, Object input) { | ||
return handleMessage(record, input, (rec, result) -> {}, cause -> {}); | ||
|
@@ -160,6 +179,20 @@ private void processAsyncResults(BiConsumer<Record, JavaExecutionResult> resultC | |
@Override | ||
public void close() { | ||
context.close(); | ||
if (null != function && function instanceof Hook) { | ||
try { | ||
((Hook) function).postProcess(); | ||
} catch (Exception e) { | ||
log.error("function postProcess occurred exception", e); | ||
} | ||
} | ||
if (null != javaUtilFunction && javaUtilFunction instanceof Hook) { | ||
try { | ||
((Hook) javaUtilFunction).postProcess(); | ||
} catch (Exception e) { | ||
log.error("javaUtilFunction postProcess occurred exception", e); | ||
} | ||
} | ||
executor.shutdown(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why remove this line? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have corrected it |
||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest this new api extends the
Function
interface since it's strongly coupled with theFunction
api and should not be used individually in other cases.And we should keep
java.util.Function
as it is to keep its simplicity.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have modified it, please check