Akka 使用系列之四: Future

开发 开发工具
这篇文章介绍 Akka 的同步机制,以及 Spark 和 Akka 的恩怨情仇。

这篇文章介绍 Akka 的同步机制,以及 Spark 和 Akka 的恩怨情仇。

Akka

1. Akka 中的 Future

Akka 中的 Actor 发送和接收消息默认都是异步的。为了说明异步性,我们实行下面的数学老师和历史老师的 Actor:

  1. class MathTeacherActor extends Actor with ActorLogging { 
  2.     def receive = { 
  3.         case "1+1等于多少?"           => { 
  4.         Thread.sleep(1) 
  5.         sender ! "1+1等于2" 
  6.         } 
  7.     } 
  8. class HistoryTeacherActor extends Actor with ActorLogging { 
  9.     def receive = { 
  10.         case "历史上规模***的众筹行动是什么?" => { 
  11.             Thread.sleep(1) 
  12.             sender ! "历史上规模***的众筹行动是 +1s" 
  13.         } 
  14.     } 

如果我们在询问历史老师之后访问答案(如下面代码所示),我们发现并不能获取正确答案。原因就在于 Akka 是异步非阻塞的。

  1. val res = historyteacher ? "历史上规模***的众筹行动是什么?" 
  2. println(res) 

实质上, historyteacher ? "历史上规模***的众筹行动是什么?" 返回的根本不是答案,而是一个 Future。在Akka中, 一个Future是用来获取某个并发操作的结果的数据结构。有了 Future,我们可以以同步(阻塞)或异步(非阻塞)的方式访问结果。下面是简单地以同步(阻塞)方式访问结果的示例。

  1. class StudentActor(mathteacher:ActorRef,historyteacher:ActorRef) 
  2.  extends Actor with ActorLogging{ 
  3.   def receive = { 
  4.     case res:String => { 
  5.         val future1 = historyteacher ? "历史上规模***的众筹行动是什么?" 
  6.         val future2 = mathteacher ? "1+1等于多少?" 
  7.         val res1    = Await.result(future1,10 second) 
  8.         val res2    = Await.result(future2,10 second) 
  9.         println(res1) 
  10.         println(res2) 
  11.     } 
  12.  } 

2. Akka 和 Spark

Spark 一开始使用 Akka 作为内部通信部件。在 Spark 1.3 年代,为了解决大块数据(如Shuffle)的传输问题,Spark引入了Netty通信框架。到了 Spark 1.6, Spark 可以配置使用 Akka 或者 Netty 了,这意味着 Netty 可以完全替代 Akka 了。再到 Spark 2, Spark 已经完全抛弃 Akka 了,全部使用 Netty 了。Sad。

Netty

为什么 Spark 无情地有步骤有预谋地抛弃 Akka 呢?Spark 官方倒是给了一个说法:https://issues.apache.org/jira/browse/SPARK-5293。

A lot of Spark user applications are using (or want to use) Akka. Akka as a whole can contribute great architectural simplicity and uniformity. However, because Spark depends on Akka, it is not possible for users to rely on different versions, and we have received many requests in the past asking for help about this specific issue. For example, Spark Streaming might be used as the receiver of Akka messages - but our dependency on Akka requires the upstream Akka actors to also use the identical version of Akka.

Since our usage of Akka is limited (mainly for RPC and single-threaded event loop), we can replace it with alternative RPC implementations and a common event loop in Spark.

大意就是很多 Spark 用户希望同时使用 Spark 和 Akka ,但他们必须使用 Spark 依赖的那个版本的 Akka。Spark 主要用了 Akka 的 RPC 和 单线程 event-loop,因此 Spark 没有必要依赖完全的 Akka。最终,对 Akka 心心念念的 Spark 用 netty 实现下简易版本的 Akka。真爱啊。

3. 总结

到这里,Akka 使用系列就结束了。这个系列简单地过了一下 Akka 的基础知识,介绍其梗概。

【本文为51CTO专栏作者“李立”的原创稿件,转载请通过51CTO获取联系和授权】

戳这里,看该作者更多好文

责任编辑:赵宁宁 来源: 51CTO专栏
相关推荐

2017-06-01 22:59:45

Akka层次结构Actors

2018-04-24 15:08:40

2023-11-06 08:35:23

VTK可视化开源软件库

2011-11-14 10:10:56

虚拟化vmwareVMware View

2022-06-02 11:12:10

CallableFuture

2009-09-22 13:10:22

ibmdwSOA

2009-06-30 14:52:55

APC

2023-11-15 08:28:13

PythonVTK

2021-03-16 14:45:39

Zabbix 5.2Grafana监控

2021-07-19 07:31:08

服务调用Dubbo

2009-07-07 10:08:49

Future Resp

2009-07-08 13:19:25

Future Resp

2009-09-22 09:02:10

职业规划IT职业发展

2016-12-29 11:01:03

LinuxLXD 2.0资源控制

2021-11-09 09:57:46

Webpack 前端分包优化

2022-08-30 08:43:11

Spring权限控制

2023-11-24 16:13:05

C++编程

2020-12-29 10:16:24

接口测试flaskmock

2021-04-13 09:15:16

C++插件Nodejs

2011-04-29 10:58:11

SimpleFrame
点赞
收藏

51CTO技术栈公众号