private void doCommandAll(HttpServletRequest req, HttpServletResponse resp) throws IOException { String rgName = req.getParameter("rgName"); int ringNum = Integer.parseInt(req.getParameter("ringNum")); HostCommand command = HostCommand.valueOf(req.getParameter("command")); Rings.commandAll(coordinator.getRingGroup(rgName).getRing(ringNum), command); resp.sendRedirect("/ring.jsp?g=" + rgName + "&n=" + ringNum); } }
protected void waitUntilCommand(final HostCommand command, final Host host) throws InterruptedException { WaitUntil.orDie(() -> { try { return command.equals(host.getCurrentCommand()); } catch (IOException e) { throw new RuntimeException(e); } }); } }
@Override public void enqueueCommand(HostCommand command) throws IOException { try { zk.create(ZkPath.append(path, COMMAND_QUEUE_PATH, "command_"), command.toString().getBytes(), CreateMode.PERSISTENT_SEQUENTIAL); } catch (Exception e) { throw new IOException(e); } }
@Override public List<HostCommand> getCommandQueue() throws IOException { try { List<String> children = zk.getChildren(ZkPath.append(path, COMMAND_QUEUE_PATH), false); Collections.sort(children); List<HostCommand> queue = new ArrayList<HostCommand>(); for (String child : children) { queue.add(HostCommand.valueOf(zk.getString(ZkPath.append(path, COMMAND_QUEUE_PATH, child)))); } return queue; } catch (Exception e) { throw new IOException(e); } }
protected void doEnqueueCommand(HttpServletRequest req, HttpServletResponse resp) throws IOException { RingGroup rg = coordinator.getRingGroup(req.getParameter("g")); Ring r = rg.getRing(Integer.parseInt(req.getParameter("n"))); Host h = r.getHostByAddress(PartitionServerAddress.parse(URLEnc.decode(req.getParameter("h")))); h.enqueueCommand(HostCommand.valueOf(req.getParameter("command"))); redirectBack(resp, rg, r, h); }
@Override public HostCommand nextCommand() throws IOException { try { // get the queue and sort so we have correct ordering List<String> children = zk.getChildren(ZkPath.append(path, COMMAND_QUEUE_PATH), false); Collections.sort(children); // if there are no children, the queue is empty. if (children.size() == 0) { currentCommand.set(null); return null; } // parse out the actual command String headOfQueuePath = ZkPath.append(path, COMMAND_QUEUE_PATH, children.get(0)); HostCommand nextCommand = HostCommand.valueOf(zk.getString(headOfQueuePath)); // set the current command first (modifying the queue will call the queue listeners) currentCommand.set(nextCommand); // delete the head of the queue zk.delete(headOfQueuePath, -1); return nextCommand; } catch (Exception e) { throw new IOException(e); } }