/* * (non-Javadoc) * * @see java.lang.Object#toString() */ @Override public String toString() { final StringBuilder sb = new StringBuilder("LensConnection{"); sb.append("sessionHandle=").append(sessionHandle.getPublicId()); sb.append('}'); return sb.toString(); }
/** * Test session expiry. * * @throws Exception the exception */ public void testSessionExpiry() throws Exception { HiveConf conf = LensServerConf.createHiveConf(); conf.setVar(HiveConf.ConfVars.HIVE_SESSION_IMPL_CLASSNAME, LensSessionImpl.class.getName()); conf.setLong(LensConfConstants.SESSION_TIMEOUT_SECONDS, 1L); CLIService cliService = new CLIService(); cliService.init(conf); HiveSessionService lensService = new HiveSessionService(cliService); lensService.init(conf); lensService.start(); MetricsService metricSvc = LensServices.get().getService(MetricsService.NAME); try { LensSessionHandle sessionHandle = lensService.openSession("foo", "bar", new HashMap<String, String>()); LensSessionImpl session = lensService.getSession(sessionHandle); assertTrue(session.isActive()); session.setLastAccessTime( session.getLastAccessTime() - 2000 * conf.getLong( LensConfConstants.SESSION_TIMEOUT_SECONDS, LensConfConstants.SESSION_TIMEOUT_SECONDS_DEFAULT)); assertFalse(session.isActive()); // run the expiry thread lensService.getSessionExpiryRunnable().run(); log.info( "Keeping a sleep of 3 seconds to make sure SessionExpiryService gets enough time to close" + " inactive sessions"); Thread.sleep(3000); assertTrue(metricSvc.getTotalExpiredSessions() >= 1); assertTrue(metricSvc.getTotalClosedSessions() >= 1); try { lensService.getSession(sessionHandle); // should throw exception since session should be expired by now fail("Expected get session to fail for session " + sessionHandle.getPublicId()); } catch (Exception e) { // pass } } finally { lensService.stop(); } }
/** * Create a new session with Lens server. * * @param username User name of the Lens server user * @param password Password of the Lens server user * @param database (Optional) Set current database to supplied value * @param sessionconf Key-value properties which will be used to configure this session * @return A Session handle unique to this session */ @POST @Consumes({MediaType.MULTIPART_FORM_DATA}) @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN}) public LensSessionHandle openSession( @FormDataParam("username") String username, @FormDataParam("password") String password, @FormDataParam("database") @DefaultValue("") String database, @FormDataParam("sessionconf") LensConf sessionconf) { try { Map<String, String> conf; if (sessionconf != null) { conf = sessionconf.getProperties(); } else { conf = new HashMap<String, String>(); } LensSessionHandle handle = sessionService.openSession(username, password, database, conf); openSessions.put(handle.getPublicId(), handle); return handle; } catch (LensException e) { throw new WebApplicationException(e); } }
/** * Open. * * @param password the password * @return the lens session handle */ public LensSessionHandle open(String password) { WebTarget target = getSessionWebTarget(); FormDataMultiPart mp = new FormDataMultiPart(); mp.bodyPart( new FormDataBodyPart( FormDataContentDisposition.name("username").build(), params.getUser())); mp.bodyPart( new FormDataBodyPart(FormDataContentDisposition.name("password").build(), password)); String database = params.getDbName(); mp.bodyPart( new FormDataBodyPart(FormDataContentDisposition.name("database").build(), database)); mp.bodyPart( new FormDataBodyPart( FormDataContentDisposition.name("sessionconf").fileName("sessionconf").build(), params.getSessionConf(), MediaType.APPLICATION_XML_TYPE)); try { Response response = target.request().post(Entity.entity(mp, MediaType.MULTIPART_FORM_DATA_TYPE)); if (response.getStatus() != 200) { throw new LensClientServerConnectionException(response.getStatus()); } final LensSessionHandle handle = response.readEntity(LensSessionHandle.class); if (handle != null) { sessionHandle = handle; LOG.debug("Created a new session " + sessionHandle.getPublicId()); } else { throw new IllegalStateException( "Unable to connect to lens " + "server with following paramters" + params); } } catch (ProcessingException e) { if (e.getCause() != null && e.getCause() instanceof ConnectException) { throw new LensClientServerConnectionException(e.getCause().getMessage(), e); } } LOG.debug("Successfully switched to database " + params.getDbName()); open.set(true); return sessionHandle; }