前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住给大家分享一下。点击跳转到网站:https://www.captainai.net/dongkelun
前言
学习记录一下Spark源码中的RPC,本文基于Spark3源码进行学习研究
三个重要的类
RpcEnv、RpcEndpoint、RpcEndpointRef主要是想搞懂这三个之间的关系
子类
Spark 1.6.3中默认使用了Netty作为底层的实现,但Akka的依赖依然存在;而Spark 2.1.0中的底层实现只有Netty,这样用户可以方便的使用不同版本的Akka或者将来某种更好的底层实现
NettyRpcEnv
1 | private[netty] class NettyRpcEnv( |
NettyRpcEndpointRef
1 | private[netty] class NettyRpcEndpointRef( |
RpcEndpoint 有两个子类trait(特质)
1 |
|
真正的实现是在具体的内部类里,比如
CoarseGrainedSchedulerBackend 里面的 DriverEndpoint
1 | class DriverEndpoint extends IsolatedRpcEndpoint with Logging { |
也可以是匿名内部类如在测试RpcEnvSuite里的
1 | val rpcEndpointRef = env.setupEndpoint("send-locally", new RpcEndpoint { |
注册流程
创建RpcEndpoint(子类)
1 | new DriverEndpoint() |
创建RpcEnv
实际上是通过NettyRpcEnvFactory的create方法创建并返回NettyRpcEnv
SparkContext初始化时:
调用顺序createSparkEnv->SparkEnv.createDriverEnv->create->RpcEnv.create
1 | // Create the Spark execution environment (cache, map output tracker, etc) |
RpcEnv.create方法:
1 | def create( |
实际调用的NettyRpcEnvFactory的create方法返回NettyRpcEnv
1 | def create(config: RpcEnvConfig): RpcEnv = { |
下面是测试类RpcEnvSuite的创建方法
1 | override def createRpcEnv( |
向RpcEnv注册RpcEndpoint并返回RpcEndpointRef
1 | rpcEnv.setupEndpoint(ENDPOINT_NAME, createDriverEndpoint()) |
实际上是调用NettyRpcEnv的setupEndpoint方法
1
2
3override def setupEndpoint(name: String, endpoint: RpcEndpoint): RpcEndpointRef = {
dispatcher.registerRpcEndpoint(name, endpoint)
}
然后调用dispatcher.registerRpcEndpoint创建并返回NettyRpcEndpointRef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34def registerRpcEndpoint(name: String, endpoint: RpcEndpoint): NettyRpcEndpointRef = {
val addr = RpcEndpointAddress(nettyEnv.address, name)
val endpointRef = new NettyRpcEndpointRef(nettyEnv.conf, addr, nettyEnv)
synchronized {
if (stopped) {
throw new IllegalStateException("RpcEnv has been stopped")
}
if (endpoints.containsKey(name)) {
throw new IllegalArgumentException(s"There is already an RpcEndpoint called $name")
}
// This must be done before assigning RpcEndpoint to MessageLoop, as MessageLoop sets Inbox be
// active when registering, and endpointRef must be put into endpointRefs before onStart is
// called.
endpointRefs.put(endpoint, endpointRef)
var messageLoop: MessageLoop = null
try {
messageLoop = endpoint match {
case e: IsolatedRpcEndpoint =>
new DedicatedMessageLoop(name, e, this)
case _ =>
sharedLoop.register(name, endpoint)
sharedLoop
}
endpoints.put(name, messageLoop)
} catch {
case NonFatal(e) =>
endpointRefs.remove(endpoint)
throw e
}
}
endpointRef
}
RpcEndpointRef.send
实际上调用NettyRpcEndpointRef.send
1 | override def send(message: Any): Unit = { |
然后调用NettyRpcEnv.send
1 | private[netty] def send(message: RequestMessage): Unit = { |
这里会根据address判断是否本地还是远程调用,下面按本地模式进行研究,即调用Dispatcher的postOneWayMessage方法
1 |
|
1 | private def postMessage( |
这里的loop是在上面的registerRpcEndpoint方法中根据判断endpoint是否是IsolatedRpcEndpoint的子类来区分成两种
1 | var messageLoop: MessageLoop = null |
这里先按sharedLoop来进行分析,其中
1 | private lazy val sharedLoop = new SharedMessageLoop(nettyEnv.conf, this, numUsableCores) |
loop.post:
1 | override def post(endpointName: String, message: InboxMessage): Unit = { |
向inbox插入一条InboxMessage,并将该inbox设置为active
receive
SharedMessageLoop中
1 | override protected val threadpool: ThreadPoolExecutor = { |
1 | protected val receiveLoopRunnable = new Runnable() { |
1 | private def receiveLoop(): Unit = { |
循环调用inbox.process
1 |
|
inbox.process会匹配消息类型,然后根据消息类型,执行endpoint.receive方法,这里的消息类型是OneWayMessage即
1 | case OneWayMessage(_sender, content) => |
最后看一下DedicatedMessageLoop,其实DedicatedMessageLoop和SharedMessageLoop差不多
1 | private val inbox = new Inbox(name, endpoint) |
同样有一个线程池去调用receiveLoopRunnable,下面的就和SharedMessageLoop一样了
1 | override protected val threadpool = if (endpoint.threadCount() > 1) { |
onStart
这里提一下onStart方法,因为注释中写到
1 | * An end point for the RPC that defines what functions to trigger given a message. |
即一个RpcEndpoint生命周期:构建->onStart→receive→onStop,那么onstart方法是在receive方法之前先执行的,但是在代码这个类中无法看出如何最新执行,且网上的
博客也没有写清楚如何执行,然后我就带着疑问仔细调试和阅读了一下源码,发现是这样的:
首先在上面的inbox初始化时,首先会执行下面的代码
1 | ("this") |
那么在上面提到的process方法中messages会首先取出OnStart,而在模式匹配时匹配到OnStart,就会执行OnStart方法
1 | message = messages.poll() |
在CoarseGrainedSchedulerBackend里的DriverEndpoint就重写了onStart方法来提前执行一些准备工作
1 | override def onStart(): Unit = { |
测试Demo
1 | import org.apache.spark.{SecurityManager, SparkConf, SparkEnv} |
运行结果:
1 | start hello endpoint |