/** * {@inheritDoc} */ @Override public MemcachedBackupSession createEmptySession() { return _msm.createEmptySession(); }
/** * Return the active Session, associated with this Manager, with the * specified session id (if any); otherwise return <code>null</code>. * * @param id * The session id for the session to be returned * @return the session or <code>null</code> if no session was found locally * or in memcached. * * @exception IllegalStateException * if a new session cannot be instantiated for any reason * @exception IOException * if an input/output error occurs while processing this * request */ @Override public Session findSession( final String id ) throws IOException { return _msm.findSession( id ); }
/** * Sets the session locking mode. Possible values: * <ul> * <li><code>none</code> - does not lock the session at all (default for non-sticky sessions).</li> * <li><code>all</code> - the session is locked for each request accessing the session.</li> * <li><code>auto</code> - locks the session for each request except for those the were detected to access the session only readonly.</li> * <li><code>uriPattern:<regexp></code> - locks the session for each request with a request uri (with appended querystring) matching * the provided regular expression.</li> * </ul> */ @Override public void setLockingMode( @Nullable final String lockingMode ) { _msm.setLockingMode( lockingMode ); }
/** * Initialize this manager. The storageClient parameter is there for testing * purposes. If the storageClient is provided it's used, otherwise a "real"/new * storage client is created based on the configuration (like {@link #setMemcachedNodes(String)} etc.). * * @param storageClient the storage client to use, for normal operations this should be <code>null</code>. */ protected void startInternal(final StorageClient storageClient ) throws LifecycleException { _msm.setStorageClient(storageClient); _msm.startInternal(); }
@Test public void testSessionsRefCountHandlingIssue111() throws Exception { _service.setSticky(false); _service.setLockingMode(LockingMode.ALL.name()); _service.setTranscoderService( transcoderService ); _service.setStorageClient(new MemcachedStorageClient(_memcachedMock)); _service.startInternal(); Future<BackupResult> result = _service.backupSession(session.getId(), false, "unused"); assertFalse(_service.getManager().getSessionsInternal().containsKey(session.getId())); _service.getTrackingHostValve().storeRequestThreadLocal(requestMock); final MemcachedBackupSession session2 = _service.findSession(session.getId()); assertTrue(session2.isLocked()); assertEquals(session2.getRefCount(), 1); result = _service.backupSession(session.getId(), false, null); _service.getTrackingHostValve().resetRequestThreadLocal(); assertEquals(result.get().getStatus(), BackupResultStatus.SKIPPED); assertTrue(_service.getManager().getSessionsInternal().containsKey(session.getId()));
public void testSessionTimeoutUnlimitedWithNonStickySessionNotLoaded() throws InterruptedException, ExecutionException, LifecycleException, TimeoutException { _service.setStickyInternal( false ); _service.setLockingMode( LockingMode.NONE, null, false ); _service.setMemcachedNodes( "n1:127.0.0.1:11211 n2:127.0.0.1:11212" ); // for backup support _service.startInternal(new MemcachedStorageClient(_memcachedMock)); // we must put in our mock again when( _memcachedMock.add( any( String.class ), anyInt(), any(), any( Transcoder.class ) ) ).thenReturn( futureMock ); _service.backupSession( sessionId, false, "unused" ).get(); _service.getLockingStrategy().getExecutorService().shutdown();
@Test public void testInvalidNonStickySessionDoesNotCallOnBackupWithoutLoadedSessionIssue137() throws Exception { _service.setStickyInternal( false ); _service.setLockingMode( LockingMode.NONE, null, false ); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); // we must put in our mock again final String sessionId = "nonStickySessionToTimeOut-n1"; // For findSession needed final Request requestMock = mock(Request.class); when(requestMock.getNote(eq(RequestTrackingContextValve.INVOKED))).thenReturn(Boolean.TRUE); _service.getTrackingHostValve().storeRequestThreadLocal(requestMock); final MemcachedBackupSession session = _service.findSession(sessionId); assertNull(session); _service.backupSession( sessionId, false, null ).get(); // check that validity info is not loaded - this would trigger the // WARNING: Found no validity info for session id ... final String validityKey = new SessionIdFormat().createValidityInfoKeyName( sessionId ); verify( _memcachedMock, times( 0 ) ).get( eq( validityKey ) ); }
/** * {@inheritDoc} */ @Override public void stopInternal() throws LifecycleException { setState(LifecycleState.STOPPING); if ( _msm.isSticky() ) { _log.info( "Removing sessions from local session map." ); for( final Session session : sessions.values() ) { swapOut( (StandardSession) session ); } } _msm.shutdown(); super.stopInternal(); }
private void removeInternal( final Session session, final boolean update, final boolean removeFromMemcached ) { if ( _log.isDebugEnabled() ) { _log.debug( "remove invoked, removeFromMemcached: " + removeFromMemcached + ", id: " + session.getId() ); } if ( removeFromMemcached ) { _msm.deleteFromMemcached( session.getId() ); } super.remove( session, update ); _msm.sessionRemoved((MemcachedBackupSession) session); }
if ( !_manager.isSessionBackupAsync() ) { validityResult.get( _manager.getSessionBackupTimeout(), TimeUnit.MILLISECONDS );
@Test public void testConfigurationFormatFailoverNodesFeature44() throws LifecycleException { _service.setMemcachedNodes( "n1:127.0.0.1:11211 n2:127.0.0.1:11212" ); _service.setFailoverNodes( "n1" ); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); Assert.assertEquals( _service.getFailoverNodeIds(), Arrays.asList( "n1" ) ); _service.setMemcachedNodes( "n1:127.0.0.1:11211 n2:127.0.0.1:11212 n3:127.0.0.1:11213" ); _service.setFailoverNodes( "n1 n2" ); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); Assert.assertEquals( _service.getFailoverNodeIds(), Arrays.asList( "n1", "n2" ) ); _service.setMemcachedNodes( "n1:127.0.0.1:11211 n2:127.0.0.1:11212 n3:127.0.0.1:11213" ); _service.setFailoverNodes( "n1,n2" ); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); Assert.assertEquals( _service.getFailoverNodeIds(), Arrays.asList( "n1", "n2" ) ); }
/** * Test for issue #105: Make memcached node optional for single-node setup * http://code.google.com/p/memcached-session-manager/issues/detail?id=105 */ @Test public void testBackupSessionFailureWithoutMemcachedNodeIdConfigured105() throws Exception { _service.setMemcachedNodes( "127.0.0.1:11211" ); _service.setSessionBackupAsync(false); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); final MemcachedBackupSession session = createSession( _service ); session.access(); session.endAccess(); session.setAttribute( "foo", "bar" ); @SuppressWarnings( "unchecked" ) final OperationFuture<Boolean> futureMock = mock( OperationFuture.class ); when( futureMock.get( ) ).thenThrow(new ExecutionException(new RuntimeException("Simulated exception."))); when( futureMock.get( anyInt(), any( TimeUnit.class ) ) ).thenThrow(new ExecutionException(new RuntimeException("Simulated exception."))); when( _memcachedMock.set( eq( session.getId() ), anyInt(), any(), any( Transcoder.class ) ) ).thenReturn( futureMock ); final BackupResult backupResult = _service.backupSession( session.getIdInternal(), false, null ).get(); assertEquals(backupResult.getStatus(), BackupResultStatus.FAILURE); verify( _memcachedMock, times( 1 ) ).set( eq( session.getId() ), anyInt(), any(), any( Transcoder.class ) ); }
/** * Test for issue #105: Make memcached node optional for single-node setup * http://code.google.com/p/memcached-session-manager/issues/detail?id=105 */ @Test public void testConfigurationFormatMemcachedNodesFeature105() throws LifecycleException { _service.setMemcachedNodes( "127.0.0.1:11211" ); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); assertEquals(_service.getMemcachedNodesManager().getCountNodes(), 1); assertEquals(_service.getMemcachedNodesManager().isEncodeNodeIdInSessionId(), false); assertEquals(_service.getMemcachedNodesManager().isValidForMemcached("123456"), true); _service.shutdown(); _service.setMemcachedNodes( "n1:127.0.0.1:11211" ); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); assertEquals(_service.getMemcachedNodesManager().getCountNodes(), 1); assertEquals(_service.getMemcachedNodesManager().isEncodeNodeIdInSessionId(), true); assertEquals(_service.getMemcachedNodesManager().isValidForMemcached("123456"), false); assertEquals(_service.getMemcachedNodesManager().isValidForMemcached("123456-n1"), true); }
/** * Return the compiled pattern used for including session attributes to a session-backup. * * @return the sessionAttributePattern */ @CheckForNull Pattern getSessionAttributePattern() { return _msm.getSessionAttributePattern(); }
public long getOperationTimeout() { return _msm.getOperationTimeout(); }
/** * Initialize this manager. The memcachedClient parameter is there for testing * purposes. If the memcachedClient is provided it's used, otherwise a "real"/new * memcached client is created based on the configuration (like {@link #setMemcachedNodes(String)} etc.). * * @param storage the storage client to use, for normal operations this should be <code>null</code>. */ void startInternal( final StorageClient storage ) throws LifecycleException { if (storage == null) _storage = null; else _storage = storage; startInternal(); }
@Test public void testConfigurationFormatMemcachedNodesFeature44() throws LifecycleException { _service.setMemcachedNodes( "n1:127.0.0.1:11211" ); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); Assert.assertEquals( _service.getNodeIds(), Arrays.asList( "n1" ) ); _service.setMemcachedNodes( "n1:127.0.0.1:11211 n2:127.0.0.1:11212" ); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); Assert.assertEquals( _service.getNodeIds(), Arrays.asList( "n1", "n2" ) ); _service.setMemcachedNodes( "n1:127.0.0.1:11211,n2:127.0.0.1:11212" ); _service.startInternal(new MemcachedStorageClient(_memcachedMock)); Assert.assertEquals( _service.getNodeIds(), Arrays.asList( "n1", "n2" ) ); }
public boolean isSticky() { return _msm.isSticky(); }
@Override public BackupResult call() throws Exception { final MemcachedBackupSession session3 = _service.findSession(session.getId()); assertSame(session3, session2); assertEquals(session3.getRefCount(), 2); // let the other thread proceed (or wait) barrier.await(); // and wait again so that the other thread can do some work barrier.await(); final Future<BackupResult> result = _service.backupSession(session.getId(), false, "unused"); _service.getTrackingHostValve().resetRequestThreadLocal(); assertEquals(result.get().getStatus(), BackupResultStatus.SUCCESS); // The session should be released now and no longer stored assertFalse(_service.getManager().getSessionsInternal().containsKey(session.getId())); // just some double checking on expectations... assertEquals(session2.getRefCount(), 0); return result.get(); }
public void testChangeSessionId( final SessionAffinityMode stickyness ) throws InterruptedException, ExecutionException, TimeoutException { _service.setStickyInternal( stickyness.isSticky() ); if ( !stickyness.isSticky() ) { _service.setLockingMode( LockingMode.NONE, null, false ); _service.backupSession( session.getIdInternal(), false, "foo" ).get(); _service.getManager().changeSessionId( session ); _service.backupSession( session.getIdInternal(), false, "foo" );