AndCode
AndCode

Reputation: 488

Scheduling a broadcast message in a sharded Akka Cluster

I'm trying to figure out the best way to trigger all actors of a certain type in a sharded cluster, based on a time schedule (e.g. at 8am, 9am, etc - in cron-like fashion).

My plan was to have a single "timer" actor in the cluster that would send out a broadcast message to other cluster's actors on schedule. However, I'm not sure if this is workable and optimal. Akka Scheduler doesn't provide cron-like configuration. akka-quartz-scheduler doesn't seem to be fitted for Akka cluster.

Is it possible at all to trigger schedule-based actions from inside sharded Akka cluster, perhaps using some other framework's capabilities such as Spring scheduling? Or it's better to deploy a scheduling service outside of sharded Akka cluster, and use it to send periodic triggering events to Akka cluster?

Also, is it possible to broadcast a message to all actors of certain type in sharded Akka cluster?

Upvotes: 1

Views: 469

Answers (2)

Spyros K
Spyros K

Reputation: 2605

Is it possible at all to trigger schedule-based actions from inside sharded Akka cluster?

In general yes. But it depends on the precision of the scheduled events and the type of the schedule. Let's assume a simple timetable schedule, e.g. everyday at 08:00, with no interest in high precision. It is possible to create an actor with TimerScheduler, lets name this actor TimerSchedulerActor. Given a specific time schedule e.g. everyday at 08:00 TimerSchedulerActor calculates the next time that the alarm needs to go off e.g. 01.09.2020 08:00, then TimerSchedulerActor compute the duration that it needs to wait from the current time java.lang.System.currentTimeMillis to 01.09.2020 08:00. When the TimerScheduler goes off TimerSchedulerActorsends the message and calculates the next alarm timestamp for this schedule.

If one TimerSchedulerActor is responsible for all messages it should be made sure that there is only TimerSchedulerActor running (Singleton actor) as multiple TimerSchedulerActors would send multiple messages for each scheduled event. You could also split have different TimerSchedulerActors to notify different groups of Actors or to be responsible for different events.

Or it's better to deploy a scheduling service outside of sharded Akka cluster, and use it to send periodic events to Akka cluster?

It could be easier to maintain, easier to deploy and easier debug a scheduling service inside the Akka Cluster. However, the answer depends on the author skills and experience. Someone familiar with a scheduling system outside Akka, (e.g. cron) can perhaps find a faster solution by thinking outside Akka. I would advocate for a solution inside Akka Cluster as it offers more flexibility. For example, if facing a request to change cron (or another external system) from inside the java cluster the complexity would increase.

Also, is it possible to broadcast a message to all actors of certain type in sharded Akka cluster?

It depends on what the actor type is. Cluster Receptionist can be used to look up specific actors. If you are in control of the message and can add the desired actor type inside the messages and actors a good strategy could be to send the message to all potential actors of this type and let them act on the message based on their type. Actors of the correct type can act on the message and actors of another type could just ignore it.

Upvotes: 1

yiksanchan
yiksanchan

Reputation: 1940

You may either:

  • Expose an HTTP endpoint that will trigger some tasks, and let airflow or simply a cron job to hit the endpoint
  • Use quartz, either akka-quartz-scheduler or raw quartz. I think it fits akka cluster without a problem. What do you think is the blocker? A viable practice may be to run a cluster singleton actor inside which you run schedule-based actions based on quartz.

Upvotes: 0

Related Questions