jischeng opened a new issue, #60274:
URL: https://github.com/apache/spark/issues/60274
### What problem are you trying to solve?
Following up on SPARK-60313 (#59570) and SPARK-60343 (#59597),
`GroupedPythonArrowInput` (used by grouped map / aggregate / window Pandas and
Arrow UDFs) and `CoGroupedArrowPythonRunner` (used by cogrouped map Pandas and
Arrow UDFs) still undercount the `pythonDataSent` (`data sent to Python
workers`) SQL metric:
1. **Per-group header and Arrow schema omitted in
`GroupedPythonArrowInput`**:
Unlike `CoGroupedArrowPythonRunner` (which records `val startData =
dataOut.size()` at the start of `writeNextInputToStream`),
`GroupedPythonArrowInput` records `val startData = dataOut.size()` only inside
`if (nextBatchStart.hasNext)`, *after* writing the 4-byte group marker
(`dataOut.writeInt(1)`) and the per-group Arrow schema message
(`ArrowWriterWrapper.createAndStartArrowWriter`, which calls
`streamWriter.start()`). When there are many groups, the per-group Arrow schema
messages represent a large fraction of the bytes sent to the Python worker and
are never counted.
2. **Per-group Arrow stream footer (`streamWriter.end()`) lost across buffer
refills in both `GroupedPythonArrowInput` and `CoGroupedArrowPythonRunner`**:
Each group (or left/right group in `CoGroupedArrowPythonRunner`) creates
an `ArrowWriterWrapper` whose `streamWriter: ArrowStreamWriter` is bound to the
`dataOut: DataOutputStream` of the *first* batch of that group. Subsequent
batches in the group are serialized directly to the current call's `dataOut`
via `BatchedPythonArrowInput.writeSizedBatch`. However, when the group finishes
(`!nextBatchStart.hasNext`), `streamWriter.end()` writes the 8-byte Arrow
end-of-stream marker through the *first* call's `DataOutputStream`. When
`ReaderInputStream` (SPARK-44705) or `PipelinedWriterRunnable` (SPARK-60343)
passes a new `DataOutputStream` on buffer refills, `dataOut.size() - startData`
on the current stream does not include the bytes written by
`streamWriter.end()`.
3. **End-of-data marker (`dataOut.writeInt(0)`) omitted in both runners**:
When all groups are exhausted, both `GroupedPythonArrowInput` and
`CoGroupedArrowPythonRunner` write the 4-byte end-of-data marker `0` and return
`false` without adding those 4 bytes to `pythonDataSent`.
### Proposed Fix
- In `GroupedPythonArrowInput`, capture `val startData = dataOut.size()` at
the beginning of `writeNextInputToStream` (matching
`CoGroupedArrowPythonRunner` and `BasePythonUDFRunner`).
- In both `GroupedPythonArrowInput` and `CoGroupedArrowPythonRunner`,
measure `dataOut.size() - startData` before calling `streamWriter.end()`, and
add the `streamWriter.bytesWritten()` delta around `streamWriter.end()` so the
8-byte Arrow stream footer is counted regardless of whether `dataOut` is the
original stream or a new stream from a buffer refill.
- Update `pythonDataSent` when writing the final `dataOut.writeInt(0)`
end-of-data marker.
--
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]