Github user andrewor14 commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5096#discussion_r27765041
  
    --- Diff: core/src/main/scala/org/apache/spark/api/r/RBackendHandler.scala 
---
    @@ -0,0 +1,222 @@
    +/*
    + * 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.{ByteArrayInputStream, ByteArrayOutputStream, 
DataInputStream, DataOutputStream}
    +
    +import scala.collection.mutable.HashMap
    +
    +import io.netty.channel.ChannelHandler.Sharable
    +import io.netty.channel.{ChannelHandlerContext, 
SimpleChannelInboundHandler}
    +
    +import org.apache.spark.Logging
    +import org.apache.spark.api.r.SerDe._
    +
    +/**
    + * Handler for RBackend
    + * TODO: This is marked as sharable to get a handle to RBackend. Is it 
safe to re-use
    + * this across connections ?
    + */
    +@Sharable
    +private[r] class RBackendHandler(server: RBackend)
    +  extends SimpleChannelInboundHandler[Array[Byte]] with Logging {
    +
    +  override def channelRead0(ctx: ChannelHandlerContext, msg: Array[Byte]) {
    +    val bis = new ByteArrayInputStream(msg)
    +    val dis = new DataInputStream(bis)
    +
    +    val bos = new ByteArrayOutputStream()
    +    val dos = new DataOutputStream(bos)
    +
    +    // First bit is isStatic
    +    val isStatic = readBoolean(dis)
    +    val objId = readString(dis)
    +    val methodName = readString(dis)
    +    val numArgs = readInt(dis)
    +
    +    if (objId == "SparkRHandler") {
    +      methodName match {
    +        case "stopBackend" =>
    +          writeInt(dos, 0)
    +          writeType(dos, "void")
    +          server.close()
    +        case "rm" =>
    +          try {
    +            val t = readObjectType(dis)
    +            assert(t == 'c')
    +            val objToRemove = readString(dis)
    +            JVMObjectTracker.remove(objToRemove)
    +            writeInt(dos, 0)
    +            writeObject(dos, null)
    +          } catch {
    +            case e: Exception =>
    +              logError(s"Removing $objId failed", e)
    +              writeInt(dos, -1)
    +          }
    +        case _ => dos.writeInt(-1)
    +      }
    +    } else {
    +      handleMethodCall(isStatic, objId, methodName, numArgs, dis, dos)
    +    }
    +
    +    val reply = bos.toByteArray
    +    ctx.write(reply)
    +  }
    +  
    +  override def channelReadComplete(ctx: ChannelHandlerContext) {
    +    ctx.flush()
    +  }
    +
    +  override def exceptionCaught(ctx: ChannelHandlerContext, cause: 
Throwable) {
    +    // Close the connection when an exception is raised.
    +    cause.printStackTrace()
    +    ctx.close()
    +  }
    +
    +  def handleMethodCall(
    +      isStatic: Boolean,
    +      objId: String,
    +      methodName: String,
    +      numArgs: Int,
    +      dis: DataInputStream,
    +      dos: DataOutputStream) {
    +    var obj: Object = null
    +    try {
    +      val cls = if (isStatic) {
    +        Class.forName(objId)
    +      } else {
    +        JVMObjectTracker.get(objId) match {
    +          case None => throw new IllegalArgumentException("Object not 
found " + objId)
    +          case Some(o) =>
    +            obj = o
    +            o.getClass
    +        }
    +      }
    +
    +      val args = readArgs(numArgs, dis)
    +
    +      val methods = cls.getMethods
    +      val selectedMethods = methods.filter(m => m.getName == methodName)
    +      if (selectedMethods.length > 0) {
    +        val methods = selectedMethods.filter { x =>
    +          matchMethod(numArgs, args, x.getParameterTypes)
    +        }
    +        if (methods.isEmpty) {
    +          logWarning(s"cannot find matching method ${cls}.$methodName. "
    +            + s"Candidates are:")
    +          selectedMethods.foreach { method =>
    +            
logWarning(s"$methodName(${method.getParameterTypes.mkString(",")})")
    +          }
    +          throw new Exception(s"No matched method found for 
$cls.$methodName")
    +        }
    +        val ret = methods.head.invoke(obj, args:_*)
    +
    +        // Write status bit
    +        writeInt(dos, 0)
    +        writeObject(dos, ret.asInstanceOf[AnyRef])
    +      } else if (methodName == "<init>") {
    +        // methodName should be "<init>" for constructor
    +        val ctor = cls.getConstructors.filter { x =>
    +          matchMethod(numArgs, args, x.getParameterTypes)
    +        }.head
    +
    +        val obj = ctor.newInstance(args:_*)
    +
    +        writeInt(dos, 0)
    +        writeObject(dos, obj.asInstanceOf[AnyRef])
    +      } else {
    +        throw new IllegalArgumentException("invalid method " + methodName 
+ " for object " + objId)
    +      }
    +    } catch {
    +      case e: Exception =>
    +        logError(s"$methodName on $objId failed", e)
    +        writeInt(dos, -1)
    +    }
    +  }
    +
    +  // Read a number of arguments from the data input stream
    +  def readArgs(numArgs: Int, dis: DataInputStream): 
Array[java.lang.Object] =  {
    --- End diff --
    
    1 too many spaces


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to