2016-09-13 13:26:23
来 源
中存储网
Spark
。Spark的RDD执行完成之后会保存检查点,便于当整个作业运行失败重新运行时候,从检查点恢复之前已经运行成功的RDD结果,这样就会大大减少重新计算的成本,提高任务恢复效率和执行效率,节省Spark各个计算节点的资源。

概述

《深入理解Spark:核心思想与源码分析》一书中只是简单介绍了下RDD的checkpoint,对本书是个遗憾。所以此文的目的旨在查漏补缺,完善本书的内容。Spark的RDD执行完成之后会保存检查点,便于当整个作业运行失败重新运行时候,从检查点恢复之前已经运行成功的RDD结果,这样就会大大减少重新计算的成本,提高任务恢复效率和执行效率,节省Spark各个计算节点的资源。本文着重分析检查点的代码实现,更深入理解其原理。在《深入理解Spark:核心思想与源码分析》一书的第5章中讲到在获取RDD分区信息时会调用partitions方法(见代码清单5-11),在获取依赖时会调用dependencies方法(见代码清单5-28)。

代码清单5-11   partitions方法的实现

  1. final def partitions: Array[Partition] = {  
  2.   checkpointRDD.map(_.partitions).getOrElse {  
  3.     if (partitions_ == null) {  
  4.       partitions_ = getPartitions  
  5.     }  
  6.     partitions_  
  7.   }  
  8. }  
代码清单5-28   dependencies方法的实现
  1. final def dependencies: Seq[Dependency[_]] = {  
  2.   checkpointRDD.map(r => List(new OneToOneDependency(r))).getOrElse {  
  3.     if (dependencies_ == null) {  
  4.       dependencies_ = getDependencies  
  5.     }  
  6.     dependencies_  
  7.   }  
  8. }  

在代码清单5-11所示的RDD的partitions方法和代码清单5-28所示的RDD的dependencies方法中都使用了checkpointRDD,checkpointRDD的定义如下。

  1. private def checkpointRDD: Option[RDD[T]] = checkpointData.flatMap(_.checkpointRDD)  

从checkpointRDD的定义看到,checkpointRDD的信息来自于checkpointData,checkpointData的定义如下:

  1. private[spark] var checkpointData: Option[RDDCheckpointData[T]] = None  

所以checkpointRDD实际是通过调用RDDCheckpointData的checkpointRDD方法得到的,checkpointRDD的实现见代码清单5-63。

代码清单5-63         获取检查点数据
 
  1. def checkpointRDD: Option[RDD[T]] = {  
  2.   RDDCheckpointData.synchronized {  
  3.     cpRDD  
  4.   }  
  5. }  

cpRDD在保存了检查点之后将持有CheckpointRDD,其类型定义如下:

  1. var cpState = Initialized  
  2. @transient var cpFile: Option[String] = None  
  3. var cpRDD: Option[RDD[T]] = None  

上面的代码除了展示cpRDD的类型外,还定义了cpFile和cpState。cpFile用于保存检查点写入HDFS的文件目录,cpState用于表示当前RDD设置检查点的状态,包括初始化(Initialized)、标记将要保存检查点(MarkedForCheckpoint)、保存检查点中(CheckpointingInProgress)和设置检查点完成(Checkpointed)几个状态。RDDCheckpointData的cpState字段默认是Initialized状态。

检查点的启用

用户提交的Spark作业必须主动调用RDD的checkpoint方法(见代码清单5-64),才会启动检查点功能。

代码清单5-64         启用检查点功能
  1. def checkpoint() {  
  2.   if (context.checkpointDir.isEmpty) {  
  3.     throw new SparkException("Checkpoint directory has not been set in the SparkContext")  
  4.   } else if (checkpointData.isEmpty) {  
  5.     checkpointData = Some(new RDDCheckpointData(this))  
  6.     checkpointData.get.markForCheckpoint()  
  7.   }  
  8. }  

只有调用了checkpoint方法,RDD才会创建RDDCheckpointData对象,并由checkpointData持有。同时会调用RDDCheckpointData的markForCheckpoint方法(见代码清单5-65)将cpState状态置为MarkedForCheckpoint。
 
代码清单5-65         标记启用检查点
  1. def markForCheckpoint() {  
  2.   RDDCheckpointData.synchronized {  
  3.     if (cpState == Initialized) cpState = MarkedForCheckpoint  
  4.   }  
  5. }  

markForCheckpoint方法中将cpState置为MarkedForCheckpoint,有着重要意义:一方面表示启用检查点,另一方面只有当cpState等于MarkedForCheckpoint时,才能够保存检查点。

检查点的保存

cpRDD用于持有CheckpointRDD,但是它是什么时候持有的呢?下面我们将分析检查点的保存时机。在介绍代码清单5-21时,简单的提到了调用RDD的doCheckpoint方法保存检查点,现在来具体分析其代码实现,见代码清单5-66。
 
代码清单5-66         保存检查点

  1. def doCheckpoint() {  
  2.   RDDCheckpointData.synchronized {  
  3.     if (cpState == MarkedForCheckpoint) {  
  4.       cpState = CheckpointingInProgress  
  5.     } else {  
  6.       return  
  7.     }  
  8.   }  
  9.   
  10.   // Create the output path for the checkpoint  
  11.   val path = new Path(rdd.context.checkpointDir.get, "rdd-" + rdd.id)  
  12.   val fs = path.getFileSystem(rdd.context.hadoopConfiguration)  
  13.   if (!fs.mkdirs(path)) {  
  14.     throw new SparkException("Failed to create checkpoint path " + path)  
  15.   }  
  16.   
  17.   // Save to file, and reload it as an RDD  
  18.   val broadcastedConf = rdd.context.broadcast(  
  19.     new SerializableWritable(rdd.context.hadoopConfiguration))  
  20.   rdd.context.runJob(rdd, CheckpointRDD.writeToFile[T](path.toString, broadcastedConf) _)  
  21.   val newRDD = new CheckpointRDD[T](rdd.context, path.toString)  
  22.   if (newRDD.partitions.size != rdd.partitions.size) {  
  23.     throw new SparkException(  
  24.       "Checkpoint RDD " + newRDD + "(" + newRDD.partitions.size + ") has different " +  
  25.         "number of partitions than original RDD " + rdd + "(" + rdd.partitions.size + ")")  
  26.   }  
  27.   
  28.   // Change the dependencies and partitions of the RDD  
  29.   RDDCheckpointData.synchronized {  
  30.     cpFile = Some(path.toString)  
  31.     cpRDD = Some(newRDD)  
  32.     rdd.markCheckpointed(newRDD)   // Update the RDD's dependencies and partitions  
  33.     cpState = Checkpointed  
  34.   }  
  35.   logInfo("Done checkpointing RDD " + rdd.id + " to " + path + ", new parent is RDD " + newRDD.id)  
  36. }  

通过阅读doCheckpoint方法,其执行步骤总结如下:

1)        校验用户作业是否启用了检查点,即是否调用了checkpoint方法将cpState置为MarkedForCheckpoint。如果没有启用检查点,则直接返回,不继续进行检查点的保存。

2)        在HDFS上创建用于保存检查点数据的文件路径。其中checkpointDir必须由用户作业调用SparkContext的setCheckpointDir方法(见代码清单5-67)设置。

3)        运行作业,此作业实际执行了CheckpointRDD的writeToFile方法(见代码清单5-68),将检查点数据保存的HDFS上。

4)        将构造的CheckpointRDD由cpRDD持有,检查点保存目录由cpFile持有,最后将cpState设置为Checkpointed。由于保存了检查点,说明此RDD已经成功执行,其依赖和分区相关的信息将不再使用,即便是Job恢复也只需要从检查点读取数据,所以调用RDD的markCheckpointed方法(见代码清单5-69)清除依赖与分区信息。

代码清单5-67         设置作业检查点在HDFS上的保存路径

  1. def setCheckpointDir(directory: String) {  
  2.   checkpointDir = Option(directory).map { dir =>  
  3.     val path = new Path(dir, UUID.randomUUID().toString)  
  4.     val fs = path.getFileSystem(hadoopConfiguration)  
  5.     fs.mkdirs(path)  
  6.     fs.getFileStatus(path).getPath.toString  
  7.   }  
  8. }  
代码清单5-68         将检查点写入HDFS
 
  1. def writeToFile[T: ClassTag](  
  2.     path: String,  
  3.     broadcastedConf: Broadcast[SerializableWritable[Configuration]],  
  4.     blockSize: Int = -1  
  5.   )(ctx: TaskContext, iterator: Iterator[T]) {  
  6.   val env = SparkEnv.get  
  7.   val outputDir = new Path(path)  
  8.   val fs = outputDir.getFileSystem(broadcastedConf.value.value)  
  9.   
  10.   val finalOutputName = splitIdToFile(ctx.partitionId)  
  11.   val finalOutputPath = new Path(outputDir, finalOutputName)  
  12.   val tempOutputPath = new Path(outputDir, "." + finalOutputName + "-attempt-" + ctx.attemptId)  
  13.   
  14.   if (fs.exists(tempOutputPath)) {  
  15.     throw new IOException("Checkpoint failed: temporary path " +  
  16.       tempOutputPath + " already exists")  
  17.   }  
  18.   val bufferSize = env.conf.getInt("spark.buffer.size", 65536)  
  19.   
  20.   val fileOutputStream = if (blockSize < 0) {  
  21.     fs.create(tempOutputPath, false, bufferSize)  
  22.   } else {  
  23.     // This is mainly for testing purpose  
  24.     fs.create(tempOutputPath, false, bufferSize, fs.getDefaultReplication, blockSize)  
  25.   }  
  26.   val serializer = env.serializer.newInstance()  
  27.   val serializeStream = serializer.serializeStream(fileOutputStream)  
  28.   serializeStream.writeAll(iterator)  
  29.   serializeStream.close()  
  30.   
  31.   if (!fs.rename(tempOutputPath, finalOutputPath)) {  
  32.     if (!fs.exists(finalOutputPath)) {  
  33.       logInfo("Deleting tempOutputPath " + tempOutputPath)  
  34.       fs.delete(tempOutputPath, false)  
  35.       throw new IOException("Checkpoint failed: failed to save output of task: "  
  36.         + ctx.attemptId + " and final output path does not exist")  
  37.     } else {  
  38.       // Some other copy of this task must've finished before us and renamed it  
  39.       logInfo("Final output path " + finalOutputPath + " already exists; not overwriting it")  
  40.       fs.delete(tempOutputPath, false)  
  41.     }  
  42.   }  
  43. }  

代码清单5-69         清除RDD的依赖与分区
  1. private[spark] def markCheckpointed(checkpointRDD: RDD[_]) {  
  2.   clearDependencies()  
  3.   partitions_ = null  
  4.   deps = null    // Forget the constructor argument for dependencies too  
  5. }  
  6.   
  7. protected def clearDependencies() {  
  8.   dependencies_ = null  
  9. }  

使用检查点

前两个小节分别讲解了检查点如何启用以及启用后如何实现保存的原理和分析,在5.7节一开始介绍了检查点的两种使用场景:

1)        获取RDD的依赖时,如果有了检查点,则从检查点中读取;

2)        获取RDD的分区时,如果有了检查点,则从检查点中读取。

除了以上两种场景,还有一种场景会间接使用RDD的检查点数据,那就是在计算过程中调用RDD的computeOrReadCheckpoint方法(见代码清单5-70)以便直接从检查点读取保存的计算结果,关于此方法的具体使用放在第6章的分析代码清单6-1时介绍,此处只分析其使用检查点的实现。

代码清单5-70         从检查点读取计算结果

  1. private[spark] def computeOrReadCheckpoint(split: Partition, context: TaskContext): Iterator[T] =  
  2. {  
  3.   if (isCheckpointed) firstParent[T].iterator(split, context) else compute(split, context)  
  4. }  
这里的isCheckpointed实际是一个方法,代码如下。
 
  1. def isCheckpointed: Boolean = checkpointData.exists(_.isCheckpointed)  

isCheckpointed实际代理了RDDCheckpointData的isCheckpointed方法(见代码清单5-71),用于判断当前RDD是否已经设置了检查点。

代码清单5-71         判断RDD是否已经保持了检查点

  1. def isCheckpointed: Boolean = {  
  2.   RDDCheckpointData.synchronized { cpState == Checkpointed }  
  3. }  

根据之前的分析,我们知道如果已经保存了检查点,那么cpState必然等于Checkpointed,所以isCheckpointed方法将返回true。因此代码清单5-70将会继续执行firstParent[T].iterator(split, context)。而firstParent(见代码清单5-13)首先会调用代码清单5-28所示的dependencies方法,这样计算过程中调用computeOrReadCheckpoint,使用检查点的过程实际退化为我们说的获取RDD依赖时使用检查点的方式。而此时的依赖已经被CheckpointRDD所替代,经过迭代计算(请参考第6章),最终会调用CheckpointRDD的compute方法(见代码清单5-72),从其实现可知从检查点读取计算结果实际就是读取之前分析的写入HDFS的数据。

代码清单5-72         从HDFS保存的检查点读取数据

  1. override def compute(split: Partition, context: TaskContext): Iterator[T] = {  
  2.   val file = new Path(checkpointPath, CheckpointRDD.splitIdToFile(split.index))  
  3.   CheckpointRDD.readFromFile(file, broadcastedConf, context)  
  4. }  

作者博客:http://blog.csdn.net/beliefer/article/details/51206980

声明: 此文观点不代表本站立场;转载须要保留原文链接;版权疑问请联系我们。