Spark源码学习(8)——NetWork

16 篇文章 4 订阅

本文要解决的问题:

通过对Spark源码的分析,对它Network实现模块有更加深入的了解。


网络管理,由于分布式集群,那么无论master还是worker都离不开网络通讯。Network包位于核心源码org.apache.spark.network中。

Connection

Connection是一个抽象,它有两个子类ReceivingConnection、SendingConnection。接收连接和发送连接。

ReceivingConnection

接收连接。这里面有几个比较重要的方法:getRemoteConnectionManagerId()、processConnectionManagerId(header: MessageChunkHeader)、read()

getRemoteConnectionManagerId():获取远程连接的消息Id,这个方法调用了父类的实现。

这里面有个内部类Inbox,它是一个消息存储集合。里面有个属性

val messages = new HashMapInt,BufferMessage

所有连接到该节点的机器都会被记录到这个messages集合中。

SendingConnection

发送连接。它和ReceivingConnection恰恰相反。

ConnectionId
生成连接的ID对象。生成的原则包括:

override def toString =connectionManagerId.host + “” + connectionManagerId.port +”” + uniqId

ConnectionManager
ConnectionManager,顾名思义管理connection。里面定义定了内部类MessageStatus、配置参数还有一系列的线程池等等。

MessageStatus:消息状态,用于跟踪连接消息状态。



Netty

Server

BlockServer

BlockServer服务器提供的Spark数据块。它有两层协议:

l C2S:用于请求blocks协议(客户端到服务器):按照目录结构

l S2C:用于请blocks协议(服务器到客户端)

frame-length (4bytes), block-id-length (4 bytes), block-id, block-data.

frame-length不包括自身长度。如果block-id-length长度为负,那么这是一个错误消息, 而不是块的数据。真正的长度是frame-length的绝对值。

下面是初始化init源码:

override def init(blockDataManager: BlockDataManager): Unit = {
    val rpcHandler = new NettyBlockRpcServer(conf.getAppId, serializer, blockDataManager)
    var serverBootstrap: Option[TransportServerBootstrap] = None
    var clientBootstrap: Option[TransportClientBootstrap] = None
    if (authEnabled) {
      serverBootstrap = Some(new SaslServerBootstrap(transportConf, securityManager))
      clientBootstrap = Some(new SaslClientBootstrap(transportConf, conf.getAppId, securityManager,
        securityManager.isSaslEncryptionEnabled()))
    }
    transportContext = new TransportContext(transportConf, rpcHandler)
    clientFactory = transportContext.createClientFactory(clientBootstrap.toSeq.asJava)
    server = createServer(serverBootstrap.toList)
    appId = conf.getAppId
    logInfo(s"Server created on ${hostName}:${server.getPort}")
  }

BlockServerHandler

BlockServerHandler请求从客户端和写数据块block回来的处理程序。消息应已被LineBasedFrameDecoder处理和StringDecoder首次如此channelRead0被调用一次每行(即block ID)。

Client
BlockFetchingClient
BlockFetchingClient从org.apache.spark.network.netty.NettyBlockTransferService抓取数据。

查看里面一个比较中要的方法:fetchBlocks。该方法向远程服务器的序列划block,并执行回调。它是异步的,并立即返回。

源码如下:

override def fetchBlocks(
      host: String,
      port: Int,
      execId: String,
      blockIds: Array[String],
      listener: BlockFetchingListener): Unit = {
    logTrace(s"Fetch blocks from $host:$port (executor id $execId)")
    try {
      val blockFetchStarter = new RetryingBlockFetcher.BlockFetchStarter {
        override def createAndStart(blockIds: Array[String], listener: BlockFetchingListener) {
          val client = clientFactory.createClient(host, port)
          new OneForOneBlockFetcher(client, appId, execId, blockIds.toArray, listener).start()
        }
      }

      val maxRetries = transportConf.maxIORetries()
      if (maxRetries > 0) {
        // Note this Fetcher will correctly handle maxRetries == 0; we avoid it just in case there's
        // a bug in this code. We should remove the if statement once we're sure of the stability.
        new RetryingBlockFetcher(transportConf, blockFetchStarter, blockIds, listener).start()
      } else {
        blockFetchStarter.createAndStart(blockIds, listener)
      }
    } catch {
      case e: Exception =>
        logError("Exception while beginning fetchBlocks", e)
        blockIds.foreach(listener.onBlockFetchFailure(_, e))
    }
  }

Conf

Property NameDefaultMeaning
col 3 isright-aligned$1600
spark.driver.host(local hostname)Hostname or IP address for the driver to listen on. This is used for communicating with the executors and the standalone Master.
spark.driver.port(random)Port for the driver to listen on. This is used for communicating with the executors and the standalone Master.
spark.fileserver.port(random)Port for the driver’s HTTP file server to listen on.
spark.broadcast.port(random)Port for the driver’s HTTP broadcast server to listen on. This is not relevant for torrent broadcast.
spark.replClassServer.port(random)Port for the driver’s HTTP class server to listen on. This is only relevant for the Spark shell.
spark.blockManager.port(random)Port for all block managers to listen on. These exist on both the driver and the executors.
spark.executor.port(random)Port for the executor to listen on. This is used for communicating with the driver.
spark.port.maxRetries16Default maximum number of retries when binding to a port before giving up.
spark.akka.frameSize10Maximum message size to allow in “control plane” communication (for serialized tasks and task results), in MB. Increase this if your tasks need to send back large results to the driver (e.g. using collect() on a large dataset).
spark.akka.threads4Number of actor threads to use for communication. Can be useful to increase on large clusters when the driver has a lot of CPU cores.
spark.akka.timeout100Communication timeout between Spark nodes, in seconds.
spark.akka.heartbeat.pauses6000This is set to a larger value to disable failure detector that comes inbuilt akka. It can be enabled again, if you plan to use this feature (Not recommended). Acceptable heart beat pause in seconds for akka. This can be used to control sensitivity to gc pauses. Tune this in combination of spark.akka.heartbeat.interval and spark.akka.failure-detector.threshold if you need to.
spark.akka.failure-detector.threshold300.0This is set to a larger value to disable failure detector that comes inbuilt akka. It can be enabled again, if you plan to use this feature (Not recommended). This maps to akka’s akka.remote.transport-failure-detector.threshold. Tune this in combination of spark.akka.heartbeat.pauses and spark.akka.heartbeat.interval if you need to.
spark.akka.heartbeat.interval1000This is set to a larger value to disable failure detector that comes inbuilt akka. It can be enabled again, if you plan to use this feature (Not recommended). A larger interval value in seconds reduces network overhead and a smaller value ( ~ 1 s) might be more informative for akka’s failure detector. Tune this in combination of spark.akka.heartbeat.pauses and spark.akka.failure-detector.threshold if you need to. Only positive use case for using failure detector can be, a sensistive failure detector can help evict rogue executors really quick. However this is usually not the case as gc pauses and network lags are expected in a real Spark cluster. Apart from that enabling this leads to a lot of exchanges of heart beats between nodes leading to flooding the network with those.
  • 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、付费专栏及课程。

余额充值