-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-3151] [Block Manager] DiskStore.getBytes fails for files larger than 2GB #18855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
fc3f1d7
8468738
1580449
c5028f5
908c786
67f4259
8338b4e
4a320e6
5a5c344
6cbe8d0
f6fb9e9
0e3cd82
a911c85
c877dcf
8be899f
732073c
d0c98a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ import java.util.concurrent.ConcurrentHashMap | |
| import scala.collection.mutable.ListBuffer | ||
|
|
||
| import com.google.common.io.{ByteStreams, Closeables, Files} | ||
| import io.netty.channel.FileRegion | ||
| import io.netty.channel.{DefaultFileRegion, FileRegion} | ||
| import io.netty.util.AbstractReferenceCounted | ||
|
|
||
| import org.apache.spark.{SecurityManager, SparkConf} | ||
|
|
@@ -47,6 +47,8 @@ private[spark] class DiskStore( | |
| securityManager: SecurityManager) extends Logging { | ||
|
|
||
| private val minMemoryMapBytes = conf.getSizeAsBytes("spark.storage.memoryMapThreshold", "2m") | ||
| private val maxMemoryMapBytes = conf.getSizeAsBytes("spark.storage.memoryMapLimitForTests", | ||
| s"${Int.MaxValue}b") | ||
| private val blockSizes = new ConcurrentHashMap[String, Long]() | ||
|
|
||
| def getSize(blockId: BlockId): Long = blockSizes.get(blockId.name) | ||
|
|
@@ -108,25 +110,7 @@ private[spark] class DiskStore( | |
| new EncryptedBlockData(file, blockSize, conf, key) | ||
|
|
||
| case _ => | ||
| val channel = new FileInputStream(file).getChannel() | ||
| if (blockSize < minMemoryMapBytes) { | ||
| // For small files, directly read rather than memory map. | ||
| Utils.tryWithSafeFinally { | ||
| val buf = ByteBuffer.allocate(blockSize.toInt) | ||
| JavaUtils.readFully(channel, buf) | ||
| buf.flip() | ||
| new ByteBufferBlockData(new ChunkedByteBuffer(buf), true) | ||
| } { | ||
| channel.close() | ||
| } | ||
| } else { | ||
| Utils.tryWithSafeFinally { | ||
| new ByteBufferBlockData( | ||
| new ChunkedByteBuffer(channel.map(MapMode.READ_ONLY, 0, file.length)), true) | ||
| } { | ||
| channel.close() | ||
| } | ||
| } | ||
| new DiskBlockData(minMemoryMapBytes, maxMemoryMapBytes, file, blockSize) | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -165,6 +149,64 @@ private[spark] class DiskStore( | |
|
|
||
| } | ||
|
|
||
| private class DiskBlockData( | ||
| minMemoryMapBytes : Long, | ||
|
||
| maxMemoryMapBytes : Long, | ||
| file: File, | ||
| blockSize: Long) extends BlockData { | ||
|
|
||
| override def toInputStream(): InputStream = new FileInputStream(file) | ||
|
|
||
| /** | ||
| * Returns a Netty-friendly wrapper for the block's data. | ||
| * | ||
| * Please see `ManagedBuffer.convertToNetty()` for more details. | ||
| */ | ||
| override def toNetty(): AnyRef = new DefaultFileRegion(file, 0, size) | ||
|
|
||
| override def toChunkedByteBuffer(allocator: (Int) => ByteBuffer): ChunkedByteBuffer = { | ||
| Utils.tryWithResource(open()) { channel => | ||
| var remaining = blockSize | ||
| val chunks = new ListBuffer[ByteBuffer]() | ||
| while (remaining > 0) { | ||
| val chunkSize = math.min(remaining, maxMemoryMapBytes) | ||
| val chunk = allocator(chunkSize.toInt) | ||
| remaining -= chunkSize | ||
| JavaUtils.readFully(channel, chunk) | ||
| chunk.flip() | ||
| chunks += chunk | ||
| } | ||
| new ChunkedByteBuffer(chunks.toArray) | ||
| } | ||
| } | ||
|
|
||
| override def toByteBuffer(): ByteBuffer = { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we will still hit the 2g limitation here, I'm wondering which end-to-end use cases are affected by it.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. indeed. further more, I think this plays well with the comment about future deprecation of
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cloud-fan |
||
| // I chose to leave to original error message here | ||
| // since users are unfamiliar with the configureation key | ||
| // controling maxMemoryMapBytes for tests | ||
| require(blockSize < maxMemoryMapBytes, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why we need this? I think we can see the original error message if we don't have this check and go to the memory map code path.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh this is to verify the bug fix. |
||
| s"can't create a byte buffer of size $blockSize" + | ||
| s" since it exceeds Int.MaxValue ${Int.MaxValue}.") | ||
|
||
| Utils.tryWithResource(open()) { channel => | ||
| if (blockSize < minMemoryMapBytes) { | ||
| // For small files, directly read rather than memory map. | ||
| val buf = ByteBuffer.allocate(blockSize.toInt) | ||
| JavaUtils.readFully(channel, buf) | ||
| buf.flip() | ||
| buf | ||
| } else { | ||
| channel.map(MapMode.READ_ONLY, 0, file.length) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override def size: Long = blockSize | ||
|
|
||
| override def dispose(): Unit = {} | ||
|
|
||
| private def open() = new FileInputStream(file).getChannel | ||
| } | ||
|
|
||
| private class EncryptedBlockData( | ||
| file: File, | ||
| blockSize: Long, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,18 +50,18 @@ class DiskStoreSuite extends SparkFunSuite { | |
| val diskStoreMapped = new DiskStore(conf.clone().set(confKey, "0"), diskBlockManager, | ||
| securityManager) | ||
| diskStoreMapped.putBytes(blockId, byteBuffer) | ||
| val mapped = diskStoreMapped.getBytes(blockId).asInstanceOf[ByteBufferBlockData].buffer | ||
| val mapped = diskStoreMapped.getBytes(blockId).toByteBuffer() | ||
| assert(diskStoreMapped.remove(blockId)) | ||
|
|
||
| val diskStoreNotMapped = new DiskStore(conf.clone().set(confKey, "1m"), diskBlockManager, | ||
| securityManager) | ||
| diskStoreNotMapped.putBytes(blockId, byteBuffer) | ||
| val notMapped = diskStoreNotMapped.getBytes(blockId).asInstanceOf[ByteBufferBlockData].buffer | ||
| val notMapped = diskStoreNotMapped.getBytes(blockId).toByteBuffer() | ||
|
|
||
| // Not possible to do isInstanceOf due to visibility of HeapByteBuffer | ||
| assert(notMapped.getChunks().forall(_.getClass.getName.endsWith("HeapByteBuffer")), | ||
| assert(notMapped.getClass.getName.endsWith("HeapByteBuffer"), | ||
| "Expected HeapByteBuffer for un-mapped read") | ||
| assert(mapped.getChunks().forall(_.isInstanceOf[MappedByteBuffer]), | ||
| assert(mapped.isInstanceOf[MappedByteBuffer], | ||
| "Expected MappedByteBuffer for mapped read") | ||
|
|
||
| def arrayFromByteBuffer(in: ByteBuffer): Array[Byte] = { | ||
|
|
@@ -70,8 +70,8 @@ class DiskStoreSuite extends SparkFunSuite { | |
| array | ||
| } | ||
|
|
||
| assert(Arrays.equals(mapped.toArray, bytes)) | ||
| assert(Arrays.equals(notMapped.toArray, bytes)) | ||
| assert(Arrays.equals(new ChunkedByteBuffer(mapped).toArray, bytes)) | ||
| assert(Arrays.equals(new ChunkedByteBuffer(notMapped).toArray, bytes)) | ||
| } | ||
|
|
||
| test("block size tracking") { | ||
|
|
@@ -92,6 +92,45 @@ class DiskStoreSuite extends SparkFunSuite { | |
| assert(diskStore.getSize(blockId) === 0L) | ||
| } | ||
|
|
||
| test("blocks larger than 2gb") { | ||
| val conf = new SparkConf() | ||
| .set("spark.storage.memoryMapLimitForTests", "10k" ) | ||
| val diskBlockManager = new DiskBlockManager(conf, deleteFilesOnStop = true) | ||
| val diskStore = new DiskStore(conf, diskBlockManager, new SecurityManager(conf)) | ||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: remove this empty line |
||
|
|
||
| val blockId = BlockId("rdd_1_2") | ||
| diskStore.put(blockId) { chan => | ||
| val arr = new Array[Byte](1024) | ||
| for { | ||
| _ <- 0 until 20 | ||
| } { | ||
| val buf = ByteBuffer.wrap(arr) | ||
| while (buf.hasRemaining()) { | ||
| chan.write(buf) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| val blockData = diskStore.getBytes(blockId) | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kiszk, this is the test case I was referring to. |
||
| assert(blockData.size == 20 * 1024) | ||
|
|
||
| val chunkedByteBuffer = blockData.toChunkedByteBuffer(ByteBuffer.allocate) | ||
| val chunks = chunkedByteBuffer.chunks | ||
| assert(chunks.size === 2) | ||
| for( chunk <- chunks ) { | ||
|
||
| assert(chunk.limit === 10 * 1024) | ||
| } | ||
|
|
||
| val e = intercept[IllegalArgumentException]{ | ||
| blockData.toByteBuffer() | ||
| } | ||
|
|
||
| assert(e.getMessage == | ||
|
||
| s"requirement failed: can't create a byte buffer of size ${blockData.size}" + | ||
| s" since it exceeds Int.MaxValue ${Int.MaxValue}.") | ||
| } | ||
|
|
||
| test("block data encryption") { | ||
| val testDir = Utils.createTempDir() | ||
| val testData = new Array[Byte](128 * 1024) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: just
Int.MaxValue.toString