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

    https://github.com/apache/spark/pull/9530#discussion_r44455097
  
    --- Diff: core/src/main/scala/org/apache/spark/rpc/netty/NettyRpcEnv.scala 
---
    @@ -302,6 +325,138 @@ private[netty] class NettyRpcEnv(
         }
       }
     
    +  override def fileServer: RpcEnvFileServer = streamManager
    +
    +  override def openChannel(uri: String): ReadableByteChannel = {
    +    val parsedUri = new URI(uri)
    +    require(parsedUri.getHost() != null, "Host name must be defined.")
    +    require(parsedUri.getPort() > 0, "Port must be defined.")
    +    require(parsedUri.getPath() != null && parsedUri.getPath().nonEmpty, 
"Path must be defined.")
    +
    +    val pipe = Pipe.open()
    +    val source = new FileDownloadChannel(pipe.source())
    +    try {
    +      val client = fileDownloadClient(parsedUri.getHost(), 
parsedUri.getPort())
    +      val callback = new FileDownloadCallback(pipe.sink(), source, client)
    +      client.stream(parsedUri.getPath(), callback)
    +    } catch {
    +      case e: Exception =>
    +        pipe.sink().close()
    +        source.close()
    +        throw e
    +    }
    +
    +    source
    +  }
    +
    +  private def fileDownloadClient(host: String, port: Int): TransportClient 
= synchronized {
    +    if (stopped.get()) {
    +      throw new IllegalStateException("RpcEnv already stopped.")
    +    }
    +
    +    val address = RpcAddress(host, port)
    +    val client = 
fileClients.get(address).filter(_.isActive()).getOrElse(newDownloadClient(address))
    +
    +    // Tell the timeout handler this client is in use. This will prevent 
the handler from
    +    // closing the client if the timeout even triggers before data starts 
flowing for this
    +    // download.
    +    client.synchronized {
    +      val timeoutHandler = 
client.getChannel().pipeline().get(classOf[TimeoutHandler])
    +      timeoutHandler.setInUse(true)
    +
    +      // After notifying the timeout handler, check that the client is 
really active, and if not,
    +      // create a new one.
    +      if (client.isActive()) client else newDownloadClient(address)
    +    }
    +  }
    +
    +  /**
    +   * Create a new client and install a handler that will respond to 
IdleStateEvent. The events
    +   * are generated by the IdleStateHandler installed by the 
TransportContext when creating
    +   * clients, and the timeout value is controlled by the transport 
configuration.
    +   */
    +  private def newDownloadClient(addr: RpcAddress): TransportClient = {
    +    val c = clientFactory.createUnmanagedClient(addr.host, addr.port)
    +    c.getChannel().pipeline().addLast("rpcEnvTimeoutHandler", new 
TimeoutHandler(c))
    +    fileClients.put(addr, c)
    +    c
    +  }
    +
    +  private class TimeoutHandler(client: TransportClient) extends 
ChannelInboundHandlerAdapter {
    +
    +    @volatile private var inUse = true
    +
    +    def setInUse(inUse: Boolean): Unit = this.inUse = inUse
    +
    +    override def userEventTriggered(ctx: ChannelHandlerContext, evt: 
Object): Unit = {
    +      client.synchronized {
    +        if (!inUse && evt.isInstanceOf[IdleStateEvent] && 
client.isActive()) {
    +          logDebug(s"Closing transport client $client after idle timeout.")
    +          val socketAddr = 
client.getChannel().remoteAddress().asInstanceOf[InetSocketAddress]
    +          val address = RpcAddress(socketAddr.getHostName(), 
socketAddr.getPort())
    +          ctx.close()
    +          NettyRpcEnv.this.synchronized {
    --- End diff --
    
    The lock order here is in reverse order of `fileDownloadClient`. A 
potential dead-lock issue?


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