|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.spark.sql.execution.python |
| 18 | + |
| 19 | +import java.io.DataOutputStream |
| 20 | +import java.net.Socket |
| 21 | + |
| 22 | +import org.apache.arrow.vector.VectorSchemaRoot |
| 23 | +import org.apache.arrow.vector.ipc.ArrowStreamWriter |
| 24 | + |
| 25 | +import org.apache.spark.{SparkEnv, TaskContext} |
| 26 | +import org.apache.spark.api.python.{BasePythonRunner, PythonRDD} |
| 27 | +import org.apache.spark.sql.catalyst.InternalRow |
| 28 | +import org.apache.spark.sql.execution.arrow.ArrowWriter |
| 29 | +import org.apache.spark.sql.types.StructType |
| 30 | +import org.apache.spark.sql.util.ArrowUtils |
| 31 | +import org.apache.spark.util.Utils |
| 32 | + |
| 33 | +/** |
| 34 | + * A trait that can be mixed-in with [[BasePythonRunner]]. It implements the logic from |
| 35 | + * JVM (an iterator of internal rows) to Python (Arrow). |
| 36 | + */ |
| 37 | +private[python] trait PythonArrowInput { self: BasePythonRunner[Iterator[InternalRow], _] => |
| 38 | + protected val workerConf: Map[String, String] |
| 39 | + |
| 40 | + protected val schema: StructType |
| 41 | + |
| 42 | + protected val timeZoneId: String |
| 43 | + |
| 44 | + protected def handleMetadataBeforeExec(stream: DataOutputStream): Unit = { |
| 45 | + // Write config for the worker as a number of key -> value pairs of strings |
| 46 | + stream.writeInt(workerConf.size) |
| 47 | + for ((k, v) <- workerConf) { |
| 48 | + PythonRDD.writeUTF(k, stream) |
| 49 | + PythonRDD.writeUTF(v, stream) |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + protected override def newWriterThread( |
| 54 | + env: SparkEnv, |
| 55 | + worker: Socket, |
| 56 | + inputIterator: Iterator[Iterator[InternalRow]], |
| 57 | + partitionIndex: Int, |
| 58 | + context: TaskContext): WriterThread = { |
| 59 | + new WriterThread(env, worker, inputIterator, partitionIndex, context) { |
| 60 | + |
| 61 | + protected override def writeCommand(dataOut: DataOutputStream): Unit = { |
| 62 | + handleMetadataBeforeExec(dataOut) |
| 63 | + PythonUDFRunner.writeUDFs(dataOut, funcs, argOffsets) |
| 64 | + } |
| 65 | + |
| 66 | + protected override def writeIteratorToStream(dataOut: DataOutputStream): Unit = { |
| 67 | + val arrowSchema = ArrowUtils.toArrowSchema(schema, timeZoneId) |
| 68 | + val allocator = ArrowUtils.rootAllocator.newChildAllocator( |
| 69 | + s"stdout writer for $pythonExec", 0, Long.MaxValue) |
| 70 | + val root = VectorSchemaRoot.create(arrowSchema, allocator) |
| 71 | + |
| 72 | + Utils.tryWithSafeFinally { |
| 73 | + val arrowWriter = ArrowWriter.create(root) |
| 74 | + val writer = new ArrowStreamWriter(root, null, dataOut) |
| 75 | + writer.start() |
| 76 | + |
| 77 | + while (inputIterator.hasNext) { |
| 78 | + val nextBatch = inputIterator.next() |
| 79 | + |
| 80 | + while (nextBatch.hasNext) { |
| 81 | + arrowWriter.write(nextBatch.next()) |
| 82 | + } |
| 83 | + |
| 84 | + arrowWriter.finish() |
| 85 | + writer.writeBatch() |
| 86 | + arrowWriter.reset() |
| 87 | + } |
| 88 | + // end writes footer to the output stream and doesn't clean any resources. |
| 89 | + // It could throw exception if the output stream is closed, so it should be |
| 90 | + // in the try block. |
| 91 | + writer.end() |
| 92 | + } { |
| 93 | + // If we close root and allocator in TaskCompletionListener, there could be a race |
| 94 | + // condition where the writer thread keeps writing to the VectorSchemaRoot while |
| 95 | + // it's being closed by the TaskCompletion listener. |
| 96 | + // Closing root and allocator here is cleaner because root and allocator is owned |
| 97 | + // by the writer thread and is only visible to the writer thread. |
| 98 | + // |
| 99 | + // If the writer thread is interrupted by TaskCompletionListener, it should either |
| 100 | + // (1) in the try block, in which case it will get an InterruptedException when |
| 101 | + // performing io, and goes into the finally block or (2) in the finally block, |
| 102 | + // in which case it will ignore the interruption and close the resources. |
| 103 | + root.close() |
| 104 | + allocator.close() |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | +} |
0 commit comments