@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); } } }
@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; }
@Test public void testIncrementAndDecrementConnectionsUser() 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 { // open 5 connections for (int i = 0; i < limit / 2; i++) { SessionHandle session = service.openSession(CLIService.SERVER_VERSION, "foo", "bar", "127.0.0.1", null); sessionHandles.add(session); } // close them all for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } sessionHandles.clear(); // open till limit but not exceed for (int i = 0; i < limit; i++) { SessionHandle session = service.openSession(CLIService.SERVER_VERSION, "ff", "bar", "127.0.0.1", null); sessionHandles.add(session); } } finally { for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
@Test public void testNoLimit() throws HiveSQLException { conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 0); 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, "foo", "bar", "127.0.0.1", null); sessionHandles.add(session); } } finally { for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
@Test public void testInvalidUserIpaddress() throws HiveSQLException { conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 0); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 10); 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, " ", "bar", null, null); sessionHandles.add(session); } } finally { for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
@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(); } }
@Test public void testInvalidIpaddress() throws HiveSQLException { conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 0); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 10); 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, "foo", "bar", null, null); sessionHandles.add(session); } for (int i = 0; i < limit + 1; i++) { SessionHandle session = service.openSession(CLIService.SERVER_VERSION, "foo", "bar", "", null); sessionHandles.add(session); } } finally { for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
@Test public void testConnectionForwardedIpAddresses() throws HiveSQLException { thrown.expect(HiveSQLException.class); thrown.expectMessage("Connection limit per ipaddress reached (ipaddress: 194.167.0.3 limit: 10)"); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 0); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 10); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER_IPADDRESS, 10); CLIService service = getService(conf); SessionManager.setForwardedAddresses(Lists.newArrayList("194.167.0.3", "194.167.0.2", "194.167.0.1")); List<SessionHandle> sessionHandles = new ArrayList<>(); try { for (int i = 0; i < limit + 1; i++) { SessionHandle session = service.openSession(CLIService.SERVER_VERSION, "foo", "bar", "194.167.0.1", null); sessionHandles.add(session); } } finally { SessionManager.setForwardedAddresses(Collections.emptyList()); for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
@Test public void testConnectionLimitPerUserIpAddress() throws HiveSQLException { thrown.expect(HiveSQLException.class); thrown.expectMessage("Connection limit per user:ipaddress reached (user:ipaddress: foo:127.0.0.1 limit: 10)"); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 0); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 0); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER_IPADDRESS, 10); 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, "foo", "bar", "127.0.0.1", null); sessionHandles.add(session); } } finally { for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
@Test public void testConnectionLimitPerIpAddress() throws HiveSQLException { thrown.expect(HiveSQLException.class); thrown.expectMessage("Connection limit per ipaddress reached (ipaddress: 127.0.0.1 limit: 10)"); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 0); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 10); 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, "foo", "bar", "127.0.0.1", null); sessionHandles.add(session); } } finally { for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
@Test public void testConnectionLimitPerUser() throws HiveSQLException { thrown.expect(HiveSQLException.class); thrown.expectMessage("Connection limit per user reached (user: foo limit: 10)"); 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, "foo", "bar", "127.0.0.1", null); sessionHandles.add(session); } } finally { for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
@Test public void testConnectionMultipleLimitsUserAndIP() throws HiveSQLException { thrown.expect(HiveSQLException.class); thrown.expectMessage("Connection limit per user reached (user: foo limit: 5)"); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 5); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 10); 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, "foo", "bar", "127.0.0.1", null); sessionHandles.add(session); } } finally { for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
@Test public void testConnectionMultipleLimitsIPAndUserIP() throws HiveSQLException { thrown.expect(HiveSQLException.class); thrown.expectMessage("Connection limit per ipaddress reached (ipaddress: 127.0.0.1 limit: 5)"); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 0); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 5); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER_IPADDRESS, 10); 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, "foo", "bar", "127.0.0.1", null); sessionHandles.add(session); } } finally { for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
@Test public void testConnectionMultipleLimitsUserIPAndUser() throws HiveSQLException { thrown.expect(HiveSQLException.class); thrown.expectMessage("Connection limit per user:ipaddress reached (user:ipaddress: foo:127.0.0.1 limit: 10)"); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER, 15); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_IPADDRESS, 0); conf.setIntVar(HiveConf.ConfVars.HIVE_SERVER2_LIMIT_CONNECTIONS_PER_USER_IPADDRESS, 10); 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, "foo", "bar", "127.0.0.1", null); sessionHandles.add(session); } } finally { for (SessionHandle sessionHandle : sessionHandles) { service.closeSession(sessionHandle); } service.stop(); } }
private void closeHiveSession(SessionHandle sessionHandle) { try { cliService.closeSession(sessionHandle); } catch (Throwable e) { LOG.error("Got error closing session", e); } }
private void closeHiveSession(SessionHandle sessionHandle) { try { cliService.closeSession(sessionHandle); } catch (Throwable e) { LOG.error("Got error closing session", e); } }
@Override public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) { ThriftCLIServerContext context = (ThriftCLIServerContext)serverContext; SessionHandle sessionHandle = context.getSessionHandle(); if (sessionHandle != null) { LOG.info("Session disconnected without closing properly, close it now"); try { cliService.closeSession(sessionHandle); } catch (HiveSQLException e) { LOG.warn("Failed to close session: " + e, e); } } }
@Override public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) { ThriftCLIServerContext context = (ThriftCLIServerContext)serverContext; SessionHandle sessionHandle = context.getSessionHandle(); if (sessionHandle != null) { LOG.info("Session disconnected without closing properly, close it now"); try { cliService.closeSession(sessionHandle); } catch (HiveSQLException e) { LOG.warn("Failed to close session: " + e, e); } } }
@Override public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) { ThriftCLIServerContext context = (ThriftCLIServerContext)serverContext; SessionHandle sessionHandle = context.getSessionHandle(); if (sessionHandle != null) { LOG.info("Session disconnected without closing properly, close it now"); try { cliService.closeSession(sessionHandle); } catch (HiveSQLException e) { LOG.warn("Failed to close session: " + e, e); } } }
@Override public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output) { ThriftCLIServerContext context = (ThriftCLIServerContext)serverContext; SessionHandle sessionHandle = context.getSessionHandle(); if (sessionHandle != null) { LOG.info("Session disconnected without closing properly, close it now"); try { cliService.closeSession(sessionHandle); } catch (HiveSQLException e) { LOG.warn("Failed to close session: " + e, e); } } }