You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/*** Sends a one-way asynchronous message. Fire-and-forget semantics.* 属于 at-most-once 投递规则*/defsend(message: Any):Unit/** * Send a message to the corresponding [[RpcEndpoint.receiveAndReply)]] and return a [[Future]] to * receive the reply within a default timeout. * 这个就要等待服务端的返回了 * This method only sends the message once and never retries.*/defask[T:ClassTag](message: Any):Future[T] = ask(message, defaultAskTimeout)
valmetricsSystem=if (isDriver) {
// Don't start metrics system right now for Driver.// We need to wait for the task scheduler to give us an app ID.// Then we can start the metrics system.MetricsSystem.createMetricsSystem(MetricsSystemInstances.DRIVER, conf, securityManager)
} else {
// We need to set the executor ID before the MetricsSystem is created because sources and// sinks specified in the metrics configuration file will want to incorporate this executor's// ID into the metrics they report.
conf.set(EXECUTOR_ID, executorId)
valms=MetricsSystem.createMetricsSystem(MetricsSystemInstances.EXECUTOR, conf,
securityManager)
ms.start()
ms
}
5.1 SparkEnv 概述
SparkEnv
的私有方法create
用于创建SparkEnv
。其内部组件如图:5.2 安全管理器 SecurityManager
SecurityManager主要对帐号、权限以及身份认证进行设置和管理。如果 Spark 的部署模式为 YARN,则需要生成 secret key (密钥)并存储 Hadoop UGI。而在其他模式下,则需要设置环境变量 _SPARK_AUTH_SECRET(优先级更高)或者 spark.authenticate.secret 属性指定 secret key (密钥)。最后SecurityManager 中设置了默认的口令认证实例 Authenticator,此实例采用匿名内部类实现,用于每次使用 HTTP client 从 HTTP 服务器获取用户的用户和密码。这是由于 Spark 的节点间通信往往需要动态协商用户名、密码,这种方式灵活地支持了这种需求。
SecurityManager内部有很多属性。
5.3 RPC 环境
RpcEnv组件肩负着替代Spark 2.x.x以前版本中采用的Akka。SparkEnv创建RpcEnv代码如下:
在RpcEnv的
create
方法中也只有如下代码:5.3.1 RPC 端点 RpcEndpoint
RPC端点是对Spark的PRC通信实体的统一抽象, 所有运行与RPC框架至上的实体都应该继承
RpcEndpoint
。RpcEndpoint
是替代Akka
的Actor
。RpcEndpoint
是能够处理RPC请求,给一特定服务提供本地调用及跨节点调用的RPC组件的抽象。5.3.1.1 RpcEndpoint 的定义
查看源码,自带注释。
此
RpcEndpoint
的一些接口非常类似于Akka的Actor
。5.3.1.2 特质 RpcEndpoint 的继承体系
RpcEndpoint
只是一个特质,除了对接口的定义,并没有任何实现的逻辑。下图展示了那些子类实现了RpcEndpoint
`。其中灰色的子类型
DummyMaster
(Mummy意为虚拟的、假的),它只是用来测试的。ThreadSafeRpcEndpoint
适用于必须是线程安全的场景,被很多继承者实现,比如HeartbeatReceiver
、Master
,遇到了再做具体分析。5.3.2 RPC 端点引用 RpcEndpointRef
RpcEndpointRef
是ActorRef
的替代品。要向远端的RpcEndpoint
发起请求,必须持有这个RpcEndpoint
的RpcEndpointRef
。下面介绍什么事消息投递规则。
5.3.2.1 消息投递规则
5.3.2.2 RpcEndpointRef 的定义
它定义了所有
RpcEndpoint
引用的属性与接口。代码请看源码5.3.3 创建传输上下文 TransportConf
由于RPC环境
RpcEnv
的底层需要依赖于数据总线,因此需要创建传输上下文TransportConf
。源代码
5.3.4 消息调度器 Dispatcher (*)
Dispatcher
是有效提法哦NettyRpcEnv对消息异步处理并最大提升并行性处理能力的前提。Dispatcher
负责将RPC消息路由到要该对此消息处理的RPCEndpoint。5.3.5 传输上下文 TransportContext
在
NettyRpcEnv
中,创建TransportContext
的代码如下:5.3.5.1 NettyStreamManager
NettyStreamManager
专门用来提供文件服务的能力。定义了3个文件与目录缓存。5.3.5.2 NettyRpcHandler
两个重载的receive的方法。
对客户端进行响应的receive方法。
对客户端进行响应的receive重载方法。
5.3.6 创建传输客户端工厂 TransportClientFactory
Spark与远端RpcEnv进行通信都依赖于
TransportClientFactory
创建的TransportClient
。5.3.7 创建TransportServer
TransportServer
并未在这实例化,而是在启动EpcEnv的偏函数StartNerryRpcEnv
,它负责调用NettyRpcEnv的startServer
方法。5.3.8 客户端请求发送
5.3.9 NettyRpcEnv 中常用方法
获取
RpcEndPoint
的引用对象RpcEndPointRef
。得到对应的
RPCEndpointRef
。设置
Endpoint
。5.4 序列化管理器 SerializerManager
SparkEnv
中有两个序列化的组件:5.5 广播管理器 BroadcastManager
BroadcastManager
用于将配置信息和序列化后的RDD、Job及ShuffleDependency等信息在本地存储。如果为了容灾,也会复制到其他节点上。在sparkEnv.scala
中:5.6 map任务输出跟踪器
mapOutputTracker
用于跟踪map
任务的输出状态,此状态便于reduce
任务定位map
输出结果所在节点地址,进而获取中间输出结果。每个map
任务或者reduce
任务都会有其唯一的表示,分别为mapId
和reduceId
。每个reduce
任务的输入可能是多个map
任务的输出,reduce
会到各个map
任务所在节点上拉取Block
,这一过程叫Shuffle
。每次shuffle
都有唯一标识shuffleId
。5.6.1 mapOutputTracker 的实现
5.6.2 mapOutputTrackerMaster 的实现
mapOutputTrackerWorker
将map
任务的跟踪信息,通过mapOutputTrackerMasterEndpoint
的RPCEndpointRef
发送给mapOutputTrackerMaster
,由mapOutputTrackerMaster
负责管理和维护所有的map
任务的输出跟踪信息。5.7 构架存储体系
ShuffleManager
MemoryManager
BlockTransferService
BlockManagerMaster
DiskBlockManager
BlockInfoManager
BlockManager
第6章会讲每个组件的功能。
5.8 创建度量系统
在
SparkEnv
中,度量系统也是必不可少的一个子组件。在
metrics
中,createMetricsSystem是MetricsSystem
伴生对象提供的实现:5.9 输出提交协调器
当Spark应用程序用了
Spark SQL
(包括Hive)或者需要将任务的输出保存到HDFS时,就会用到输出提交协调器OutputCommitCoordinator
,它将决定任务是否可以提交输出到HDFS。无论是Driver还是Executor,在SparkEnv
中都包含子组件OutputCommitCoordinator
。在
SparkEnv
中,创建OutputCommitCoordinator
的代码如下:5.9.1 OutputCommitCoordinatorEndpoint的实现
5.9.2 OutputCommitCoordinator的实现
5.9.2 OutputCommitCoordinator的工作原理
5.10 创建SparkEnv
当SparkEnv内部的所有组件都是丽华完毕,将正式创建SparkEnv。
The text was updated successfully, but these errors were encountered: