spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From zsxwing <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-6028][Core]A new RPC implemetation base...
Date Sun, 20 Sep 2015 03:48:05 GMT
Github user zsxwing commented on a diff in the pull request:

    https://github.com/apache/spark/pull/6457#discussion_r39926406
  
    --- Diff: core/src/main/scala/org/apache/spark/rpc/netty/NettyRpcEnv.scala ---
    @@ -0,0 +1,474 @@
    +/*
    + * 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.rpc.netty
    +
    +import java.io._
    +import java.net.{InetSocketAddress, URI}
    +import java.nio.ByteBuffer
    +import java.util.Arrays
    +import java.util.concurrent._
    +
    +import scala.collection.JavaConverters._
    +import scala.collection.mutable
    +import scala.concurrent.{Future, Promise}
    +import scala.reflect.ClassTag
    +import scala.util.{DynamicVariable, Failure, Success}
    +import scala.util.control.NonFatal
    +
    +import org.apache.spark.{Logging, SecurityManager, SparkConf}
    +import org.apache.spark.network.TransportContext
    +import org.apache.spark.network.client._
    +import org.apache.spark.network.netty.SparkTransportConf
    +import org.apache.spark.network.sasl.{SaslClientBootstrap, SaslServerBootstrap}
    +import org.apache.spark.network.server._
    +import org.apache.spark.rpc._
    +import org.apache.spark.serializer.{JavaSerializer, Serializer}
    +import org.apache.spark.util.{ThreadUtils, Utils}
    +
    +private[netty] class NettyRpcEnv(
    +    val conf: SparkConf, serializer: Serializer, host: String, securityManager: SecurityManager)
    +  extends RpcEnv(conf) with Logging {
    +
    +  private val transportConf =
    +    SparkTransportConf.fromSparkConf(conf, conf.getInt("spark.rpc.io.threads", 0))
    +
    +  private val dispatcher: Dispatcher = new Dispatcher(this)
    +
    +  private val transportContext =
    +    new TransportContext(transportConf, new NettyRpcHandler(dispatcher, this))
    +
    +  private val clientFactory = {
    +    val bootstraps: Seq[TransportClientBootstrap] =
    +      if (securityManager.isAuthenticationEnabled()) {
    +        Seq(new SaslClientBootstrap(transportConf, "", securityManager,
    +          securityManager.isSaslEncryptionEnabled()))
    +      } else {
    +        Seq.empty
    +      }
    +    transportContext.createClientFactory(bootstraps.asJava)
    +  }
    +
    +  val timeoutScheduler = ThreadUtils.newDaemonSingleThreadScheduledExecutor("netty-rpc-env-timeout")
    +
    +  private val clientConnectionExecutor = ThreadUtils.newDaemonCachedThreadPool(
    +    "netty-rpc-connection",
    +    conf.getInt("spark.rpc.connect.threads", 256))
    +
    +  @volatile private var server: TransportServer = _
    +
    +  def start(port: Int): Unit = {
    +    val bootstraps: Seq[TransportServerBootstrap] =
    +      if (securityManager.isAuthenticationEnabled()) {
    +        Seq(new SaslServerBootstrap(transportConf, securityManager))
    +      } else {
    +        Seq.empty
    +      }
    +    server = transportContext.createServer(port, bootstraps.asJava)
    +    dispatcher.registerRpcEndpoint(IDVerifier.NAME, new IDVerifier(this, dispatcher))
    +  }
    +
    +  override lazy val address: RpcAddress = {
    +    require(server != null, "NettyRpcEnv has not yet started")
    +    RpcAddress(host, server.getPort())
    +  }
    +
    +  override def setupEndpoint(name: String, endpoint: RpcEndpoint): RpcEndpointRef = {
    +    dispatcher.registerRpcEndpoint(name, endpoint)
    +  }
    +
    +  def asyncSetupEndpointRefByURI(uri: String): Future[RpcEndpointRef] = {
    +    val addr = NettyRpcAddress(uri)
    +    val endpointRef = new NettyRpcEndpointRef(conf, addr, this)
    +    val idVerifierRef =
    +      new NettyRpcEndpointRef(conf, NettyRpcAddress(addr.host, addr.port, IDVerifier.NAME),
this)
    +    idVerifierRef.ask[Boolean](ID(endpointRef.name)).flatMap(find =>
    +      if (find) {
    +        Future.successful(endpointRef)
    +      } else {
    +        Future.failed(new RpcEndpointNotFoundException(uri))
    +      }
    +    )(ThreadUtils.sameThread)
    +  }
    +
    +  override def stop(endpointRef: RpcEndpointRef): Unit = {
    +    require(endpointRef.isInstanceOf[NettyRpcEndpointRef])
    +    dispatcher.stop(endpointRef)
    +  }
    +
    +  private[netty] def send(message: RequestMessage): Unit = {
    +    val remoteAddr = message.receiver.address
    +    if (remoteAddr == address) {
    +      val promise = Promise[Any]()
    +      dispatcher.postMessage(message, promise)
    +      promise.future.onComplete {
    +        case Success(response) =>
    +          val ack = response.asInstanceOf[SendAck]
    +          logDebug(s"Receive ack from ${ack.sender}")
    +        case Failure(e) =>
    +          logError(s"Exception when sending $message", e)
    +      }(ThreadUtils.sameThread)
    +    } else {
    +      try {
    +        // `createClient` will block if it cannot find a known connection, so we should
run it in
    +        // clientConnectionExecutor
    +        clientConnectionExecutor.execute(new Runnable {
    +          override def run(): Unit = Utils.tryLogNonFatalError {
    +            val client = clientFactory.createClient(remoteAddr.host, remoteAddr.port)
    +            client.sendRpc(serialize(message), new RpcResponseCallback {
    +
    +              override def onFailure(e: Throwable): Unit = {
    +                logError(s"Exception when sending $message", e)
    +              }
    +
    +              override def onSuccess(response: Array[Byte]): Unit = {
    +                val ack = deserialize[SendAck](response)
    +                logDebug(s"Receive ack from ${ack.sender}")
    +              }
    +            })
    +          }
    +        })
    +      } catch {
    +        case e: RejectedExecutionException => {
    +          // `send` after shutting clientConnectionExecutor down, ignore it
    +        }
    +      }
    +    }
    +  }
    +
    +  private[netty] def ask(message: RequestMessage): Future[Any] = {
    +    val promise = Promise[Any]()
    +    val remoteAddr = message.receiver.address
    +    if (remoteAddr == address) {
    +      val p = Promise[Any]()
    +      dispatcher.postMessage(message, p)
    +      p.future.onComplete {
    +        case Success(response) =>
    +          val reply = response.asInstanceOf[AskResponse]
    +          if (reply.reply != null && reply.reply.isInstanceOf[RpcFailure]) {
    +            if (!promise.tryFailure(reply.reply.asInstanceOf[RpcFailure].e)) {
    +              logWarning(s"Ignore failure: ${reply.reply}")
    +            }
    +          } else if (!promise.trySuccess(reply.reply)) {
    +            logWarning(s"Ignore message: ${reply}")
    +          }
    +        case Failure(e) =>
    +          if (!promise.tryFailure(e)) {
    +            logWarning("Ignore Exception", e)
    +          }
    +      }(ThreadUtils.sameThread)
    +    } else {
    +      val client = clientFactory.createClient(remoteAddr.host, remoteAddr.port)
    --- End diff --
    
    `ask` is already a blocking action, so I think it's fine for  `clientFactory.createClient`
to block the current thread.


---
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 infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

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


Mime
View raw message