本文主要是介绍launcher/memory两个包的数据结构
private[spark] abstract class LauncherBackend {
用来和启动服务器进行对话的。会创建BackendConnection对象,立面又封装了Socket,Socket有能够获取输入输出流
private[spark] class WorkerCommandBuilder(sparkHome: String, memoryMb: Int,
command: Command) extends AbstractCommandBuilder {
用来构建的是java jvm命令
private[memory] class ExecutionMemoryPool( lock: Object, memoryMode: MemoryMode
) extends MemoryPool(lock) with Logging {
立面维护了一个
@GuardedBy("lock") private val memoryForTask = new mutable.HashMap[Long, Long]() 变量。key是任务ID,value是
当前任务使用的内存大小。任务申请了内存就会在集合立面注册、释放内存就会从集合立面移除或者减少value的值。
private[spark] abstract class MemoryManager( conf: SparkConf, numCores: Int, onHeapStorageMemory: Long, onHeapExecutionMemory: Long) extends Logging { 用于管理堆上、非堆上运行和存储的内存分配
@GuardedBy("this") protected val onHeapStorageMemoryPool = new StorageMemoryPool(this, MemoryMode.ON_HEAP) @GuardedBy("this") protected val offHeapStorageMemoryPool = new StorageMemoryPool(this, MemoryMode.OFF_HEAP) @GuardedBy("this") protected val onHeapExecutionMemoryPool = new ExecutionMemoryPool(this, MemoryMode.ON_HEAP) @GuardedBy("this") protected val offHeapExecutionMemoryPool = new ExecutionMemoryPool(this, MemoryMode.OFF_HEAP)
private[memory] abstract class MemoryPool(lock: Object) { 内存池
@GuardedBy("lock") private[this] var _poolSize: Long = 0立面就维护了这么一个字段,用于表示当前内存池的大小,因为是var 所以可以增加内存大小、亦可以减少。
private[spark] class StaticMemoryManager( conf: SparkConf, maxOnHeapExecutionMemory: Long, override val maxOnHeapStorageMemory: Long, numCores: Int) extends MemoryManager( conf, numCores, maxOnHeapStorageMemory, maxOnHeapExecutionMemory) {只支持对堆的内存管理,其实立面就维护了pool对象,pool立面有一个总大小、已使用的字段,申请分配的时候就是
修改已使用的字段。
private[memory] class StorageMemoryPool( lock: Object, memoryMode: MemoryMode
) extends MemoryPool(lock) with Logging {
主要是通过修改
@GuardedBy("lock") private[this] var _memoryUsed: Long = 0L
@GuardedBy("lock") private[this] var _poolSize: Long = 0
这两个字段来进行的管理。
private[spark] class UnifiedMemoryManager private[memory] ( conf: SparkConf, val maxHeapMemory: Long, onHeapStorageRegionSize: Long, numCores: Int) extends MemoryManager( conf, numCores, onHeapStorageRegionSize, maxHeapMemory - onHeapStorageRegionSize) {
简单地说,这个类的策略是这样的。比如:总共分配2G(用于存储和执行任务),1g用于存储,1g用于运行。当
存储用满了1G后,发现运行还有剩余的0.5G,那么存储会到运行这边去借用。反过来也是。