文章目录
概述
RpcEndpointRef是远程 [RpcEndpoint] 的引用。[RpcEndpointRef] 是线程安全的。
1. 源码剖析
1.1 变量
RpcEndpointRef
是一个抽象类,有三个val变量(通过在sparkConf
中设置来修改):
maxRetries
= RpcUtils.numRetries(SparkConf)retryWaitMs
= RpcUtils.retryWaitMs(SparkConf)defaultAskTimeout
= RpcUtils.askRpcTimeout(SparkConf)
1.2 两个getter
1.2.1 获取address
def address: RpcAddress
1.2.2 获取name
def name: String
1.3 通信方法
1.3.1 send():单向异步消息
/**
* Sends a one-way asynchronous message. Fire-and-forget semantics.
* 发送单向异步消息。"一时无二"的语义。
*/
def send(message: Any): Unit
1.3.2 ask():发送异步消息,非阻塞等待回复
指定超时时间:
- 将消息发送到相应的
[Rpcendpoint.receiveAndReplyAndReply)]
,并返回 [Future] 以在指定超时内接收回复。 - 此方法仅发送消息一次,从不重试。
def ask[T: ClassTag](message: Any, timeout: RpcTimeout): Future[T]
默认超时时间:
- 将消息发送到相应的
[Rpcendpoint.receiveAndReplyAndReply)]
,并返回 [Future] 以在默认超时内接收回复。 - 此方法仅发送消息一次,从不重试。
def ask[T: ClassTag](message: Any): Future[T] = ask(message, defaultAskTimeout)
1.3.3 askSync():发送同步消息,阻塞等待回复
指定超时时间:
- 将消息发送到相应的
[RpcEndpoint.receiveAndReply]
并在指定超时内获取结果,如果失败,将引发异常。 - 注意:这是一个阻塞操作,可能会花费大量时间,因此不要在
[[RpcEndpoint]
的消息循环中调用它。
def askSync[T: ClassTag](message: Any, timeout: RpcTimeout): T = {
val future = ask[T](message, timeout)
timeout.awaitResult(future)
}
默认超时时间:
- 将消息发送到相应的
[RpcEndpoint.receiveAndReply]
并在默认超时内获取结果,如果失败,将引发异常。 - 注意:这是一个阻塞操作,可能会花费大量时间,因此不要在
[[RpcEndpoint]
的消息循环中调用它。
def askSync[T: ClassTag](message: Any): T = askSync(message, defaultAskTimeout)