Spark源代码::Spark多线程::NettyRpcEnv.ask解读

{"type":"doc","content":[{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"背景","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Spark中有很多异步处理的例子,每一个地方都值得好好去审视一番,对辅助理解spark的机理以及为自己写出优雅的代码都会有很大的帮助。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"NettyRpcEnv.ask解读","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"RpcEnv作用","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"NettyRpcEnv","attrs":{}}],"attrs":{}},{"type":"text","text":"是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEnv","attrs":{}}],"attrs":{}},{"type":"text","text":"的在spark中的唯一一个实现。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEnv","attrs":{}}],"attrs":{}},{"type":"text","text":"是什么呢,可以先看一下它的class头信息","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"/**\n * An RPC environment. [[RpcEndpoint]]s need to register itself with a name to [[RpcEnv]] to\n * receives messages. Then [[RpcEnv]] will process messages sent from [[RpcEndpointRef]] or remote\n * nodes, and deliver them to corresponding [[RpcEndpoint]]s. For uncaught exceptions caught by\n * [[RpcEnv]], [[RpcEnv]] will use [[RpcCallContext.sendFailure]] to send exceptions back to the\n * sender, or logging them if no such sender or `NotSerializableException`.\n *\n * [[RpcEnv]] also provides some methods to retrieve [[RpcEndpointRef]]s given name or uri.\n */\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"就是一句话,RPC的环境。在这里,最重要的2个操作莫过于","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以去注册","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpoint","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"可以去异步获取","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpointRef","attrs":{}}],"attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"而","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpoint","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":"是什么呢,在这里不做详细赘述,其他的文章中会详细说明,简单来讲一下","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"简单回顾","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpoint","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpointRef","attrs":{}}],"attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"RpcEndpoint","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"RpcEndpoint","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"众所周知,spark内部会有","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"executor","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"driver","attrs":{}}],"attrs":{}},{"type":"text","text":"等角色,他们之间的通信都采用利用Netty,在executor或者driver上并不是只启动1个Netty的服务,针对不同的功能会有多个Netty的RPC服务开启,利用不同的端口号进行区分。服务间通信后,通的“信”被很多种逻辑单元来处理,如","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Inbox","attrs":{}}],"attrs":{}},{"type":"text","text":",如","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"EventLoop","attrs":{}}],"attrs":{}},{"type":"text","text":"等,这些都是工具级别的单元,而被抽象出来作为可插拔可扩展的大的逻辑功能模块在Spark中就叫做","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpoint","attrs":{}}],"attrs":{}},{"type":"text","text":",它是用来处理从其他","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"client","attrs":{}}],"attrs":{}},{"type":"text","text":"端发送或者","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"server","attrs":{}}],"attrs":{}},{"type":"text","text":"端返回过来的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"message","attrs":{}}],"attrs":{}},{"type":"text","text":"的模块。","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpoint","attrs":{}}],"attrs":{}},{"type":"text","text":"本身是一个trait,它可以有多种的实现","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"RpcEndpointRef","attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"RpcEndpointRef","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"spark之前的网络通信都是采用akka,改版后采用的是Netty,在akka中,如果一个两个节点间的通信是利用目的方的actorRef来进行的通信的,即AActor 希望发送消息到 BActor,需要BActorRef来发送消息。Spark的网络通信升级到Netty后,Endpoint就可以间接理解成原来的Actor,那么发送消息到另一个Actor的话,也需要","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpoint","attrs":{}}],"attrs":{}},{"type":"text","text":"的Ref,即","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":"。这个概念乍一看有点懵,试想,从A发送消息到B,能发送的前提是A先拥有了一个B的”引用“,这在普通的Http服务中貌似很不能被理解,我想访问某一台机器按说只需要知道对方的IP和Port不就OK了,现在还需要对方的一个“替身”?这是什么鬼?带着问题我们可以持续往下看即可,这里你只需要这样意识即可:","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"用来访问B machine的","attrs":{}},{"type":"codeinline","content":[{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"RpcEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"你理解成就是B machine的IP和Port的一个被包装后的实例即可","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"图解","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpoint","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpointRef","attrs":{}}],"attrs":{}}]},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"图解一下","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"A machine可以是物理机可以是虚拟机,B machine可以是和A同一台物理机、虚拟机(端口号不同),也可以是不同的(在spark中甚至于有自己发给自己的msg,后续会讲)。那么从A发送消息到B的话,使用的是B的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":",通过它发送消息到B machine","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【图1】","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"要如何访问","attrs":{}}]}]}],"attrs":{}},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/38/38d663ccc569ce30c90304b9d67a8ac4.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【图2】","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"内部的原理","attrs":{}}]}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/5b/5b3f0f1209c4560e0d3a230e7d464f04.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【图3】","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"B machine的RpcEndpointRef的实例是啥(简化版)","attrs":{}}]}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/b8/b8b41e646727257263f49f6317664c1a.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"Driver和Executor","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"k,顾名思义——问。可能是打个招呼,看看在不在,询问一下,等等。这个就是NettyRpcEnv.ask的作用所在。为了讲NettyRpcEnv.ask的作用,还需要简单的串一下一下概念和流程","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"Driver线程和Executor进程","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,需要明确两个事情,在yarn环境下","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"是在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicatioMaster","attrs":{}}],"attrs":{}},{"type":"text","text":"进程中执行的一个线程","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"严格来说,其实这个说法也不太正确,因为","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"其实是在用户的class的时候,在形成","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"sparkContext","attrs":{}}],"attrs":{}},{"type":"text","text":"上下文环境的一个产物,本身执行的其实是用户class线程,在这个线程中建立了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SparkEnv","attrs":{}}],"attrs":{}},{"type":"text","text":"以及","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEnv","attrs":{}}],"attrs":{}},{"type":"text","text":"等等,并且建立了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"的Netty的Service等等,与","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Executor","attrs":{}}],"attrs":{}},{"type":"text","text":"相互通信","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"Executor","attrs":{}}],"attrs":{}},{"type":"text","text":"则是一个个的进程,通过java命令在每一个节点上启动的","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Yarn系列以及 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicatioMaster","attrs":{}}],"attrs":{}},{"type":"text","text":"是什么这里不做赘述,其他文章中会细讲。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其次,在这里只需要了解到,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"本身是一个协调调度节点,它可以去分配任务给","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Executor","attrs":{}}],"attrs":{}},{"type":"text","text":",并且掌握着","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Executor","attrs":{}}],"attrs":{}},{"type":"text","text":"的情况,分配就是把","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"Task","attrs":{}},{"type":"text","text":"发送给","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Executor","attrs":{}}],"attrs":{}},{"type":"text","text":",掌握则指的是需要知道","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Executor","attrs":{}}],"attrs":{}},{"type":"text","text":"的运行情况等等。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【图4】","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"图解一下","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"举个栗子,1个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"和2个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Executor","attrs":{}}],"attrs":{}},{"type":"text","text":"进行交互通信,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"手握2个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Executor","attrs":{}}],"attrs":{}},{"type":"text","text":"(一个叫做E1,一个叫做E2)的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":",姑且简称为E1Ref和E2Ref,通过这2个Ref发送msg到E1节点和E2节点,这2个节点本身通过自身的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEndpoint","attrs":{}}],"attrs":{}},{"type":"text","text":"来处理msg。而E1和E2本身还要定期起的向Driver汇报自身的情况,这里叫做heartbeat心跳,那么反过来则是利用各自内部掌握的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverEpcEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":"来发送","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"heartbeat","attrs":{}},{"type":"text","text":"到","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":",而","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"利用其自己的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverRpcEndpoint","attrs":{}}],"attrs":{}},{"type":"text","text":"来处理","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"heartbeat","attrs":{}},{"type":"text","text":"的msg。所有节点的上面的组建则都在自身的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"NettyRpcEnv","attrs":{}}],"attrs":{}},{"type":"text","text":"中,也就是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEnv","attrs":{}}],"attrs":{}},{"type":"text","text":"的实现。","attrs":{}}]}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/55/55ea37fa20d2410ce9828387af481e8e.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"举例:在RpcEnv中建立一个DriverRpcEndpointRef","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"背景","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"终于要说到本篇的内容了,","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"NettyRpcEnv.ask","attrs":{}},{"type":"text","text":"的解读,需要有一个场景调用","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"NettyRpcEnv.ask","attrs":{}},{"type":"text","text":"的方法才可以,那可以在题中所述的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEnv","attrs":{}}],"attrs":{}},{"type":"text","text":"中建立一个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverRpcEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":"这个场景中描述","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"RpcEnv中为啥建立DriverRpcEndpointRef","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面的【图4】介绍了一个Driver和和Executor之间通信的过程。其实,在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicationMaster","attrs":{}}],"attrs":{}},{"type":"text","text":"中构建","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"线程的时候,有一部分的通信是需要通过","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverRpcEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":"进行的,即利用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverRpcEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":"发送msg给","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverRpcEndpoint","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverRpcEndpoint","attrs":{}}],"attrs":{}},{"type":"text","text":"做出处理并响应","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【图5】图解一下","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"ApplicationMaster","attrs":{}}],"attrs":{}},{"type":"text","text":" 中启动【","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Run","attrs":{}},{"type":"text","text":"】Driver的线程后,从Driver线程中拿到了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"NettyRpcEnv","attrs":{}}],"attrs":{}},{"type":"text","text":",","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"并且利用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"NettyRpcEnv","attrs":{}}],"attrs":{}},{"type":"text","text":"的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"setupEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":"方法【","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Get","attrs":{}},{"type":"text","text":"】到两个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverEndpointRef","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"后续通过【","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Use","attrs":{}},{"type":"text","text":"】这个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":"去访问Driver的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverEndpoint","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"有一点需要说明的是,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicationMaster","attrs":{}}],"attrs":{}},{"type":"text","text":"的节点本身也是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"的节点,其实访问","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverEndpoint","attrs":{}}],"attrs":{}},{"type":"text","text":"按说是可以直接访问的(Spark源代码中没有这样实现,还是为了隔离和封装的更好,减少耦合,今后","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"如果作为进程执行,不再","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicationMaster","attrs":{}}],"attrs":{}},{"type":"text","text":"上运行也会修改的较为简单),但是这里还是采用了Netty的Rpc访问方式","attrs":{}}]}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/a7/a750a5fceb21a2b9b2a970e0fe39d969.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"源代码","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这部分代码在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicationMaster.scala","attrs":{}}],"attrs":{}},{"type":"text","text":"中,关注方法","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"runDriver","attrs":{}}],"attrs":{}},{"type":"text","text":"即可","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"【图6】图解一下","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(I) 有一台IP是10.1.2.5的服务器,启动了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicationMaster","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(II) **a过程,**在这个节点上启动了Driver的线程,并且初始化了用户的class,并且在10.1.2.5节点上启动了一个Netty的serviec,IP和Port为10.1.2.5:13200","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(III) b**过程,**在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"ApplicationMaster","attrs":{}}],"attrs":{}},{"type":"text","text":"节点上继续调用","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"RpcEnv.setupEndpointRef","attrs":{}},{"type":"text","text":",目的是setup一个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Driver","attrs":{}}],"attrs":{}},{"type":"text","text":"的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":"到","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"RpcEnv","attrs":{}}],"attrs":{}},{"type":"text","text":"中,这个setup的过程就是去10.1.2.5:13200访问一下,如果服务通了,则构建出","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"DriverEndpointRef","attrs":{}}],"attrs":{}},{"type":"text","text":",这个“访问一下”即本文所述要用到的","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"NettyRpcEnv.ask","attrs":{}},{"type":"text","text":"的方法。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"可以看到调用顺序为","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(ApplicationMaster.scala) rpcEnv.setupEndpointRef ↓","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(NettyRpcEnv.scala) NettyRpcEnv.asyncSetupEndpointRefByURI ↓","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(NettyRpcEndpointRef.scala) NettyRpcEndpointRef.ask ↓","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"(NettyRpcEnv.scala) NettyRpcEnv.ask — — — — ↓ (经过多个步骤,中间部分省略,其他文章会讲)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"10.1.2.5:13200 的netty服务","attrs":{}}]}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/91/917d0b73f3a02a28ce25d0f6c85f8dd5.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"代码如下","attrs":{}}]}]}],"attrs":{}},{"type":"codeblock","attrs":{"lang":"scala"},"content":[{"type":"text","text":" private def runDriver(): Unit = {\n addAmIpFilter(None)\n\n /*\n 这里,调用startUserApplication方法来执行用户的class,也就是我们的jar包,\n invoke我们的main方法,从而启动了sparkContext,内部启动一系列的scheduler以及\n backend,以及taskscheduler等等等等core的内容,其他篇章会详细讲解\n */ \n userClassThread = startUserApplication()\n\n // This a bit hacky, but we need to wait until the spark.driver.port property has\n // been set by the Thread executing the user class.\n logInfo(\"Waiting for spark context initialization...\")\n val totalWaitTime = sparkConf.get(AM_MAX_WAIT_TIME)\n try {\n\n /*\n 这里,阻塞的等待SparkContext从Driver线程中返回回来\n */\n val sc = ThreadUtils.awaitResult(sparkContextPromise.future,\n Duration(totalWaitTime, TimeUnit.MILLISECONDS))\n if (sc != null) {\n rpcEnv = sc.env.rpcEnv\n\n val userConf = sc.getConf\n val host = userConf.get(\"spark.driver.host\")\n val port = userConf.get(\"spark.driver.port\").toInt\n registerAM(host, port, userConf, sc.ui.map(_.webUrl))\n\n /*\n **这里,上演了好戏,通过NettyRpcEnv的setupEndpointRef方法来获取到driverRef\n 这个里面其实是去ask一下Driver你在吗?是否存在这个Driver的服务,如果存在,则\n 返回OK,构建出Driver的Ref**\n */\n val driverRef = rpcEnv.setupEndpointRef(\n RpcAddress(host, port),\n YarnSchedulerBackend.ENDPOINT_NAME)\n createAllocator(driverRef, userConf)\n } else {\n // Sanity check; should never happen in normal operation, since sc should only be null\n // if the user app did not create a SparkContext.\n throw new IllegalStateException(\"User did not initialize spark context!\")\n }\n resumeDriver()\n userClassThread.join()\n } catch {\n case e: SparkException if e.getCause().isInstanceOf[TimeoutException] =>\n logError(\n s\"SparkContext did not initialize after waiting for $totalWaitTime ms. \" +\n \"Please check earlier log output for errors. Failing the application.\")\n finish(FinalApplicationStatus.FAILED,\n ApplicationMaster.EXIT_SC_NOT_INITED,\n \"Timed out waiting for SparkContext.\")\n } finally {\n resumeDriver()\n }\n }\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"解读NettyRpcEnv.ask","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"回顾Future","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"如何理解Future呢,从字面意思可以很好的理解,Future即未来,也是期货的意思。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"说到期货,就充满了不确定性,因为毕竟没有发生,谁也不知道未来会怎样。所以,定义一个Future就是定义了一个不在现在这个时空(线程)发生的(未来)的另一个(另一个线程的)事件,相比java的鸡肋的Future,scala的Future可谓是非常优雅且完美,搜索我的博客可以看到针对scala的Future的详细介绍。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"官方文章","attrs":{}},{"type":"text","text":":","attrs":{}},{"type":"link","attrs":{"href":"https://docs.scala-lang.org/zh-cn/overviews/core/futures.html","title":"","type":null},"content":[{"type":"text","text":"https://docs.scala-lang.org/zh-cn/overviews/core/futures.html","attrs":{}}]}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里不从源代码的角度去构建Future和Promise的认知观念,会有其他的文章再做解释","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"【图7】图解一下","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在java中定义一个线程是右侧的做法,而在左侧的scala中,利用Future则优雅了很多","attrs":{}}]}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/f1/f14ee590972a2a14c5ec970ffc9d5177.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"代码","attrs":{}}]}]}],"attrs":{}},{"type":"codeblock","attrs":{"lang":"scala"},"content":[{"type":"text","text":" import scala.concurrent.ExecutionContext.Implicits.global\n import scala.concurrent.Future\n\n /**\n * 解读Future的基础\n */\n object DocFutureTest {\n\n def apply(): Unit = {\n println(\"I am DocFutureTest\")\n }\n\n def main(args: Array[String]): Unit = {\n\n val sleeping = 3000;\n val main_thread = Thread.currentThread().getName;\n \n /*\n 定义另一个线程发生的事件\n 这个事件相当于java中的如下的代码块:\n 从整体的间接性上看,scala的更为优雅一些,直接一个Future可以包裹住左右需要处理的内容\n 后续如果需要进行异常处理的话还可以根据Success和Failture进行模式匹配\n public class JavaThreading {\n \n public static void main(String[] args) throws InterruptedException {\n new Thread(\n () -> System.out.println(\"这是一条发生在另一个叫做叫做\" + Thread.currentThread().getName() + \" 线程的故事\")\n ).start();\n \n System.out.println(Thread.currentThread().getName());\n Thread.sleep(3000);\n }\n }\n */\n var future_run = Future {\n Thread.sleep(1000)\n println(\"这是一条发生在另一个叫做叫做\" + Thread.currentThread().getName +\" 线程的故事\")\n }\n\n // 主线程休息3000ms\n // 如果不休息的话,main线程会先停止,导致上面的Future定义的thread还没有被执行到就结束了\n Thread.sleep(sleeping)\n println(s\"$main_thread 线程休息 $sleeping 毫秒\")\n\n }\n\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Future + callback(截取部分)","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"scala"},"content":[{"type":"text","text":" case class ExceptionError(error: String) extends Exception(error)\n\n def main(args: Array[String]): Unit = {\n\n val sleeping = 3000;\n val main_thread = Thread.currentThread().getName;\n\n // 定义另一个线程发生的事件\n var future_run = Future {\n Thread.sleep(1000)\n prntln(\"这是一条发生在另一个叫做叫做\" + Thread.currentThread().getName + \" 线程的故事\")\n // 如果需要onFailure的话 则释放此句\n // throw ExceptionError(\"error\") \n\n future_run onFailure {\n case t => println(\"exception \" + t.getMessage)\n }\n\n future_run onSuccess {\n case _ => println(\"success\")\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"注意点","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"定义了Future,则定义了需要执行的线程的执行体(body),那么执行也是立刻马上,类似于java定义了一个Thread,然后直接调用了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"start()","attrs":{}}],"attrs":{}},{"type":"text","text":"一样","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Future中大量运用了scala的Try[],如果出现了异常,没有做","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"onFailure","attrs":{}}],"attrs":{}},{"type":"text","text":"的处理,那么可能看不到异常被抛出来,这点和java有较大区别","attrs":{}}]}]}],"attrs":{}},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"回顾Promise","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"从皮毛简单的说完了Future,那Promise又是什么呢?其实在Future的实现中包含了Promise的实现,也就是说没有Promise,Future是无法被运行的。从字面的理解,Promise是承诺,有了Future的未来的定义,那么需要给出一个确切的承诺才可以进行,否则都是空口无凭天马行空无法兑现的大话。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"官方文章","attrs":{}},{"type":"text","text":":","attrs":{}},{"type":"link","attrs":{"href":"https://docs.scala-lang.org/zh-cn/overviews/core/futures.html","title":"","type":null},"content":[{"type":"text","text":"https://docs.scala-lang.org/zh-cn/overviews/core/futures.html","attrs":{}}]}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"说到现在,包括看完以上的Future的介绍,很多人肯定还是懵b状态,因为我刚开始接触的时候也是这样,但我喜欢的就是用最直观的图和想象来描述一个抽象的问题,二话不说,继续上图","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"【图8】","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"图解一下","attrs":{}},{"type":"text","text":":","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"Future","attrs":{}},{"type":"text","text":"与","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"Promise","attrs":{}},{"type":"text","text":"的关系","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Future的含义","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"主线人生是你的Main Thread,在spark中可能是某一个处理的Thread","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Now这个时刻,你开启了变成star之路(become star的Thread)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Now这个时刻,你开启了变帅之路(become handsome的Thread)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一旦你开启这两条路,只要你的Main Thread没有结束,那么你可以一直持续的去走完这两个“之路”,直到","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"Success","attrs":{}},{"type":"text","text":"或者","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"Failure","attrs":{}},{"type":"text","text":",这就是Future,可以理解为,开启了一条新的轨迹","attrs":{}}]}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fb/fbc41a05c0d961669c21edda3f63842d.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Promiose的含义","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当你开启了两条新的“之路”的时候,我可以在你两条路的重点给你不同的承诺","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当你success的完成了Future的时候,我promise你一个结果","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"当你failure的完成了Future的时候,我promise你另一个结果","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/32/3247dc7a89b69f7a1384012e6a833156.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"Future与Promise的对比","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Future是一条线,包含执行过程的一条线,按照Timeline要去走下去","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":2,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"Promise是一个点,一个被触发的点,想达到这个点必须又一个Future搭出一条路径才可以","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面两句话如何理解呢,你可以这么想,人生(Main Thread)是一个数轴,你如果希望按照timeline向着右侧一直前进就需要有一条连续的道路,这个“道路”就是一个Thread,也可以是Future定义出的道路,我们只能脚踏实地的通过道路走到目标终点,而不能直接跳到终点。Promise类似于一个milestone点,如果只有一个Promise,不定义出“道路”也就是不定义出一个Future(或Main Thread)的话,是无法实现这个Promise的。只定义了Promise,不去考虑直线路径(Future),无法实现,但只定义Future,不定义Promise(其实在Future中是内置了Default的Promise的)是可以直接执行Future的。如下图所示,开出了两张空头支票,没有定义具体的路线(Future 实现方式),那么这两个Promise是无法兑现的。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"需要注意一点,这张图只是画出了定义了Promise,但是如果想对象这个Promise的话,是可以通过Promise中的方法来搭建出一个Future来执行的,与Future不同的是,Future只要定义了就可以马上执行,Promise定义了的话,必须要显式的触发“搭建Future”的操作才可以。","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/77/77fcaf771d8378bc5e2ec409581448ee.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"看看Promise不执行的代码","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"这里,我们定义了一个Promise,并且“承诺”在Promise对应的future结束后调用一个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"map","attrs":{}}],"attrs":{}},{"type":"text","text":"操作打印出一句话future:....","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"但我们执行以下语句的时候会发现什么都没有执行","attrs":{}}]}]}],"attrs":{}},{"type":"codeblock","attrs":{"lang":"scala"},"content":[{"type":"text","text":" import scala.concurrent.Promise\n import scala.util.{Failure, Success}\n\n object PromiseTest {\n def main(args: Array[String]): Unit = {\n import scala.concurrent.ExecutionContext.Implicits.global\n val promise = Promise[String]\n promise.future.onComplete(v => println(\"onComplete \" + v))\n promise.future.map(str => println(\"future: \" + str + \" ==> \" + Thread.currentThread().getName))\n promise.future.failed.foreach(e => println(e + \" ==> \" + Thread.currentThread().getName))\n Thread.sleep(3000)\n }\n\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"看看可以执行的代码","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"与上面代码唯一不同的就是,加入了","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"promise.trySuccess","attrs":{}}],"attrs":{}},{"type":"text","text":"的处理","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代码细节在其他篇章我们具体看,这里你可以这样理解,加入和trySuccess,就是为达到Promise搭建了一条Future之路,并触发这条路开始执行(start)","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"至于trySuccess,tryComplete等具体的细节讲scala多线程的地方可以细说","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"promise.future.onComplete","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Future执行完毕后的callback处理,无论是","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"Success","attrs":{}},{"type":"text","text":"还是","attrs":{}},{"type":"text","marks":[{"type":"italic","attrs":{}}],"text":"Failure","attrs":{}},{"type":"text","text":"都可以执行这个","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"onComplete","attrs":{}}],"attrs":{}},{"type":"text","text":"处理","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"promise.future.map","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"promise.future.onComplete","attrs":{}}],"attrs":{}},{"type":"text","text":"之后对Future进行的继续的map处理","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"promise.trySuccess","attrs":{}}],"attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":1,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"触发整个Future执行的trigger","attrs":{}}]}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"scala"},"content":[{"type":"text","text":" import scala.concurrent.Promise\n import scala.util.{Failure, Success}\n\n object PromiseTest {\n def main(args: Array[String]): Unit = {\n import scala.concurrent.ExecutionContext.Implicits.global\n val promise = Promise[String]\n promise.future.onComplete(v => println(\"onComplete \" + v))\n promise.future.map(str => println(\"future: \" + str + \" ==> \" + Thread.currentThread().getName))\n promise.future.failed.foreach(e => println(e + \" ==> \" + Thread.currentThread().getName))\n **promise.trySuccess(\"try success \" + \" --> \" + Thread.currentThread().getName)**\n Thread.sleep(3000)\n }\n\n }\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"ask的代码","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"其实,讲完了上面的所有的内容后,ask的代码感觉几句话就可以讲解完毕了。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"ask本身返回的是Future,本身是异步处理","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"【图9】图解一下","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一台10.1.1.1的client机器通过rpc访问一台10.1.1.2的Netty的service,当response正确返回后,在client机器中的","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"TransportResponseHandler","attrs":{}}],"attrs":{}},{"type":"text","text":"中进行判断处理,并且调用listener的onSuccess方法,这个onSuccess方法则是下面的ask代码中定义的方法。在这个方法中本身又去执行了promise的tryComplete,从而触发了promise的future之路执行","attrs":{}}]}]}],"attrs":{}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/fd/fda0cbf71394fe8100dd8dd5adbc31d5.png","alt":null,"title":null,"style":[{"key":"width","value":"75%"},{"key":"bordertype","value":"none"}],"href":null,"fromPaste":true,"pastePass":true}},{"type":"bulletedlist","content":[{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}}]}],"attrs":{}},{"type":"codeblock","attrs":{"lang":"scala"},"content":[{"type":"text","text":"private[netty] def ask[T: ClassTag](message: RequestMessage, timeout: RpcTimeout): Future[T] = {\n // 定义了一个Any的promise\n val promise = Promise[Any]()\n val remoteAddr = message.receiver.address\n\n def onFailure(e: Throwable): Unit = {\n if (!promise.tryFailure(e)) {\n e match {\n case e : RpcEnvStoppedException => logDebug (s\"Ignored failure: $e\")\n case _ => logWarning(s\"Ignored failure: $e\")\n }\n }\n }\n\n /* \n 这里声明的onSuccess会被填充到RpcResponseCallback的onSuccess中,这个\n RpcResponseCallback就是上面【图9】中的listener,当我们从Server端获取到response后\n 注意,获取的不是RpcFailure类型的response,则都会进入到【图9】的\n else if (message instanceof RpcResponse) { 分支中\n\n */\n def onSuccess(reply: Any): Unit = reply match {\n case RpcFailure(e) => onFailure(e)\n case rpcReply =>\n /*\n 当返回的response是OK的没有问题后,onSuccess被callback,这里promise的trySuccess也\n 进行call操作,这里就是上面所说的,为了一个promise铺设了一条future,从而可以执行\n 这个Future的线程了\n */\n if (!promise.trySuccess(rpcReply)) {\n logWarning(s\"Ignored message: $reply\")\n }\n }\n\n try {\n if (remoteAddr == address) {\n val p = Promise[Any]()\n p.future.onComplete {\n case Success(response) => onSuccess(response)\n case Failure(e) => onFailure(e)\n }(ThreadUtils.sameThread)\n dispatcher.postLocalMessage(message, p)\n } else {\n val rpcMessage = RpcOutboxMessage(message.serialize(this),\n onFailure,\n (client, response) => **onSuccess**(deserialize[Any](client, response)))\n postToOutbox(message.receiver, rpcMessage)\n /*\n 如果是callback了Failure,则这里会被执行\n */\n promise.future.failed.foreach {\n case _: TimeoutException => rpcMessage.onTimeout()\n case _ =>\n }(ThreadUtils.sameThread)\n }\n\n val timeoutCancelable = timeoutScheduler.schedule(new Runnable {\n override def run(): Unit = {\n onFailure(new TimeoutException(s\"Cannot receive any reply from ${remoteAddr} \" +\n s\"in ${timeout.duration}\"))\n }\n }, timeout.duration.toNanos, TimeUnit.NANOSECONDS)\n\n /*\n 当promise的future执行后,会调用这里的onComplete方法\n */\n promise.future.onComplete { v =>\n timeoutCancelable.cancel(true)\n }(ThreadUtils.sameThread)\n } catch {\n case NonFatal(e) =>\n onFailure(e)\n }\n\n /*\n 利用RpcTimeout中的addMessageIfTimeout的偏函数再去模式匹配一下产生的Throwable内容\n 如果是RpcTimeoutException 则 直接throw这个ex\n 如果是TimeoutException 则包装成RpcTimeoutException后再throw出去\n */\n promise.future.mapTo[T].recover(timeout.addMessageIfTimeout)(ThreadUtils.sameThread)\n }\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":1},"content":[{"type":"text","text":"总结","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"本篇用小篇幅讲解了一下*o.a.s.rpc.netty.NettyRpcEnv.ask()*的方法,简单描述了一个spark的异步处理的小case,这个小case需要不少的先验知识点,可能突然间看到这里有点懵,学习需要融会贯通一点点的来积累才可以,如果不明白可以慢慢积累其他模块的知识再来这里看流水账会更有收获。","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章