Github user sun-rui commented on a diff in the pull request:

    https://github.com/apache/spark/pull/10947#discussion_r55477889
  
    --- Diff: core/src/main/scala/org/apache/spark/api/r/RRunner.scala ---
    @@ -0,0 +1,367 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one or more
    + * contributor license agreements.  See the NOTICE file distributed with
    + * this work for additional information regarding copyright ownership.
    + * The ASF licenses this file to You under the Apache License, Version 2.0
    + * (the "License"); you may not use this file except in compliance with
    + * the License.  You may obtain a copy of the License at
    + *
    + *    http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing, software
    + * distributed under the License is distributed on an "AS IS" BASIS,
    + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    + * See the License for the specific language governing permissions and
    + * limitations under the License.
    + */
    +
    +package org.apache.spark.api.r
    +
    +import java.io._
    +import java.net.{InetAddress, ServerSocket}
    +import java.util.Arrays
    +
    +import scala.io.Source
    +import scala.util.Try
    +
    +import org.apache.spark._
    +import org.apache.spark.broadcast.Broadcast
    +import org.apache.spark.util.Utils
    +
    +/**
    + * A helper class to run R UDFs in Spark.
    + */
    +private[spark] class RRunner[U](
    +    func: Array[Byte],
    +    deserializer: String,
    +    serializer: String,
    +    packageNames: Array[Byte],
    +    broadcastVars: Array[Broadcast[Object]],
    +    numPartitions: Int = -1)
    +  extends Logging {
    +  private var bootTime: Double = _
    +  private var dataStream: DataInputStream = _
    +  val readData = numPartitions match {
    +    case -1 =>
    +      serializer match {
    +        case SerializationFormats.STRING => readStringData _
    +        case _ => readByteArrayData _
    +      }
    +    case _ => readShuffledData _
    +  }
    +
    +  def compute(
    +      inputIterator: Iterator[_],
    +      partitionIndex: Int,
    +      context: TaskContext): Iterator[U] = {
    +    // Timing start
    +    bootTime = System.currentTimeMillis / 1000.0
    +
    +    // we expect two connections
    +    val serverSocket = new ServerSocket(0, 2, 
InetAddress.getByName("localhost"))
    +    val listenPort = serverSocket.getLocalPort()
    +
    +    // The stdout/stderr is shared by multiple tasks, because we use one 
daemon
    +    // to launch child process as worker.
    +    val errThread = RRunner.createRWorker(listenPort)
    +
    +    // We use two sockets to separate input and output, then it's easy to 
manage
    +    // the lifecycle of them to avoid deadlock.
    +    // TODO: optimize it to use one socket
    +
    +    // the socket used to send out the input of task
    +    serverSocket.setSoTimeout(10000)
    +    val inSocket = serverSocket.accept()
    +    startStdinThread(inSocket.getOutputStream(), inputIterator, 
partitionIndex)
    +
    +    // the socket used to receive the output of task
    +    val outSocket = serverSocket.accept()
    +    val inputStream = new BufferedInputStream(outSocket.getInputStream)
    +    dataStream = new DataInputStream(inputStream)
    +    serverSocket.close()
    +
    +    try {
    +      return new Iterator[U] {
    +        def next(): U = {
    +          val obj = _nextObj
    +          if (hasNext) {
    +            _nextObj = read()
    +          }
    +          obj
    +        }
    +
    +        var _nextObj = read()
    +
    +        def hasNext(): Boolean = {
    +          val hasMore = (_nextObj != null)
    +          if (!hasMore) {
    +            dataStream.close()
    +          }
    +          hasMore
    +        }
    +      }
    +    } catch {
    +      case e: Exception =>
    +        throw new SparkException("R computation failed with\n " + 
errThread.getLines())
    +    }
    +  }
    +
    +  /**
    +   * Start a thread to write RDD data to the R process.
    +   */
    +  private def startStdinThread(
    +      output: OutputStream,
    +      iter: Iterator[_],
    +      partitionIndex: Int): Unit = {
    +    val env = SparkEnv.get
    +    val taskContext = TaskContext.get()
    +    val bufferSize = System.getProperty("spark.buffer.size", "65536").toInt
    +    val stream = new BufferedOutputStream(output, bufferSize)
    +
    +    new Thread("writer for R") {
    +      override def run(): Unit = {
    +        try {
    +          SparkEnv.set(env)
    +          TaskContext.setTaskContext(taskContext)
    +          val dataOut = new DataOutputStream(stream)
    +          dataOut.writeInt(partitionIndex)
    +
    +          SerDe.writeString(dataOut, deserializer)
    +          SerDe.writeString(dataOut, serializer)
    +
    +          dataOut.writeInt(packageNames.length)
    +          dataOut.write(packageNames)
    +
    +          dataOut.writeInt(func.length)
    +          dataOut.write(func)
    +
    +          dataOut.writeInt(broadcastVars.length)
    +          broadcastVars.foreach { broadcast =>
    +            // TODO(shivaram): Read a Long in R to avoid this cast
    +            dataOut.writeInt(broadcast.id.toInt)
    +            // TODO: Pass a byte array from R to avoid this cast ?
    +            val broadcastByteArr = 
broadcast.value.asInstanceOf[Array[Byte]]
    +            dataOut.writeInt(broadcastByteArr.length)
    +            dataOut.write(broadcastByteArr)
    +          }
    +
    +          dataOut.writeInt(numPartitions)
    +
    +          if (!iter.hasNext) {
    +            dataOut.writeInt(0)
    +          } else {
    +            dataOut.writeInt(1)
    +          }
    +
    +          val printOut = new PrintStream(stream)
    +
    +          def writeElem(elem: Any): Unit = {
    +            if (deserializer == SerializationFormats.BYTE) {
    +              val elemArr = elem.asInstanceOf[Array[Byte]]
    +              dataOut.writeInt(elemArr.length)
    +              dataOut.write(elemArr)
    +            } else if (deserializer == SerializationFormats.ROW) {
    +              dataOut.write(elem.asInstanceOf[Array[Byte]])
    +            } else if (deserializer == SerializationFormats.STRING) {
    +              // write string(for StringRRDD)
    +              // scalastyle:off println
    +              printOut.println(elem)
    +              // scalastyle:on println
    +            }
    +          }
    +
    +          for (elem <- iter) {
    +            elem match {
    +              case (key, value) =>
    +                writeElem(key)
    +                writeElem(value)
    +              case _ =>
    +                writeElem(elem)
    +            }
    +          }
    +          stream.flush()
    +        } catch {
    +          // TODO: We should propogate this error to the task thread
    +          case e: Exception =>
    +            logError("R Writer thread got an exception", e)
    +        } finally {
    +          Try(output.close())
    +        }
    +      }
    +    }.start()
    +  }
    +
    +  private def read(): U = {
    +    try {
    +      val length = dataStream.readInt()
    +
    +      length match {
    +        case SpecialLengths.TIMING_DATA =>
    +          // Timing data from R worker
    +          val boot = dataStream.readDouble - bootTime
    +          val init = dataStream.readDouble
    +          val broadcast = dataStream.readDouble
    +          val input = dataStream.readDouble
    +          val compute = dataStream.readDouble
    +          val output = dataStream.readDouble
    +          logInfo(
    +            ("Times: boot = %.3f s, init = %.3f s, broadcast = %.3f s, " +
    +              "read-input = %.3f s, compute = %.3f s, write-output = %.3f 
s, " +
    +              "total = %.3f s").format(
    +                boot,
    +                init,
    +                broadcast,
    +                input,
    +                compute,
    +                output,
    +                boot + init + broadcast + input + compute + output))
    +          read()
    +        case length if length >= 0 =>
    +          readData(length).asInstanceOf[U]
    --- End diff --
    
    call the function in readData to return data from the R worker.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to