Spark源码学习(6)——Shuffle

16 篇文章 4 订阅

本文要解决的问题:

通过Spark源码学习,进一步深入了解Shuffle过程。


Shuffle 介绍

在Map和Reduce之间的过程就是Shuffle,Shuffle的性能直接影响整个Spark的性能。所以Shuffle至关重要。

这里写图片描述

从图中得知,Map输出的结构产生在bucket中。而bucket的数量是map*reduce的个数。这里的每一个bucket都对应一个文件。Map对bucket书是写入数据,而reduce是对bucket是抓取数据也就是读的过程。


ShuffleManager

ShuffleManager中有五个方法:

  • registerShuffleShuffle:注册

  • getWriter:获得写数据的对象

  • getReader:获得读取数据的对象

  • unregisterShuffle:移除元数据

  • Stop :停止ShuffleManager


Shuffle Write
Shuffle写的过程需要落到磁盘。在参数consolidateShuffleFiles中可以配置。

如果consolidateShuffleFiles为true写文件,为false在completedMapTasks中添加mapId。

接下来看下recycleFileGroup这个方法。参数ShuffleFileGroup是一组shuffle文件,每一个特定的map都会分配一组ShuffleFileGroup写入文件。

SortShuffleManager中的读取对象调用了HashShuffleReader

源码中的ShuffleState是记录shuffle的一个特定状态。

ShuffleWrite有两个子类:

private[spark] abstract class ShuffleWriter[K, V] {
  /** Write a sequence of records to this task's output */
  @throws[IOException]
  def write(records: Iterator[Product2[K, V]]): Unit

  /** Close this writer, passing along whether the map completed */
  def stop(success: Boolean): Option[MapStatus]
}

SortShuffleWriter的write方法:

override def write(records: Iterator[Product2[K, V]]): Unit = {
    sorter = if (dep.mapSideCombine) {
      require(dep.aggregator.isDefined, "Map-side combine without Aggregator specified!")
      new ExternalSorter[K, V, C](
        context, dep.aggregator, Some(dep.partitioner), dep.keyOrdering, dep.serializer)
    } else {
      // In this case we pass neither an aggregator nor an ordering to the sorter, because we don't
      // care whether the keys get sorted in each partition; that will be done on the reduce side
      // if the operation being run is sortByKey.
      new ExternalSorter[K, V, V](
        context, aggregator = None, Some(dep.partitioner), ordering = None, dep.serializer)
    }
    sorter.insertAll(records)

    // Don't bother including the time to open the merged output file in the shuffle write time,
    // because it just opens a single file, so is typically too fast to measure accurately
    // (see SPARK-3570).
    val output = shuffleBlockResolver.getDataFile(dep.shuffleId, mapId)
    val tmp = Utils.tempFileWith(output)
    val blockId = ShuffleBlockId(dep.shuffleId, mapId, IndexShuffleBlockResolver.NOOP_REDUCE_ID)
    val partitionLengths = sorter.writePartitionedFile(blockId, tmp)
    shuffleBlockResolver.writeIndexFileAndCommit(dep.shuffleId, mapId, partitionLengths, tmp)
    mapStatus = MapStatus(blockManager.shuffleServerId, partitionLengths)
  }

ShuffleReader
HashShuffleReader

override def read(): Iterator[Product2[K, C]] = {
    val blockFetcherItr = new ShuffleBlockFetcherIterator(
      context,
      blockManager.shuffleClient,
      blockManager,
      mapOutputTracker.getMapSizesByExecutorId(handle.shuffleId, startPartition, endPartition),
      // Note: we use getSizeAsMb when no suffix is provided for backwards compatibility
      SparkEnv.get.conf.getSizeAsMb("spark.reducer.maxSizeInFlight", "48m") * 1024 * 1024,
      SparkEnv.get.conf.getInt("spark.reducer.maxReqsInFlight", Int.MaxValue))

    // Wrap the streams for compression based on configuration
    val wrappedStreams = blockFetcherItr.map { case (blockId, inputStream) =>
      serializerManager.wrapForCompression(blockId, inputStream)
    }

    val serializerInstance = dep.serializer.newInstance()

    // Create a key/value iterator for each stream
    val recordIter = wrappedStreams.flatMap { wrappedStream =>
      // Note: the asKeyValueIterator below wraps a key/value iterator inside of a
      // NextIterator. The NextIterator makes sure that close() is called on the
      // underlying InputStream when all records have been read.
      serializerInstance.deserializeStream(wrappedStream).asKeyValueIterator
    }

    // Update the context task metrics for each record read.
    val readMetrics = context.taskMetrics.createTempShuffleReadMetrics()
    val metricIter = CompletionIterator[(Any, Any), Iterator[(Any, Any)]](
      recordIter.map { record =>
        readMetrics.incRecordsRead(1)
        record
      },
      context.taskMetrics().mergeShuffleReadMetrics())

    // An interruptible iterator must be used here in order to support task cancellation
    val interruptibleIter = new InterruptibleIterator[(Any, Any)](context, metricIter)

    val aggregatedIter: Iterator[Product2[K, C]] = if (dep.aggregator.isDefined) {
      if (dep.mapSideCombine) {
        // We are reading values that are already combined
        val combinedKeyValuesIterator = interruptibleIter.asInstanceOf[Iterator[(K, C)]]
        dep.aggregator.get.combineCombinersByKey(combinedKeyValuesIterator, context)
      } else {
        // We don't know the value type, but also don't care -- the dependency *should*
        // have made sure its compatible w/ this aggregator, which will convert the value
        // type to the combined type C
        val keyValuesIterator = interruptibleIter.asInstanceOf[Iterator[(K, Nothing)]]
        dep.aggregator.get.combineValuesByKey(keyValuesIterator, context)
      }
    } else {
      require(!dep.mapSideCombine, "Map-side combine without Aggregator specified!")
      interruptibleIter.asInstanceOf[Iterator[Product2[K, C]]]
    }

SortShuffleManager中的读取对象调用了HashShuffleReader

  override def getReader[K, C](
      handle: ShuffleHandle,
      startPartition: Int,
      endPartition: Int,
      context: TaskContext): ShuffleReader[K, C] = {
    new BlockStoreShuffleReader(
      handle.asInstanceOf[BaseShuffleHandle[K, _, C]], startPartition, endPartition, context)
  }

Shuffle partition

在RDD API中当调用reduceByKey等类似的操作,则会产生Shuffle了。

这里写图片描述

根据不同的业务场景,reduce的个数一般由程序猿自己设置大小。可通过“spark.default.par allelism”参数设置。
这里写图片描述

1、在第一个MapPartitionsRDD这里先做一次map端的聚合操作。

2、ShuffledRDD主要是做从这个抓取数据的工作。

3、第二个MapPartitionsRDD把抓取过来的数据再次进行聚合操作。

4、步骤1和步骤3都会涉及到spill的过程。

在作业提交的时候,DAGSchuduler会把Shuffle的成过程切分成map和reduce两个部分。每个部分的任务聚合成一个stage。


Shuffle在map端的时候通过ShuffleMapTask的runTask方法运行Task。

override def runTask(context: TaskContext): MapStatus = {
    // Deserialize the RDD using the broadcast variable.
    //使用广播变量反序列化RDD
    val deserializeStartTime = System.currentTimeMillis()
    val ser = SparkEnv.get.closureSerializer.newInstance()
    val (rdd, dep) = ser.deserialize[(RDD[_], ShuffleDependency[_, _, _])](
      ByteBuffer.wrap(taskBinary.value), Thread.currentThread.getContextClassLoader)
    _executorDeserializeTime = System.currentTimeMillis() - deserializeStartTime

    var writer: ShuffleWriter[Any, Any] = null
    try {
      val manager = SparkEnv.get.shuffleManager
      writer = manager.getWriter[Any, Any](dep.shuffleHandle, partitionId, context)
      writer.write(rdd.iterator(partition, context).asInstanceOf[Iterator[_ <: Product2[Any, Any]]])
      writer.stop(success = true).get
    } catch {
      case e: Exception =>
        try {
          if (writer != null) {
            writer.stop(success = false)
          }
        } catch {
          case e: Exception =>
            log.debug("Could not stop writer", e)
        }
        throw e
    }
  }

ShuffleMapTask结束之后,最后走到DAGScheduler的handleTaskCompletion方法当中源码如下:
以下截取handleTaskCompletion方法的一部分源码。

case smt: ShuffleMapTask =>
            val shuffleStage = stage.asInstanceOf[ShuffleMapStage]
            updateAccumulators(event)
            val status = event.result.asInstanceOf[MapStatus]
            val execId = status.location.executorId
            logDebug("ShuffleMapTask finished on " + execId)
            if (failedEpoch.contains(execId) && smt.epoch <= failedEpoch(execId)) {
              logInfo(s"Ignoring possibly bogus $smt completion from executor $execId")
            } else {
            //把结果添加到OutputLoc数组里,他是按照数据的分区关系来存储数据的
              shuffleStage.addOutputLoc(smt.partitionId, status)
            }

            if (runningStages.contains(shuffleStage) && shuffleStage.pendingPartitions.isEmpty) {
              markStageAsFinished(shuffleStage)
              logInfo("looking for newly runnable stages")
              logInfo("running: " + runningStages)
              logInfo("waiting: " + waitingStages)
              logInfo("failed: " + failedStages)

              // We supply true to increment the epoch number here in case this is a
              // recomputation of the map outputs. In that case, some nodes may have cached
              // locations with holes (from when we detected the error) and will need the
              // epoch incremented to refetch them.
              // TODO: Only increment the epoch number if this is not the first time
              //       we registered these map outputs.
              mapOutputTracker.registerMapOutputs(
                shuffleStage.shuffleDep.shuffleId,
                shuffleStage.outputLocInMapOutputTrackerFormat(),
                changeEpoch = true)

              clearCacheLocs()

              if (!shuffleStage.isAvailable) {
                // Some tasks had failed; let's resubmit this shuffleStage
                // TODO: Lower-level scheduler should also deal with this
                logInfo("Resubmitting " + shuffleStage + " (" + shuffleStage.name +
                  ") because some of its tasks had failed: " +
                  shuffleStage.findMissingPartitions().mkString(", "))
                submitStage(shuffleStage)
              } else {
                // Mark any map-stage jobs waiting on this stage as finished
                if (shuffleStage.mapStageJobs.nonEmpty) {
                  val stats = mapOutputTracker.getStatistics(shuffleStage.shuffleDep)
                  for (job <- shuffleStage.mapStageJobs) {
                    markMapStageJobAsFinished(job, stats)
                  }
                }
                submitWaitingChildStages(shuffleStage)
              }
            }
        }

这部分由于时间仓促,后续会完善补充。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spark是一种大数据处理的框架,它可以处理大量的数据并进行分析。初学者可以通过学习Spark的基本概念和使用方法,了解Spark的工作原理和应用场景。在学习Spark的过程中,需要掌握Spark的核心组件和API,例如Spark Core、Spark SQL、Spark Streaming等。此外,还需要学习Spark的部署和调优,以及与其他大数据技术的集成。 ### 回答2: Spark是一种基于内存的分布式计算框架,是大数据处理中最流行的技术之一。Spark简单易用,能够快速地处理海量数据,尤其是在机器学习和数据挖掘领域中表现突出。本文将从初识Spark的角度入手,介绍Spark的基本概念和使用。 一、Spark的基本概念 1. RDD RDD全称为Resilient Distributed Datasets,中文意思是弹性分布式数据集,它是Spark的核心数据结构。RDD是一个不可变的分布式的对象集合,可以跨越多个节点进行并行处理。一个RDD可以分为多个分区,每个分区可以在不同的节点上存储。 2. DAG DAG即Directed Acyclic Graph(有向无环图),它是Spark中的一个概念,用来表示作业的依赖关系。Spark将一个作业拆分成一系列具有依赖关系的任务,每个任务之间的依赖形成了DAG。 3. 窄依赖和宽依赖 对于一个RDD,如果一个子RDD的每个分区只依赖于父RDD的一个分区,这种依赖就称为窄依赖。如果一个子RDD的每个分区依赖于父RDD的多个分区,这种依赖就称为宽依赖。宽依赖会影响Spark的性能,应尽量避免。 二、Spark的使用 1. 安装Spark 要使用Spark,首先需要在本地或者集群上安装Spark。下载安装包解压缩即可,然后设置环境变量,即可在命令行中运行Spark。 2. Spark Shell Spark Shell是Spark的交互式命令行界面,类似于Python的交互式控制台,可以快速测试Spark代码。在命令行中输入spark-shell即可进入。 3. Spark应用程序 除了Spark Shell,Spark还支持以应用程序的形式运行。要创建一个Spark应用程序,可以使用Scala、Java、Python等语言进行编写。使用Spark API,读取数据、处理数据、保存数据等操作都可以通过编写代码完成。 总之,Spark是一种优秀的分布式计算框架,能够在海量数据处理中发挥出强大的作用。初学者可以从掌握RDD、DAG、依赖关系等基本概念开始,逐步深入学习Spark的使用。 ### 回答3: Spark是一种快速、分布式数据处理框架,它能够在成千上万个计算节点之间分配数据和计算任务。Spark的优势在于它支持多种语言和数据源,可以在内存中快速存储和处理数据。 在初学Spark时,我们需要对Spark的架构和核心组件有一些了解。首先,Spark的核心组件是Spark Core,它是一个可以用于建立各种应用程序的计算引擎。与此同时,Spark持有丰富的库,包括Spark SQL、Spark Streaming、MLLib和GraphX等,以支持在各种数据类型(文本、图像、视频、地理定位数据等)上运行各种算法。 若想要在Spark中进行任务,有两种编程API可供选择:Spark的核心API和Spark的SQL及DataFrame API。Spark的核心API基于RDDs(弹性分布式数据集),它是不可变的分布式对象集合,Spark使用RDD来处理、缓存和共享数据。此外,Spark的SQL及DataFrame API提供了更高层次的语言,可以处理结构化和半结构化数据。 除了组件和API之外,我们还需要了解Spark的4个运行模式:本地模式、Standalone模式、YARN模式和Mesos模式。本地模式由单个JVM上单个线程(本地模式)或四个线程(local[*]模式)运行。Standalone通常用于小规模集群或开发和测试环境。在YARN或Mesos模式下,Spark将任务提交给集群管理器,并通过管理器分配和管理资源。 总体来说,初学Spark时,我们需要了解Spark的核心组件、编程API和运行模式。熟悉这些概念以及Spark的架构,可以帮助我们更好地理解Spark和构建高效且可扩展的Spark应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值