当MemoryStore没有足够的空间时,就会使用DiskStore将块存入磁盘。DiskStore继承自BlockStore,实现了getBytes、putBytes、putArray、putIterator等方法。
val minMemoryMapBytes = blockManager.conf.getSizeAsBytes("spark.storage.memoryMapThreshold", "2m")
spark.storage.memoryMapThreshold属性用于设置spark从磁盘上读取一个块后,映射到内存块的最小大小。这阻止了spark映射过小的内存块。通常,内存映射块是有开销的,应该比接近或小于操作系统的页大小。而对于小于该值的Block文件,则直接将该文件的内容读取到字节缓存区,而不是映射到内存块。
1. NIO读取方法 getBytes
private def getBytes(file: File, offset: Long, length: Long): Option[ByteBuffer] = {
val channel = new RandomAccessFile(file, "r").getChannel
Utils.tryWithSafeFinally {
// For small files, directly read rather than memory map
if (length < minMemoryMapBytes) {
val buf = ByteBuffer.allocate(length.toI