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

   ### What changes were proposed in this pull request?
   
   This PR fixes `pythonDataSent` (`data sent to Python workers`) SQL metric 
undercounting in `GroupedPythonArrowInput` and `CoGroupedArrowPythonRunner`:
   
   1. **`GroupedPythonArrowInput`**:
      - Move `val startData = dataOut.size()` to the start of 
`writeNextInputToStream` (matching `CoGroupedArrowPythonRunner` and 
`BasePythonUDFRunner`) so that each group's 4-byte marker 
(`dataOut.writeInt(1)`), per-group Arrow schema message 
(`ArrowWriterWrapper.createAndStartArrowWriter`), and final 4-byte end-of-data 
marker (`dataOut.writeInt(0)`) are included in `pythonDataSent`.
      - Measure `dataOut.size() - startData` before calling 
`writer.streamWriter.end()`, and add the `writer.streamWriter.bytesWritten()` 
delta around `writer.streamWriter.end()`.
   2. **`CoGroupedArrowPythonRunner`**:
      - Measure `dataOut.size() - startData` before calling 
`groupWriterToClose.streamWriter.end()`, and add the 
`groupWriterToClose.streamWriter.bytesWritten()` delta around 
`streamWriter.end()`.
      - Update `pythonDataSent` when writing the final 4-byte end-of-data 
marker (`dataOut.writeInt(0)`).
   
   ### Why are the changes needed?
   
   Following up on SPARK-60313 (#59570) and SPARK-60343 (#59597):
   
   1. In `GroupedPythonArrowInput`, each group starts a fresh Arrow IPC stream 
by writing `dataOut.writeInt(1)` and calling 
`ArrowWriterWrapper.createAndStartArrowWriter` (which writes the per-group 
Arrow schema message via `streamWriter.start()`). Because `startData = 
dataOut.size()` was recorded after that block, neither the group header nor the 
per-group Arrow schema message was counted in `pythonDataSent`. When there are 
many small groups, the per-group schema messages account for a large share of 
the bytes sent to the Python worker.
   2. In both `GroupedPythonArrowInput` and `CoGroupedArrowPythonRunner`, each 
group's `ArrowWriterWrapper.streamWriter` is bound to the `DataOutputStream` of 
the first batch of that group, while `BatchedPythonArrowInput.writeSizedBatch` 
writes record batches to the current call's `dataOut`. When a group spans 
multiple batches across buffer refills—where `ReaderInputStream` (SPARK-44705) 
and `PipelinedWriterRunnable` (SPARK-60343) create a new 
`DataOutputStream`—`streamWriter.end()` writes the 8-byte Arrow EOS footer 
through the first batch's `DataOutputStream`, so `dataOut.size() - startData` 
on the current stream missed those 8 bytes.
   3. Neither runner updated `pythonDataSent` when writing the final 4-byte 
end-of-data marker (`dataOut.writeInt(0)`).
   
   Closes #60274.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. The `data sent to Python workers` metric for grouped and cogrouped 
Pandas/Arrow UDFs (`GroupedPythonArrowInput` and `CoGroupedArrowPythonRunner`) 
now counts all bytes written to the stream.
   
   ### How was this patch tested?
   
   Added `GroupedPythonRunnerDataSentSuite` (`build/sbt 'sql/testOnly 
*GroupedPythonRunnerDataSentSuite'`), driving `GroupedPythonArrowInput` and 
`CoGroupedArrowPythonRunner` with both a new `DataOutputStream` per call and a 
single shared stream across multi-batch groups, empty left/right cogroups, and 
empty partitions.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Google Antigravity


-- 
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