/** Broadcast an operation to all nodes. */ public CountDownLatch broadcastOperation(final BroadcastOpFactory of) { final CountDownLatch latch = new CountDownLatch(locator.getAll().size()); for (MemcachedNode node : locator.getAll()) { Operation op = of.newOp(node, latch); op.initialize(); node.addOp(op); addedQueue.offer(node); } Selector s = selector.wakeup(); assert s == selector : "Wakeup returned the wrong selector."; return latch; }
private boolean selectorsMakeSense() { for (MemcachedNode qa : locator.getAll()) { if (qa.getSk() != null && qa.getSk().isValid()) { if (qa.getChannel().isConnected()) { int sops = qa.getSk().interestOps(); int expected = 0; if (qa.hasReadOp()) { expected |= SelectionKey.OP_READ; } if (qa.hasWriteOp()) { expected |= SelectionKey.OP_WRITE; } if (qa.getBytesRemainingToWrite() > 0) { expected |= SelectionKey.OP_WRITE; } assert sops == expected : "Invalid ops: " + qa + ", expected " + expected + ", got " + sops; } else { int sops = qa.getSk().interestOps(); assert sops == SelectionKey.OP_CONNECT : "Not connected, and not watching for connect: " + sops; } } } getLogger().debug("Checked the selectors."); return true; }
@Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append("{MemcachedConnection to"); for (MemcachedNode qa : locator.getAll()) { sb.append(" "); sb.append(qa.getSocketAddress()); } sb.append("}"); return sb.toString(); }
/** * Add an operation to the given connection. * * @param which the connection offset * @param o the operation */ public void addOperation(final String key, final Operation o) { MemcachedNode placeIn = null; MemcachedNode primary = locator.getPrimary(key); if (primary.isActive()) { placeIn = primary; } else { // Look for another node in sequence that is ready. for (Iterator<MemcachedNode> i = locator.getSequence(key); placeIn == null && i.hasNext(); ) { MemcachedNode n = i.next(); if (n.isActive()) { placeIn = n; } } // If we didn't find an active node, queue it in the primary node // and wait for it to come back online. if (placeIn == null) { placeIn = primary; } } assert placeIn != null : "No node found for key " + key; addOperation(placeIn, o); }
/** Shut down all of the connections. */ public void shutdown() throws IOException { shutDown = true; Selector s = selector.wakeup(); assert s == selector : "Wakeup returned the wrong selector."; for (MemcachedNode qa : locator.getAll()) { if (qa.getChannel() != null) { qa.getChannel().close(); qa.setSk(null); if (qa.getBytesRemainingToWrite() > 0) { getLogger() .warn("Shut down with %d bytes remaining to write", qa.getBytesRemainingToWrite()); } getLogger().debug("Shut down channel %s", qa.getChannel()); } } selector.close(); getLogger().debug("Shut down selector %s", selector); }