PooledSession pooledSession = null; synchronized (lock) { pooledSession = new PooledSession(session); numSessionsBeingCreated--; if (closureFuture != null) {
private void keepAliveSessions(Instant currTime) { long numSessionsToKeepAlive = 0; synchronized (lock) { // In each cycle only keep alive a subset of sessions to prevent burst of traffic. numSessionsToKeepAlive = (long) Math.ceil((double) totalSessions() / numKeepAliveCycles); } // Now go over all the remaining sessions and see if they need to be kept alive explicitly. Instant keepAliveThreshold = currTime.minus(keepAliveMilis); // Keep chugging till there is no session that needs to be kept alive. while (numSessionsToKeepAlive > 0) { PooledSession sessionToKeepAlive = null; synchronized (lock) { sessionToKeepAlive = findSessionToKeepAlive(readSessions, keepAliveThreshold); if (sessionToKeepAlive == null) { sessionToKeepAlive = findSessionToKeepAlive(writePreparedSessions, keepAliveThreshold); } } if (sessionToKeepAlive == null) { break; } try { logger.log(Level.FINE, "Keeping alive session " + sessionToKeepAlive.getName()); numSessionsToKeepAlive--; sessionToKeepAlive.keepAlive(); releaseSession(sessionToKeepAlive); } catch (SpannerException e) { handleException(e, sessionToKeepAlive); } } }
PooledSession pooledSession = null; synchronized (lock) { pooledSession = new PooledSession(session); numSessionsBeingCreated--; if (closureFuture != null) {
logger.log(Level.FINE, "Closing session {0}", sess.getName()); closeSession(sess);
private void keepAliveSessions(Instant currTime) { long numSessionsToKeepAlive = 0; synchronized (lock) { // In each cycle only keep alive a subset of sessions to prevent burst of traffic. numSessionsToKeepAlive = (long) Math.ceil((double) totalSessions() / numKeepAliveCycles); } // Now go over all the remaining sessions and see if they need to be kept alive explicitly. Instant keepAliveThreshold = currTime.minus(keepAliveMilis); // Keep chugging till there is no session that needs to be kept alive. while (numSessionsToKeepAlive > 0) { PooledSession sessionToKeepAlive = null; synchronized (lock) { sessionToKeepAlive = findSessionToKeepAlive(readSessions, keepAliveThreshold); if (sessionToKeepAlive == null) { sessionToKeepAlive = findSessionToKeepAlive(writePreparedSessions, keepAliveThreshold); } } if (sessionToKeepAlive == null) { break; } try { logger.log(Level.FINE, "Keeping alive session " + sessionToKeepAlive.getName()); numSessionsToKeepAlive--; sessionToKeepAlive.keepAlive(); releaseSession(sessionToKeepAlive); } catch (SpannerException e) { handleException(e, sessionToKeepAlive); } } }
@Override public void run() { try { logger.log(Level.FINE, "Preparing session"); sess.prepareReadWriteTransaction(); logger.log(Level.FINE, "Session prepared"); synchronized (lock) { numSessionsBeingPrepared--; if (!isClosed()) { if (readWriteWaiters.size() > 0) { readWriteWaiters.poll().put(sess); } else if (readWaiters.size() > 0) { readWaiters.poll().put(sess); } else { writePreparedSessions.add(sess); } } } } catch (Throwable t) { synchronized (lock) { numSessionsBeingPrepared--; if (!isClosed()) { handlePrepareSessionFailure(newSpannerException(t), sess); } } } } });
private void closeSession(PooledSession sess) { try { sess.delegate.close(); } catch (SpannerException e) { // Backend will delete these sessions after a while even if we fail to close them. if (logger.isLoggable(Level.FINE)) { logger.log(Level.FINE, "Failed to close session: " + sess.getName(), e); } } finally { synchronized (lock) { allSessions.remove(sess); if (isClosed()) { decrementPendingClosures(); return; } // Create a new session if needed to unblock some waiter. if (numWaiters() > numSessionsBeingCreated) { createSession(); } } } }
@Override @Nullable public <T> T run(TransactionCallable<T> callable) { try { markUsed(); T result = runner.run(callable); return result; } catch (SpannerException e) { throw lastException = e; } finally { close(); } }
@Override public TransactionManager transactionManager() { markUsed(); return new AutoClosingTransactionManager(delegate.transactionManager(), this); } }
private PooledSession(Session delegate) { this.delegate = delegate; this.state = SessionState.AVAILABLE; markUsed(); }
@Override public ReadOnlyTransaction singleUseReadOnlyTransaction(TimestampBound bound) { try { return new AutoClosingReadTransaction( delegate.singleUseReadOnlyTransaction(bound), this, true); } catch (Exception e) { close(); throw e; } }
@Override public ReadOnlyTransaction singleUseReadOnlyTransaction() { try { return new AutoClosingReadTransaction(delegate.singleUseReadOnlyTransaction(), this, true); } catch (Exception e) { close(); throw e; } }
@Override public ReadContext singleUse(TimestampBound bound) { try { return new AutoClosingReadContext(delegate.singleUse(bound), this, true); } catch (Exception e) { close(); throw e; } }
@Override public ReadContext singleUse() { try { return new AutoClosingReadContext(delegate.singleUse(), this, true); } catch (Exception e) { close(); throw e; } }
@Override public ReadOnlyTransaction readOnlyTransaction(TimestampBound bound) { try { return new AutoClosingReadTransaction(delegate.readOnlyTransaction(bound), this, false); } catch (Exception e) { close(); throw e; } }
@Override public ReadOnlyTransaction readOnlyTransaction() { try { return new AutoClosingReadTransaction(delegate.readOnlyTransaction(), this, false); } catch (Exception e) { close(); throw e; } }
@Override public void close() { if (closed) { return; } closed = true; delegate.close(); session.close(); } }
@Override public void prepareReadWriteTransaction() { markUsed(); delegate.prepareReadWriteTransaction(); }