protected NodeAvailabilityCache<String> createNodeAvailabilityCache( final int size, final long ttlInMillis, @Nonnull final StorageClientCallback storageClientCallback) { return new NodeAvailabilityCache<String>( size, ttlInMillis, new CacheLoader<String>() { @Override public boolean isNodeAvailable( final String key ) { try { storageClientCallback.get(_sessionIdFormat.createSessionId( "ping", key ) ); return true; } catch ( final Exception e ) { return false; } } } ); }
/** * Determines, if the node is available. If it's not cached, it's loaded * from the cache loader. * * @param key * the key to check * @return <code>true</code> if the node is marked as available. */ public boolean isNodeAvailable( @Nonnull final K key ) { final ManagedItem<Boolean> item = _map.get( key ); if ( item == null ) { return updateIsNodeAvailable( key ); } else if ( isExpired( item ) ) { _map.remove( key ); return updateIsNodeAvailable( key ); } else { return item._value; } }
/** * Determines, if the given nodeId is available. * @param nodeId the node to check, not <code>null</code>. * @return <code>true</code>, if the node is marked as available */ public boolean isNodeAvailable( @Nonnull final String nodeId ) { return _nodeAvailabilityCache.isNodeAvailable( nodeId ); }
/** * Mark the given nodeId as available as specified. * @param nodeId the nodeId to update * @param available specifies if the node was abailable or not */ public void setNodeAvailable( final String nodeId, final boolean available ) { _nodeAvailabilityCache.setNodeAvailable( nodeId, available ); }
/** * A set of nodes that are stored as unavailable. * * @return a set of unavailable nodes, never <code>null</code>. */ public Set<K> getUnavailableNodes() { final Set<K> result = new HashSet<K>(); for ( final Map.Entry<K, ManagedItem<Boolean>> entry : _map.entrySet() ) { if ( !entry.getValue()._value.booleanValue() && !isExpired( entry.getValue() ) ) { result.add( entry.getKey() ); } } return result; }
/** * Mark the given nodeId as available as specified. * @param nodeId the nodeId to update * @param available specifies if the node was abailable or not */ public void setNodeAvailable( final String nodeId, final boolean available ) { _nodeAvailabilityCache.setNodeAvailable( nodeId, available ); }
/** * A set of nodes that are stored as unavailable. * * @return a set of unavailable nodes, never <code>null</code>. */ public Set<K> getUnavailableNodes() { final Set<K> result = new HashSet<K>(); for ( final Map.Entry<K, ManagedItem<Boolean>> entry : _map.entrySet() ) { if ( !entry.getValue()._value.booleanValue() && !isExpired( entry.getValue() ) ) { result.add( entry.getKey() ); } } return result; }
@Test public final void testGetNextNodeId_SingleNode() { final CacheLoader<String> cacheLoader = new DummyCacheLoader( null ); final NodeIdService cut = new NodeIdService( new NodeAvailabilityCache<String>( 10, 100, cacheLoader ), NodeIdList.create( "n1" ), null ); final String actual = cut.getAvailableNodeId( "n1" ); assertNull( actual, "For a sole existing node we cannot get a next node" ); }
/** * Determines, if the node is available. If it's not cached, it's loaded * from the cache loader. * * @param key * the key to check * @return <code>true</code> if the node is marked as available. */ public boolean isNodeAvailable( @Nonnull final K key ) { final ManagedItem<Boolean> item = _map.get( key ); if ( item == null ) { return updateIsNodeAvailable( key ); } else if ( isExpired( item ) ) { _map.remove( key ); return updateIsNodeAvailable( key ); } else { return item._value; } }
/** * Determines, if the given nodeId is available. * @param nodeId the node to check, not <code>null</code>. * @return <code>true</code>, if the node is marked as available */ public boolean isNodeAvailable( @Nonnull final String nodeId ) { return _nodeAvailabilityCache.isNodeAvailable( nodeId ); }
private NodeAvailabilityCache<String> createNodeAvailabilityCache( final String ... unavailableNodes ) { final List<String> unavailable = unavailableNodes != null ? Arrays.asList( unavailableNodes ) : null; return new NodeAvailabilityCache<String>( 10, 100, new DummyCacheLoader( unavailable ) ); }
/** * Test two memcached nodes: * - node n2 is the currently used node, which failed * - node n1 is also unavailable * - the result must be null */ @Test public final void testGetNextNodeId_TwoNodes_NoNodeLeft() { final String nodeId1 = "n1"; final String nodeId2 = "n2"; final CacheLoader<String> cacheLoader = new DummyCacheLoader( Arrays.asList( nodeId1 ) ); final NodeIdService cut = new NodeIdService( new NodeAvailabilityCache<String>( 10, 100, cacheLoader ), NodeIdList.create( nodeId1, nodeId2 ), null ); final String actual = cut.getAvailableNodeId( nodeId2 ); assertNull( actual ); }
/** * Test two memcached nodes: * - node n1 is the currently used node, which failed * - node n2 must be the next node * * Also test that if the current node is n2, then n1 must be chosen. */ @Test public final void testGetNextNodeId_TwoNodes() { final String nodeId1 = "n1"; final String nodeId2 = "n2"; final CacheLoader<String> cacheLoader = new DummyCacheLoader( null ); final NodeIdService cut = new NodeIdService( new NodeAvailabilityCache<String>( 10, 100, cacheLoader ), NodeIdList.create( nodeId1, nodeId2 ), null ); String actual = cut.getAvailableNodeId( nodeId1 ); assertEquals( nodeId2, actual ); /* let's switch nodes, so that the session is bound to node 2 */ actual = cut.getAvailableNodeId( nodeId2 ); assertEquals( nodeId1, actual ); }
@Test public void testSetNodeAvailability() { final String nodeId1 = "n1"; final CacheLoader<String> cacheLoader = new CacheLoader<String>() { @Override public boolean isNodeAvailable( final String key ) { return true; } }; final NodeIdService cut = new NodeIdService( new NodeAvailabilityCache<String>( 10, 100, cacheLoader ), NodeIdList.create( nodeId1 ), Collections.<String> emptyList() ); Assert.assertTrue( cut.isNodeAvailable( nodeId1 ) ); cut.setNodeAvailable( nodeId1, false ); Assert.assertFalse( cut.isNodeAvailable( nodeId1 ) ); cut.setNodeAvailable( nodeId1, true ); Assert.assertTrue( cut.isNodeAvailable( nodeId1 ) ); }
protected NodeAvailabilityCache<String> createNodeAvailabilityCache( final int size, final long ttlInMillis, @Nonnull final StorageClientCallback storageClientCallback) { return new NodeAvailabilityCache<String>( size, ttlInMillis, new CacheLoader<String>() { @Override public boolean isNodeAvailable( final String key ) { try { storageClientCallback.get(_sessionIdFormat.createSessionId( "ping", key ) ); return true; } catch ( final Exception e ) { return false; } } } ); }