用于处理在接收异步操作模式阿卡 [英] Akka pattern for handling asynchronous actions in receive

查看:142
本文介绍了用于处理在接收异步操作模式阿卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接收指标数据点,并定期汇总,并将它们保存到磁盘上的演员。后者的操作执行I / O,所以我不希望使用阻塞操作。但是,如果我把它切换到异步的,我怎么prevent其他数据点从聚合不阻塞的地方完成之前被接收。

I have an Actor that receives metrics datapoints and periodically aggregates and persists them to disk. This latter operation does I/O, so i don't want to use a blocking operation. But if i switch it to asynchronous, how do i prevent other datapoints from being received before the aggregation is completed without blocking somewhere.

我见过的一种模式是使用藏匿,是这样的:

One pattern I've seen is to use Stash, something like this:

class Aggregator extends Actor with Stash {
  def receive = processing

  def processing: Receive = {
    case "aggregate" => {
      context.become(aggregating)
      aggregate().onComplete {
        case Success => self ! "aggregated"
        case Failure => self ! "aggregated"
      }
    }
    case msg => ??? // Process task
  }

  def aggregating: Receive = {
    case "aggregated" =>
      unstashAll()
      context.become(processing)
    case msg =>
      stash()
  }
}

的担忧我有,这是我的骨料动作的完成仅仅是一个信息任何人都可以发送。据我了解,我无法从我的未来的完成内施行的不恰当的。

The misgiving i have with this is that the completion of my aggregate action is simply a message anyone could send. As i understand, i cannot effect the "unbecoming" from within my Future's completion.

作为一个方面说明,我一直无法确定是否像的onComplete 完井莫名其妙地由同一调度执行的接收,因为如果他们没有,落成将打破单线程保护,演员,否则提供的。

As a side note, I have not been able to determine whether completions like onComplete are somehow executed by the same dispatcher as receive, since if they are not, completions would break the single-threaded protections that actors otherwise offer.

还是有完成的不同步和即时内行动收到更好的模式同时保证我的状态不能改变的,直到我完成?看来这种情况下随时演员状态涉及任何形式(如DB)的I / O并明确你希望避免同步I / O如果你能。

Or is there a better pattern for completing actions that are not synchronous and immediate inside of receive while guaranteeing that my state cannot be altered until i complete? It seems this scenario anytime actor state deals with I/O of any kind (like a DB) and clearly you would want to avoid synchronous I/O if you can.

推荐答案

您汇集的演员目前正在做两件事情:汇集和存储。你既可以解决你的问题,并通过分割这两个任务简化您的系统。该单责任原则也适用于演员。

Your aggregator actor is currently doing two things: aggregating and storing. You can both solve your issue and simplify your system by splitting these two tasks. The single-responsibility-principle also applies to actors.

我想创建写作和保持聚集数据的邮件类敬业的演员。这位演员子系统应该是这样的:

I'd create a dedicated actor for writing and a message class for holding the aggregated data. This actor sub-system should look like this:

理想情况下,它需要写入磁盘的时间比聚合间隔短,使得系统保持稳定。在尖峰的情况下,数据存储演员的队列意愿服务器作为缓冲器的消息被写入到存储

Ideally, the time it takes to write to disk is shorter than the aggregation interval, such that your system remains stable. In case of spikes, the DataStore actor's queue will server as buffer for messages to be written to storage.

根据您的应用程序,您可能需要实现某种形式的ACK和放大器;万一重要确保聚集的数据已被写入

Depending on your application, you might need to implement some form of ack & retries in case you want to ensure that aggregated data has been written.

这篇关于用于处理在接收异步操作模式阿卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆