@Override public Long getValue() { return getValue(0); }
public boolean isAvailable() { return isActive(); }
@Override public CountDownLatch broadcastOperation(BroadcastOpFactory of, Collection<MemcachedNode> nodes) { for (MemcachedNode node : nodes) { ((EVCacheNodeImpl) node).incrOps(); } return super.broadcastOperation(of, nodes); }
public void shutdown() { removeMonitoring(); writeQ.clear(); readQ.clear(); inputQueue.clear(); try { // Cleanup the ByteBuffers only if they are sun.nio.ch.DirectBuffer // If we don't cleanup then we will leak 16K of memory if (getRbuf() instanceof DirectBuffer) { Cleaner cleaner = ((DirectBuffer) getRbuf()).cleaner(); if (cleaner != null) cleaner.clean(); cleaner = ((DirectBuffer) getWbuf()).cleaner(); if (cleaner != null) cleaner.clean(); } } catch (Throwable t) { getLogger().error("Exception cleaning ByteBuffer.", t); } }
public void reconnectNode(EVCacheNodeImpl evcNode ) { final long upTime = System.currentTimeMillis() - evcNode.getCreateTime(); if (log.isDebugEnabled()) log.debug("Reconnecting node : " + evcNode + "; UpTime : " + upTime); if(upTime > 30000) { //not more than once every 30 seconds : TODO make this configurable EVCacheMetricsFactory.getCounter(appName + "-RECONNECT", evcNode.getBaseTags()).increment(); evcNode.setConnectTime(System.currentTimeMillis()); mconn.queueReconnect(evcNode); } } private final String BULK_OPERATION_STRING = "BulkOperation";
private boolean ensureWriteQueueSize(MemcachedNode node, String key) throws EVCacheException { if (node instanceof EVCacheNodeImpl) { final EVCacheNodeImpl evcNode = (EVCacheNodeImpl) node; int i = 0; while (true) { final int size = evcNode.getWriteQueueSize(); final boolean canAddToOpQueue = size < maxWriteQueueSize; if (log.isDebugEnabled()) log.debug("App : " + appName + "; zone : " + zone + "; key : " + key + "; WriteQSize : " + size); if (canAddToOpQueue) break; EVCacheMetricsFactory.getCounter("EVCacheClient-" + appName + "-WRITE_BLOCK", evcNode.getBaseTags()).increment(); try { Thread.sleep(writeBlock.get()); } catch (InterruptedException e) { throw new EVCacheException("Thread was Interrupted", e); } if(i++ > 3) { EVCacheMetricsFactory.getCounter("EVCacheClient-" + appName + "-INACTIVE_NODE", evcNode.getBaseTags()).increment(); if (log.isDebugEnabled()) log.debug("Node : " + evcNode + " for app : " + appName + "; zone : " + zone + " is not active. Will Fail Fast and the write will be dropped for key : " + key); evcNode.shutdown(); return false; } } } return true; }
private boolean validateNode(String key, boolean _throwException) throws EVCacheException, EVCacheConnectException { final MemcachedNode node = evcacheMemcachedClient.getEVCacheNode(key); // First check if the node is active if (node instanceof EVCacheNodeImpl) { final EVCacheNodeImpl evcNode = (EVCacheNodeImpl) node; if (!evcNode.isAvailable()) { EVCacheMetricsFactory.getCounter("EVCacheClient-" + appName + "-INACTIVE_NODE", evcNode.getBaseTags()).increment(); if (log.isDebugEnabled()) log.debug("Node : " + node + " for app : " + appName + "; zone : " + zone + " is not active. Will Fail Fast so that we can fallback to Other Zone if available."); if (_throwException) throw new EVCacheConnectException("Connection for Node : " + node + " for app : " + appName + "; zone : " + zone + " is not active"); return false; } final int size = evcNode.getReadQueueSize(); final boolean canAddToOpQueue = size < maxReadQueueSize.get(); if (log.isDebugEnabled()) log.debug("Current Read Queue Size - " + size + " for app " + appName + " & zone " + zone + " and node : " + evcNode); if (!canAddToOpQueue) { EVCacheMetricsFactory.getCounter(appName + "-READ_QUEUE_FULL", evcNode.getBaseTags()).increment(); if (log.isDebugEnabled()) log.debug("Read Queue Full for Node : " + node + "; app : " + appName + "; zone : " + zone + "; Current Size : " + size + "; Max Size : " + maxReadQueueSize.get()); if (_throwException) throw new EVCacheReadQueueException("Read Queue Full for Node : " + node + "; app : " + appName + "; zone : " + zone + "; Current Size : " + size + "; Max Size : " + maxReadQueueSize.get()); return false; } } return true; }
public MemcachedNode createMemcachedNode(SocketAddress sa, SocketChannel c, int bufSize) { boolean doAuth = false; final EVCacheNodeImpl node = new EVCacheNodeImpl(sa, c, bufSize, createReadOperationQueue(), createWriteOperationQueue(), createOperationQueue(), opMaxBlockTime, doAuth, getOperationTimeout(), getAuthWaitTime(), this, appName, id, serverGroup, startTime); node.registerMonitors(); return node; }
@Override public void receivedStatus(OperationStatus s) { rv.set(new Long(s.isSuccess() ? s.getMessage() : "-1")); final MemcachedNode node = getEVCacheNode(key); if(node instanceof EVCacheNodeImpl) { getCounter("MutateOperation-"+ s.getStatusCode().name(), ((EVCacheNodeImpl)node).getBaseTags()).increment(); } else { getCounter("MutateOperation-"+ s.getStatusCode().name(), BasicTagList.of("HOST", node.getSocketAddress().toString())).increment(); } }
@Override public String getSocketChannelLocalAddress() { try { if(getChannel() != null) { return getChannel().getLocalAddress().toString(); } } catch (IOException e) { log.error("Exception", e); } return "NULL"; }
public void removeMonitoring() { try { final ObjectName mBeanName = ObjectName.getInstance(getMonitorName()); final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); if (mbeanServer.isRegistered(mBeanName)) { if (log.isDebugEnabled()) log.debug("MBEAN with name " + mBeanName + " has been registered. Will unregister the previous instance and register a new one."); mbeanServer.unregisterMBean(mBeanName); } } catch (Exception e) { if (log.isDebugEnabled()) log.debug("Exception while setting up the monitoring.", e); } }
public int getWriteQueueLength() { final Collection<MemcachedNode> allNodes = evcacheMemcachedClient.getNodeLocator().getAll(); int size = 0; for(MemcachedNode node : allNodes) { if(node instanceof EVCacheNodeImpl) { size += ((EVCacheNodeImpl)node).getWriteQueueSize(); } } return size; }
public int getReadQueueLength() { final Collection<MemcachedNode> allNodes = evcacheMemcachedClient.getNodeLocator().getAll(); int size = 0; for(MemcachedNode node : allNodes) { if(node instanceof EVCacheNodeImpl) { size += ((EVCacheNodeImpl)node).getReadQueueSize(); } } return size; }
@Override public void shutdown() throws IOException { try { super.shutdown(); for (MemcachedNode qa : getLocator().getAll()) { if (qa instanceof EVCacheNodeImpl) { ((EVCacheNodeImpl) qa).shutdown(); } } } finally { if(running) { running = false; if(log.isWarnEnabled()) log.warn("Forceful shutdown by interrupting the thread.", new Exception()); interrupt(); } } }
protected void addOperation(final MemcachedNode node, final Operation o) { super.addOperation(node, o); ((EVCacheNodeImpl) node).incrOps(); }
@Override public void receivedStatus(OperationStatus status) { operationDuration.stop(); rv.set(Boolean.TRUE, status); if (status.getStatusCode().equals(StatusCode.SUCCESS)) { getCounter(DELETE_OPERATION_SUCCESS_STRING).increment(); } else { if (status.getStatusCode().equals(StatusCode.TIMEDOUT)) { final MemcachedNode node = getEVCacheNode(key); if(node instanceof EVCacheNodeImpl) { getCounter("DeleteOperation-"+ status.getStatusCode().name(), ((EVCacheNodeImpl)node).getBaseTags()).increment(); } else { getCounter("DeleteOperation-"+ status.getStatusCode().name(), BasicTagList.of("HOST", node.getSocketAddress().toString())).increment(); } } else { getCounter("DeleteOperation-"+ status.getStatusCode().name()).increment(); } } }
@Override public String getSocketChannelRemoteAddress() { try { if(getChannel() != null) { return getChannel().getRemoteAddress().toString(); } } catch (IOException e) { log.error("Exception", e); } return "NULL"; }
private void setupMonitoring() { try { final ObjectName mBeanName = ObjectName.getInstance(getMonitorName()); final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); if (mbeanServer.isRegistered(mBeanName)) { if (log.isDebugEnabled()) log.debug("MBEAN with name " + mBeanName + " has been registered. Will unregister the previous instance and register a new one."); mbeanServer.unregisterMBean(mBeanName); } mbeanServer.registerMBean(this, mBeanName); } catch (Exception e) { if (log.isWarnEnabled()) log.warn("Exception while setting up monitoring.", e); } }
@Override public void addOperations(Map<MemcachedNode, Operation> ops) { super.addOperations(ops); for (MemcachedNode node : ops.keySet()) { ((EVCacheNodeImpl) node).incrOps(); } }
@Override public void receivedStatus(OperationStatus status) { operationDuration.stop(); rv.set(status.isSuccess(), status); if (status.getStatusCode().equals(StatusCode.SUCCESS)) { getCounter(TOUCH_OPERATION_STRING + "-SUCCESS").increment(); } else { if (status.getStatusCode().equals(StatusCode.TIMEDOUT)) { final MemcachedNode node = getEVCacheNode(key); if(node instanceof EVCacheNodeImpl) { getCounter(TOUCH_OPERATION_STRING + "-" + status.getStatusCode().name(), ((EVCacheNodeImpl)node).getBaseTags()).increment(); } else { getCounter(TOUCH_OPERATION_STRING + "-"+ status.getStatusCode().name(), BasicTagList.of("HOST", node.getSocketAddress().toString())).increment(); } } else { getCounter(TOUCH_OPERATION_STRING + "-"+ status.getStatusCode().name()).increment(); } } }