Spark Rpc通信源码分析

本文深入探讨了Spark1.6+中基于RPCEnv、RPCEndpoint、RPCEndpointRef的新型RPC通信架构,包括Akka和Netty两种实现方式。详细分析了RpcEnvironment、RpcEndpoint、RpcEndpointRef的功能与交互过程,揭示了它们之间的关系及工作流程。

Spark 1.6+推出了以RPCEnv、RPCEndpoint、RPCEndpointRef为核心的新型架构下的RPC通信方式。其具体实现有Akka和Netty两种方式,Akka是基于Scala的Actor的分布式消息通信系统,Netty是由JBOSS提供的一个java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。

Rpc Environment(RpcEnv)是一个RpcEndpoints用于处理消息的环境,它管理着整个RpcEndpoints的声明周期:(1)根据name或uri注册endpoints(2)管理各种消息的处理(3)停止endpoints。RpcEnv必须通过工厂类RpcEnvFactory创建。

RpcEndpoint需要注册到RpcEnv,RpcEnv处理从RpcEndpointRef或远程节点发送过来的消息,然后把响应消息给RpcEndpoint。对于Rpc捕获到的异常消息,RpcEnv将会用RpcCallContext.sendFailure将失败消息发送给发送者,或者将没有发送者、‘NotSerializableException’等记录到日志中。同时,RpcEnv也提供了根据name或uri获取RpcEndpointRef的方法。


图1 Rpc、RpcEndpoint、RpcEndpointRef三者关系

1.RpcEnv源码分析

1.根据RpcEndpoint返回RpcEndpointRef,具体实现在RpcEndpoint.self方法中,如果RpcEndpointRef不存在,将返回null

private[rpc] def endpointRef(endpoint: RpcEndpoint): RpcEndpointRef

2.根据RpcEndpoint的name注册到RpcEnv中并返回它的一个引用RpcEndpointRef

def setupEndpoint(name: String, endpoint: RpcEndpoint): RpcEndpointRef

3.获取RpcEndpointRef的方法

(1)通过url获取RpcEndpointRef

//通过url异步获取RpcEndpointRef

def asyncSetupEndpointRefByURI(uri: String): Future[RpcEndpointRef]

//通过url同步获取RpcEndpointRef,这是一个阻塞操作

def setupEndpointRefByURI(uri: String): RpcEndpointRef = {

defaultLookupTimeout.awaitResult(asyncSetupEndpointRefByURI(uri))}

(2)根据systemName、address、endpointName获取RpcEndpointRef,其实是将三者拼接为uri,根据uri获取

//异步获取

def asyncSetupEndpointRef(

systemName: String, address: RpcAddress, endpointName: String): Future[RpcEndpointRef] = {

asyncSetupEndpointRefByURI(uriOf(systemName, address, endpointName))}

//同步获取

def setupEndpointRef(

systemName: String, address: RpcAddress, endpointName: String): RpcEndpointRef = {

setupEndpointRefByURI(uriOf(systemName, address, endpointName))

}

4.根据RpcEndpointRef停止RpcEndpoint

def stop(endpoint: RpcEndpointRef): Unit

5.等待直到RpcEnv退出

def awaitTermination: Unit

6.RpcEndpointRef需要RpcEnv来反序列化,所以当反序列化RpcEndpointRefs的任何object时,应该通过该方法来操作

def deserialize[T](deserializationAction: => T): T

2.RpcEndpoint源码分析

RpcEndpoint定义了由消息触发的一些函数,`onStart`, `receive` and `onStop`的调用是顺序发生的。它的声明周期是constructor -> onStart -> receive* -> onStop。注意,`receive`能并发操作,如果你想要`receive`是线程安全的,请使用ThreadSafeRpcEndpoint,如果RpcEndpoint抛出错误,它的`onError`方法将会触发。它有51个实现子类,我们比较熟悉的是Master、Worker、ClientEndpoint等。

1.启动RpcEndpoint处理任何消息

def onStart: Unit = {}

2.停止RpcEndpoint

def onStop: Unit = {}

3.处理RpcEndpointRef.send或RpcCallContext.reply方法,如果收到不匹配的消息,将抛出SparkException

def receive: PartialFunction[Any, Unit] = {

case _ => throw new SparkException(self + " does not implement 'receive'")}

4.处理RpcEndpointRef.ask方法,如果不匹配消息,将抛出SparkException

def receiveAndReply(context: RpcCallContext): PartialFunction[Any, Unit] = {

case _ => context.sendFailure(new SparkException(self + " won't reply anything"))}

5.当处理消息发生异常时

def onError(cause: Throwable): Unit = {

throw cause}

6.当远程地址连接到当前的节点地址时触发

def onConnected(remoteAddress: RpcAddress): Unit = {

}

7.当远程地址连接断开时触发

def onDisconnected(remoteAddress: RpcAddress): Unit = {

}

8.当远程地址和当前节点的连接发生网络异常时触发

def onNetworkError(cause: Throwable, remoteAddress: RpcAddress): Unit = {

// By default, do nothing.

}

3.RpcEndpointRef源码分析

RpcEndpointRef是RpcEndpoint的一个远程引用,是线程安全的。它有两个实现子类:即AkkaRpcEndpointRef和NettyRpcEndpointRef。

1.发送单方面的异步消息

def send(message: Any): Unit

2.发送一个消息给RpcEndpoint.receiveAndReply并返回一个Future在指定的时间内接受响应,本方法值请求一次

def ask[T: ClassTag](message: Any, timeout: RpcTimeout): Future[T]

3.发送消息给RpcEndpoint并在默认的超时内得到结果,否则抛出SparkException,注意,本方法是一个阻塞操作可能消耗时间,所以不要早消息循环中调用它

def askWithRetry[T: ClassTag](message: Any): T = askWithRetry(message, defaultAskTimeout)

最后,画图说明一下两者的消息传递的过程,RpcEndpointRef作为消息的主动者,RpcEndpoint作为消息的被动者


RpcEndpoint、RpcEndpointRef

首先不要急于去启动服务。 根据经验,电脑有很大可能存在病毒。. 首先,你看看注册表HKEY_LOCAL_MACHINE\SYSTEM \CurrentControlSet\Services\RpcSs\Parameters 下,有没有ServiceDll的键值,其类型为"REG_EXPAND_SZ".如果有,在看看数值数据是不 是%SystemRoot%\system32\rpcss.dll 如果没有.那么在c:\windows\system32目录下面你也找不到"rpcss.dll"文件的.此时你就得新建此键值,并从网上或者其它电脑 上拷贝"rpcss.dll"文件放到c:\windows\system32目录下面(注意:如果RPC服务不能启动的话."粘贴"功能是不能用的,所 以在用U盘之类的拷贝RPCSS.dll文件,最好是把RPCSS.dll压缩成包.然后通过解压的方式放到system32目录).到此.重启计算机. 应该就行了. 把以下内容保存成reg文件: Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\RpcSs] "Description"="提供终结点映射程序 (endpoint mapper) 以及其它 RPC 服务。" "DisplayName"="Remote Procedure Call (RPC)" "ErrorControl"=dword:00000001 "Group"="COM Infrastructure" "ImagePath"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,\ 74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,73,\ 00,76,00,63,00,68,00,6f,00,73,00,74,00,20,00,2d,00,6b,00,20,00,72,00,70,00,\ 63,00,73,00,73,00,00,00 "ObjectName"="NT AUTHORITY\\NetworkService" "Start"=dword:00000002 "Type"=dword:00000020 "FailureActions"=hex:00,00,00,00,00,00,00,00,00,00,00,00,01,00,00,00,00,00,00,\ 00,02,00,00,00,60,ea,00,00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\RpcSs\Parameters] "ServiceDll"=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,\ 00,74,00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,\ 72,00,70,00,63,00,73,00,73,00,2e,00,64,00,6c,00,6c,00,00,00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\RpcSs\Security] "Security"=hex:01,00,14,80,a8,00,00,00,b4,00,00,00,14,00,00,00,30,00,00,00,02,\ 00,1c,00,01,00,00,00,02,80,14,00,ff,01,0f,00,01,01,00,00,00,00,00,01,00,00,\ 00,00,02,00,78,00,05,00,00,00,00,00,14,00,8d,00,02,00,01,01,00,00,00,00,00,\ 05,0b,00,00,00,00,00,18,00,ff,01,0f,00,01,02,00,00,00,00,00,05,20,00,00,00,\ 20,02,00,00,00,00,18,00,8d,00,02,00,01,02,00,00,00,00,00,05,20,00,00,00,23,\ 02,00,00,00,00,14,00,9d,00,00,00,01,01,00,00,00,00,00,05,04,00,00,00,00,00,\ 18,00,9d,00,00,00,01,02,00,00,00,00,00,05,20,00,00,00,21,02,00,00,01,01,00,\ 00,00,00,00,05,12,00,00,00,01,01,00,00,00,00,00,05,12,00,00,00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\RpcSs\Enum] "0"="Root\\LEGACY_RPCSS\\0000" "Count"=dword:00000001 "NextInstance"=dword:00000001 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\Root\LEGACY_RPCSS] "NextInstance"=dword:00000001 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\Root\LEGACY_RPCSS\0000] "Service"="RpcSs" "Legacy"=dword:00000001 "ConfigFlags"=dword:00000020 "Class"="LegacyDriver" "ClassGUID"="{8ECC055D-047F-11D1-A537-0000F8753ED1}" "DeviceDesc"="Remote Procedure Call (RPC)" [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\Root\LEGACY_RPCSS\0000\Control] "ActiveService"="RpcSs" 然后双击导入注册表。 接着在cmd下用sc开服务: sc config Remote Procedure Call(RPC) binpath= c:\windows\system32\svchost.exe -k rpcss start= auto 希望有用~~
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值