Github user rxin commented on a diff in the pull request:
https://github.com/apache/spark/pull/2330#discussion_r17650983
--- Diff:
core/src/main/scala/org/apache/spark/network/netty/BlockClientFactory.scala ---
@@ -0,0 +1,182 @@
+/*
+ * 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.network.netty
+
+import java.io.Closeable
+import java.util.concurrent.{ConcurrentHashMap, TimeoutException}
+
+import io.netty.bootstrap.Bootstrap
+import io.netty.buffer.PooledByteBufAllocator
+import io.netty.channel._
+import io.netty.channel.epoll.{Epoll, EpollEventLoopGroup,
EpollSocketChannel}
+import io.netty.channel.nio.NioEventLoopGroup
+import io.netty.channel.oio.OioEventLoopGroup
+import io.netty.channel.socket.SocketChannel
+import io.netty.channel.socket.nio.NioSocketChannel
+import io.netty.channel.socket.oio.OioSocketChannel
+import io.netty.util.internal.PlatformDependent
+
+import org.apache.spark.{Logging, SparkConf}
+import org.apache.spark.util.Utils
+
+
+/**
+ * Factory for creating [[BlockClient]] by using createClient.
+ *
+ * The factory maintains a connection pool to other hosts and should
return the same [[BlockClient]]
+ * for the same remote host. It also shares a single worker thread pool
for all [[BlockClient]]s.
+ */
+private[netty]
+class BlockClientFactory(val conf: NettyConfig) extends Logging with
Closeable {
+
+ def this(sparkConf: SparkConf) = this(new NettyConfig(sparkConf))
+
+ /** A thread factory so the threads are named (for debugging). */
+ private[this] val threadFactory =
Utils.namedThreadFactory("spark-netty-client")
+
+ /** Socket channel type, initialized by [[init]] depending ioMode. */
+ private[this] var socketChannelClass: Class[_ <: Channel] = _
+
+ /** Thread pool shared by all clients. */
+ private[this] var workerGroup: EventLoopGroup = _
+
+ private[this] val connectionPool = new ConcurrentHashMap[(String, Int),
BlockClient]
+
+ // The encoders are stateless and can be shared among multiple clients.
+ private[this] val encoder = new ClientRequestEncoder
+ private[this] val decoder = new ServerResponseDecoder
+
+ init()
+
+ /** Initialize [[socketChannelClass]] and [[workerGroup]] based on
ioMode. */
+ private def init(): Unit = {
+ def initOio(): Unit = {
+ socketChannelClass = classOf[OioSocketChannel]
+ workerGroup = new OioEventLoopGroup(0, threadFactory)
+ }
+ def initNio(): Unit = {
+ socketChannelClass = classOf[NioSocketChannel]
+ workerGroup = new NioEventLoopGroup(0, threadFactory)
+ }
+ def initEpoll(): Unit = {
+ socketChannelClass = classOf[EpollSocketChannel]
+ workerGroup = new EpollEventLoopGroup(0, threadFactory)
+ }
+
+ // For auto mode, first try epoll (only available on Linux), then nio.
+ conf.ioMode match {
+ case "nio" => initNio()
+ case "oio" => initOio()
+ case "epoll" => initEpoll()
+ case "auto" => if (Epoll.isAvailable) initEpoll() else initNio()
+ }
+ }
+
+ /**
+ * Create a new BlockFetchingClient connecting to the given remote host
/ port.
+ *
+ * This blocks until a connection is successfully established.
+ *
+ * Concurrency: This method is safe to call from multiple threads.
+ */
+ def createClient(remoteHost: String, remotePort: Int): BlockClient = {
+ // Get connection from the connection pool first.
+ // If it is not found or not active, create a new one.
+ val cachedClient = connectionPool.get((remoteHost, remotePort))
+ if (cachedClient != null && cachedClient.isActive) {
+ return cachedClient
+ }
+
+ logInfo(s"Creating new connection to $remoteHost:$remotePort")
+
+ // There is a chance two threads are creating two different clients
connecting to the same host.
+ // But that's probably ok ...
+
+ val handler = new BlockClientHandler
--- End diff --
i don't think so -- it is stateful? also it is tricky to handle connection
failure in this case.
---
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]