@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"); }
@Override public long incr(final String key, final int by, final long def) throws TimeoutException, CacheException { try { return memcachedClient.incr(key, by, def); } catch (OperationTimeoutException e) { LOGGER.warn("Operation timeout while incr {}", key, e); throw new TimeoutException(e.getMessage()); } catch (RuntimeException e) { if (translateException(e)) { throw new CacheException(e); } throw e; } }
@Override public Map<String, Object> getBulk(final Collection<String> keys) throws TimeoutException, CacheException { try { return memcachedClient.getBulk(keys); } catch (OperationTimeoutException e) { LOGGER.warn("Operation timeout while getBulk {}", keys, e); throw (TimeoutException) e.getCause(); } catch (RuntimeException e) { if (translateException(e)) { throw new CacheException(e); } throw e; } }
@JRubyMethod(name = { "decrement", "decr" }, required = 1, optional = 2) public IRubyObject decr(ThreadContext context, IRubyObject[] args) { Ruby ruby = context.getRuntime(); String key = getFullKey(args[0].toString()); int by = getIncrDecrBy(args); int expiry = getExpiry(args); int retry = 0; while (true) { try { long result = client.decr(key, by, 0, expiry); return ruby.newFixnum(result); } catch (OperationTimeoutException e) { if (retry == exceptionRetryLimit) { throw Error.newATimeoutOccurred(ruby, e.getLocalizedMessage()); } retry++; } } }
@Override public Map<String, Object> getBulk(final Collection<String> keys) throws TimeoutException, CacheException { try { return memcachedClient.getBulk(keys); } catch (OperationTimeoutException e) { LOGGER.warn("Operation timeout while getBulk {}", keys, e); throw (TimeoutException) e.getCause(); } catch (RuntimeException e) { if (translateException(e)) { throw new CacheException(e); } throw e; } }
@JRubyMethod(name = { "increment", "incr" }, required = 1, optional = 2) public IRubyObject incr(ThreadContext context, IRubyObject[] args) { Ruby ruby = context.getRuntime(); String key = getFullKey(args[0].toString()); int by = getIncrDecrBy(args); int expiry = getExpiry(args); int retry = 0; while (true) { try { long result = client.incr(key, by, 1, expiry); return ruby.newFixnum(result); } catch (OperationTimeoutException e) { if (retry == exceptionRetryLimit) { throw Error.newATimeoutOccurred(ruby, e.getLocalizedMessage()); } retry++; } } }
@Override public <T> T getFromReplica(String key, Transcoder<T> tc) { try { return asyncGetFromReplica(key, tc).get(operationTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new RuntimeException("Interrupted waiting for value", e); } catch (ExecutionException e) { throw new RuntimeException("Exception waiting for value", e); } catch (TimeoutException e) { throw new OperationTimeoutException("Timeout waiting for value", e); } }
@Override public long decr(final String key, final int by, final long def) throws TimeoutException, CacheException { try { return memcachedClient.decr(key, by, def); } catch (OperationTimeoutException e) { LOGGER.warn("Operation timeout while decr {}", key, e); throw new TimeoutException(e.getMessage()); } catch (RuntimeException e) { if (translateException(e)) { throw new CacheException(e); } throw e; } }
@Override public <T> Map<String, T> getBulk(final Collection<String> keys, final CacheTranscoder transcoder) throws TimeoutException, CacheException { try { return memcachedClient.getBulk(keys, this.<T> getTranscoder(transcoder)); } catch (OperationTimeoutException e) { LOGGER.warn("Operation timeout while getBulk {}", keys, e); throw (TimeoutException) e.getCause(); } catch (RuntimeException e) { if (translateException(e)) { throw new CacheException(e); } throw e; } }
@JRubyMethod public IRubyObject flush(ThreadContext context) { Ruby ruby = context.getRuntime(); try { client.flush().get(); return context.nil; } catch (OperationTimeoutException e) { throw Error.newATimeoutOccurred(ruby, e.getLocalizedMessage()); } catch (ExecutionException e) { throw ruby.newRuntimeError(e.getLocalizedMessage()); } catch (InterruptedException e) { throw ruby.newThreadError(e.getLocalizedMessage()); } }
/** * Gets (with CAS support) with a single key. * * @param key the key to get * @param tc the transcoder to serialize and unserialize value * @return the result from the cache and CAS id (null if there is none) * @throws OperationTimeoutException if global operation timeout is * exceeded * @throws IllegalStateException in the rare circumstance where queue * is too full to accept any more requests */ public <T> CASValue<T> gets(String key, Transcoder<T> tc) { try { return asyncGets(key, tc).get( operationTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new RuntimeException("Interrupted waiting for value", e); } catch (ExecutionException e) { throw new RuntimeException("Exception waiting for value", e); } catch (TimeoutException e) { throw new OperationTimeoutException("Timeout waiting for value", e); } }
@Override public long incr(final String key, final int by) throws TimeoutException, CacheException { try { return memcachedClient.incr(key, by); } catch (OperationTimeoutException e) { LOGGER.warn("Operation timeout while incr {}", key, e); throw new TimeoutException(e.getMessage()); } catch (RuntimeException e) { if (translateException(e)) { throw new CacheException(e); } throw e; } }
@Override public <T> Map<String, T> getBulk(final Collection<String> keys, final CacheTranscoder transcoder) throws TimeoutException, CacheException { try { return memcachedClient.getBulk(keys, this.<T> getTranscoder(transcoder)); } catch (OperationTimeoutException e) { LOGGER.warn("Operation timeout while getBulk {}", keys, e); throw (TimeoutException) e.getCause(); } catch (RuntimeException e) { if (translateException(e)) { throw new CacheException(e); } throw e; } }
} catch (OperationTimeoutException e) { if (retry == exceptionRetryLimit) { throw Error.newATimeoutOccurred(ruby, e.getLocalizedMessage());
/** * Get with a single key. * * @param key the key to get * @param tc the transcoder to serialize and unserialize value * @return the result from the cache (null if there is none) * @throws OperationTimeoutException if the global operation timeout is * exceeded * @throws IllegalStateException in the rare circumstance where queue * is too full to accept any more requests */ public <T> T get(String key, Transcoder<T> tc) { try { return asyncGet(key, tc).get( operationTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new RuntimeException("Interrupted waiting for value", e); } catch (ExecutionException e) { throw new RuntimeException("Exception waiting for value", e); } catch (TimeoutException e) { throw new OperationTimeoutException("Timeout waiting for value", e); } }
@Override public long decr(final String key, final int by) throws TimeoutException, CacheException { try { return memcachedClient.decr(key, by); } catch (OperationTimeoutException e) { LOGGER.warn("Operation timeout while decr {}", key, e); throw new TimeoutException(e.getMessage()); } catch (RuntimeException e) { if (translateException(e)) { throw new CacheException(e); } throw e; } }
@Override public <T> CASValue<T> getsFromReplica(String key, Transcoder<T> tc) { try { return asyncGetsFromReplica(key, tc).get(operationTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new RuntimeException("Interrupted waiting for value", e); } catch (ExecutionException e) { throw new RuntimeException("Exception waiting for value", e); } catch (TimeoutException e) { throw new OperationTimeoutException("Timeout waiting for value", e); } }
@Override public long incr(final String key, final int by, final long def, final int expiration) throws TimeoutException, CacheException { try { return memcachedClient.incr(key, by, def, expiration); } catch (OperationTimeoutException e) { LOGGER.warn("Operation timeout while incr {}", key, e); throw new TimeoutException(e.getMessage()); } catch (RuntimeException e) { if (translateException(e)) { throw new CacheException(e); } throw e; } }
/** * Gets (with CAS support) with a single key. * * @param <T> * @param key the key to get * @param tc the transcoder to serialize and unserialize value * @return the result from the cache and CAS id (null if there is none) * @throws OperationTimeoutException if global operation timeout is * exceeded * @throws IllegalStateException in the rare circumstance where queue * is too full to accept any more requests */ public <T> CASValue<T> gets(String key, Transcoder<T> tc) { try { return asyncGets(key, tc).get( operationTimeout, TimeUnit.MILLISECONDS); } catch (InterruptedException e) { throw new RuntimeException("Interrupted waiting for value", e); } catch (ExecutionException e) { throw new RuntimeException("Exception waiting for value", e); } catch (TimeoutException e) { throw new OperationTimeoutException("Timeout waiting for value", e); } }
@Override public long decr(final String key, final int by) throws TimeoutException, CacheException { try { return memcachedClient.decr(key, by); } catch (OperationTimeoutException e) { LOGGER.warn("Operation timeout while decr {}", key, e); throw new TimeoutException(e.getMessage()); } catch (RuntimeException e) { if (translateException(e)) { throw new CacheException(e); } throw e; } }