Akka Stream文档翻译:Motivation

想搞下Akka HTTP,用于用于我们的开源项目football,发现Akka HTTP的例子里大量用到Akka Stream,而且Akka HTTP是构建Akka Stream之上的。

所以还是先看下Akka Stream吧,先了解下这个Akka Stream这个项目的动机。


动机

Motivation

The way we consume services from the internet today includes many instances of streaming data, both down- loading from a service as well as uploading to it or peer-to-peer data transfers. Regarding data as a stream of elements instead of in its entirety is very useful because it matches the way computers send and receive them (for example via TCP), but it is often also a necessity because data sets frequently become too large to be handled as a whole. We spread computations or analyses over large clusters and call it “big data”, where the whole principle of processing them is by feeding those data sequentially—as a stream—through some CPUs.

现如今我们从因特网上获取服务的方式包括了很多流式的数据,比如下载、上传或是p2p(peer to peer)的数据传输。把数据视为元素(译注:即整体的组成部分)的流而不是整体可以更有用,因为它符合计算机实际上处理它的方式(例如,通过TCP),但是经常这也是必需的,因为数据集经常变得太大而不能当作整体处理。我们把计算和分析分布到一个大集群中,称之为“大数据”,它的处理原则就是把数据顺序地(作为流)提供给一些CPU

Actors can be seen as dealing with streams as well: they send and receive series of messages in order to transfer knowledge (or data) from one place to another. We have found it tedious and error-prone to implement all the proper measures in order to achieve stable streaming between actors, since in addition to sending and receiving we also need to take care to not overflow any buffers or mailboxes in the process. Another pitfall is that Actor messages can be lost and must be retransmitted in that case lest the stream have holes on the receiving side. When dealing with streams of elements of a fixed given type, Actors also do not currently offer good static guarantees that no wiring errors are made: type-safety could be improved in this case.

也可以认为actor处理的也是流:它们接收消息、发送消息,来把知识(或者数据)从一个地方传送到另一个地方。我们发现想要使用恰当的实现手段来在actor之间构造一个稳定的流非常繁杂、容易出错,因为在接收和发送之外,我们还得确保不会使得缓冲区和mailbox(actor的mailbox)溢出。另一个陷阱是,Actor的消息可能会丢失,因此需要进行重传,以免在流的接收端出现漏洞。有的流的元素是一个给定的类型,在处理这种情况是,actor并不能提供很好的静态保证(译注:指编译器的类型检查)来确保不变有交织时的错误(译注:应该是指消息流的编织),在这种情况下类型安全可以改进。

(译注:这一段说明了设计Akka Stream的动机:

  1. 确保stream经过的各处的缓冲不会溢出(可以认为mailbox是actor的消息缓冲)

  2. 保证消息传递的可靠性,提供高于at-least-once的消息传递语义。

  3. 在处理元素类型给定的流时,提供类型安全。

  这些问题在构造一个actor系统时,是非常核心的问题。特别是前两个,自己构造actor系统时的确得采用很繁琐的手段才能实现。actor系统存在的问题,可以参考下这篇文章

  Why I Don‘t Like Akka Actors

 )

For these reasons we decided to bundle up a solution to these problems as an Akka Streams API. The purpose is to offer an intuitive and safe way to formulate stream processing setups such that we can then execute them efficiently and with bounded resource usage—no more OutOfMemoryErrors. In order to achieve this our streams need to be able to limit the buffering that they employ, they need to be able to slow down producers if the consumers cannot keep up. This feature is called back-pressure and is at the core of the Reactive Streams initiative of which Akka is a founding member. For you this means that the hard problem of propagating and reacting to back-pressure has been incorporated in the design of Akka Streams already, so you have one less thing to worry about; it also means that Akka Streams interoperate seamlessly with all other Reactive Streams implementations (where Reactive Streams interfaces define the interoperability SPI while implementations like Akka Streams offer a nice user API).

由于这些原因,我们想要把解决方案打包进Akka Stream API里。目的是提供一个直观和安全的方式来设计流处理过程,使得我们可以高效地执行它们,而且使用有界的资源消耗——不再有OutOfMemoryErrors。为了实现这点,我们的流需要能够限制它们采用的缓冲大小,在消费者跟不上生产者时,我们需要能使用生产者慢下来。这个特性称为后向压力(back-pressure),它是Reactive Streams的核心提议,而Akka是Reactive Streams的创始成员。对你而言这意味着传递和应对back-pressure的问题已经被纳入了Akka Stream的设计,所以你的担心可以少一个了;这也意味着Akka Streams可以无缝地与其它Reactive Streams的实现(Reactive Streams接口定义了互操作的Service Provider Interface,而像Akka这样的实现提供了一个很好地用户API)互操作。



Reactive Streams好像很NB的样子, 在github上已经有五百多个星了。

时间: 2024-08-12 16:20:49

Akka Stream文档翻译:Motivation的相关文章

Akka Stream文档翻译:Quick Start Guide: Reactive Tweets

Quick Start Guide: Reactive Tweets 快速入门指南: Reactive Tweets (reactive tweets 大概可以理解为“响应式推文”,在此可以测试下GFW是否还在正常工作 Twitter) A typical use case for stream processing is consuming a live stream of data that we want to extract or aggregate some other data fr

Akka Stream之Graph

最近在项目中需要实现图的一些操作,因此,初步考虑使用Akka Stream的Graph实现.从而学习了下: 一.介绍 我们知道在Akka Stream中有三种简单的线性数据流操作:Source/Flow/Sink.但是当我们需要使用一些复杂的操作,例如扇入和扇出时,可能就需要使用图相关的流操作了.因此,我们可以这样认为,Akka Stream的Graph是一种运算方案,他可能是简单的线性数据流,也可以由基础的流图组合而成的复杂的数据流程.因为Graph只是对数据流运算的简单描述,所以它是可以重复

Akka(21): Stream:实时操控:人为中断-KillSwitch

akka-stream是多线程non-blocking模式的,一般来说,运算任务提交到另外线程后这个线程就会在当前程序控制之外自由运行了.任何时候如果需要终止运行中的数据流就必须采用一种任务柄(handler)方式来控制在其它线程内运行的任务.这个handler可以在提交运算任务时获取.akka-stream提供了KillSwitch trait来支持这项功能: /** * A [[KillSwitch]] allows completion of [[Graph]]s from the out

Akka(22): Stream:实时操控:动态管道连接-MergeHub,BroadcastHub and PartitionHub

在现实中我们会经常遇到这样的场景:有一个固定的数据源Source,我们希望按照程序运行状态来接驳任意数量的下游接收方subscriber.又或者我需要在程序运行时(runtime)把多个数据流向某个固定的数据流终端Sink推送.这就涉及到动态连接合并型Merge或扩散型Broadcast的数据流连接点junction.从akka-stream的技术文档得知:一对多,多对一或多对多类型的复杂数据流组件必须用GraphDSL来设计,产生Graph类型结果.前面我们提到过:Graph就是一种运算预案,

Akka(26): Stream:异常处理-Exception handling

akka-stream是基于Actor模式的,所以也继承了Actor模式的“坚韧性(resilient)”特点,在任何异常情况下都有某种整体统一的异常处理策略和具体实施方式.在akka-stream的官方文件中都有详细的说明和示范例子.我们在这篇讨论里也没有什么更好的想法和范例,也只能略做一些字面翻译和分析理解的事了.下面列出了akka-stream处理异常的一些实用方法: 1.recover:这是一个函数,发出数据流最后一个元素然后根据上游发生的异常终止当前数据流 2.recoverWithR

Akka(24): Stream:从外部系统控制数据流-control live stream from external system

在数据流应用的现实场景中常常会遇到与外界系统对接的需求.这些外部系统可能是Actor系统又或者是一些其它类型的系统.与这些外界系统对接的意思是在另一个线程中运行的数据流可以接收外部系统推送的事件及做出行为改变的响应. 如果一个外界系统需要控制一个运行中数据流的功能环节GraphStage,首先必须在这个GraphStage内部构建一个控制函数,这样才能接触并更改GraphStage的内部状态.外部系统可以通过调用这个控制函数来向GraphStage发送信息,控制GraphStage行为.akka

Akka(25): Stream:对接外部系统-Integration

在现实应用中akka-stream往往需要集成其它的外部系统形成完整的应用.这些外部系统可能是akka系列系统或者其它类型的系统.所以,akka-stream必须提供一些函数和方法来实现与各种不同类型系统的信息交换.在这篇讨论里我们就介绍几种通用的信息交换方法和函数. akka-stream提供了mapAsync+ask模式可以从一个运算中的数据流向外连接某个Actor来进行数据交换.这是一种akka-stream与Actor集成的应用.说到与Actor集成,联想到如果能把akka-stream

Akka(23): Stream:自定义流构件功能-Custom defined stream processing stages

从总体上看:akka-stream是由数据源头Source,流通节点Flow和数据流终点Sink三个框架性的流构件(stream components)组成的.这其中:Source和Sink是stream的两个独立端点,而Flow处于stream Source和Sink中间可能由多个通道式的节点组成,每个节点代表某些数据流元素转化处理功能,它们的链接顺序则可能代表整体作业的流程.一个完整的数据流(可运行数据流)必须是一个闭合的数据流,即:从外表上看,数据流两头必须连接一个Source和一个Sin

Akka(17): Stream:数据流基础组件-Source,Flow,Sink简介

在大数据程序流行的今天,许多程序都面临着共同的难题:程序输入数据趋于无限大,抵达时间又不确定.一般的解决方法是采用回调函数(callback-function)来实现的,但这样的解决方案很容易造成“回调地狱(callback hell)”,即所谓的“goto-hell”:程序控制跳来跳去很难跟踪,特别是一些变量如果在回调函数中更改后产生不可预料的结果.数据流(stream)是一种解决问题的有效编程方式.Stream是一个抽象概念,能把程序数据输入过程和其它细节隐蔽起来,通过申明方式把数据处理过程