Speculative Execution implementation: how do you force an actor to stop the process and move on?

Hello!

I have one Dynamic Supervisor with 3 Gen Server actors (call them workers), and in parallel a Load Balancer actor. The Load Balancer actor receives a stream of data and sends in round robin fashion the data to be manipulated by the workers.

Now comes the problem. Speculative execution (at least the version I try to implement) states that given a task to 2 different actors, one of them will execute it faster and the other one should be stopped from finishing it (mid-process) and told to move on to the next task.

Solution 1: kill it with Process.exit(), the worker will be respawned and continue. But the further messages in the mailbox will be lost.

Solution 2: Part the process in several steps and log the progress on shared resource (be it a database or using Agent). When trying to move to the next one, look up if the other worker has finished and stop if so. But this adds additional computation time and not every process can be parted into several steps. Smells like overhead much.

Solution 3: Use a special function that, once the Load Balancer receives the answer from an actor, will be called so the second actor stops the message execution (if started) or never execute it (if still in queue).

Has anyone any idea of such a special function for Solution 3? I’ve spent like 2 days reading the documentation but did not come close to such. Or maybe there is a method to create such function that will force the Gen Server to at least stop mid-process and consider the new high-priority message?

Thanks!

You cannot force another process from the outside to stop what it’s doing and read a message. You can only send it a message, which will queue like any other and needs to wait for the process to check its mailbox. So the best you could do is similar to your option 2, where the worker regularly checks its mailbox to see if its supposed to stop / drop work from the queue.

1 Like

The question is why would you give the same task to be executed to 2 different processes?

There is no concept of message priority, all messages are read from the mailbox sequentially, the only way for you to achieve this is to make your own abstraction on top of GenServers.

2 Likes

I’m not sure this is the bes approach but anyways, here is an idea:

Keep you worker responsive:

  • new work items should not stay in the message queue, put them in an internal queue in your workers
  • outsource the heavy work to a Task/separate process from the worker, then the worker can just cancel/kill that process when it gets the “already computed message”.
1 Like

That was my first question too :smiley:
It’s a learning task at a Real-Time Programming university course. We are simulating a case of real time processing of streams of data and in order to achieve faster results comes the Speculative Execution solution we should implement in this particular way. The situation is that having multiple resources with performance variation and a limited time the result should be received in, send the task to multiple actors, achieve the result in the fastest time possible and tell the other ones to drop it.

Thanks! I think the approach will make the Solution 2 better than it looks like now. :))

Oh I see, the problem with your approach is that you need to guarantee that all those processes will run on a different scheduler, since each scheduler runs on a separate core, otherwise there is absolutely no gain from this approach as running them on the same core will just execute them concurrently by switching the context.

2 Likes