spark源码阅读RDD中WithScope是什么?

     withScope是最近的发现版中新增加的一个模块,它是用来做DAG可视化的(DAG visualization on SparkUI)

以前的sparkUI中只有stage的执行情况,也就是说我们不可以看到上个RDD到下个RDD的具体信息。于是为了在

sparkUI中能展示更多的信息。所以把所有创建的RDD的方法都包裹起来,同时用RDDOperationScope 记录 RDD 的操作历史和关联,就能达成目标。下面就是一张WordCount的DAG visualization on SparkUI


记录关系的RDDOperationScope源码如下:

[plain]  view plain  copy
  1. /**  
  2.  * A collection of utility methods to construct a hierarchical representation of RDD scopes.  
  3.  * An RDD scope tracks the series of operations that created a given RDD.  
  4.  */  
  5. private[spark] object RDDOperationScope extends Logging {  
  6.   private val jsonMapper = new ObjectMapper().registerModule(DefaultScalaModule)  
  7.   private val scopeCounter = new AtomicInteger(0)  
  8.   
  9.  <span style="color:#ff0000;"> def fromJson(s: String): RDDOperationScope = {  
  10.     jsonMapper.readValue(s, classOf[RDDOperationScope])  
  11.   }</span>  
  12.   
  13.   
  14.   //返回一个全局独一无二的scopeID  
  15.   def nextScopeId(): Int = scopeCounter.getAndIncrement  
  16.   
  17.   /**  
  18.    * Execute the given body such that all RDDs created in this body will have the same scope.  
  19.    * The name of the scope will be the first method name in the stack trace that is not the  
  20.    * same as this method's.  
  21.    *  
  22.    * Note: Return statements are NOT allowed in body.  
  23.    */  
  24.   private[spark] def withScope[T](  
  25.       sc: SparkContext,  
  26.       allowNesting: Boolean = false)(body: => T): T = {  
  27.     //设置跟踪堆的轨迹的scope名字  
  28.     val ourMethodName = "withScope"  
  29.     val callerMethodName = Thread.currentThread.getStackTrace()  
  30.       .dropWhile(_.getMethodName != ourMethodName)  
  31.       .find(_.getMethodName != ourMethodName)  
  32.       .map(_.getMethodName)  
  33.       .getOrElse {  
  34.         // Log a warning just in case, but this should almost certainly never happen  
  35.         logWarning("No valid method name for this RDD operation scope!")  
  36.         "N/A"  
  37.       }  
  38.     withScope[T](sc, callerMethodName, allowNesting, ignoreParent = false)(body)  
  39.   }  
  40.   
  41.   /**  
  42.    * Execute the given body such that all RDDs created in this body will have the same scope.  
  43.    *  
  44.    * If nesting is allowed, any subsequent calls to this method in the given body will instantiate  
  45.    * child scopes that are nested within our scope. Otherwise, these calls will take no effect.  
  46.    *  
  47.    * Additionally, the caller of this method may optionally ignore the configurations and scopes  
  48.    * set by the higher level caller. In this case, this method will ignore the parent caller's  
  49.    * intention to disallow nesting, and the new scope instantiated will not have a parent. This  
  50.    * is useful for scoping physical operations in Spark SQL, for instance.  
  51.    *  
  52.    * Note: Return statements are NOT allowed in body.  
  53.    */  
  54.   private[spark] def withScope[T](  
  55.       sc: SparkContext,  
  56.       name: String,  
  57.       allowNesting: Boolean,  
  58.       ignoreParent: Boolean)(body: => T): T = {  
  59.     // Save the old scope to restore it later  
  60.     //先保存老的scope,之后恢复它  
  61.     val scopeKey = SparkContext.RDD_SCOPE_KEY  
  62.     val noOverrideKey = SparkContext.RDD_SCOPE_NO_OVERRIDE_KEY  
  63.     val oldScopeJson = sc.getLocalProperty(scopeKey)  
  64.     val oldScope = Option(oldScopeJson).map(RDDOperationScope.fromJson)  
  65.     val oldNoOverride = sc.getLocalProperty(noOverrideKey)  
  66.     try {  
  67.       if (ignoreParent) {  
  68.         //ignoreParent: Boolean:当ignorePatent设置为true的时候,那么回忽略之前的全部设置和scope  
  69.         //从新我们自己的scope  
  70.         sc.setLocalProperty(scopeKey, <span style="color:#ff0000;">new RDDOperationScope(name).toJson</span>)  
  71.       } else if (sc.getLocalProperty(noOverrideKey) == null) {  
  72.         // Otherwise, set the scope only if the higher level caller allows us to do so  
  73.         sc.setLocalProperty(scopeKey, <span style="color:#ff0000;">new RDDOperationScope(name, oldScope).toJson</span>)  
  74.       }  
  75.       //可选:不让我们的新的子RDD放入我们的scope中  
  76.       if (!allowNesting) {  
  77.         sc.setLocalProperty(noOverrideKey, "true")  
  78.       }  
  79.       body  
  80.     } finally {  
  81.       //把所有的新状态恢复放在一起  
  82.       sc.setLocalProperty(scopeKey, <span style="color:#ff0000;">oldScopeJson</span>)  
  83.       sc.setLocalProperty(noOverrideKey, oldNoOverride)  
  84.     }  
  85.   }  
  86. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值