protected void acquireLock( @Nonnull final String sessionId, final long retryInterval, final long maxRetryInterval, final long timeout, final long start ) throws InterruptedException, ExecutionException, TimeoutException { final Future<Boolean> result = _storage.add( _sessionIdFormat.createLockName( sessionId ), _manager.getLockExpiration(), LOCK_VALUE ); if ( result.get().booleanValue() ) { if ( _log.isDebugEnabled() ) { _log.debug( "Locked session " + sessionId ); } return; } else { checkTimeoutAndWait( sessionId, retryInterval, timeout, start ); acquireLock( sessionId, min( retryInterval * 2, maxRetryInterval ), maxRetryInterval, timeout, start ); } }
public void setLockingMode( @Nullable final LockingMode lockingMode, @Nullable final Pattern uriPattern, final boolean storeSecondaryBackup ) { _log.info( "Setting lockingMode to " + lockingMode + ( uriPattern != null ? " with pattern " + uriPattern.pattern() : "" ) ); _lockingStrategy = LockingStrategy.create( lockingMode, uriPattern, _storage, this, _memcachedNodesManager, _invalidSessionsCache, storeSecondaryBackup, _statistics, _currentRequest ); }
/** * Invoked after a non-sticky session is loaded from memcached, can be used to update some session fields based on * separately stored information (e.g. session validity info). * * @param lockStatus * the {@link LockStatus} that was returned from {@link #onBeforeLoadFromMemcached(String)}. */ protected void onAfterLoadFromMemcached( @Nonnull final MemcachedBackupSession session, @Nullable final LockStatus lockStatus ) { session.setLockStatus( lockStatus ); final long start = System.currentTimeMillis(); final SessionValidityInfo info = loadSessionValidityInfo( session.getIdInternal() ); if ( info != null ) { _stats.registerSince( NON_STICKY_AFTER_LOAD_FROM_MEMCACHED, start ); session.setLastAccessedTimeInternal( info.getLastAccessedTime() ); session.setThisAccessedTimeInternal( info.getThisAccessedTime() ); } else { _log.warn( "No validity info available for session " + session.getIdInternal() ); } }
_lockingStrategy.onBackupWithoutLoadedSession( sessionId, requestId, _backupSessionService ); _lockingStrategy.onAfterBackupSession( msmSession, force, result, requestId, _backupSessionService );
@CheckForNull protected SessionValidityInfo loadSessionValidityInfo( @Nonnull final String sessionId ) { return loadSessionValidityInfoForValidityKey( _sessionIdFormat.createValidityInfoKeyName( sessionId ) ); }
protected LockStatus lock( final String sessionId ) { return lock( sessionId, _manager.getOperationTimeout(), TimeUnit.MILLISECONDS ); }
protected void deleteFromMemcached(final String sessionId) { if ( _enabled.get() && _memcachedNodesManager.isValidForMemcached( sessionId ) ) { if ( _log.isDebugEnabled() ) { _log.debug( "Deleting session from memcached: " + sessionId ); } try { final long start = System.currentTimeMillis(); _storage.delete( _memcachedNodesManager.getStorageKeyFormat().format(sessionId) ).get(); _statistics.registerSince( DELETE_FROM_MEMCACHED, start ); if ( !_sticky ) { _lockingStrategy.onAfterDeleteFromMemcached( sessionId ); } } catch ( final Throwable e ) { _log.info( "Could not delete session from memcached.", e ); } } }
protected LockStatus lock( final String sessionId, final long timeout, final TimeUnit timeUnit ) { if ( _log.isDebugEnabled() ) { _log.debug( "Locking session " + sessionId ); } final long start = System.currentTimeMillis(); try { acquireLock( sessionId, LOCK_RETRY_INTERVAL, LOCK_MAX_RETRY_INTERVAL, timeUnit.toMillis( timeout ), System.currentTimeMillis() ); _stats.registerSince( ACQUIRE_LOCK, start ); if ( _log.isDebugEnabled() ) { _log.debug( "Locked session " + sessionId ); } return LockStatus.LOCKED; } catch ( final TimeoutException e ) { _log.warn( "Reached timeout when trying to aquire lock for session " + sessionId + ". Will use this session without this lock." ); _stats.registerSince( ACQUIRE_LOCK_FAILURE, start ); return LockStatus.COULD_NOT_AQUIRE_LOCK; } catch ( final InterruptedException e ) { Thread.currentThread().interrupt(); throw new RuntimeException( "Got interrupted while trying to lock session.", e ); } catch ( final ExecutionException e ) { _log.warn( "An exception occurred when trying to aquire lock for session " + sessionId ); _stats.registerSince( ACQUIRE_LOCK_FAILURE, start ); return LockStatus.COULD_NOT_AQUIRE_LOCK; } }
private MemcachedBackupSession loadBackupSession(final String requestedSessionId, final String newNodeId) { try { final SessionValidityInfo validityInfo = _lockingStrategy.loadBackupSessionValidityInfo( requestedSessionId ); if ( validityInfo == null || !validityInfo.isValid() ) { if(_log.isDebugEnabled()) _log.debug( "No validity info (or no valid one) found for sessionId " + requestedSessionId ); return null; } final byte[] obj = _storage.get( getSessionIdFormat().createBackupKey( requestedSessionId ) ); if ( obj == null ) { if(_log.isDebugEnabled()) _log.debug( "No backup found for sessionId " + requestedSessionId ); return null; } final MemcachedBackupSession session = _transcoderService.deserialize( obj, _manager ); session.setSticky( _sticky ); session.setLastAccessedTimeInternal( validityInfo.getLastAccessedTime() ); session.setThisAccessedTimeInternal( validityInfo.getThisAccessedTime() ); final String newSessionId = getSessionIdFormat().createNewSessionId( requestedSessionId, newNodeId ); _log.info( "Session backup loaded from secondary memcached for "+ requestedSessionId +" (will be relocated)," + " setting new id "+ newSessionId +" on session..." ); session.setIdInternal( newSessionId ); return session; } catch( final Exception e ) { _log.error( "Could not get backup validityInfo or backup session for sessionId " + requestedSessionId, e ); return null; } }
super.onAfterBackupSession( session, backupWasForced, result, requestId, backupSessionService );
_service.getLockingStrategy().getExecutorService().shutdown();
_lockingStrategy.onBackupWithoutLoadedSession( sessionId, requestId, _backupSessionService ); _lockingStrategy.onAfterBackupSession( msmSession, force, result, requestId, _backupSessionService );
@CheckForNull protected SessionValidityInfo loadBackupSessionValidityInfo( @Nonnull final String sessionId ) { final String key = _sessionIdFormat.createValidityInfoKeyName( sessionId ); final String backupKey = _sessionIdFormat.createBackupKey( key ); return loadSessionValidityInfoForValidityKey( backupKey ); }
protected LockStatus lock( final String sessionId ) { return lock( sessionId, _manager.getOperationTimeout(), TimeUnit.MILLISECONDS ); }
protected void deleteFromMemcached(final String sessionId) { if ( _enabled.get() && _memcachedNodesManager.isValidForMemcached( sessionId ) ) { if ( _log.isDebugEnabled() ) { _log.debug( "Deleting session from memcached: " + sessionId ); } try { final long start = System.currentTimeMillis(); _storage.delete( _memcachedNodesManager.getStorageKeyFormat().format(sessionId) ).get(); _statistics.registerSince( DELETE_FROM_MEMCACHED, start ); if ( !_sticky ) { _lockingStrategy.onAfterDeleteFromMemcached( sessionId ); } } catch ( final Throwable e ) { _log.info( "Could not delete session from memcached.", e ); } } }
protected LockStatus lock( final String sessionId, final long timeout, final TimeUnit timeUnit ) { if ( _log.isDebugEnabled() ) { _log.debug( "Locking session " + sessionId ); } final long start = System.currentTimeMillis(); try { acquireLock( sessionId, LOCK_RETRY_INTERVAL, LOCK_MAX_RETRY_INTERVAL, timeUnit.toMillis( timeout ), System.currentTimeMillis() ); _stats.registerSince( ACQUIRE_LOCK, start ); if ( _log.isDebugEnabled() ) { _log.debug( "Locked session " + sessionId ); } return LockStatus.LOCKED; } catch ( final TimeoutException e ) { _log.warn( "Reached timeout when trying to aquire lock for session " + sessionId + ". Will use this session without this lock." ); _stats.registerSince( ACQUIRE_LOCK_FAILURE, start ); return LockStatus.COULD_NOT_AQUIRE_LOCK; } catch ( final InterruptedException e ) { Thread.currentThread().interrupt(); throw new RuntimeException( "Got interrupted while trying to lock session.", e ); } catch ( final ExecutionException e ) { _log.warn( "An exception occurred when trying to aquire lock for session " + sessionId ); _stats.registerSince( ACQUIRE_LOCK_FAILURE, start ); return LockStatus.COULD_NOT_AQUIRE_LOCK; } }
private MemcachedBackupSession loadBackupSession(final String requestedSessionId, final String newNodeId) { try { final SessionValidityInfo validityInfo = _lockingStrategy.loadBackupSessionValidityInfo( requestedSessionId ); if ( validityInfo == null || !validityInfo.isValid() ) { if(_log.isDebugEnabled()) _log.debug( "No validity info (or no valid one) found for sessionId " + requestedSessionId ); return null; } final byte[] obj = _storage.get( getSessionIdFormat().createBackupKey( requestedSessionId ) ); if ( obj == null ) { if(_log.isDebugEnabled()) _log.debug( "No backup found for sessionId " + requestedSessionId ); return null; } final MemcachedBackupSession session = _transcoderService.deserialize( obj, _manager ); session.setSticky( _sticky ); session.setLastAccessedTimeInternal( validityInfo.getLastAccessedTime() ); session.setThisAccessedTimeInternal( validityInfo.getThisAccessedTime() ); final String newSessionId = getSessionIdFormat().createNewSessionId( requestedSessionId, newNodeId ); _log.info( "Session backup loaded from secondary memcached for "+ requestedSessionId +" (will be relocated)," + " setting new id "+ newSessionId +" on session..." ); session.setIdInternal( newSessionId ); return session; } catch( final Exception e ) { _log.error( "Could not get backup validityInfo or backup session for sessionId " + requestedSessionId, e ); return null; } }
super.onAfterBackupSession( session, backupWasForced, result, requestId, backupSessionService );
ExecutorService executorService = _service.getLockingStrategy().getExecutorService(); executorService.shutdown(); executorService.awaitTermination(1, SECONDS);
protected void acquireLock( @Nonnull final String sessionId, final long retryInterval, final long maxRetryInterval, final long timeout, final long start ) throws InterruptedException, ExecutionException, TimeoutException { final Future<Boolean> result = _storage.add( _sessionIdFormat.createLockName( sessionId ), _manager.getLockExpiration(), LOCK_VALUE ); if ( result.get().booleanValue() ) { if ( _log.isDebugEnabled() ) { _log.debug( "Locked session " + sessionId ); } return; } else { checkTimeoutAndWait( sessionId, retryInterval, timeout, start ); acquireLock( sessionId, min( retryInterval * 2, maxRetryInterval ), maxRetryInterval, timeout, start ); } }