Hi, I have a flink job that uses a `processfunctio...
# troubleshooting
t
Hi, I have a flink job that uses a
processfunction
to run a quite-heavy (CPU and memory wise, not io) algorithm on each incoming event. I want to add a timeout for that process, and handle the timeout case (collect a timeout result). Only way I figured to do that is with process time timers, but it means registering a timer for each event, which is quite expensive. Is there a better way to do that? Also, the timeouts should be in 100s of ms scale
m
Why is it heavy? IO? Try to find a good key to partition the data. If it is IO bound try to use Async I/O
You should only use timers if have business logic that requires a timer. Don’t try to manage load with timers. Focus on the root cause of the “heaviness.”
t
It is a CPU and memory heavy computation (using a package similar to PyTorch). It is not io, so I don't know if I should use async function. The algorithm is optimized for a median runtime of ~50ms, we have a stream of ~6000 events/s and out of that about ~300/s are valid for the algorithm. Sometimes the algorithm is running for 1.5 seconds when running on a full-scale cluster, which is our main suspicion for the busy we have on the operator, thus the timeout we want to add.
m
The timer is meant more for waking up, reading the state associated with that key, and then either changing the keyed state or collecting an element. You might want to consider moving this logic into a separate micro-service and then using AsyncIO or Apache Flink Stateful Functions.
Its highly likely that I am misunderstanding your problem. But I am assuming that you're getting backpressure on this operator, and that its checkpoints are failing because it takes too long, right?
t
My key partition works quite well, distributing the events evenly across the taskmanagers. Separating the logic to a mirco service will decrease performance as we try to keep latency as low as possible. Seems weird to me there is no easy way to set a time limit for normal processing. I thought about splitting the stream to 2 - 1 where the algo runs, 1 where it doesn't. I think it will help the back pressure, but it will completely destroy the stream ordering
Yes this is my problem. Although the algorithm doesn't take too long (specifications added above)
m
just to be clear, you mean that ~300 events finish within ~50 ms and the rest take significantly longer?
and you want algorithm to have an internal timeout?
is there a way to determine if the event is valid for the algorithm before you feed it to the algorithm?
Maybe there is a filter you can apply beforehand.
By the way, timers are light weight in a Flink, but I think they serve a different use case.
I tend to think of timers as separate events
It sounds like you’re using timers like a separate thread might do to check progress or kill another thread
As though timer polls and then sets a “kill” keyed state variable if a certain time elapses. But then your process element must poll if it was killed. I’m imaging that you’re trying to do something like that
I hope I’m not wasting your time
In concurrency programming, you can use an “interrupt”. But this is not the same thing.
t
I meant that ~300 out of the ~6000 are valid to be fed to the algorithm, the rest must be collected as is (like with an empty algorithm result). Out of the ~300, the median runtime of the algorithm is ~50ms. I can filter out the transactions that go to the algorithm and those who can't and must be collected with an empty result
I agree that timers are not really meant for such a purpose, but I must find some alternative. I know that I can get my required behavior using Future.get() with a timeout, but I don't know how flink handles the usage of ExecutorService and Future inside a process function
m
Yeah, using a future in a framework like this is a bad idea. Let the framework handle concurrency with its API
t
How about using result future with the async function api? I'm not sure if it's legit or is it a an antipattern because it's not IO
m
I think it’s worth a shot. But I am concerned that it will introduce another problem because your issue is CPU bound
Perhaps it’s offloading the async work into a special executor service. If that’s the case, it will work, right?
I should check the implementation or source, but away from computer right now. On phone
t
Alright let me know if you find anything. Thank you for helping! :)
m
I think async programming is not just for IO bound work but also expensive computations that you don’t want blocking the main thread.
t
That's true programming-wise, it's just that flink's async is very IO-oriented and I haven't seen any example for a non-IO usage of that
👍 1
m
Agreed
It’s also might be good to break up your algorithm into smaller pieces map-reduce style , if possible