net.spy.memcached.internal.OperationFuture Java Examples

The following examples show how to use net.spy.memcached.internal.OperationFuture. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 6 votes vote down vote up
public <T> OperationFuture<Boolean> touch(final String key, final int exp, EVCacheLatch evcacheLatch) {
    final CountDownLatch latch = new CountDownLatch(1);
    final EVCacheOperationFuture<Boolean> rv = new EVCacheOperationFuture<Boolean>(key, latch, new AtomicReference<Boolean>(null), operationTimeout, executorService, client);
    final Operation op = opFact.touch(key, exp, new OperationCallback() {
        @Override
        public void receivedStatus(OperationStatus status) {
            rv.set(status.isSuccess(), status);
        }

        @Override
        public void complete() {
            latch.countDown();
            final String host = ((rv.getStatus().getStatusCode().equals(StatusCode.TIMEDOUT) && rv.getOperation() != null) ? getHostName(rv.getOperation().getHandlingNode().getSocketAddress()) : null);
            getTimer(EVCacheMetricsFactory.TOUCH_OPERATION, EVCacheMetricsFactory.WRITE, rv.getStatus(), null, host, getWriteMetricMaxValue()).record((System.currentTimeMillis() - rv.getStartTime()), TimeUnit.MILLISECONDS);
            rv.signalComplete();
        }
    });
    rv.setOperation(op);
    if (evcacheLatch != null && evcacheLatch instanceof EVCacheLatchImpl && !client.isInWriteOnly()) ((EVCacheLatchImpl) evcacheLatch).addFuture(rv);
    mconn.enqueueOperation(key, op);
    return rv;
}
 
Example #2
Source File: EVCacheFutures.java    From EVCache with Apache License 2.0 6 votes vote down vote up
public EVCacheFutures(OperationFuture<Boolean>[] futures, String key, String app, ServerGroup serverGroup, EVCacheLatch latch) {
    this.futures = futures;
    this.app = app;
    this.serverGroup = serverGroup;
    this.key = key;
    this.latch = latch;
    this.completionCounter = new AtomicInteger(futures.length);
    if (latch != null && latch instanceof EVCacheLatchImpl) ((EVCacheLatchImpl) latch).addFuture(this);
    for (int i = 0; i < futures.length; i++) {
        final OperationFuture<Boolean> of = futures[i];
        if (of.isDone()) {
            try {
                onComplete(of);
            } catch (Exception e) {
            }
        } else {
            of.addListener(this);
        }
    }
}
 
Example #3
Source File: AbstractCouchBaseOutputOperator.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
public void setKeyValueInCouchBase(T tuple)
{
  id++;
  String key = getKey(tuple);
  Object value = getValue(tuple);
  if (!(value instanceof Boolean) && !(value instanceof Integer) && !(value instanceof String) && !(value instanceof Float) && !(value instanceof Double) && !(value instanceof Character) && !(value instanceof Long) && !(value instanceof Short) && !(value instanceof Byte)) {
    if (serializer != null) {
      value = serializer.serialize(value);
    }
  }
  OperationFuture<Boolean> future = processKeyValue(key, value);
  synchronized (syncObj) {
    future.addListener(listener);
    mapFuture.put(future, id);
    if (!mapTuples.containsKey(id)) {
      mapTuples.put(id, tuple);
    }
    numTuples++;
  }

}
 
Example #4
Source File: AbstractCouchBaseOutputOperator.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@Override
public void onComplete(OperationFuture<?> f) throws Exception
{
  if (!((Boolean)f.get())) {
    logger.error("Operation failed {}", f);
    failure = true;
    return;
  }
  synchronized (syncObj) {
    long idProcessed = mapFuture.get(f);
    mapTuples.remove(idProcessed);
    mapFuture.remove(f);
    numTuples--;
    syncObj.notify();
  }
}
 
Example #5
Source File: MemcachedCache.java    From lsmtree with Apache License 2.0 5 votes vote down vote up
public boolean checkAvailability(String key) {
    long time = System.nanoTime();
    key += "-"+UUID.randomUUID().toString();
    OperationFuture<Boolean> future = memcache.set(key, CACHE_EXPIRY_SECONDS, Longs.toByteArray(time), identityTranscoder);
    try {
        if (!future.get()) return false;
    } catch (Exception e) {
        return false;
    }
    byte[] bytes = memcache.get(key, identityTranscoder);
    memcache.delete(key);
    return bytes != null && Longs.fromByteArray(bytes) == time;
}
 
Example #6
Source File: EVCacheFutures.java    From EVCache with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    for (OperationFuture<Boolean> future : futures) {
        if (future.get(timeout, unit) == false) return false;
    }
    return true;

}
 
Example #7
Source File: EVCacheFutures.java    From EVCache with Apache License 2.0 5 votes vote down vote up
@Override
public Boolean get() throws InterruptedException, ExecutionException {
    for (OperationFuture<Boolean> future : futures) {
        if (future.get() == false) return false;
    }
    return true;
}
 
Example #8
Source File: EVCacheFutures.java    From EVCache with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isDone() {
    for (OperationFuture<Boolean> future : futures) {
        if (future.isDone() == false) return false;
    }
    return true;
}
 
Example #9
Source File: EVCacheFutures.java    From EVCache with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isCancelled() {

    for (OperationFuture<Boolean> future : futures) {
        if (future.isCancelled() == false) return false;
    }
    return true;
}
 
Example #10
Source File: EVCacheFutures.java    From EVCache with Apache License 2.0 5 votes vote down vote up
public boolean cancel(boolean mayInterruptIfRunning) {
    if(log.isDebugEnabled()) log.debug("Operation cancelled", new Exception());
    for (OperationFuture<Boolean> future : futures) {
        future.cancel();
    }
    return true;
}
 
Example #11
Source File: EVCacheFutures.java    From EVCache with Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append("EVCacheFutures [futures=[");
    for (OperationFuture<Boolean> future : futures)
        sb.append(future);
    sb.append("], app=").append(app).append(", ServerGroup=").append(serverGroup.toString()).append("]");
    return sb.toString();
}
 
Example #12
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
public Future<Boolean> delete(String key, EVCacheLatch latch) throws Exception {
    final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
    if (!ensureWriteQueueSize(node, key, Call.DELETE)) {
        final ListenableFuture<Boolean, OperationCompletionListener> defaultFuture = (ListenableFuture<Boolean, OperationCompletionListener>) getDefaultFuture();
        if (latch != null && latch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) latch).addFuture(defaultFuture);
        return defaultFuture;
    }

    if (enableChunking.get()) {
        final ChunkDetails<?> cd = getChunkDetails(key);
        if (cd == null) {
         // Paranoid delete : cases where get fails and we ensure the first key is deleted just in case
            return evcacheMemcachedClient.delete(key + "_00", latch);
        }
        if (!cd.isChunked()) {
            return evcacheMemcachedClient.delete(key, latch);
        } else {
            final List<String> keys = cd.getChunkKeys();
            OperationFuture<Boolean>[] futures = new OperationFuture[keys.size() + 1];
            futures[0] = evcacheMemcachedClient.delete(key + "_00");
            for (int i = 0; i < keys.size(); i++) {
                futures[i + 1] = evcacheMemcachedClient.delete(keys.get(i), null);
            }
            return new EVCacheFutures(futures, key, appName, serverGroup, latch);
        }
    } else if(shouldHashKey()) {
        final String hKey = getHashedKey(key);
        return evcacheMemcachedClient.delete(hKey, latch);
    } else {
        return evcacheMemcachedClient.delete(key, latch);
    }
}
 
Example #13
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
public <T> Future<Boolean> touch(String key, int timeToLive, EVCacheLatch latch) throws Exception {
	if(ignoreTouch.get()) {
		final ListenableFuture<Boolean, OperationCompletionListener> sf = new SuccessFuture();
		if (latch != null && latch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) latch).addFuture(sf);
		return sf;
	}
    final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
    if (!ensureWriteQueueSize(node, key, Call.TOUCH)) {
        final ListenableFuture<Boolean, OperationCompletionListener> defaultFuture = (ListenableFuture<Boolean, OperationCompletionListener>) getDefaultFuture();
        if (latch != null && latch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) latch).addFuture(defaultFuture);
        return defaultFuture;
    }

    if (enableChunking.get()) {
        final ChunkDetails<?> cd = getChunkDetails(key);
        if (cd.isChunked()) {
            final List<String> keys = cd.getChunkKeys();
            OperationFuture<Boolean>[] futures = new OperationFuture[keys.size() + 1];
            futures[0] = evcacheMemcachedClient.touch(key + "_00", timeToLive, latch);
            for (int i = 0; i < keys.size(); i++) {
                final String prefix = (i < 10) ? "0" : "";
                final String _key = key + "_" + prefix + i;
                futures[i + 1] = evcacheMemcachedClient.touch(_key, timeToLive, latch);
            }
            return new EVCacheFutures(futures, key, appName, serverGroup, latch);
        } else {
            return evcacheMemcachedClient.touch(key, timeToLive, latch);
        }
    } else if(shouldHashKey()) {
        final String hKey = getHashedKey(key);
        return evcacheMemcachedClient.touch(hKey, timeToLive, latch);
    } else {
        return evcacheMemcachedClient.touch(key, timeToLive, latch);
    }
}
 
Example #14
Source File: EVCacheClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
private Future<Boolean> _replace(String key, CachedData value, int timeToLive, EVCacheLatch evcacheLatch) throws Exception {
    final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key);
    if (!ensureWriteQueueSize(node, key, Call.REPLACE)) {
        if (log.isInfoEnabled()) log.info("Node : " + node + " is not active. Failing fast and dropping the replace event.");
        final ListenableFuture<Boolean, OperationCompletionListener> defaultFuture = (ListenableFuture<Boolean, OperationCompletionListener>) getDefaultFuture();
        if (evcacheLatch != null && evcacheLatch instanceof EVCacheLatchImpl && !isInWriteOnly()) ((EVCacheLatchImpl) evcacheLatch).addFuture(defaultFuture);
        return defaultFuture;
    }

    try {
        final int dataSize = ((CachedData) value).getData().length;
        if (enableChunking.get() && dataSize > chunkSize.get()) {
            final CachedData[] cd = createChunks(value, key);
            final int len = cd.length;
            final OperationFuture<Boolean>[] futures = new OperationFuture[len];
            for (int i = 0; i < cd.length; i++) {
                final String prefix = (i < 10) ? "0" : "";
                futures[i] = evcacheMemcachedClient.replace(key + "_" + prefix + i, timeToLive, cd[i], null, null);
            }
            return new EVCacheFutures(futures, key, appName, serverGroup, evcacheLatch);
        } else if(shouldHashKey()) {
            final String hKey = getHashedKey(key);
            final CachedData cVal = getEVCacheValue(key, value, timeToLive);
            return evcacheMemcachedClient.replace(hKey, timeToLive, cVal, null, evcacheLatch);
        } else {
            return evcacheMemcachedClient.replace(key, timeToLive, value, null, evcacheLatch);
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw e;
    }
}
 
Example #15
Source File: EVCacheFutures.java    From EVCache with Apache License 2.0 5 votes vote down vote up
@Override
public void onComplete(OperationFuture<?> future) throws Exception {
    int val = completionCounter.decrementAndGet();
    if (val == 0) {
        if (latch != null) latch.onComplete(future);// Pass the last future to get completed
    }
}
 
Example #16
Source File: ArcusCache.java    From arcus-spring with Apache License 2.0 5 votes vote down vote up
@Override
public void clear() {
  try {
    String prefixName = (prefix != null) ? prefix : name;
    if (logger.isDebugEnabled()) {
      logger.debug("evicting every key that uses the name: {}",
              prefixName);
    }

    OperationFuture<Boolean> future = arcusClient.flush(serviceId
            + prefixName);

    boolean success = future.get(timeoutMilliSeconds,
            TimeUnit.MILLISECONDS);

    if (logger.isDebugEnabled() && !success) {
      logger.debug(
              "failed to evicting every key that uses the name: {}",
              prefixName);
    }
  } catch (Exception e) {
    logger.info(e.getMessage());
    if (wantToGetException) {
      throw new RuntimeException(e);
    }
  }
}
 
Example #17
Source File: MemcachedCache.java    From lsmtree with Apache License 2.0 5 votes vote down vote up
public void putInCache(K key, V value, boolean addOnly, int expirationTime) {
    final String memcacheKey = prefix + keyStringifier.toString(key);
    try {
        if (addOnly) {
            final OperationFuture<Boolean> add = memcache.add(memcacheKey, expirationTime < 0 ? CACHE_EXPIRY_SECONDS : expirationTime, value, valueTranscoder);
            addFutureQueue.put(add);
        } else {
            final OperationFuture<Boolean> set = memcache.set(memcacheKey, expirationTime < 0 ? Integer.MAX_VALUE : expirationTime, value, valueTranscoder);
            setFutureQueue.put(set);
        }
    } catch (InterruptedException e) {
        log.error("interrupted while queueing", e);
    }
}
 
Example #18
Source File: CouchBaseSetTest.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
@Override
public void onComplete(OperationFuture<?> f) throws Exception
{
  if (!((Boolean)f.get())) {
    logger.error("Operation failed " + f);
  }
  logger.info("Operation completed");
}
 
Example #19
Source File: AbstractCouchBaseOutputOperator.java    From attic-apex-malhar with Apache License 2.0 5 votes vote down vote up
public AbstractCouchBaseOutputOperator()
{
  mapFuture = new HashMap<OperationFuture<Boolean>, Long>();
  mapTuples = new TreeMap<Long, T>();
  store = new CouchBaseWindowStore();
  listener = new CompletionListener();
  numTuples = 0;
  syncObj = new Object();
}
 
Example #20
Source File: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
public OperationFuture<Boolean> delete(String key, EVCacheLatch evcacheLatch) {
    final CountDownLatch latch = new CountDownLatch(1);
    final EVCacheOperationFuture<Boolean> rv = new EVCacheOperationFuture<Boolean>(key, latch, new AtomicReference<Boolean>(null), operationTimeout, executorService, client);
    final DeleteOperation op = opFact.delete(key, new DeleteOperation.Callback() {
        @Override
        public void receivedStatus(OperationStatus status) {
            rv.set(Boolean.TRUE, status);
        }

        @Override
        public void gotData(long cas) {
            rv.setCas(cas);
        }

        @Override
        public void complete() {
            latch.countDown();
            final String host = ((rv.getStatus().getStatusCode().equals(StatusCode.TIMEDOUT) && rv.getOperation() != null) ? getHostName(rv.getOperation().getHandlingNode().getSocketAddress()) : null);
            getTimer(EVCacheMetricsFactory.DELETE_OPERATION, EVCacheMetricsFactory.WRITE, rv.getStatus(), null, host, getWriteMetricMaxValue()).record((System.currentTimeMillis() - rv.getStartTime()), TimeUnit.MILLISECONDS);
            rv.signalComplete();
        }
    });

    rv.setOperation(op);
    if (evcacheLatch != null && evcacheLatch instanceof EVCacheLatchImpl && !client.isInWriteOnly()) ((EVCacheLatchImpl) evcacheLatch).addFuture(rv);
    mconn.enqueueOperation(key, op);
    return rv;
}
 
Example #21
Source File: ExceptionSwallowingMemcachedClient.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
public OperationFuture<Boolean> set(String key, int expireSeconds, Object obj) {
    try {
        return memcachedClient.set(hashKey(key), expireSeconds, obj);
    } catch (Exception ex) {
        logger.error("Memcache put expire exeption ", ex);
        return null;
    }
}
 
Example #22
Source File: ProtocolBaseTestFile.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
public void testSetWithCallback() throws Exception {
    OperationFuture<Boolean> setOp =
            client.set("setWithCallback", 0, "content");

    final CountDownLatch latch = new CountDownLatch(1);
    setOp.addListener(new OperationCompletionListener() {
        @Override
        public void onComplete(OperationFuture<?> f) throws Exception {
            latch.countDown();
        }
    });

    assertTrue(latch.await(2, TimeUnit.SECONDS));
}
 
Example #23
Source File: ProtocolBaseTestFile.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
public void testSetReturnsCAS() throws Exception {

        OperationFuture<Boolean> setOp = client.set("testSetReturnsCAS",
                0, "testSetReturnsCAS");
        setOp.get();
        assertTrue(setOp.getCas() > 0);
    }
 
Example #24
Source File: ProtocolBaseTestFile.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
public void testAsyncDecrement() throws Exception {
    String k = "async-decr";
    client.set(k, 0, "5");
    OperationFuture<Long> f = client.asyncDecr(k, 1);
    assertEquals(StatusCode.SUCCESS, f.getStatus().getStatusCode());
    assertEquals(4, (long) f.get());
}
 
Example #25
Source File: TransientElasticacheDataConnectionTest.java    From micro-server with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    memcachedClient = mock(MemcachedClient.class);

    stub(memcachedClient.get("key1")).toReturn("value1");
    stub(memcachedClient.get("key2")).toReturn("value2");
    OperationFuture<Boolean> mockedFuture = mock(OperationFuture.class);
    stub(memcachedClient.add("keyAdd", 3600, "valueadd")).toReturn(mockedFuture);
}
 
Example #26
Source File: ProtocolBaseTestFile.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
public void testAsyncIncrement() throws Exception {
    String k = "async-incr";
    client.set(k, 0, "5");
    OperationFuture<Long> f = client.asyncIncr(k, 1);
    assertEquals(StatusCode.SUCCESS, f.getStatus().getStatusCode());
    assertEquals(6, (long) f.get());
}
 
Example #27
Source File: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> OperationFuture<Boolean> add(String key, int exp, T o, final Transcoder<T> tc, EVCacheLatch latch) {
    Transcoder<T> t = (Transcoder<T>) ((tc == null) ? transcoder : tc);
    return asyncStore(StoreType.add, key, exp, o, t, latch);
}
 
Example #28
Source File: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public <T> OperationFuture<Boolean> set(String key, int exp, T o, final Transcoder<T> tc, EVCacheLatch latch) {
    Transcoder<T> t = (Transcoder<T>) ((tc == null) ? transcoder : tc);
    return asyncStore(StoreType.set, key, exp, o, t, latch);
}
 
Example #29
Source File: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public OperationFuture<Boolean> set(String key, int exp, Object o) {
    return asyncStore(StoreType.set, key, exp, o, transcoder, null);
}
 
Example #30
Source File: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public <T> OperationFuture<Boolean> set(String key, int exp, T o, final Transcoder<T> tc) {
    return asyncStore(StoreType.set, key, exp, o, tc, null);
}