简体   繁体   English

如何向闲置的akka​​演员发送消息?

[英]How do I send messages to idle akka actors?

I have an actor called a TaskRunner. 我有一个叫做TaskRunner的演员。 The tasks can take up to 1 minute to run. 这些任务最多可能需要1分钟才能运行。 Because of the library I use there can only be one actor per jvm/node. 由于我使用的库,每个jvm /节点只能有一个actor。 I have 1000 of these nodes across various machines. 我在各种机器上有1000个这样的节点。

I would like to distribute tasks to these nodes using various rules but the most important one is: 我想使用各种规则将任务分配给这些节点,但最重要的是:

  • Never queue tasks in a TaskRunner node's mailbox, always wait until a TaskRunner is free before sending it a task 永远不要在TaskRunner节点的邮箱中排队任务,在发送任务之前总是等到TaskRunner空闲

The way I was thinking of doing this is have an actor on another node (lets call this the Scheduler actor) listen to registrations from the TaskRunner nodes and keep an internal state of what has been sent to where. 我想这样做的方法是在另一个节点上有一个actor(让我们称之为Scheduler actor)从TaskRunner节点监听注册并保持已发送到哪里的内部状态。

Presumably if I did this I could only ever have one instance of this Scheduler actor because if there was more than one they wouldn't know which TaskRunner nodes were currently busy and so we would get tasks in the queue. 据推测,如果我这样做,我只能有一个这个Scheduler actor的实例,因为如果有多个,他们不知道哪个TaskRunner节点当前正忙,所以我们会得到队列中的任务。

Does this mean I should be using a Cluster Singleton for the Scheduler actor? 这是否意味着我应该使用Cluster Singleton作为Scheduler actor?

Is there a better way to achieve my goal? 有没有更好的方法来实现我的目标?

I would say you need: 我会说你需要:

  • dispatcher actor (cluster singleton) who send task to actor from pool of idle actors 调度员演员(集群单身人士)从空闲演员的游泳池向演员发送任务

  • your TaskRunner actor should have two states: running, and idle. 你的TaskRunner actor应该有两种状态:running和idle。 In idle state it should register itself regularly to dispatcher actor (notifying that it is idle). 在空闲状态下,它应该定期向调度员演员注册(通知它是空闲的)。 Regularly, because of possible state losing by dispatcher in case of node shutdown and move singleton to another node. 通常,因为在节点关闭的情况下调度程序可能会丢失状态并将单例移动到另一个节点。

  • dispatcher itself keep list of idle actors. 调度员本身保留闲置演员列表。 When new task need to be done and list is not empty, worker is taken from the list and task is sent (worker could be removed from list immediately, but safe to work with Ack to be sure that task is taken for processing, or re-send to another worker if Ack is timed out) 当需要完成新任务且列表不为空时,将从列表中取出工作人员并发送任务(工作人员可以立即从列表中删除,但可以安全地与Ack一起工作以确保执行任务以进行处理,或者重新执行 - 如果Ack超时则发送给另一名工人)

Given your requirement, rather than building everything from scratch, you might want to consider adapting Lightbend's distributed-workers template which employs a pull model. 根据您的要求,您可能需要考虑采用Lightbend的采用pull模型的分布式工作者模板 ,而不是从头开始构建所有内容。 It primarily consists of 1) a master cluster singleton that maintains state of workers, and, 2) an actor system of workers which register and pull work from the master singleton actor. 它主要包括1)维护工人状态的主集群单例,以及2)从主单例演员注册和拉取工作的工人的演员系统。

I adapted a repurposed version of the template for a R&D project in the past and it delivered the work-pulling functionality as advertised. 我过去为研发项目调整了模板的改变用途版本,它提供了宣传的工作拉动功能。 Note that the template uses the retired Activator (which can be easily detached or replaced with sbt from the main code). 请注意,该模板使用退役的Activator (可以很容易地从主代码中sbt或替换为sbt )。 It also does distributed pub-sub and persistence journal, which you can elect to exclude if not needed. 它还提供分布式pub-sub和持久性日志,如果不需要,您可以选择排除日志。 Its source code is available at GitHub. 它的源代码可以在GitHub上找到。

Reffering to your approach of singleton master and multiple workers,there can be a situation where your master is over loaded with task to schedule, which may result in more time to schedule the task to the workers. 对于您对单身主人和多个工作人员的处理方式,可能存在这样的情况:您的主人员负担过多的任务来安排,这可能导致更多时间将任务安排给工人。

So Instead of making master as Cluster singleton , you can have multiple masters having subset of workers assigned to them. 因此,您可以让多个主服务器分配给他们的工作人员子集,而不是将主服务器设置为集群单例 The distribution of work to different master can be done through cluster sharding based on sharding key. 可以通过基于分片密钥的聚类分片来完成对不同主设备的工作分配。 Akka provides cluster sharding , you can refer that. Akka提供了群集分片 ,你可以参考。

And for making your master fault tolerant, you can always have the persistent actors as masters. 为了使你的主要容错,你总是可以将持久的角色作为主人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM