Jasmine Sun created SPARK-62331:
-----------------------------------

             Summary: DiskBlockData.toNettyForSsl() reads entire disk-cached 
blocks into memory instead of streaming them, causing executor OOMs with RPC TLS
                 Key: SPARK-62331
                 URL: https://issues.apache.org/jira/browse/SPARK-62331
             Project: Spark
          Issue Type: Improvement
          Components: Spark Core
    Affects Versions: 4.3.0, 4.1.1
         Environment: Spark 4.1.1, Java 21, Kubernetes (no external shuffle 
service). spark.ssl.rpc.enabled=true, spark.ssl.rpc.openSslEnabled=true, 
spark.network.crypto.enabled=false
            Reporter: Jasmine Sun


When RPC TLS is enabled ({{spark.ssl.rpc.enabled=true}}), an executor that 
serves a disk-cached block ({{persist(StorageLevel.DISK_ONLY)}}) to another 
executor reads the entire block into memory before sending it. With large 
cached partitions, this gets executors OOMKilled.

In {{DiskStore.scala}}, {{DiskBlockData}} does:
{code:scala}
override def toNetty(): AnyRef = new DefaultFileRegion(file, 0, size)   // 
without TLS: streams the file

override def toNettyForSsl(): AnyRef =
  toChunkedByteBuffer(ByteBuffer.allocate).toNettyForSsl                 // 
with TLS: reads the whole block into heap buffers
{code}

So serving a 1.5 GB block under TLS allocates 1.5 GB of heap, plus a temporary 
direct buffer of the same size while {{FileChannelImpl.read}} reads into the 
heap buffer. Several concurrent fetches from the same executor exceed the 
container's memory limit.

Zero-copy ({{DefaultFileRegion}}) can't be used with TLS, but the block doesn't 
have to be loaded whole. Shuffle files are already streamed under TLS: 
{{FileSegmentManagedBuffer.convertToNettyForSsl()}} wraps the file in a 
{{ChunkedStream}} of {{spark.network.ssl.maxEncryptedBlockSize}} (64 KB by 
default). {{DiskBlockData}} could do the same.

h3. Reproduction

A test job caches 24 partitions of 1.5 GB of random bytes with 
{{persist(StorageLevel.DISK_ONLY)}}, then runs rounds in which every task 
fetches one cached partition from an executor on another host. Each executor 
has 8 GB of heap and a 10 GB container limit. Same job, same sizing, only the 
RPC encryption differs:

||Metric||AES ({{spark.network.crypto.enabled=true}})||RPC TLS||
|Executor OOM kills|0|5|
|Peak memory per executor|1.9 GB|10 GB (the limit)|
|Peak heap committed|1.6 GB|8 GB (full)|
|Peak direct memory|~0|4.5 GB|

Allocation profiles from the TLS run: {{DiskBlockData.toNettyForSsl}} -> 
{{toChunkedByteBuffer}} -> {{ByteBuffer.allocate}} accounted for about 40% of 
all heap allocations, and {{JavaUtils.readFully}} -> {{FileChannelImpl.read}} 
for about 90% of all direct memory allocations. Neither appears in the AES run. 
Every OOM kill happened within seconds of a fetch round starting.

With 96 partitions of 384 MB (the same total data), peak direct memory dropped 
to 0.75 GB, but there were still 2 OOM kills, because every block is still 
copied onto the heap in full.

{{-Djdk.nio.maxCachedBufferSize=262144}} prevents the JDK from caching the 
temporary direct buffers, which helps, but it doesn't remove the full-block 
copy.

h3. Proposed change

Stream the block from the file under TLS, the same way 
{{FileSegmentManagedBuffer}} does:
{code:scala}
override def toNettyForSsl(): AnyRef =
  new ChunkedStream(new LimitedInputStream(new FileInputStream(file), 
blockSize), sslChunkSize)
{code}
where {{sslChunkSize}} comes from {{spark.network.ssl.maxEncryptedBlockSize}}. 
{{SslMessageEncoder}} already accepts a {{ChunkedStream}} body (it wraps it in 
{{EncryptedMessageWithHeader}}), and the {{ChunkedWriteHandler}} is already in 
the pipeline when TLS is enabled.

{{EncryptedBlockData.toNettyForSsl()}} (used with 
{{spark.io.encryption.enabled}}) has the same pattern and could be changed the 
same way.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to