Repository navigation
Description
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:
-
Per-group header and Arrow schema omitted in
GroupedPythonArrowInput:
UnlikeCoGroupedArrowPythonRunner(which recordsval startData = dataOut.size()at the start ofwriteNextInputToStream),GroupedPythonArrowInputrecordsval startData = dataOut.size()only insideif (nextBatchStart.hasNext), after writing the 4-byte group marker (dataOut.writeInt(1)) and the per-group Arrow schema message (ArrowWriterWrapper.createAndStartArrowWriter, which callsstreamWriter.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. -
Per-group Arrow stream footer (
streamWriter.end()) lost across buffer refills in bothGroupedPythonArrowInputandCoGroupedArrowPythonRunner:
Each group (or left/right group inCoGroupedArrowPythonRunner) creates anArrowWriterWrapperwhosestreamWriter: ArrowStreamWriteris bound to thedataOut: DataOutputStreamof the first batch of that group. Subsequent batches in the group are serialized directly to the current call'sdataOutviaBatchedPythonArrowInput.writeSizedBatch. However, when the group finishes (!nextBatchStart.hasNext),streamWriter.end()writes the 8-byte Arrow end-of-stream marker through the first call'sDataOutputStream. WhenReaderInputStream(SPARK-44705) orPipelinedWriterRunnable(SPARK-60343) passes a newDataOutputStreamon buffer refills,dataOut.size() - startDataon the current stream does not include the bytes written bystreamWriter.end(). -
End-of-data marker (
dataOut.writeInt(0)) omitted in both runners:
When all groups are exhausted, bothGroupedPythonArrowInputandCoGroupedArrowPythonRunnerwrite the 4-byte end-of-data marker0and returnfalsewithout adding those 4 bytes topythonDataSent.
Proposed Fix
- In
GroupedPythonArrowInput, captureval startData = dataOut.size()at the beginning ofwriteNextInputToStream(matchingCoGroupedArrowPythonRunnerandBasePythonUDFRunner). - In both
GroupedPythonArrowInputandCoGroupedArrowPythonRunner, measuredataOut.size() - startDatabefore callingstreamWriter.end(), and add thestreamWriter.bytesWritten()delta aroundstreamWriter.end()so the 8-byte Arrow stream footer is counted regardless of whetherdataOutis the original stream or a new stream from a buffer refill. - Update
pythonDataSentwhen writing the finaldataOut.writeInt(0)end-of-data marker.
Activity