diff --git beeline/src/java/org/apache/hive/beeline/BeeLine.java beeline/src/java/org/apache/hive/beeline/BeeLine.java index 5322ca6..79922d2 100644 --- beeline/src/java/org/apache/hive/beeline/BeeLine.java +++ beeline/src/java/org/apache/hive/beeline/BeeLine.java @@ -1778,7 +1778,25 @@ void handleSQLException(SQLException e) { } if (e.getCause() instanceof TTransportException) { - error(loc("hs2-unavailable")); + switch (((TTransportException)e.getCause()).getType()) { + case TTransportException.ALREADY_OPEN: + error(loc("hs2-connection-already-open")); + break; + case TTransportException.END_OF_FILE: + error(loc("hs2-unexpected-end-of-file")); + break; + case TTransportException.NOT_OPEN: + error(loc("hs2-could-not-open-connection")); + break; + case TTransportException.TIMED_OUT: + error(loc("hs2-connection-timed-out")); + break; + case TTransportException.UNKNOWN: + error(loc("hs2-unknown-connection-problem")); + break; + default: + error(loc("hs2-unexpected-error")); + } } error(loc(e instanceof SQLWarning ? "Warning" : "Error", diff --git beeline/src/main/resources/BeeLine.properties beeline/src/main/resources/BeeLine.properties index 13321d2..7e46169 100644 --- beeline/src/main/resources/BeeLine.properties +++ beeline/src/main/resources/BeeLine.properties @@ -142,7 +142,16 @@ active-connections: 0#No active connections|1#{0} active connection:|1<{0} activ time-ms: ({0,number,#.###} seconds) -hs2-unavailable: HS2 may be unavailable, check server status +hs2-connection-already-open: Socket already connected. +hs2-unexpected-end-of-file: Unexpected end of file when reading from HS2 server. The root \ +cause might be too many concurrent connections. Please check the number of active \ +connections, and adjust hive.server2.thrift.max.worker.threads if applicable. +hs2-could-not-open-connection: Could not open connection to the HS2 server. Please check the \ +server URI and server status. +hs2-connection-timed-out: Connection timeout when connecting to HS2 server. +hs2-unknown-connection-problem: Unknown HS2 problem when connecting to Thrift server. +hs2-unexpected-error: Unexpected HS2 error when connecting to the Thrift server. + cmd-usage: Usage: java org.apache.hive.cli.beeline.BeeLine \n \ \ -u the JDBC URL to connect to\n \ diff --git beeline/src/test/org/apache/hive/beeline/TestBeeLineExceptionHandling.java beeline/src/test/org/apache/hive/beeline/TestBeeLineExceptionHandling.java new file mode 100644 index 0000000..08579e8 --- /dev/null +++ beeline/src/test/org/apache/hive/beeline/TestBeeLineExceptionHandling.java @@ -0,0 +1,72 @@ +/** + * 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.hive.beeline; + +import junit.framework.Assert; +import org.apache.thrift.transport.TTransportException; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class TestBeeLineExceptionHandling { + + public class TestBeeline extends BeeLine { + private String expectedLoc; + private int logCount; + public TestBeeline(String expectedLoc) { + this.expectedLoc = expectedLoc; + this.logCount = 0; + } + + @Override + boolean error(String log) { + if (logCount == 0) { + Assert.assertEquals(loc(expectedLoc), log); + } else { + Assert.assertEquals("Error: org.apache.thrift.transport.TTransportException " + + "(state=,code=0)", log); + } + logCount++; + return false; + } + } + + @Test + public void testHandleSQLExceptionLog() throws Exception { + checkException(TTransportException.ALREADY_OPEN, "hs2-connection-already-open"); + checkException(TTransportException.END_OF_FILE, "hs2-unexpected-end-of-file"); + checkException(TTransportException.NOT_OPEN, "hs2-could-not-open-connection"); + checkException(TTransportException.TIMED_OUT, "hs2-connection-timed-out"); + checkException(TTransportException.UNKNOWN, "hs2-unknown-connection-problem"); + checkException(-1, "hs2-unexpected-error"); + } + + private void checkException(int type, String loc) { + BeeLine testBeeLine = new TestBeeline(loc); + TTransportException tTransportException = new TTransportException(type); + SQLException sqlException = new SQLException(tTransportException); + testBeeLine.handleSQLException(sqlException); + } +}