安排在分片Akka集群中广播消息

huangapple go评论86阅读模式
英文:

Scheduling a broadcast message in a sharded Akka Cluster

问题

我正在尝试找出在分片集群中触发特定类型的所有 Actor 的最佳方法,基于时间计划(例如在上午8点、9点等 - 类似于 cron 的方式)。

我的计划是在集群中拥有一个单独的“定时器” Actor,在预定时间向集群中的其他 Actor 发送广播消息。然而,我不确定这是否可行和最优。Akka 调度器 不提供类似 cron 的配置。akka-quartz-scheduler 似乎不适用于 Akka 集群。

是否有可能从分片的 Akka 集群内部触发基于计划的操作,也许可以利用其他框架(如 Spring 调度)的能力?还是更好地在分片的 Akka 集群外部部署调度服务,并使用它定期向 Akka 集群发送触发事件?

另外,在分片的 Akka 集群中是否有可能向特定类型的所有 Actor 进行广播消息?

英文:

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?

答案1

得分: 1

> 在分片的 Akka 集群内部触发基于计划的动作是否可能?

总体上是可以的。但这取决于计划事件的精度和计划类型。让我们假设一个简单的时间表计划,例如每天在 08:00,不需要高精度。可以创建一个带有 TimerScheduler 的 actor,我们将这个 actor 命名为 TimerSchedulerActor。给定一个特定的时间计划,比如每天在 08:00,TimerSchedulerActor 计算下一次需要触发警报的时间,比如 2020 年 9 月 1 日 08:00,然后 TimerSchedulerActor 计算从当前时间 java.lang.System.currentTimeMillis 到 2020 年 9 月 1 日 08:00 需要等待的持续时间。当 TimerScheduler 触发时,TimerSchedulerActor 发送消息并计算此计划的下一个警报时间戳。

如果一个 TimerSchedulerActor 负责所有消息,必须确保只有一个正在运行的 TimerSchedulerActor(单例 actor),因为多个 TimerSchedulerActor 会为每个计划事件发送多条消息。您还可以拆分不同的 TimerSchedulerActor 来通知不同组的 Actors 或负责不同的事件。

> 还是部署在分片的 Akka 集群之外的调度服务更好,然后使用它向 Akka 集群发送周期性事件?

在 Akka 集群内部部署调度服务可能更易于维护、部署和调试。然而,答案取决于作者的技能和经验。熟悉 Akka 外部调度系统(例如 cron)的人可能可以通过在 Akka 之外思考来找到更快的解决方案。我会倡导在 Akka 集群内部寻找解决方案,因为它提供了更大的灵活性。例如,如果需要从 Java 集群内部更改 cron(或另一个外部系统),复杂性会增加。

> 此外,在分片的 Akka 集群中是否可以向某种特定类型的所有 actor 广播消息?

这取决于 actor 的类型。可以使用 Cluster Receptionist 来查找特定的 actors。如果您控制消息并且可以在消息和 actors 内添加所需的 actor 类型,一个好的策略可能是将消息发送给此类型的所有潜在 actors,让它们根据其类型对消息作出反应。正确类型的 actors 可以对消息作出响应,而另一种类型的 actors 可以忽略它。

英文:

> 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.

答案2

得分: 0

你可以选择:

  • 暴露一个 HTTP 端点,以触发某些任务,然后由 Airflow 或者仅使用定时任务(cron job)来访问该端点。
  • 使用 Quartz,可以选择 akka-quartz-scheduler 或者纯 Quartz。我认为它很适合 akka 集群,不会有问题。你认为会有什么障碍?一个可行的做法是在集群单例(cluster singleton) actor 内运行,然后在其中基于 Quartz 运行基于计划的动作。
英文:

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.

huangapple
  • 本文由 发表于 2020年8月20日 14:38:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63499561.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定