@Test public void testInvalidUserName() throws HiveSQLException { conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 10); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 0); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER_IPADDRESS, 0); CLIService service = getService(conf); List<SessionHandle> sessionHandles = new ArrayList<>(); try { for (int i = 0; i < limit + 1; i++) { SessionHandle session = service.openSession(CLIService.SERVER_VERSION, null, "bar", "127.0.0.1", null); sessionHandles.add(session); } for (int i = 0; i < limit + 1; i++) { SessionHandle session = service.openSession(CLIService.SERVER_VERSION, "", "bar", "127.0.0.1", null); sessionHandles.add(session); } } finally { for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
public FakeEmbeddedThriftBinaryCLIService(HiveConf hiveConf) { super(new CLIService(null, true), null); isEmbedded = true; cliService.init(hiveConf); cliService.start(); }
@Override public TExecuteStatementResp ExecuteStatement(TExecuteStatementReq req) throws TException { TExecuteStatementResp resp = new TExecuteStatementResp(); try { SessionHandle sessionHandle = new SessionHandle(req.getSessionHandle()); String statement = req.getStatement(); Map<String, String> confOverlay = req.getConfOverlay(); Boolean runAsync = req.isRunAsync(); long queryTimeout = req.getQueryTimeout(); OperationHandle operationHandle = runAsync ? cliService.executeStatementAsync(sessionHandle, statement, confOverlay, queryTimeout) : cliService.executeStatement(sessionHandle, statement, confOverlay, queryTimeout); resp.setOperationHandle(operationHandle.toTOperationHandle()); resp.setStatus(OK_STATUS); } catch (Exception e) { // Note: it's rather important that this (and other methods) catch Exception, not Throwable; // in combination with HiveSessionProxy.invoke code, perhaps unintentionally, it used // to also catch all errors; and now it allows OOMs only to propagate. LOG.warn("Error executing statement: ", e); resp.setStatus(HiveSQLException.toTStatus(e)); } return resp; }
@Override public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) { Metrics metrics = MetricsFactory.getInstance(); if (metrics != null) { try { metrics.decrementCounter(MetricsConstant.OPEN_CONNECTIONS); } catch (Exception e) { LOG.warn("Error Reporting JDO operation to Metrics system", e); } } ThriftCLIServerContext context = (ThriftCLIServerContext) serverContext; SessionHandle sessionHandle = context.getSessionHandle(); if (sessionHandle != null) { LOG.info("Session disconnected without closing properly. "); try { boolean close = cliService.getSessionManager().getSession(sessionHandle).getHiveConf() .getBoolVar(ConfVars.HIVE_SERVER2_CLOSE_SESSION_ON_DISCONNECT); LOG.info((close ? "" : "Not ") + "Closing the session: " + sessionHandle); if (close) { cliService.closeSession(sessionHandle); } } catch (HiveSQLException e) { LOG.warn("Failed to close session: " + e, e); } } }
@Test public void testRestore() throws HiveSQLException { SessionHandle session = service.openSession("foo", "bar", null); service.stop(); service = getService(); try { service.getSessionManager().getSession(session); Assert.fail("session already exists before restore"); } catch (HiveSQLException e) { Assert.assertTrue(e.getMessage().contains("Invalid SessionHandle")); } service.createSessionWithSessionHandle(session, "foo", "bar", null); Assert.assertNotNull(service.getSessionManager().getSession(session)); service.stop(); }
@Test public void testSessionImplWithUGI() throws Exception { HiveConf hiveConf = new HiveConf(); hiveConf.setVar(HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER, HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER.getDefaultValue()); hiveConf.setVar(HiveConf.ConfVars.HIVE_SESSION_IMPL_WITH_UGI_CLASSNAME, SampleHiveSessionImplWithUGI.class.getName()); hiveConf.setBoolVar(HiveConf.ConfVars.HIVE_SERVER2_ENABLE_DOAS, true); CLIService cliService = new CLIService(null, true); cliService.init(hiveConf); ThriftBinaryCLIService service = new ThriftBinaryCLIService(cliService, null); service.init(hiveConf); ThriftCLIServiceClient client = new ThriftCLIServiceClient(service); SessionHandle sessionHandle = null; sessionHandle = client.openSession("tom", "password"); assertEquals(SampleHiveSessionImplWithUGI.class.getName(), service.getHiveConf().getVar(HiveConf.ConfVars.HIVE_SESSION_IMPL_WITH_UGI_CLASSNAME)); HiveSession session = cliService.getSessionManager().getSession(sessionHandle); assertEquals(SampleHiveSessionImplWithUGI.MAGIC_RETURN_VALUE, session.getNoOperationTime()); client.closeSession(sessionHandle); }
@Override public RowSet fetchResults(OperationHandle opHandle) throws HiveSQLException { return fetchResults(opHandle, Operation.DEFAULT_FETCH_ORIENTATION, defaultFetchRows, FetchType.QUERY_OUTPUT); }
/** * Create a session handle * @param req * @param res * @return * @throws HiveSQLException * @throws LoginException * @throws IOException */ SessionHandle getSessionHandle(TOpenSessionReq req, TOpenSessionResp res) throws HiveSQLException, LoginException, IOException { String userName = getUserName(req); String ipAddress = getIpAddress(); TProtocolVersion protocol = getMinVersion(CLIService.SERVER_VERSION, req.getClient_protocol()); SessionHandle sessionHandle; if (cliService.getHiveConf().getBoolVar(ConfVars.HIVE_SERVER2_ENABLE_DOAS) && (userName != null)) { String delegationTokenStr = getDelegationToken(userName); sessionHandle = cliService.openSessionWithImpersonation(protocol, userName, req.getPassword(), ipAddress, req.getConfiguration(), delegationTokenStr); } else { sessionHandle = cliService.openSession(protocol, userName, req.getPassword(), ipAddress, req.getConfiguration()); } res.setServerProtocolVersion(protocol); return sessionHandle; }
@Override protected OperationHandle executeSync(SessionHandle sessionHandle, String statement) throws HiveSQLException, ExploreException { return getCliService().executeStatement(sessionHandle, statement, new HashMap<String, String>()); }
@Override public void assertFunctionCall(String functionCallString, Object expectedOutputData, Object expectedOutputType) { String query = "SELECT " + functionCallString; try { // Execute the SQL statement and fetch the result OperationHandle handle = _client.executeStatement(_sessionHandle, query, null); if (handle.hasResultSet()) { RowSet rowSet = _client.fetchResults(handle); if (rowSet.numRows() > 1 || rowSet.numColumns() > 1) { throw new RuntimeException( "Expected 1 row and 1 column in query output. Received " + rowSet.numRows() + " rows and " + rowSet.numColumns() + " columns.\nQuery: \"" + query + "\""); } Object[] row = rowSet.iterator().next(); Object result = row[0]; Assert.assertEquals(result, expectedOutputData, "UDF output does not match"); // Get the output data type and convert them to TypeInfo to compare ColumnDescriptor outputColumnDescriptor = _client.getResultSetMetadata(handle).getColumnDescriptors().get(0); Assert.assertEquals(TypeInfoUtils.getTypeInfoFromTypeString(outputColumnDescriptor.getTypeName().toLowerCase()), TypeInfoUtils.getTypeInfoFromObjectInspector((ObjectInspector) expectedOutputType), "UDF output type does not match"); } else { throw new RuntimeException("Query did not return any rows. Query: \"" + query + "\""); } } catch (HiveSQLException e) { throw new RuntimeException("Error running Hive query: \"" + query + "\"", e); } } }
@Override protected SessionHandle doOpenHiveSession(Map<String, String> sessionConf) throws HiveSQLException { SessionHandle sessionHandle = super.doOpenHiveSession(sessionConf); // HDP Hive 12 only sets SessionState when an operation is executed. // The SessionState is not set during an openSession call. // Hence execute a dummy statement during openSession, so that SessionState gets set. OperationHandle opHandle = null; try { opHandle = getCliService().executeStatement(sessionHandle, "set cdap_tmp_var=1", Collections.<String, String>emptyMap()); } catch (Throwable t) { LOG.error("Error setting temporary variable to set session state", t); } finally { if (opHandle != null) { try { getCliService().closeOperation(opHandle); } catch (Throwable t) { LOG.debug("Caught exception while closing opHandle", t); } } } return sessionHandle; } }
public EmbeddedThriftBinaryCLIService() { // The non-test path that allows connections for the embedded service. super(new CLIService(null, true), null); isEmbedded = true; HiveConf.setLoadHiveServer2Config(true); }
cliService = new CLIService(this, false); addService(cliService); final HiveServer2 hiveServer2 = this; cliService.getSessionManager()); hiveConf.set("startcode", String.valueOf(System.currentTimeMillis()));
@Override public TCloseOperationResp CloseOperation(TCloseOperationReq req) throws TException { TCloseOperationResp resp = new TCloseOperationResp(); try { cliService.closeOperation(new OperationHandle(req.getOperationHandle())); resp.setStatus(OK_STATUS); } catch (Exception e) { LOG.warn("Error closing operation: ", e); resp.setStatus(HiveSQLException.toTStatus(e)); } return resp; }
@Override public TGetResultSetMetadataResp GetResultSetMetadata(TGetResultSetMetadataReq req) throws TException { TGetResultSetMetadataResp resp = new TGetResultSetMetadataResp(); try { TableSchema schema = cliService.getResultSetMetadata(new OperationHandle(req.getOperationHandle())); resp.setSchema(schema.toTTableSchema()); resp.setStatus(OK_STATUS); } catch (Exception e) { LOG.warn("Error getting result set metadata: ", e); resp.setStatus(HiveSQLException.toTStatus(e)); } return resp; }
/** * Test setting {@link HiveConf.ConfVars}} config parameter * HIVE_SERVER2_ENABLE_DOAS for unsecure mode */ public void testDoAsSetting(){ HiveConf hconf = new HiveConf(); hconf .setVar(HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER, "org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdHiveAuthorizerFactory"); assertTrue("default value of hive server2 doAs should be true", hconf.getBoolVar(ConfVars.HIVE_SERVER2_ENABLE_DOAS)); CLIService cliService = new CLIService(null, true); cliService.init(hconf); ThriftCLIService tcliService = new ThriftBinaryCLIService(cliService, null); tcliService.init(hconf); TProcessorFactory procFactory = PlainSaslHelper.getPlainProcessorFactory(tcliService); assertEquals("doAs enabled processor for unsecure mode", procFactory.getProcessor(null).getClass(), TSetIpAddressProcessor.class); } }
@Override public TCancelOperationResp CancelOperation(TCancelOperationReq req) throws TException { TCancelOperationResp resp = new TCancelOperationResp(); try { cliService.cancelOperation(new OperationHandle(req.getOperationHandle())); resp.setStatus(OK_STATUS); } catch (Exception e) { LOG.warn("Error cancelling operation: ", e); resp.setStatus(HiveSQLException.toTStatus(e)); } return resp; }
@Override public TGetCatalogsResp GetCatalogs(TGetCatalogsReq req) throws TException { TGetCatalogsResp resp = new TGetCatalogsResp(); try { OperationHandle opHandle = cliService.getCatalogs(new SessionHandle(req.getSessionHandle())); resp.setOperationHandle(opHandle.toTOperationHandle()); resp.setStatus(OK_STATUS); } catch (Exception e) { LOG.warn("Error getting catalogs: ", e); resp.setStatus(HiveSQLException.toTStatus(e)); } return resp; }
@Override protected OperationHandle executeAsync(SessionHandle sessionHandle, String statement) throws HiveSQLException, ExploreException { return getCliService().executeStatementAsync(sessionHandle, statement, new HashMap<String, String>()); } }
@Override public TCloseSessionResp CloseSession(TCloseSessionReq req) throws TException { TCloseSessionResp resp = new TCloseSessionResp(); try { SessionHandle sessionHandle = new SessionHandle(req.getSessionHandle()); cliService.closeSession(sessionHandle); resp.setStatus(OK_STATUS); ThriftCLIServerContext context = (ThriftCLIServerContext)currentServerContext.get(); if (context != null) { context.setSessionHandle(null); } } catch (Exception e) { LOG.warn("Error closing session: ", e); resp.setStatus(HiveSQLException.toTStatus(e)); } return resp; }