private byte[] serializeAttributes( final MemcachedBackupSession session, final ConcurrentMap<String, Object> attributes ) { final long start = System.currentTimeMillis(); final byte[] attributesData = _transcoderService.serializeAttributes( session, attributes ); _statistics.registerSince( ATTRIBUTES_SERIALIZATION, start ); return attributesData; }
protected void releaseLock( @Nonnull final String sessionId ) { try { if ( _log.isDebugEnabled() ) { _log.debug( "Releasing lock for session " + sessionId ); } final long start = System.currentTimeMillis(); _storage.delete( _sessionIdFormat.createLockName( sessionId ) ).get(); _stats.registerSince( RELEASE_LOCK, start ); } catch ( final Exception e ) { _log.warn( "Caught exception when trying to release lock for session " + sessionId, 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; } }
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 ); } } }
/** * Invoked after a non-sticky session is removed from memcached. */ protected void onAfterDeleteFromMemcached( @Nonnull final String sessionId ) { final long start = System.currentTimeMillis(); final String validityInfoKey = _sessionIdFormat.createValidityInfoKeyName( sessionId ); _storage.delete( validityInfoKey ); if (_storeSecondaryBackup) { try { _storage.delete(_sessionIdFormat.createBackupKey(sessionId)); _storage.delete(_sessionIdFormat.createBackupKey(validityInfoKey)); } catch (Exception e) { _log.info("Could not delete backup data for session " + sessionId + " (not critical, data will be evicted by memcached automatically).", e); } } _stats.registerSince( NON_STICKY_AFTER_DELETE_FROM_MEMCACHED, start ); }
private void releaseLock( @Nonnull final MemcachedBackupSession session ) { if ( session.isLocked() ) { try { if ( _log.isDebugEnabled() ) { _log.debug( "Releasing lock for session " + session.getIdInternal() ); } final long start = System.currentTimeMillis(); _storage.delete( _memcachedNodesManager.getSessionIdFormat().createLockName( session.getIdInternal() ) ).get(); _statistics.registerSince( RELEASE_LOCK, start ); session.releaseLock(); } catch( final Exception e ) { _log.warn( "Caught exception when trying to release lock for session " + session.getIdInternal(), e ); } } }
/** * 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() ); } }
private void storeSessionInMemcached( final MemcachedBackupSession session, final byte[] data) throws InterruptedException, ExecutionException, TimeoutException { /* calculate the expiration time (instead of using just maxInactiveInterval), as * this is relevant for the update of the expiration time: if we would just use * maxInactiveInterval, the session would exist longer in memcached than it would * be valid in tomcat */ final int expirationTime = session.getMemcachedExpirationTimeToSet(); final long start = System.currentTimeMillis(); try { final Future<Boolean> future = _storage.set( _memcachedNodesManager.getStorageKeyFormat().format(session.getId()), toMemcachedExpiration(expirationTime), data ); if ( !_sessionBackupAsync ) { future.get( _sessionBackupTimeout, TimeUnit.MILLISECONDS ); session.setLastMemcachedExpirationTime( expirationTime ); session.setLastBackupTime( System.currentTimeMillis() ); } else { /* in async mode, we asume the session was stored successfully */ session.setLastMemcachedExpirationTime( expirationTime ); session.setLastBackupTime( System.currentTimeMillis() ); } } finally { _statistics.registerSince( MEMCACHED_UPDATE, start ); } }
_stats.registerSince( NON_STICKY_ON_BACKUP_WITHOUT_LOADED_SESSION, start );
private void releaseLock() { if ( _session.isLocked() ) { try { if ( _log.isDebugEnabled() ) { _log.debug( "Releasing lock for session " + _session.getIdInternal() ); } final long start = System.currentTimeMillis(); _storage.delete( _memcachedNodesManager.getSessionIdFormat().createLockName( _session.getIdInternal() ) ).get(); _statistics.registerSince( RELEASE_LOCK, start ); _session.releaseLock(); } catch( final Exception e ) { _log.warn( "Caught exception when trying to release lock for session " + _session.getIdInternal(), e ); } } }
_stats.registerSince( NON_STICKY_AFTER_BACKUP, start );
/** * Store the provided session in memcached if the session was modified * or if the session needs to be relocated. * * @param session * the session to save * @param sessionRelocationRequired * specifies, if the session id was changed due to a memcached failover or tomcat failover. * @return the {@link BackupResultStatus} */ public Future<BackupResult> backupSession( final String sessionId, final boolean sessionIdChanged, final String requestId ) { final MemcachedBackupSession session = _manager.getSessionInternal( sessionId ); if ( session == null ) { if(_log.isDebugEnabled()) _log.debug( "No session found in session map for " + sessionId ); return new SimpleFuture<BackupResult>( BackupResult.SKIPPED ); } _log.info( "Serializing session data for session " + session.getIdInternal() ); final long startSerialization = System.currentTimeMillis(); final byte[] data = _transcoderService.serializeAttributes( (MemcachedBackupSession) session, ((MemcachedBackupSession) session).getAttributesFiltered() ); _log.info( String.format( "Serializing %1$,.3f kb session data for session %2$s took %3$d ms.", (double)data.length / 1000, session.getIdInternal(), System.currentTimeMillis() - startSerialization ) ); _sessionData.put( session.getIdInternal(), data ); _statistics.registerSince( ATTRIBUTES_SERIALIZATION, startSerialization ); _statistics.register( CACHED_DATA_SIZE, data.length ); return new SimpleFuture<BackupResult>( new BackupResult( BackupResultStatus.SUCCESS ) ); }
final long startDeserialization = System.currentTimeMillis(); final MemcachedBackupSession result = _transcoderService.deserialize( object, _manager ); _statistics.registerSince( SESSION_DESERIALIZATION, startDeserialization ); _statistics.registerSince( LOAD_FROM_MEMCACHED, start );
private byte[] serializeAttributes( final MemcachedBackupSession session, final ConcurrentMap<String, Object> attributes ) { final long start = System.currentTimeMillis(); final byte[] attributesData = _transcoderService.serializeAttributes( session, attributes ); _statistics.registerSince( ATTRIBUTES_SERIALIZATION, start ); return attributesData; }
_statistics.registerSince( EFFECTIVE_BACKUP, start );
break; case SUCCESS: _statistics.registerSince( BACKUP, startBackup ); _session.storeThisAccessedTimeFromLastBackupCheck(); _session.backupFinished();
protected void releaseLock( @Nonnull final String sessionId ) { try { if ( _log.isDebugEnabled() ) { _log.debug( "Releasing lock for session " + sessionId ); } final long start = System.currentTimeMillis(); _storage.delete( _sessionIdFormat.createLockName( sessionId ) ).get(); _stats.registerSince( RELEASE_LOCK, start ); } catch ( final Exception e ) { _log.warn( "Caught exception when trying to release lock for session " + sessionId, e ); } }
/** * Invoked after a non-sticky session is removed from memcached. */ protected void onAfterDeleteFromMemcached( @Nonnull final String sessionId ) { final long start = System.currentTimeMillis(); final String validityInfoKey = _sessionIdFormat.createValidityInfoKeyName( sessionId ); _storage.delete( validityInfoKey ); if (_storeSecondaryBackup) { try { _storage.delete(_sessionIdFormat.createBackupKey(sessionId)); _storage.delete(_sessionIdFormat.createBackupKey(validityInfoKey)); } catch (Exception e) { _log.info("Could not delete backup data for session " + sessionId + " (not critical, data will be evicted by memcached automatically).", e); } } _stats.registerSince( NON_STICKY_AFTER_DELETE_FROM_MEMCACHED, start ); }
private void releaseLock( @Nonnull final MemcachedBackupSession session ) { if ( session.isLocked() ) { try { if ( _log.isDebugEnabled() ) { _log.debug( "Releasing lock for session " + session.getIdInternal() ); } final long start = System.currentTimeMillis(); _storage.delete( _memcachedNodesManager.getSessionIdFormat().createLockName( session.getIdInternal() ) ).get(); _statistics.registerSince( RELEASE_LOCK, start ); session.releaseLock(); } catch( final Exception e ) { _log.warn( "Caught exception when trying to release lock for session " + session.getIdInternal(), e ); } } }
private void releaseLock() { if ( _session.isLocked() ) { try { if ( _log.isDebugEnabled() ) { _log.debug( "Releasing lock for session " + _session.getIdInternal() ); } final long start = System.currentTimeMillis(); _storage.delete( _memcachedNodesManager.getSessionIdFormat().createLockName( _session.getIdInternal() ) ).get(); _statistics.registerSince( RELEASE_LOCK, start ); _session.releaseLock(); } catch( final Exception e ) { _log.warn( "Caught exception when trying to release lock for session " + _session.getIdInternal(), e ); } } }