@Override public void closeSession(SparkSession sparkSession) throws HiveException { if (sparkSession == null) { return; } if (LOG.isDebugEnabled()) { LOG.debug(String.format("Closing Spark session (%s).", sparkSession.getSessionId())); } sparkSession.close(); createdSessions.remove(sparkSession); }
@Override public void closeSession(SparkSession sparkSession) throws HiveException { if (sparkSession == null) { return; } if (LOG.isDebugEnabled()) { LOG.debug(String.format("Closing session (%s).", sparkSession.getSessionId())); } sparkSession.close(); createdSessions.remove(sparkSession); }
/** * If the <i>existingSession</i> can be reused return it. * Otherwise * - close it and remove it from the list. * - create a new session and add it to the list. */ @Override public SparkSession getSession(SparkSession existingSession, HiveConf conf, boolean doOpen) throws HiveException { setup(conf); if (existingSession != null) { // Open the session if it is closed. if (!existingSession.isOpen() && doOpen) { existingSession.open(conf); } return existingSession; } SparkSession sparkSession = new SparkSessionImpl(); if (doOpen) { sparkSession.open(conf); } if (LOG.isDebugEnabled()) { LOG.debug(String.format("New session (%s) is created.", sparkSession.getSessionId())); } createdSessions.add(sparkSession); return sparkSession; }
/** * If the <i>existingSession</i> can be reused return it. * Otherwise * - close it and remove it from the list. * - create a new session and add it to the list. */ @Override public SparkSession getSession(SparkSession existingSession, HiveConf conf, boolean doOpen) throws HiveException { setup(conf); if (existingSession != null) { // Open the session if it is closed. if (!existingSession.isOpen() && doOpen) { existingSession.open(conf); createdSessions.add(existingSession); } return existingSession; } SparkSession sparkSession = new SparkSessionImpl(SessionState.get().getNewSparkSessionId()); if (doOpen) { sparkSession.open(conf); } if (LOG.isDebugEnabled()) { LOG.debug(String.format("New session (%s) is created.", sparkSession.getSessionId())); } createdSessions.add(sparkSession); return sparkSession; }
@Override public void run() { try { SessionState.setCurrentSessionState(ss); Random random = new Random(Thread.currentThread().getId()); String threadName = Thread.currentThread().getName(); System.out.println(threadName + " started."); HiveConf conf = getHiveConf(); SparkSession prevSession = null; SparkSession currentSession = null; for(int i = 0; i < 5; i++) { currentSession = sessionManagerHS2.getSession(prevSession, conf, true); assertTrue(prevSession == null || prevSession == currentSession); assertTrue(currentSession.isOpen()); System.out.println(String.format("%s got session (%d): %s", threadName, i, currentSession.getSessionId())); Thread.sleep((random.nextInt(3)+1) * 1000); sessionManagerHS2.returnSession(currentSession); prevSession = currentSession; } sessionManagerHS2.closeSession(currentSession); System.out.println(threadName + " ended."); } catch (Throwable e) { anyFailedSessionThread = true; String msg = String.format("Error executing '%s'", Thread.currentThread().getName()); LOG.error(msg, e); fail(msg + " " + StringUtils.stringifyException(e)); } } }
@Test public void testGetSessionId() throws HiveException { SessionState ss = SessionState.start(SESSION_HIVE_CONF); SparkSessionManager ssm = SparkSessionManagerImpl.getInstance(); ss.setSparkSession(ssm.getSession(null, SESSION_HIVE_CONF, true)); assertEquals(ss.getSessionId() + "_0", ss.getSparkSession().getSessionId()); ss.setSparkSession(ssm.getSession(null, SESSION_HIVE_CONF, true)); assertEquals(ss.getSessionId() + "_1", ss.getSparkSession().getSessionId()); ss = SessionState.start(SESSION_HIVE_CONF); ss.setSparkSession(ssm.getSession(null, SESSION_HIVE_CONF, true)); assertEquals(ss.getSessionId() + "_0", ss.getSparkSession().getSessionId()); }
@Override public void closeSession(SparkSession sparkSession) throws HiveException { if (sparkSession == null) { return; } if (LOG.isDebugEnabled()) { LOG.debug(String.format("Closing session (%s).", sparkSession.getSessionId())); } sparkSession.close(); createdSessions.remove(sparkSession); }
/** * If the <i>existingSession</i> can be reused return it. * Otherwise * - close it and remove it from the list. * - create a new session and add it to the list. */ @Override public SparkSession getSession(SparkSession existingSession, HiveConf conf, boolean doOpen) throws HiveException { setup(conf); if (existingSession != null) { // Open the session if it is closed. if (!existingSession.isOpen() && doOpen) { existingSession.open(conf); } return existingSession; } SparkSession sparkSession = new SparkSessionImpl(); createdSessions.add(sparkSession); if (doOpen) { sparkSession.open(conf); } if (LOG.isDebugEnabled()) { LOG.debug(String.format("New session (%s) is created.", sparkSession.getSessionId())); } return sparkSession; }