diff --git a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftBinaryCLIService.java b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftBinaryCLIService.java index a632277..de31699 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftBinaryCLIService.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftBinaryCLIService.java @@ -19,11 +19,9 @@ package org.apache.hive.service.cli.thrift; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.apache.hive.service.auth.HiveAuthFactory.AuthTypes; -import org.apache.thrift.transport.TTransport; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -40,7 +38,6 @@ public class TestThriftBinaryCLIService extends ThriftCLIServiceTest { private static String transportMode = "binary"; - private static TTransport transport; /** * @throws java.lang.Exception @@ -57,20 +54,12 @@ public static void setUpBeforeClass() throws Exception { hiveConf.setBoolVar(ConfVars.HIVE_SERVER2_ENABLE_DOAS, false); hiveConf.setVar(ConfVars.HIVE_SERVER2_THRIFT_BIND_HOST, host); hiveConf.setIntVar(ConfVars.HIVE_SERVER2_THRIFT_PORT, port); - hiveConf.setVar(ConfVars.HIVE_SERVER2_AUTHENTICATION, AuthTypes.NOSASL.toString()); + hiveConf.setVar(ConfVars.HIVE_SERVER2_AUTHENTICATION, AuthTypes.NONE.toString()); hiveConf.setVar(ConfVars.HIVE_SERVER2_TRANSPORT_MODE, transportMode); startHiveServer2WithConf(hiveConf); - // Open a binary transport - // Fail if the transport doesn't open - transport = createBinaryTransport(); - try { - transport.open(); - } - catch (Exception e) { - fail("Exception: " + e); - } + client = getServiceClientInternal(); } /** @@ -87,9 +76,7 @@ public static void tearDownAfterClass() throws Exception { @Override @Before public void setUp() throws Exception { - // Create and set the client - initClient(transport); - assertNotNull(client); + } /** diff --git a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftHttpCLIService.java b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftHttpCLIService.java index 43f92a3..3e913da 100644 --- a/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftHttpCLIService.java +++ b/itests/hive-unit/src/test/java/org/apache/hive/service/cli/thrift/TestThriftHttpCLIService.java @@ -19,12 +19,15 @@ package org.apache.hive.service.cli.thrift; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.fail; +import static org.junit.Assert.assertTrue; import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.apache.hive.jdbc.HttpBasicAuthInterceptor; +import org.apache.hive.service.auth.HiveAuthFactory; import org.apache.hive.service.auth.HiveAuthFactory.AuthTypes; import org.apache.http.impl.client.DefaultHttpClient; +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.THttpClient; import org.apache.thrift.transport.TTransport; import org.junit.After; @@ -44,7 +47,6 @@ private static String transportMode = "http"; private static String thriftHttpPath = "cliservice"; - private static TTransport transport; /** * @throws java.lang.Exception @@ -67,15 +69,7 @@ public static void setUpBeforeClass() throws Exception { startHiveServer2WithConf(hiveConf); - // Open an http transport - // Fail if the transport doesn't open - transport = createHttpTransport(); - try { - transport.open(); - } - catch (Exception e) { - fail("Exception: " + e); - } + client = getServiceClientInternal(); } /** @@ -92,9 +86,7 @@ public static void tearDownAfterClass() throws Exception { @Override @Before public void setUp() throws Exception { - // Create and set the client before every test from the transport - initClient(transport); - assertNotNull(client); + } /** @@ -107,74 +99,68 @@ public void tearDown() throws Exception { } @Test - public void testIncompatibeClientServer() throws Exception { - // A binary client communicating with an http server should throw an exception - // Close the older http client transport - // The server is already running in Http mode - if (transport != null) { - transport.close(); - } - // Create a binary transport and init the client - transport = createBinaryTransport(); - // Create and set the client - initClient(transport); - assertNotNull(client); + /** + * Tests calls from a raw (NOSASL) binary client, + * to a HiveServer2 running in http mode. + * This should throw an expected exception due to incompatibility. + * @throws Exception + */ + public void testBinaryClientHttpServer() throws Exception { + TTransport transport = getRawBinaryTransport(); + TCLIService.Client rawBinaryClient = getClient(transport); // This will throw an expected exception since client-server modes are incompatible - testOpenSessionExpectedException(); - - // Close binary client transport - if (transport != null) { - transport.close(); - } - // Create http transport (client is inited in setUp before every test from the transport) - transport = createHttpTransport(); - try { - transport.open(); - } - catch (Exception e) { - fail("Exception: " + e); - } + testOpenSessionExpectedException(rawBinaryClient); } + /** + * Configure a wrong service endpoint for the client transport, + * and test for error. + * @throws Exception + */ @Test public void testIncorrectHttpPath() throws Exception { - // Close the older http client transport - if (transport != null) { - transport.close(); - } - // Create an http transport with incorrect http path endpoint - thriftHttpPath = "wrong_path"; - transport = createHttpTransport(); - // Create and set the client - initClient(transport); - assertNotNull(client); + thriftHttpPath = "wrongPath"; + TTransport transport = getHttpTransport(); + TCLIService.Client httpClient = getClient(transport); // This will throw an expected exception since // client is communicating with the wrong http service endpoint - testOpenSessionExpectedException(); + testOpenSessionExpectedException(httpClient); - // Close incorrect client transport - // Reinit http client transport + // Reset to correct http path thriftHttpPath = "cliservice"; - if (transport != null) { - transport.close(); - } - transport = createHttpTransport(); + } + + private void testOpenSessionExpectedException(TCLIService.Client client) { + boolean caughtEx = false; + // Create a new open session request object + TOpenSessionReq openReq = new TOpenSessionReq(); try { - transport.open(); - } - catch (Exception e) { - fail("Exception: " + e); + client.OpenSession(openReq).getSessionHandle(); + } catch (Exception e) { + caughtEx = true; + System.out.println("Exception expected: " + e.toString()); } + assertTrue("Exception expected", caughtEx); + } + + private TCLIService.Client getClient(TTransport transport) throws Exception { + // Create the corresponding client + TProtocol protocol = new TBinaryProtocol(transport); + return new TCLIService.Client(protocol); + } + + private TTransport getRawBinaryTransport() throws Exception { + return HiveAuthFactory.getSocketTransport(host, port, 0); } - private static TTransport createHttpTransport() throws Exception { + private static TTransport getHttpTransport() throws Exception { DefaultHttpClient httpClient = new DefaultHttpClient(); String httpUrl = transportMode + "://" + host + ":" + port + "/" + thriftHttpPath + "/"; httpClient.addRequestInterceptor( - new HttpBasicAuthInterceptor(anonymousUser, anonymousPasswd)); + new HttpBasicAuthInterceptor(USERNAME, PASSWORD)); return new THttpClient(httpUrl, httpClient); } diff --git a/service/src/test/org/apache/hive/service/cli/thrift/ThriftCLIServiceTest.java b/service/src/test/org/apache/hive/service/cli/thrift/ThriftCLIServiceTest.java index 21dc63b..66b4a81 100644 --- a/service/src/test/org/apache/hive/service/cli/thrift/ThriftCLIServiceTest.java +++ b/service/src/test/org/apache/hive/service/cli/thrift/ThriftCLIServiceTest.java @@ -18,19 +18,20 @@ package org.apache.hive.service.cli.thrift; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import java.util.HashMap; +import java.util.Map; + import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.metastore.MetaStoreUtils; -import org.apache.hive.service.auth.PlainSaslHelper; +import org.apache.hive.service.Service; +import org.apache.hive.service.cli.OperationHandle; +import org.apache.hive.service.cli.OperationState; +import org.apache.hive.service.cli.OperationStatus; +import org.apache.hive.service.cli.SessionHandle; import org.apache.hive.service.server.HiveServer2; -import org.apache.thrift.protocol.TBinaryProtocol; -import org.apache.thrift.protocol.TProtocol; -import org.apache.thrift.transport.TSocket; -import org.apache.thrift.transport.TTransport; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -48,10 +49,10 @@ protected static int port; protected static String host = "localhost"; protected static HiveServer2 hiveServer2; - protected static TCLIService.Client client; + protected static ThriftCLIServiceClient client; protected static HiveConf hiveConf; - protected static String anonymousUser = "anonymous"; - protected static String anonymousPasswd = "anonymous"; + protected static String USERNAME = "anonymous"; + protected static String PASSWORD = "anonymous"; /** * @throws java.lang.Exception @@ -93,57 +94,60 @@ protected static void stopHiveServer2() throws Exception { } } - protected static TTransport createBinaryTransport() throws Exception { - return PlainSaslHelper.getPlainTransport(anonymousUser, anonymousPasswd, - new TSocket(host, port)); + protected static ThriftCLIServiceClient getServiceClientInternal() { + for (Service service : hiveServer2.getServices()) { + if (service instanceof ThriftBinaryCLIService) { + return new ThriftCLIServiceClient((ThriftBinaryCLIService) service); + } + if (service instanceof ThriftHttpCLIService) { + return new ThriftCLIServiceClient((ThriftHttpCLIService) service); + } + } + throw new IllegalStateException("HiveServer2 not running Thrift service"); } - protected static void initClient(TTransport transport) { - // Create the corresponding client - TProtocol protocol = new TBinaryProtocol(transport); - client = new TCLIService.Client(protocol); + /** + * @throws java.lang.Exception + */ + @Before + public void setUp() throws Exception { } - @Test - public void testOpenSession() throws Exception { - // Create a new request object - TOpenSessionReq openReq = new TOpenSessionReq(); + /** + * @throws java.lang.Exception + */ + @After + public void tearDown() throws Exception { - // Get the response; ignore exception if any - TOpenSessionResp openResp = client.OpenSession(openReq); - assertNotNull("Response should not be null", openResp); + } - TSessionHandle sessHandle = openResp.getSessionHandle(); + @Test + public void testOpenSession() throws Exception { + // Open a new client session + SessionHandle sessHandle = client.openSession(USERNAME, + PASSWORD, new HashMap()); + // Session handle should not be null assertNotNull("Session handle should not be null", sessHandle); - - assertEquals(openResp.getStatus().getStatusCode(), TStatusCode.SUCCESS_STATUS); - - // Close the session; ignore exception if any - TCloseSessionReq closeReq = new TCloseSessionReq(sessHandle); - client.CloseSession(closeReq); + // Close client session + client.closeSession(sessHandle); } @Test public void testGetFunctions() throws Exception { - // Create a new open session request object - TOpenSessionReq openReq = new TOpenSessionReq(); - TSessionHandle sessHandle = client.OpenSession(openReq).getSessionHandle(); - assertNotNull(sessHandle); - - TGetFunctionsReq funcReq = new TGetFunctionsReq(); - funcReq.setSessionHandle(sessHandle); - funcReq.setFunctionName("*"); - funcReq.setCatalogName(null); - funcReq.setSchemaName(null); - - TGetFunctionsResp funcResp = client.GetFunctions(funcReq); - assertNotNull(funcResp); - assertNotNull(funcResp.getStatus()); - assertFalse(funcResp.getStatus().getStatusCode() == TStatusCode.ERROR_STATUS); - - // Close the session; ignore exception if any - TCloseSessionReq closeReq = new TCloseSessionReq(sessHandle); - client.CloseSession(closeReq); + SessionHandle sessHandle = client.openSession(USERNAME, + PASSWORD, new HashMap()); + assertNotNull("Session handle should not be null", sessHandle); + + String catalogName = null; + String schemaName = null; + String functionName = "*"; + + OperationHandle opHandle = client.getFunctions(sessHandle, catalogName, + schemaName, functionName); + + assertNotNull("Operation handle should not be null", opHandle); + + client.closeSession(sessHandle); } /** @@ -152,83 +156,85 @@ public void testGetFunctions() throws Exception { */ @Test public void testExecuteStatement() throws Exception { - // Create a new request object - TOpenSessionReq openReq = new TOpenSessionReq(); - TSessionHandle sessHandle = client.OpenSession(openReq).getSessionHandle(); - assertNotNull(sessHandle); + Map opConf = new HashMap(); + // Open a new client session + SessionHandle sessHandle = client.openSession(USERNAME, + PASSWORD, opConf); + // Session handle should not be null + assertNotNull("Session handle should not be null", sessHandle); // Change lock manager to embedded mode String queryString = "SET hive.lock.manager=" + "org.apache.hadoop.hive.ql.lockmgr.EmbeddedLockManager"; - executeQuery(queryString, sessHandle, false); + client.executeStatement(sessHandle, queryString, opConf); // Drop the table if it exists queryString = "DROP TABLE IF EXISTS TEST_EXEC_THRIFT"; - executeQuery(queryString, sessHandle, false); + client.executeStatement(sessHandle, queryString, opConf); // Create a test table queryString = "CREATE TABLE TEST_EXEC_THRIFT(ID STRING)"; - executeQuery(queryString, sessHandle, false); + client.executeStatement(sessHandle, queryString, opConf); // Execute another query queryString = "SELECT ID FROM TEST_EXEC_THRIFT"; - TExecuteStatementResp execResp = executeQuery(queryString, sessHandle, false); - TOperationHandle operationHandle = execResp.getOperationHandle(); - assertNotNull(operationHandle); - - TGetOperationStatusReq opStatusReq = new TGetOperationStatusReq(); - opStatusReq.setOperationHandle(operationHandle); - assertNotNull(opStatusReq); - TGetOperationStatusResp opStatusResp = client.GetOperationStatus(opStatusReq); - TOperationState state = opStatusResp.getOperationState(); + OperationHandle opHandle = client.executeStatement(sessHandle, + queryString, opConf); + assertNotNull(opHandle); + + OperationStatus opStatus = client.getOperationStatus(opHandle); + assertNotNull(opStatus); + + OperationState state = opStatus.getState(); // Expect query to be completed now - assertEquals("Query should be finished", TOperationState.FINISHED_STATE, state); + assertEquals("Query should be finished", OperationState.FINISHED, state); // Cleanup queryString = "DROP TABLE TEST_EXEC_THRIFT"; - executeQuery(queryString, sessHandle, false); + client.executeStatement(sessHandle, queryString, opConf); - // Close the session; ignore exception if any - TCloseSessionReq closeReq = new TCloseSessionReq(sessHandle); - client.CloseSession(closeReq); + client.closeSession(sessHandle); } /** - * Test asynchronous query execution and error message reporting to the client + * Test asynchronous query execution and error reporting to the client * @throws Exception */ @Test public void testExecuteStatementAsync() throws Exception { - // Create a new request object - TOpenSessionReq openReq = new TOpenSessionReq(); - TSessionHandle sessHandle = client.OpenSession(openReq).getSessionHandle(); - assertNotNull(sessHandle); + Map opConf = new HashMap(); + // Open a new client session + SessionHandle sessHandle = client.openSession(USERNAME, + PASSWORD, opConf); + // Session handle should not be null + assertNotNull("Session handle should not be null", sessHandle); + + OperationHandle opHandle; + OperationStatus opStatus; + OperationState state = null; // Change lock manager to embedded mode String queryString = "SET hive.lock.manager=" + "org.apache.hadoop.hive.ql.lockmgr.EmbeddedLockManager"; - executeQuery(queryString, sessHandle, false); + client.executeStatement(sessHandle, queryString, opConf); // Drop the table if it exists queryString = "DROP TABLE IF EXISTS TEST_EXEC_ASYNC_THRIFT"; - executeQuery(queryString, sessHandle, false); + client.executeStatement(sessHandle, queryString, opConf); // Create a test table queryString = "CREATE TABLE TEST_EXEC_ASYNC_THRIFT(ID STRING)"; - executeQuery(queryString, sessHandle, false); + client.executeStatement(sessHandle, queryString, opConf); // Execute another query queryString = "SELECT ID FROM TEST_EXEC_ASYNC_THRIFT"; System.out.println("Will attempt to execute: " + queryString); - TExecuteStatementResp execResp = executeQuery(queryString, sessHandle, true); - TOperationHandle operationHandle = execResp.getOperationHandle(); - assertNotNull(operationHandle); + opHandle = client.executeStatementAsync(sessHandle, + queryString, opConf); + assertNotNull(opHandle); // Poll on the operation status till the query is completed boolean isQueryRunning = true; - TGetOperationStatusReq opStatusReq; - TGetOperationStatusResp opStatusResp = null; - TOperationState state = null; long pollTimeout = System.currentTimeMillis() + 100000; while(isQueryRunning) { @@ -237,31 +243,31 @@ public void testExecuteStatementAsync() throws Exception { System.out.println("Polling timed out"); break; } - opStatusReq = new TGetOperationStatusReq(); - opStatusReq.setOperationHandle(operationHandle); - assertNotNull(opStatusReq); - opStatusResp = client.GetOperationStatus(opStatusReq); - state = opStatusResp.getOperationState(); + opStatus = client.getOperationStatus(opHandle); + assertNotNull(opStatus); + state = opStatus.getState(); System.out.println("Current state: " + state); - if (state == TOperationState.CANCELED_STATE || state == TOperationState.CLOSED_STATE - || state == TOperationState.FINISHED_STATE || state == TOperationState.ERROR_STATE) { + if (state == OperationState.CANCELED || + state == OperationState.CLOSED || + state == OperationState.FINISHED || + state == OperationState.ERROR) { isQueryRunning = false; } Thread.sleep(1000); } // Expect query to be successfully completed now - assertEquals("Query should be finished", - TOperationState.FINISHED_STATE, state); + assertEquals("Query should be finished", OperationState.FINISHED, state); // Execute a malformed query // This query will give a runtime error queryString = "CREATE TABLE NON_EXISTING_TAB (ID STRING) location 'hdfs://localhost:10000/a/b/c'"; System.out.println("Will attempt to execute: " + queryString); - execResp = executeQuery(queryString, sessHandle, true); - operationHandle = execResp.getOperationHandle(); - assertNotNull(operationHandle); + opHandle = client.executeStatementAsync(sessHandle, queryString, opConf); + assertNotNull(opHandle); + opStatus = client.getOperationStatus(opHandle); + assertNotNull(opStatus); isQueryRunning = true; while(isQueryRunning) { // Break if polling times out @@ -269,72 +275,28 @@ public void testExecuteStatementAsync() throws Exception { System.out.println("Polling timed out"); break; } - opStatusReq = new TGetOperationStatusReq(); - opStatusReq.setOperationHandle(operationHandle); - assertNotNull(opStatusReq); - opStatusResp = client.GetOperationStatus(opStatusReq); - state = opStatusResp.getOperationState(); + state = opStatus.getState(); System.out.println("Current state: " + state); - - if (state == TOperationState.CANCELED_STATE || state == TOperationState.CLOSED_STATE - || state == TOperationState.FINISHED_STATE || state == TOperationState.ERROR_STATE) { + if (state == OperationState.CANCELED || + state == OperationState.CLOSED || + state == OperationState.FINISHED || + state == OperationState.ERROR) { isQueryRunning = false; } Thread.sleep(1000); + opStatus = client.getOperationStatus(opHandle); } - // Expect query to return an error state - assertEquals("Operation should be in error state", TOperationState.ERROR_STATE, state); - + assertEquals("Operation should be in error state", + OperationState.ERROR, state); // sqlState, errorCode should be set to appropriate values - assertEquals(opStatusResp.getSqlState(), "08S01"); - assertEquals(opStatusResp.getErrorCode(), 1); + assertEquals(opStatus.getOperationException().getSQLState(), "08S01"); + assertEquals(opStatus.getOperationException().getErrorCode(), 1); // Cleanup queryString = "DROP TABLE TEST_EXEC_ASYNC_THRIFT"; - executeQuery(queryString, sessHandle, false); - - // Close the session; ignore exception if any - TCloseSessionReq closeReq = new TCloseSessionReq(sessHandle); - client.CloseSession(closeReq); - } - - private TExecuteStatementResp executeQuery(String queryString, TSessionHandle sessHandle, boolean runAsync) - throws Exception { - TExecuteStatementReq execReq = new TExecuteStatementReq(); - execReq.setSessionHandle(sessHandle); - execReq.setStatement(queryString); - execReq.setRunAsync(runAsync); - TExecuteStatementResp execResp = client.ExecuteStatement(execReq); - assertNotNull(execResp); - return execResp; - } - - protected void testOpenSessionExpectedException() { - boolean caughtEx = false; - // Create a new open session request object - TOpenSessionReq openReq = new TOpenSessionReq(); - try { - client.OpenSession(openReq).getSessionHandle(); - } catch (Exception e) { - caughtEx = true; - System.out.println("Exception expected: " + e.toString()); - } - assertTrue("Exception expected", caughtEx); - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { + client.executeStatement(sessHandle, queryString, opConf); + client.closeSession(sessHandle); } }