sadikovi opened a new pull request, #60269:
URL: https://github.com/apache/spark/pull/60269

   ### What changes were proposed in this pull request?
   
   Serializing a task, or the task binary that a stage's tasks share, can fail 
with an `OutOfMemoryError` when the serialized bytes exceed Java's maximum 
array size (about 2 GB). This PR detects that case and fails the stage with a 
new user-facing error condition, `TASK_SERIALIZATION_TOO_LARGE`, instead of 
treating it as a generic failure.
   
   - `Utils.isJavaArraySizeLimitExceeded` recognizes the two messages the JVM 
uses when an array cannot grow any further: `Required array length ... is too 
large` and `Requested array size exceeds VM limit`. Other `OutOfMemoryError`s, 
such as `Java heap space`, do not match.
   - The new `JavaArraySizeLimitExceeded` exception is a `SparkThrowable` with 
one of two error conditions:
     - `TASK_SERIALIZATION_TOO_LARGE.TASK`: a single task is too large, usually 
because its partition holds too much data created on the driver (e.g. 
`sc.parallelize` of a large local collection).
     - `TASK_SERIALIZATION_TOO_LARGE.TASK_BINARY`: the functions or objects 
shared by the stage's tasks are too large, usually because a closure captures a 
large object.
   
     It does not keep the original `OutOfMemoryError` as its cause, so this 
deterministic size-limit failure is not mistaken for heap exhaustion, which 
more memory would not fix. The JVM's message is kept in `jvmMessage` and logged.
   - `TaskSetManager.resourceOffer`: when serializing a task hits the limit, 
the task set is aborted with the new error, like other task serialization 
failures. `NonFatal` does not match an `OutOfMemoryError`, so before this 
change the error escaped from the scheduler instead.
   - `DAGScheduler.submitMissingTasks`: when serializing the task binary hits 
the limit, the stage is aborted with the new error instead of the generic `Task 
serialization failed: java.lang.OutOfMemoryError: ...` with the 
`OutOfMemoryError` as cause.
   
   ### Why are the changes needed?
   
   The array size limit is deterministic: retrying, or adding driver or 
executor memory, can never make the task fit. Before this change, users saw 
either an `OutOfMemoryError` thrown out of the scheduler, or a stage failure 
whose cause was an `OutOfMemoryError`. Both look like heap exhaustion and 
suggest raising memory, which does not help. The new error says which limit was 
hit and how to fix it: read the data from files or a table, split it into more 
partitions, or use broadcast variables instead of capturing large objects in 
closures.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. When a serialized task or task binary exceeds the 2 GB array limit, the 
job now fails with:
   
   ```
   [TASK_SERIALIZATION_TOO_LARGE.TASK] Cannot run stage 0 because a serialized 
task exceeds the 2 GB size limit. Partition 0 holds too much data created on 
the driver. Read the data from files or a table, or split it into more 
partitions. SQLSTATE: 54000
   ```
   
   or
   
   ```
   [TASK_SERIALIZATION_TOO_LARGE.TASK_BINARY] Cannot run stage 0 because a 
serialized task exceeds the 2 GB size limit. The functions or objects shared by 
its tasks are too large. Use broadcast variables for large objects instead of 
capturing them in functions. SQLSTATE: 54000
   ```
   
   Previously the task case let the `OutOfMemoryError` escape the scheduler, 
and the task binary case failed with `Task serialization failed: 
java.lang.OutOfMemoryError: Required array length ... is too large`. Other 
`OutOfMemoryError`s during serialization behave as before.
   
   ### How was this patch tested?
   
   New tests:
   
   - `FailureSuite`:
     - A task too large to serialize fails with 
`TASK_SERIALIZATION_TOO_LARGE.TASK`, without an `OutOfMemoryError` cause, and 
the driver keeps running jobs afterwards.
     - The same for a task binary, with 
`TASK_SERIALIZATION_TOO_LARGE.TASK_BINARY`.
     - A `Java heap space` error during serialization is still reported as 
before, with the `OutOfMemoryError` as cause (negative case).
   - `TaskSetManagerSuite`: for both JVM messages, `resourceOffer` aborts the 
task set with the new error instead of letting the `OutOfMemoryError` escape.
   - `UtilsSuite`: `isJavaArraySizeLimitExceeded` matches both array-limit 
messages and rejects heap-space errors, an `OutOfMemoryError` without a 
message, and non-OOM throwables.
   - `SparkThrowableSuite` passes with the new error condition.
   
   The tests make serialization throw the `OutOfMemoryError` from 
`writeObject`, so they do not allocate 2 GB.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Claude Opus 5.5)
   
   This pull request and its description were written by Isaac.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to