You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by li...@apache.org on 2017/06/12 08:20:21 UTC

hive git commit: HIVE-16854: SparkClientFactory is locked too aggressively (Rui reviewed by Xuefu)

Repository: hive
Updated Branches:
  refs/heads/master 80c9c2efd -> 06b683b48


HIVE-16854: SparkClientFactory is locked too aggressively (Rui reviewed by Xuefu)


Project: http://git-wip-us.apache.org/repos/asf/hive/repo
Commit: http://git-wip-us.apache.org/repos/asf/hive/commit/06b683b4
Tree: http://git-wip-us.apache.org/repos/asf/hive/tree/06b683b4
Diff: http://git-wip-us.apache.org/repos/asf/hive/diff/06b683b4

Branch: refs/heads/master
Commit: 06b683b48b9e53e3d57fa9590dbef3e7037d2ca2
Parents: 80c9c2e
Author: Rui Li <li...@apache.org>
Authored: Mon Jun 12 16:20:16 2017 +0800
Committer: Rui Li <li...@apache.org>
Committed: Mon Jun 12 16:20:16 2017 +0800

----------------------------------------------------------------------
 .../hive/spark/client/SparkClientFactory.java   | 22 ++++----
 .../hive/spark/client/rpc/RpcConfiguration.java |  4 +-
 .../apache/hive/spark/client/rpc/RpcServer.java |  1 -
 .../apache/hive/spark/client/rpc/TestRpc.java   | 55 +++++++++++++++++---
 4 files changed, 63 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hive/blob/06b683b4/spark-client/src/main/java/org/apache/hive/spark/client/SparkClientFactory.java
----------------------------------------------------------------------
diff --git a/spark-client/src/main/java/org/apache/hive/spark/client/SparkClientFactory.java b/spark-client/src/main/java/org/apache/hive/spark/client/SparkClientFactory.java
index cf60b13..8cedd30 100644
--- a/spark-client/src/main/java/org/apache/hive/spark/client/SparkClientFactory.java
+++ b/spark-client/src/main/java/org/apache/hive/spark/client/SparkClientFactory.java
@@ -43,20 +43,24 @@ public final class SparkClientFactory {
   /** Used by client and driver to share a secret for establishing an RPC session. */
   static final String CONF_KEY_SECRET = "spark.client.authentication.secret";
 
-  private static RpcServer server = null;
-  private static final Object stopLock = new Object();
+  private static volatile RpcServer server = null;
+  private static final Object serverLock = new Object();
 
   /**
    * Initializes the SparkClient library. Must be called before creating client instances.
    *
    * @param conf Map containing configuration parameters for the client library.
    */
-  public static synchronized void initialize(Map<String, String> conf) throws IOException {
+  public static void initialize(Map<String, String> conf) throws IOException {
     if (server == null) {
-      try {
-        server = new RpcServer(conf);
-      } catch (InterruptedException ie) {
-        throw Throwables.propagate(ie);
+      synchronized (serverLock) {
+        if (server == null) {
+          try {
+            server = new RpcServer(conf);
+          } catch (InterruptedException ie) {
+            throw Throwables.propagate(ie);
+          }
+        }
       }
     }
   }
@@ -64,7 +68,7 @@ public final class SparkClientFactory {
   /** Stops the SparkClient library. */
   public static void stop() {
     if (server != null) {
-      synchronized (stopLock) {
+      synchronized (serverLock) {
         if (server != null) {
           server.close();
           server = null;
@@ -79,7 +83,7 @@ public final class SparkClientFactory {
    * @param sparkConf Configuration for the remote Spark application, contains spark.* properties.
    * @param hiveConf Configuration for Hive, contains hive.* properties.
    */
-  public static synchronized SparkClient createClient(Map<String, String> sparkConf, HiveConf hiveConf)
+  public static SparkClient createClient(Map<String, String> sparkConf, HiveConf hiveConf)
       throws IOException, SparkException {
     Preconditions.checkState(server != null, "initialize() not called.");
     return new SparkClientImpl(server, sparkConf, hiveConf);

http://git-wip-us.apache.org/repos/asf/hive/blob/06b683b4/spark-client/src/main/java/org/apache/hive/spark/client/rpc/RpcConfiguration.java
----------------------------------------------------------------------
diff --git a/spark-client/src/main/java/org/apache/hive/spark/client/rpc/RpcConfiguration.java b/spark-client/src/main/java/org/apache/hive/spark/client/rpc/RpcConfiguration.java
index 8c59015..b6fd4f8 100644
--- a/spark-client/src/main/java/org/apache/hive/spark/client/rpc/RpcConfiguration.java
+++ b/spark-client/src/main/java/org/apache/hive/spark/client/rpc/RpcConfiguration.java
@@ -19,6 +19,7 @@ package org.apache.hive.spark.client.rpc;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -66,7 +67,8 @@ public final class RpcConfiguration {
   private static final HiveConf DEFAULT_CONF = new HiveConf();
 
   public RpcConfiguration(Map<String, String> config) {
-    this.config = config;
+    // make sure we don't modify the config in RpcConfiguration
+    this.config = Collections.unmodifiableMap(config);
   }
 
   long getConnectTimeoutMs() {

http://git-wip-us.apache.org/repos/asf/hive/blob/06b683b4/spark-client/src/main/java/org/apache/hive/spark/client/rpc/RpcServer.java
----------------------------------------------------------------------
diff --git a/spark-client/src/main/java/org/apache/hive/spark/client/rpc/RpcServer.java b/spark-client/src/main/java/org/apache/hive/spark/client/rpc/RpcServer.java
index 08fb535..6c6ab74 100644
--- a/spark-client/src/main/java/org/apache/hive/spark/client/rpc/RpcServer.java
+++ b/spark-client/src/main/java/org/apache/hive/spark/client/rpc/RpcServer.java
@@ -110,7 +110,6 @@ public class RpcServer implements Closeable {
 
           }
       })
-      .option(ChannelOption.SO_BACKLOG, 1)
       .option(ChannelOption.SO_REUSEADDR, true)
       .childOption(ChannelOption.SO_KEEPALIVE, true);
 

http://git-wip-us.apache.org/repos/asf/hive/blob/06b683b4/spark-client/src/test/java/org/apache/hive/spark/client/rpc/TestRpc.java
----------------------------------------------------------------------
diff --git a/spark-client/src/test/java/org/apache/hive/spark/client/rpc/TestRpc.java b/spark-client/src/test/java/org/apache/hive/spark/client/rpc/TestRpc.java
index 5a4801c..21e4595 100644
--- a/spark-client/src/test/java/org/apache/hive/spark/client/rpc/TestRpc.java
+++ b/spark-client/src/test/java/org/apache/hive/spark/client/rpc/TestRpc.java
@@ -20,10 +20,17 @@ package org.apache.hive.spark.client.rpc;
 import java.io.Closeable;
 import java.net.InetAddress;
 import java.io.IOException;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.Callable;
 import java.util.concurrent.ExecutionException;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -53,7 +60,7 @@ public class TestRpc {
   private static final Logger LOG = LoggerFactory.getLogger(TestRpc.class);
 
   private Collection<Closeable> closeables;
-  private Map<String, String> emptyConfig =
+  private static final Map<String, String> emptyConfig =
       ImmutableMap.of(HiveConf.ConfVars.SPARK_RPC_CHANNEL_LOG_LEVEL.varname, "DEBUG");
 
   @Before
@@ -242,7 +249,7 @@ public class TestRpc {
       .put(RpcConfiguration.RPC_SASL_OPT_PREFIX + "qop", Rpc.SASL_AUTH_CONF)
       .build();
     RpcServer server = autoClose(new RpcServer(eConf));
-    Rpc[] rpcs = createRpcConnection(server, eConf);
+    Rpc[] rpcs = createRpcConnection(server, eConf, null);
     Rpc client = rpcs[1];
 
     TestMessage outbound = new TestMessage("Hello World!");
@@ -278,6 +285,35 @@ public class TestRpc {
     }
   }
 
+  @Test
+  public void testRpcServerMultiThread() throws Exception {
+    final RpcServer server = autoClose(new RpcServer(emptyConfig));
+    final String msg = "Hello World!";
+    Callable<String> callable = () -> {
+      Rpc[] rpcs = createRpcConnection(server, emptyConfig, UUID.randomUUID().toString());
+      Rpc rpc;
+      if (ThreadLocalRandom.current().nextBoolean()) {
+        rpc = rpcs[0];
+      } else {
+        rpc = rpcs[1];
+      }
+      TestMessage outbound = new TestMessage("Hello World!");
+      Future<TestMessage> call = rpc.call(outbound, TestMessage.class);
+      TestMessage reply = call.get(10, TimeUnit.SECONDS);
+      return reply.message;
+    };
+    final int numThreads = ThreadLocalRandom.current().nextInt(5) + 5;
+    ExecutorService executor = Executors.newFixedThreadPool(numThreads);
+    List<java.util.concurrent.Future<String>> futures = new ArrayList<>(numThreads);
+    for (int i = 0; i < numThreads; i++) {
+      futures.add(executor.submit(callable));
+    }
+    executor.shutdown();
+    for (java.util.concurrent.Future<String> future : futures) {
+      assertEquals(msg, future.get());
+    }
+  }
+
   private void transfer(Rpc serverRpc, Rpc clientRpc) {
     EmbeddedChannel client = (EmbeddedChannel) clientRpc.getChannel();
     EmbeddedChannel server = (EmbeddedChannel) serverRpc.getChannel();
@@ -308,20 +344,23 @@ public class TestRpc {
    * @return two-tuple (server rpc, client rpc)
    */
   private Rpc[] createRpcConnection(RpcServer server) throws Exception {
-    return createRpcConnection(server, emptyConfig);
+    return createRpcConnection(server, emptyConfig, null);
   }
 
-  private Rpc[] createRpcConnection(RpcServer server, Map<String, String> clientConf)
-      throws Exception {
+  private Rpc[] createRpcConnection(RpcServer server, Map<String, String> clientConf,
+      String clientId) throws Exception {
+    if (clientId == null) {
+      clientId = "client";
+    }
     String secret = server.createSecret();
-    Future<Rpc> serverRpcFuture = server.registerClient("client", secret, new TestDispatcher());
+    Future<Rpc> serverRpcFuture = server.registerClient(clientId, secret, new TestDispatcher());
     NioEventLoopGroup eloop = new NioEventLoopGroup();
     Future<Rpc> clientRpcFuture = Rpc.createClient(clientConf, eloop,
-        "localhost", server.getPort(), "client", secret, new TestDispatcher());
+        "localhost", server.getPort(), clientId, secret, new TestDispatcher());
 
     Rpc serverRpc = autoClose(serverRpcFuture.get(10, TimeUnit.SECONDS));
     Rpc clientRpc = autoClose(clientRpcFuture.get(10, TimeUnit.SECONDS));
-    return new Rpc[] { serverRpc, clientRpc };
+    return new Rpc[]{serverRpc, clientRpc};
   }
 
   private static class TestMessage {