I think the piece you are missing is you cannot guarantee where the
function will run in general. It may get sent to several different task
executors, and each executor may not be on the same machine or JVM so the
code has to init once distributed at least once.

You have to think that every function/task you write may be instantiated
many times in many different places and each of them has to manage its own
resources separately. That is especially relevant when you are doing things
like database connections or HTTP calls and so on.

On Thu, Aug 4, 2022 at 10:09 AM Allen Zoo <alinz...@gmail.com> wrote:

> Thanks a lot!
>
> In our scenario, doing init in open function or  at static block is not
> good as excepted.
> 1. it is too late, we expect the init will happen in a task init stage,
> means init it even before the open was called method.
> 2. it is not reusable or not convenient for end user, we have manny
> functions(eg. RichFunctions/SourceFunctions and so on), and we need add
> init code in every function.
>
> We want the init code should at level of Task, in this way there is no
> need to repeat the initialization of whatever functions is running in task.
> I browsed the Flink's source code of version 1.15.1, and I didn't find a
> relevant interface that meet our needs at startup
> process(TaskExecutor#submitTask,Task#doRun)。
> I think Flink has no similar interface available so far , can anyone help
> to confirm?
>
> Best,
> Allen
>
> On Thu, Aug 4, 2022, 12:15 PM Lijie Wang <wangdachui9...@gmail.com> wrote:
>
>> Hi Allen,
>> From my experience, you can do your init setup by the following 2 ways:
>>
>> 1. Do your init setup in RichFunction#open method, see [1] for details.
>> 2. Do your init setup in static block, it will be executed when the class
>> is loaded.
>>
>> [1]
>> https://nightlies.apache.org/flink/flink-docs-master/docs/dev/datastream/user_defined_functions/#rich-functions
>>
>> Best,
>> Lijie
>>
>> Allen Zoo <alinz...@gmail.com> 于2022年8月2日周二 16:11写道:
>>
>>> Hi all,
>>> We went to do some init env setup before the flink task run, And we have
>>> noticed the Task Lifecycle | Apache Flink
>>> <https://nightlies.apache.org/flink/flink-docs-master/docs/internals/task_lifecycle/>
>>>  doc
>>> described, but we can't find  listener/hook/SPI interface do some custom
>>> init jobs before task  running. Does flink now have relevant interfaces ?
>>>
>>

Reply via email to