关于spark:Livy探究五-解释器的实现

4次阅读

共计 1830 个字符,预计需要花费 5 分钟才能阅读完成。

本篇咱们深刻源码,探索一下 livy 解释器的实现原理。

ReplDriver

ReplDriver是真正最终运行的 Driver 程序对应的类(其基类是第三篇中提到的RSCDrvier)。在这一层,重点关注 handle 系列办法:

def handle(ctx: ChannelHandlerContext, msg: BaseProtocol.ReplJobRequest): Int = {...}

def handle(ctx: ChannelHandlerContext, msg: BaseProtocol.CancelReplJobRequest): Unit = {...}

def handle(ctx: ChannelHandlerContext, msg: BaseProtocol.ReplCompleteRequest): Array[String] = {...}

def handle(ctx: ChannelHandlerContext, msg: BaseProtocol.GetReplJobResults): ReplJobResults = {...}

这些办法其实负责解决各种类型的 request,例如 BaseProtocol.ReplJobRequest 就是解决 执行代码 申请。后面有篇提到的 RpcServer,负责基于 netty 启动服务端,并且绑定解决申请的类,其外部的dispatcher 会负责通过反射,找到对应的 handle 办法并调用。

对于 RPC,这里只是提一下,前面的篇章再跟大家一起剖析细节

本篇的重点是探索 REPL,所以咱们重点从 BaseProtocol.ReplJobRequest 解决办法跟入:

def handle(ctx: ChannelHandlerContext, msg: BaseProtocol.ReplJobRequest): Int = {session.execute(EOLUtils.convertToSystemEOL(msg.code), msg.codeType)
}

这里调用了 session 对象的 execute,所以持续进去看 session 对象

Session

ReplDriver持有 Session 对象的实例,在 ReplDriver 初始化阶段实例化,并调用了 session.start() 办法:

session 会创立SparkInterpreter,并调用SparkInterpreter.start

session 的 execute 办法最终会调用SparkInterpreter.execute

SparkInterpreter

LivySparkInterpreter是一种 Interpreter(接口)。同样是Interpreter 的还有:

  • PythonInterpreter
  • SparkRInterpreter
  • SQLInterpreter

SparkInterpreter.start次要干的事件就是初始化 SparkILoopSparkILooporg.apache.spark.repl包下的类,它其实就是 spark 自身实现 REPL 的外围类。livy 在这里其实只是包装了 spark 自身曾经实现的性能。另外一件事件,就是第三篇中提到的在解释器中 bind 变量,上面的代码就是 bind 变量的过程:

下面代码中的 bind 办法和 execute 办法就是外围办法,其实现办法就是间接调用 SparkILoop 的对应办法:

// execute 其实最初调到 interpret
// code 就是要执行的代码
override protected def interpret(code: String): Result = {sparkILoop.interpret(code)
}

// name: 变量名
// tpe: 变量类型
// value: 变量对象实在援用
// modifier: 变量各种修饰符
override protected def bind(name: String,
 tpe: String,
 value: Object,
 modifier: List[String]): Unit = {
  sparkILoop.beQuietDuring {sparkILoop.bind(name, tpe, value, modifier)
  }
}

到这里其实思路曾经比拟清晰了,咱们失去上面的档次关系图:

总结

本篇从源码的角度剖析了 livy 如何利用 spark 实现的 REPL,实现交互式代码运行。因而,有了 livy,相当于把 spark 的 REPL 搬到了 web。

正文完
 0