diff --git a/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftBinaryCLIService.java b/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftBinaryCLIService.java index ffca1070d0..8085c8d12e 100644 --- a/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftBinaryCLIService.java +++ b/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftBinaryCLIService.java @@ -39,6 +39,7 @@ import org.apache.hive.service.server.ThreadFactoryWithGarbageCleanup; import org.apache.thrift.TException; import org.apache.thrift.TProcessorFactory; import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.server.TServer; import org.apache.thrift.server.TThreadPoolServer; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TTransportFactory; @@ -46,6 +47,8 @@ import org.apache.thrift.transport.TTransportFactory; public class ThriftBinaryCLIService extends ThriftCLIService { + protected TServer server; + public ThriftBinaryCLIService(CLIService cliService) { super(cliService, ThriftBinaryCLIService.class.getSimpleName()); } @@ -111,6 +114,13 @@ public class ThriftBinaryCLIService extends ThriftCLIService { } } + @Override + protected void stopServer() { + server.stop(); + server = null; + LOG.info("Thrift server has stopped"); + } + @Override public TGetQueryIdResp GetQueryId(TGetQueryIdReq req) throws TException { try { diff --git a/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftCLIService.java b/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftCLIService.java index 150f1d60fc..a65951c8ae 100644 --- a/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftCLIService.java +++ b/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftCLIService.java @@ -40,7 +40,6 @@ import org.apache.hive.service.server.HiveServer2; import org.apache.thrift.TException; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.server.ServerContext; -import org.apache.thrift.server.TServer; import org.apache.thrift.server.TServerEventHandler; import org.apache.thrift.transport.TTransport; import org.slf4j.Logger; @@ -61,8 +60,7 @@ public abstract class ThriftCLIService extends AbstractService implements TCLISe protected int portNum; protected InetAddress serverIPAddress; protected String hiveHost; - protected TServer server; - protected org.eclipse.jetty.server.Server httpServer; + private Thread serverThread = null; private boolean isStarted = false; protected boolean isEmbedded = false; @@ -177,26 +175,23 @@ public abstract class ThriftCLIService extends AbstractService implements TCLISe super.start(); if (!isStarted && !isEmbedded) { initializeServer(); - new Thread(this).start(); + serverThread = new Thread(this); + serverThread.setName(getName()); + serverThread.start(); isStarted = true; } } + protected abstract void stopServer(); + @Override public synchronized void stop() { if (isStarted && !isEmbedded) { - if(server != null) { - server.stop(); - LOG.info("Thrift server has stopped"); - } - if((httpServer != null) && httpServer.isStarted()) { - try { - httpServer.stop(); - LOG.info("Http server has stopped"); - } catch (Exception e) { - LOG.error("Error stopping Http server: ", e); - } + if (serverThread != null) { + serverThread.interrupt(); + serverThread = null; } + stopServer(); isStarted = false; } super.stop(); diff --git a/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftHttpCLIService.java b/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftHttpCLIService.java index 13fc552a9a..bc568be5d2 100644 --- a/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftHttpCLIService.java +++ b/sql/hive-thriftserver/src/main/java/org/apache/hive/service/cli/thrift/ThriftHttpCLIService.java @@ -51,6 +51,8 @@ import org.eclipse.jetty.util.thread.ScheduledExecutorScheduler; public class ThriftHttpCLIService extends ThriftCLIService { + protected org.eclipse.jetty.server.Server httpServer; + public ThriftHttpCLIService(CLIService cliService) { super(cliService, ThriftHttpCLIService.class.getSimpleName()); } @@ -152,6 +154,19 @@ public class ThriftHttpCLIService extends ThriftCLIService { } } + @Override + protected void stopServer() { + if ((httpServer != null) && httpServer.isStarted()) { + try { + httpServer.stop(); + httpServer = null; + LOG.info("Thrift HTTP server has been stopped"); + } catch (Exception e) { + LOG.error("Error stopping HTTP server: ", e); + } + } + } + /** * Configure Jetty to serve http requests. Example of a client connection URL: * http://localhost:10000/servlets/thrifths2/ A gateway may cause actual target URL to differ, @@ -162,10 +177,14 @@ public class ThriftHttpCLIService extends ThriftCLIService { try { httpServer.join(); } catch (Throwable t) { - LOG.error( - "Error starting HiveServer2: could not start " - + ThriftHttpCLIService.class.getSimpleName(), t); - System.exit(-1); + if (t instanceof InterruptedException) { + // This is likely a shutdown + LOG.info("Caught " + t.getClass().getSimpleName() + ". Shutting down thrift server."); + } else { + LOG.error("Error starting HiveServer2: could not start " + + ThriftHttpCLIService.class.getSimpleName(), t); + System.exit(-1); + } } }