private String getNodeId( final String key ) { final String nodeId = _sessionIdFormat.extractMemcachedId( key ); if ( !_sessionIdFormat.isBackupKey( key ) ) { return nodeId; } return _memcachedNodesManager.getNextAvailableNodeId( nodeId ); }
@Test public void testGetNextAvailableNodeId() throws IOException { assertNull(createFor( "n1:localhost:11211", null, null, _mcc ).getNextAvailableNodeId("n1")); assertEquals(createFor( "n1:localhost:11211,n2:localhost:11212", null, null, _mcc ).getNextAvailableNodeId("n1"), "n2"); final StorageClientCallback mcc = mock(StorageClientCallback.class); when(mcc.get(anyString())).thenReturn(null); when(mcc.get(endsWith("n2"))).thenThrow(new OperationTimeoutException("SimulatedException")); assertNull(createFor( "n1:localhost:11211,n2:localhost:11212", null, null, mcc).getNextAvailableNodeId("n1")); assertEquals(createFor( "n1:localhost:11211,n2:localhost:11212,n3:localhost:11213", null, null, mcc).getNextAvailableNodeId("n1"), "n3"); }
@CheckForNull private MemcachedBackupSession loadBackupSession( @Nonnull final String requestedSessionId ) { final String nodeId = getSessionIdFormat().extractMemcachedId( requestedSessionId ); if ( nodeId == null ) { _log.info( "Cannot load backupSession for sessionId without nodeId: "+ requestedSessionId ); return null; } final String newNodeId = _memcachedNodesManager.getNextAvailableNodeId(nodeId); if ( newNodeId == null ) { _log.info( "No next available node found for nodeId "+ nodeId ); return null; } MemcachedBackupSession result = loadBackupSession(requestedSessionId, newNodeId); String nextNodeId = nodeId; // if we didn't find the backup in the next node, let's go through other nodes // to see if the backup is there. For this we have to fake the session id so that // the SuffixBasedNodeLocator selects another backup node. while(result == null && (nextNodeId = _memcachedNodesManager.getNextAvailableNodeId(nextNodeId)) != null && !nextNodeId.equals(nodeId)) { final String newSessionId = getSessionIdFormat().createNewSessionId(requestedSessionId, nextNodeId); result = loadBackupSession(newSessionId, newNodeId); } if ( result == null ) { _log.info( "No backup found for sessionId " + requestedSessionId ); return null; } return result; }
private String getNodeId( final String key ) { final String nodeId = _sessionIdFormat.extractMemcachedId( key ); if ( !_sessionIdFormat.isBackupKey( key ) ) { return nodeId; } return _memcachedNodesManager.getNextAvailableNodeId( nodeId ); }
@CheckForNull private MemcachedBackupSession loadBackupSession( @Nonnull final String requestedSessionId ) { final String nodeId = getSessionIdFormat().extractMemcachedId( requestedSessionId ); if ( nodeId == null ) { _log.info( "Cannot load backupSession for sessionId without nodeId: "+ requestedSessionId ); return null; } final String newNodeId = _memcachedNodesManager.getNextAvailableNodeId(nodeId); if ( newNodeId == null ) { _log.info( "No next available node found for nodeId "+ nodeId ); return null; } MemcachedBackupSession result = loadBackupSession(requestedSessionId, newNodeId); String nextNodeId = nodeId; // if we didn't find the backup in the next node, let's go through other nodes // to see if the backup is there. For this we have to fake the session id so that // the SuffixBasedNodeLocator selects another backup node. while(result == null && (nextNodeId = _memcachedNodesManager.getNextAvailableNodeId(nextNodeId)) != null && !nextNodeId.equals(nodeId)) { final String newSessionId = getSessionIdFormat().createNewSessionId(requestedSessionId, nextNodeId); result = loadBackupSession(newSessionId, newNodeId); } if ( result == null ) { _log.info( "No backup found for sessionId " + requestedSessionId ); return null; } return result; }