/** * Filter map of attributes using our name pattern. * * @return the filtered attribute map that only includes attributes that shall be stored in memcached. */ public ConcurrentMap<String, Object> getAttributesFiltered() { if ( this.manager == null ) { throw new IllegalStateException( "There's no manager set." ); } final Pattern pattern = ((SessionManager)manager).getMemcachedSessionService().getSessionAttributePattern(); final ConcurrentMap<String, Object> attributes = getAttributesInternal(); if ( pattern == null ) { return attributes; } final ConcurrentMap<String, Object> result = new ConcurrentHashMap<String, Object>( attributes.size() ); for ( final Map.Entry<String, Object> entry: attributes.entrySet() ) { if ( pattern.matcher(entry.getKey()).matches() ) { result.put( entry.getKey(), entry.getValue() ); } } return result; }
/** * Check whether the given attribute name matches our name pattern and shall be stored in memcached. * * @return true if the name matches */ private boolean filterAttribute( final String name ) { if ( this.manager == null ) { throw new IllegalStateException( "There's no manager set." ); } final Pattern pattern = ((SessionManager)manager).getMemcachedSessionService().getSessionAttributePattern(); if ( pattern == null ) { return true; } return pattern.matcher(name).matches(); }
@Nonnull protected SessionManager createSessionManager() { final SessionManager manager = mock( SessionManager.class ); final StandardContext context = createContext(); when( manager.getContext() ).thenReturn(context); // needed for createSession when( manager.getContainer() ).thenReturn(context); // needed for createSession when( manager.getMemcachedSessionService() ).thenReturn(newMemcachedSessionService(manager)); when( manager.newMemcachedBackupSession() ).thenReturn( new MemcachedBackupSession( manager ) ); when( manager.willAttributeDistribute(anyString(), anyObject())).thenReturn(true); return manager; }
/** * Filter map of attributes using our name pattern. * * @return the filtered attribute map that only includes attributes that shall be stored in memcached. */ public ConcurrentMap<String, Object> getAttributesFiltered() { if ( this.manager == null ) { throw new IllegalStateException( "There's no manager set." ); } final Pattern pattern = ((SessionManager)manager).getMemcachedSessionService().getSessionAttributePattern(); final ConcurrentMap<String, Object> attributes = getAttributesInternal(); if ( pattern == null ) { return attributes; } final ConcurrentMap<String, Object> result = new ConcurrentHashMap<String, Object>( attributes.size() ); for ( final Map.Entry<String, Object> entry: attributes.entrySet() ) { if ( pattern.matcher(entry.getKey()).matches() ) { result.put( entry.getKey(), entry.getValue() ); } } return result; }
/** * Check whether the given attribute name matches our name pattern and shall be stored in memcached. * * @return true if the name matches */ private boolean filterAttribute( final String name ) { if ( this.manager == null ) { throw new IllegalStateException( "There's no manager set." ); } final Pattern pattern = ((SessionManager)manager).getMemcachedSessionService().getSessionAttributePattern(); if ( pattern == null ) { return true; } return pattern.matcher(name).matches(); }
waitForReconnect( _tomcat1.getManager().getMemcachedSessionService(), info.nextNode().getValue(), 5000 ); assertEquals( get( _httpClient, _portTomcat1, sid1 ).getSessionId(), sid1 ); Thread.sleep( 300 ); // wait for the async processes to complete / be cancelleds
manager.setMaxInactiveInterval( 5 ); manager.setMemcachedNodes(memcachedNodes); manager.getMemcachedSessionService().setSessionBackupAsync(false); waitForReconnect(manager.getMemcachedSessionService().getStorageClient(), 3, 1000);
manager.setMaxInactiveInterval( 5 ); manager.setMemcachedNodes(memcachedNodes); manager.getMemcachedSessionService().setSessionBackupAsync(false); waitForReconnect(manager.getMemcachedSessionService().getStorageClient(), 3, 1000);
/** * Test for issue #49: * Sessions not associated with a memcached node don't get associated as soon as a memcached is available * @throws InterruptedException * @throws IOException * @throws TimeoutException * @throws ExecutionException */ @Test( enabled = true ) public void testNotAssociatedSessionGetsAssociatedIssue49() throws InterruptedException, IOException, ExecutionException, TimeoutException { _daemon.stop(); final SessionManager manager = _tomcat1.getManager(); manager.setMaxInactiveInterval( 5 ); manager.setSticky( true ); final SessionIdFormat sessionIdFormat = new SessionIdFormat(); final Session session = manager.createSession( null ); assertNull( sessionIdFormat.extractMemcachedId( session.getId() ) ); _daemon.start(); // Wait so that the daemon will be available and the client can reconnect (async get didn't do the trick) waitForReconnect(manager.getMemcachedSessionService().getStorageClient(), 1, 4000); final String newSessionId = manager.getMemcachedSessionService().changeSessionIdOnMemcachedFailover( session.getId() ); assertNotNull( newSessionId ); assertEquals( newSessionId, session.getId() ); assertEquals( sessionIdFormat.extractMemcachedId( newSessionId ), _memcachedNodeId ); }
@BeforeMethod public void setup() throws LifecycleException, ClassNotFoundException, IOException { _manager = mock( SessionManager.class ); final Context context = new StandardContext(); when( _manager.getContext() ).thenReturn( context ); // needed for createSession // Manager.getContainer no longer available in tc 8.5+ if(_managerHasGetContainer) { when( _manager.getContainer() ).thenReturn( context ); } when( _manager.newMemcachedBackupSession() ).thenAnswer(new Answer<MemcachedBackupSession>() { @Override public MemcachedBackupSession answer(final InvocationOnMock invocation) throws Throwable { return newMemcachedBackupSession( _manager ); } }); final MemcachedSessionService service = new DummyMemcachedSessionService<SessionManager>( _manager ); when( _manager.createSession( anyString() ) ).thenAnswer(new Answer<MemcachedBackupSession>() { @Override public MemcachedBackupSession answer(final InvocationOnMock invocation) throws Throwable { return createSession(service); } }); when( _manager.readPrincipal( (ObjectInputStream)any() ) ).thenReturn( createPrincipal() ); when( _manager.getMemcachedSessionService() ).thenReturn( service ); when( _manager.willAttributeDistribute(anyString(), any())).thenReturn(true); }
@SuppressWarnings("unchecked") @BeforeMethod public void setup() throws Exception { final StandardContext context = createContext(); context.setBackgroundProcessorDelay( 1 ); // needed for test of updateExpiration final SessionManager manager = createSessionManager(context); _service = manager.getMemcachedSessionService(); _service.setMemcachedNodes( "n1:127.0.0.1:11211" ); _service.setSessionBackupAsync( false ); _service.setSticky( true ); _memcachedMock = mock( MemcachedClient.class ); final OperationFuture<Boolean> setResultMock = mock( OperationFuture.class ); when( setResultMock.get( ) ).thenReturn( Boolean.TRUE ); when( setResultMock.get( anyInt(), any( TimeUnit.class ) ) ).thenReturn( Boolean.TRUE ); when( _memcachedMock.set( any( String.class ), anyInt(), any(), any( Transcoder.class ) ) ).thenReturn( setResultMock ); final OperationFuture<Boolean> deleteResultMock = mock( OperationFuture.class ); when( deleteResultMock.get() ).thenReturn( Boolean.TRUE ); when( _memcachedMock.delete( anyString() ) ).thenReturn( deleteResultMock ); startInternal( manager, _memcachedMock ); _executor = Executors.newCachedThreadPool(); }
private void setStickyness(final SessionAffinityMode sessionAffinity) { if(!sessionAffinity.isSticky()) { _tomcat1.getEngine().setJvmRoute(null); } final SessionManager manager = _tomcat1.getManager(); manager.setSticky( sessionAffinity.isSticky() ); try { waitForReconnect(manager.getMemcachedSessionService().getStorageClient(), 1, 500); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); throw new RuntimeException(e); } }
private static MemcachedBackupSession createSession( final SessionManager manager, final String id ) { final MemcachedBackupSession session = manager.getMemcachedSessionService().createEmptySession(); session.setId( id ); session.setValid( true ); return session; }
private void waitForSessionExpiration(final boolean sticky) throws InterruptedException { final SessionManager manager = _tomcat1.getManager(); assertEquals( manager.getMemcachedSessionService().isSticky(), sticky ); final Container container = manager.getContext(); final long timeout = TimeUnit.SECONDS.toMillis( sticky ? container.getBackgroundProcessorDelay() + manager.getMaxInactiveInterval() : 2 * manager.getMaxInactiveInterval() ) + 1000; Thread.sleep( timeout ); }
private void checkSessionFunctionalityWithMsmDisabled() throws IOException, HttpException, InterruptedException { assertTrue( _tomcat1.getManager().getMemcachedSessionService().isSticky() ); final String sessionId1 = makeRequest( _httpClient, _portTomcat1, null ); assertNotNull( sessionId1, "No session created." ); assertNull( new SessionIdFormat().extractMemcachedId( sessionId1 ), "Got a memcached node id, even with msm disabled." ); waitForSessionExpiration( true ); final String sessionId2 = makeRequest( _httpClient, _portTomcat1, sessionId1 ); assertNotSame( sessionId2, sessionId1, "SessionId not changed." ); }