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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章