net.spy.memcached.ops.OperationStatus Java Examples

The following examples show how to use net.spy.memcached.ops.OperationStatus. 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: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 6 votes vote down vote up
private Timer getTimer(String operation, String operationType, OperationStatus status, String hit, String host, long maxDuration) {
    String name = ((status != null) ? operation + status.getMessage() : operation );
    if(hit != null) name = name + hit;

    Timer timer = timerMap.get(name);
    if(timer != null) return timer;

    final List<Tag> tagList = new ArrayList<Tag>(client.getTagList().size() + 4 + (host == null ? 0 : 1));
    tagList.addAll(client.getTagList());
    if(operation != null) tagList.add(new BasicTag(EVCacheMetricsFactory.CALL_TAG, operation));
    if(operationType != null) tagList.add(new BasicTag(EVCacheMetricsFactory.CALL_TYPE_TAG, operationType));
    if(status != null) {
        if(status.getStatusCode() == StatusCode.SUCCESS || status.getStatusCode() == StatusCode.ERR_NOT_FOUND || status.getStatusCode() == StatusCode.ERR_EXISTS) {
            tagList.add(new BasicTag(EVCacheMetricsFactory.IPC_RESULT, EVCacheMetricsFactory.SUCCESS));
        } else {
            tagList.add(new BasicTag(EVCacheMetricsFactory.IPC_RESULT, EVCacheMetricsFactory.FAIL));
        }
        tagList.add(new BasicTag(EVCacheMetricsFactory.IPC_STATUS, getStatusCode(status.getStatusCode())));
    }
    if(hit != null) tagList.add(new BasicTag(EVCacheMetricsFactory.CACHE_HIT, hit));
    if(host != null) tagList.add(new BasicTag(EVCacheMetricsFactory.FAILED_HOST, host));

    timer = EVCacheMetricsFactory.getInstance().getPercentileTimer(EVCacheMetricsFactory.IPC_CALL, tagList, Duration.ofMillis(maxDuration));
    timerMap.put(name, timer);
    return timer;
}
 
Example #3
Source File: TracingOperationCallback.java    From java-specialagent with Apache License 2.0 5 votes vote down vote up
@Override
public void receivedStatus(final OperationStatus status) {
  final Map<String,Object> event = new HashMap<>();
  event.put("status", status.getStatusCode());
  span.log(event);
  operationCallback.receivedStatus(status);
}
 
Example #4
Source File: ProtocolBaseTestFile.java    From KodeBeagle with Apache License 2.0 5 votes vote down vote up
public void testMutateWithDefaultAndExp() throws Exception {
    assertEquals(3, client.incr("mtest", 1, 3, 1));
    assertEquals(4, client.incr("mtest", 1, 3, 1));
    assertEquals(3, client.decr("mtest", 1, 9, 1));
    assertEquals(9, client.decr("mtest2", 1, 9, 1));
    Thread.sleep(2000);
    assertNull(client.get("mtest"));
    OperationStatus status = client.asyncGet("mtest").getStatus();
    assertFalse(status.isSuccess());
    assertEquals(StatusCode.ERR_NOT_FOUND, status.getStatusCode());
}
 
Example #5
Source File: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
public <T> EVCacheOperationFuture<CASValue<T>> asyncGetAndTouch(final String key, final int exp, final Transcoder<T> tc) {
    final CountDownLatch latch = new CountDownLatch(1);
    final EVCacheOperationFuture<CASValue<T>> rv = new EVCacheOperationFuture<CASValue<T>>(key, latch, new AtomicReference<CASValue<T>>(null), operationTimeout, executorService, client);
    Operation op = opFact.getAndTouch(key, exp, new GetAndTouchOperation.Callback() {
        private CASValue<T> val = null;

        public void receivedStatus(OperationStatus status) {
            if (log.isDebugEnabled()) log.debug("GetAndTouch Key : " + key + "; Status : " + status.getStatusCode().name()
                    + (log.isTraceEnabled() ?  " Node : " + getEVCacheNode(key) : "")
                    + "; Message : " + status.getMessage() + "; Elapsed Time - " + (System.currentTimeMillis() - rv.getStartTime()));
            rv.set(val, status);
        }

        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.GET_AND_TOUCH_OPERATION, EVCacheMetricsFactory.READ, rv.getStatus(), (val != null ? EVCacheMetricsFactory.YES : EVCacheMetricsFactory.NO), host, getReadMetricMaxValue()).record((System.currentTimeMillis() - rv.getStartTime()), TimeUnit.MILLISECONDS);
            rv.signalComplete();
        }

        public void gotData(String k, int flags, long cas, byte[] data) {
            if (!key.equals(k)) log.warn("Wrong key returned. Key - " + key + "; Returned Key " + k);
            if (data != null) getDataSizeDistributionSummary(EVCacheMetricsFactory.GET_AND_TOUCH_OPERATION, EVCacheMetricsFactory.READ, EVCacheMetricsFactory.IPC_SIZE_INBOUND).record(data.length);
            val = new CASValue<T>(cas, tc.decode(new CachedData(flags, data, tc.getMaxSize())));
        }
    });
    rv.setOperation(op);
    mconn.enqueueOperation(key, op);
    return rv;
}
 
Example #6
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 #7
Source File: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 5 votes vote down vote up
public EVCacheItemMetaData metaDebug(String key) {
    final CountDownLatch latch = new CountDownLatch(1);
    final EVCacheItemMetaData rv = new EVCacheItemMetaData();
    if(opFact instanceof EVCacheAsciiOperationFactory) {
    final Operation op = ((EVCacheAsciiOperationFactory)opFact).metaDebug(key, new MetaDebugOperation.Callback() {
        public void receivedStatus(OperationStatus status) {
            if (!status.isSuccess()) {
                if (log.isDebugEnabled()) log.debug("Unsuccessful stat fetch: %s", status);
              }
            if (log.isDebugEnabled()) log.debug("Getting Meta Debug: " + key + "; Status : " + status.getStatusCode().name() + (log.isTraceEnabled() ?  " Node : " + getEVCacheNode(key) : "") + "; Message : " + status.getMessage());
        }

        public void debugInfo(String k, String val) {
            if (log.isDebugEnabled()) log.debug("key " + k + "; val : " + val);
            if(k.equals("exp")) rv.setSecondsLeftToExpire(Long.parseLong(val) * -1);
            else if(k.equals("la")) rv.setSecondsSinceLastAccess(Long.parseLong(val));
            else if(k.equals("cas")) rv.setCas(Long.parseLong(val));
            else if(k.equals("fetch")) rv.setHasBeenFetchedAfterWrite(Boolean.parseBoolean(val));
            else if(k.equals("cls")) rv.setSlabClass(Integer.parseInt(val));
            else if(k.equals("size")) rv.setSizeInBytes(Integer.parseInt(val));
        }

        public void complete() {
            latch.countDown();
        }});
        mconn.enqueueOperation(key, op);
        try {
            if (!latch.await(operationTimeout, TimeUnit.MILLISECONDS)) {
                if (log.isDebugEnabled()) log.debug("meta debug operation timeout. Will return empty opbject.");
            }
        } catch (Exception e) {
            log.error("Exception on meta debug operation : Key : " + key, e);
        }
        if (log.isDebugEnabled()) log.debug("Meta Debug Data : " + rv);
    }
    return rv;
}
 
Example #8
Source File: KffMemcachedTest.java    From emissary with Apache License 2.0 4 votes vote down vote up
@Override
public OperationStatus getStatus() {
    return new OperationStatus(true, "Done");
}
 
Example #9
Source File: KffMemcachedTest.java    From emissary with Apache License 2.0 4 votes vote down vote up
@Override
public void set(Future<T> d, OperationStatus s) {
    throw new NotImplementedException("don't call set");
}
 
Example #10
Source File: CacheGrantManual.java    From oxAuth with MIT License 4 votes vote down vote up
public static void main(String[] args) throws IOException {
    final MemcachedClient client = createClients();
    final ExecutorService executorService = Executors.newFixedThreadPool(1000, daemonThreadFactory());

    int count = 10000;
    final long start = System.currentTimeMillis();

    for (int i = 0; i < count; i++) {
        final int key = i;
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                // MemcachedClient client = clients.get(random);

                Object toPut = testGrant();
                // Object toPut = UUID.randomUUID().toString();

                OperationFuture<Boolean> set = null;
                for (int j = 0; j < 3; j++) {
                    set = client.set(Integer.toString(key), 60, toPut);
                }

                OperationStatus status = set.getStatus(); // block

                System.out.println(
                        " key: " + key + ", time: " + (new Date().getTime() - start) + "ms, set: " + status);

                executorService.execute(new Runnable() {
                    @Override
                    public void run() {

                        int random = random();
                        if (random % 3 == 0) {
                            try {
                                Thread.sleep(10000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                        Object get = client.get(Integer.toString(key));
                        System.out.println("GET key: " + key + " result: " + get);
                    }
                });
            }
        });
    }

    sleep(40);
    // System.out.println(client.get("myKey"));
    //
    // client.set("myKey", 30, testState());
    //
    // sleep(12);
    // System.out.println(client.get("myKey"));
    //
    // sleep(12);
    // System.out.println(client.get("myKey"));
    client.shutdown();

}
 
Example #11
Source File: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public <T> EVCacheOperationFuture<T> asyncGet(final String key, final Transcoder<T> tc, EVCacheGetOperationListener<T> listener) {
    final CountDownLatch latch = new CountDownLatch(1);
    final EVCacheOperationFuture<T> rv = new EVCacheOperationFuture<T>(key, latch, new AtomicReference<T>(null), readTimeout.get().intValue(), executorService, client);
    final Operation op = opFact.get(key, new GetOperation.Callback() {
        private Future<T> val = null;

        public void receivedStatus(OperationStatus status) {
            if (log.isDebugEnabled()) log.debug("Getting Key : " + key + "; Status : " + status.getStatusCode().name() + (log.isTraceEnabled() ?  " Node : " + getEVCacheNode(key) : "")
                    + "; Message : " + status.getMessage() + "; Elapsed Time - " + (System.currentTimeMillis() - rv.getStartTime()));
            try {
                if (val != null) {
                    if (log.isTraceEnabled() && client.getPool().getEVCacheClientPoolManager().shouldLog(appName)) log.trace("Key : " + key + "; val : " + val.get());
                    rv.set(val.get(), status);
                } else {
                    if (log.isTraceEnabled() && client.getPool().getEVCacheClientPoolManager().shouldLog(appName)) log.trace("Key : " + key + "; val is null");
                    rv.set(null, status);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                rv.set(null, status);
            }
        }

        @SuppressWarnings("unchecked")
        public void gotData(String k, int flags, byte[] data) {

            if (!key.equals(k)) {
                log.error("Wrong key returned. Key - " + key + "; Returned Key " + k);
                return;
            }
            if (log.isDebugEnabled() && client.getPool().getEVCacheClientPoolManager().shouldLog(appName)) log.debug("Read data : key " + key + "; flags : " + flags + "; data : " + data);
            if (data != null)  {
                if (log.isDebugEnabled() && client.getPool().getEVCacheClientPoolManager().shouldLog(appName)) log.debug("Key : " + key + "; val size : " + data.length);
                getDataSizeDistributionSummary(EVCacheMetricsFactory.GET_OPERATION, EVCacheMetricsFactory.READ, EVCacheMetricsFactory.IPC_SIZE_INBOUND).record(data.length);
                if (tc == null) {
                    if (tcService == null) {
                        log.error("tcService is null, will not be able to decode");
                        throw new RuntimeException("TranscoderSevice is null. Not able to decode");
                    } else {
                        final Transcoder<T> t = (Transcoder<T>) getTranscoder();
                        val = tcService.decode(t, new CachedData(flags, data, t.getMaxSize()));
                    }
                } else {
                    if (tcService == null) {
                        log.error("tcService is null, will not be able to decode");
                        throw new RuntimeException("TranscoderSevice is null. Not able to decode");
                    } else {
                        val = tcService.decode(tc, new CachedData(flags, data, tc.getMaxSize()));
                    }
                }
            } else {
                if (log.isDebugEnabled() && client.getPool().getEVCacheClientPoolManager().shouldLog(appName)) log.debug("Key : " + key + "; val is null" );
            }
        }

        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.GET_OPERATION, EVCacheMetricsFactory.READ, rv.getStatus(), (val != null ? EVCacheMetricsFactory.YES : EVCacheMetricsFactory.NO), host, getReadMetricMaxValue()).record((System.currentTimeMillis() - rv.getStartTime()), TimeUnit.MILLISECONDS);
            rv.signalComplete();
        }
    });
    rv.setOperation(op);
    if (listener != null) rv.addListener(listener);
    mconn.enqueueOperation(key, op);
    return rv;
}
 
Example #12
Source File: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public long mutate(final Mutator m, String key, long by, long def, int exp) {
    final String operationStr = m.name();
    final long start = System.currentTimeMillis();
    final AtomicLong rv = new AtomicLong();
    final CountDownLatch latch = new CountDownLatch(1);
    final List<OperationStatus> statusList = new ArrayList<OperationStatus>(1);
    final Operation op = opFact.mutate(m, key, by, def, exp, new OperationCallback() {
        @Override
        public void receivedStatus(OperationStatus s) {
            statusList.add(s);
            rv.set(new Long(s.isSuccess() ? s.getMessage() : "-1"));
        }

        @Override
        public void complete() {
            latch.countDown();
        }
    });
    mconn.enqueueOperation(key, op);
    long retVal = def;
    try {
        if(mutateOperationTimeout == null) {
            mutateOperationTimeout = EVCacheConfig.getInstance().getPropertyRepository().get(appName + ".mutate.timeout", Long.class).orElse(connectionFactory.getOperationTimeout());
        }


        if (!latch.await(mutateOperationTimeout.get(), TimeUnit.MILLISECONDS)) {
            if (log.isDebugEnabled()) log.debug("Mutation operation timeout. Will return -1");
            retVal = -1;
        } else {
            retVal = rv.get();
        }

    } catch (Exception e) {
        log.error("Exception on mutate operation : " + operationStr + " Key : " + key + "; by : " + by + "; default : " + def + "; exp : " + exp
                + "; val : " + retVal + "; Elapsed Time - " + (System.currentTimeMillis() - start), e);

    }
    final OperationStatus status = statusList.size() > 0 ? statusList.get(0) : null;
    final String host = ((status != null && status.getStatusCode().equals(StatusCode.TIMEDOUT) && op != null) ? getHostName(op.getHandlingNode().getSocketAddress()) : null);
    getTimer(operationStr, EVCacheMetricsFactory.WRITE, status, null, host, getWriteMetricMaxValue()).record((System.currentTimeMillis() - start), TimeUnit.MILLISECONDS);
    if (log.isDebugEnabled() && client.getPool().getEVCacheClientPoolManager().shouldLog(appName)) log.debug(operationStr + " Key : " + key + "; by : " + by + "; default : " + def + "; exp : " + exp
            + "; val : " + retVal + "; Elapsed Time - " + (System.currentTimeMillis() - start));
    return retVal;
}
 
Example #13
Source File: EVCacheMemcachedClient.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public Map<SocketAddress, String> execCmd(final String cmd, String[] ips) {
    final Map<SocketAddress, String> rv = new HashMap<SocketAddress, String>();
    Collection<MemcachedNode> nodes = null;
    if(ips == null || ips.length == 0) {
        nodes = mconn.getLocator().getAll();
    } else {
        nodes = new ArrayList<MemcachedNode>(ips.length);
        for(String ip : ips) {
            for(MemcachedNode node : mconn.getLocator().getAll()) {
                if(((InetSocketAddress)node.getSocketAddress()).getAddress().getHostAddress().equals(ip)) {
                    nodes.add(node);
                }
            }
        }
    }
    if(nodes != null && !nodes.isEmpty()) {
        CountDownLatch blatch = broadcastOp(new BroadcastOpFactory() {
            @Override
            public Operation newOp(final MemcachedNode n, final CountDownLatch latch) {
                final SocketAddress sa = n.getSocketAddress();
                return ((EVCacheAsciiOperationFactory)opFact).execCmd(cmd, new ExecCmdOperation.Callback() {

                    @Override
                    public void receivedStatus(OperationStatus status) {
                        if (log.isDebugEnabled()) log.debug("cmd : " + cmd + "; MemcachedNode : " + n + "; Status : " + status);
                        rv.put(sa, status.getMessage());
                    }

                    @Override
                    public void complete() {
                        latch.countDown();
                    }
                });
            }
        }, nodes);
        try {
            blatch.await(operationTimeout, TimeUnit.MILLISECONDS);
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted waiting for stats", e);
        }
    }
    return rv;
}