Skip to content

Commit 90f73fc

Browse files
aarondavrxin
authored andcommitted
[SPARK-3889] Attempt to avoid SIGBUS by not mmapping files in ConnectionManager
In general, individual shuffle blocks are frequently small, so mmapping them often creates a lot of waste. It may not be bad to mmap the larger ones, but it is pretty inconvenient to get configuration into ManagedBuffer, and besides it is unlikely to help all that much. Author: Aaron Davidson <[email protected]> Closes #2742 from aarondav/mmap and squashes the following commits: a152065 [Aaron Davidson] Add other pathway back 52b6cd2 [Aaron Davidson] [SPARK-3889] Attempt to avoid SIGBUS by not mmapping files in ConnectionManager
1 parent 411cf29 commit 90f73fc

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

core/src/main/scala/org/apache/spark/network/ManagedBuffer.scala

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,27 @@ sealed abstract class ManagedBuffer {
6666
final class FileSegmentManagedBuffer(val file: File, val offset: Long, val length: Long)
6767
extends ManagedBuffer {
6868

69+
/**
70+
* Memory mapping is expensive and can destabilize the JVM (SPARK-1145, SPARK-3889).
71+
* Avoid unless there's a good reason not to.
72+
*/
73+
private val MIN_MEMORY_MAP_BYTES = 2 * 1024 * 1024;
74+
6975
override def size: Long = length
7076

7177
override def nioByteBuffer(): ByteBuffer = {
7278
var channel: FileChannel = null
7379
try {
7480
channel = new RandomAccessFile(file, "r").getChannel
75-
channel.map(MapMode.READ_ONLY, offset, length)
81+
// Just copy the buffer if it's sufficiently small, as memory mapping has a high overhead.
82+
if (length < MIN_MEMORY_MAP_BYTES) {
83+
val buf = ByteBuffer.allocate(length.toInt)
84+
channel.read(buf, offset)
85+
buf.flip()
86+
buf
87+
} else {
88+
channel.map(MapMode.READ_ONLY, offset, length)
89+
}
7690
} catch {
7791
case e: IOException =>
7892
Try(channel.size).toOption match {

0 commit comments

Comments
 (0)