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); } }); } }
public static void enqueueCommandIfNotPresent(Host host, HostCommand command) throws IOException { if (host.getCurrentCommand() != command && !host.getCommandQueue().contains(command)) { host.enqueueCommand(command); } }
public void processCommandOnStartup() { try { HostCommand command = host.getCurrentCommand(); LOG.info("Current command is: " + command); if (command != null) { commandQueue.put(command); } else { host.nextCommand(); } } catch (Exception e) { LOG.error("Failed to process current command on startup.", e); stop(); } hasProcessedCommandOnStartup = true; }
@Override public synchronized void onCommandQueueChange(Host host) { LOG.info("Command queue changed."); // Do not process anything when we have not yet tried to process a command when starting up. if (!hasProcessedCommandOnStartup) { LOG.info("Ignoring command queue change as commands have not yet been executed on startup."); return; } try { HostCommand command = host.getCurrentCommand(); if (command == null) { // When command queue changes, and current command is empty, move on to next command host.nextCommand(); } else { // A current command was already in place, we are still executing it. Do nothing. LOG.info("Ignoring command queue change as a command is currently being executed."); } } catch (IOException e) { LOG.error("Failed to move on to next command.", e); stop(); } }
private static boolean isServingAndAboutToServe(Host host) throws IOException { return host.getState().equals(HostState.SERVING) && host.getCurrentCommand() == null && host.getCommandQueue().size() == 0; }
@Test public void testCommandAll() throws IOException { final Host hc = new MockHost(LOCALHOST); SlightlyLessAbstractRing rc = new SlightlyLessAbstractRing(1, null) { @Override public Set<Host> getHosts() { return Collections.singleton(hc); } }; assertNull(hc.getCurrentCommand()); assertTrue(hc.getCommandQueue().isEmpty()); Rings.commandAll(rc, HostCommand.SERVE_DATA); assertNull(hc.getCurrentCommand()); assertEquals(Arrays.asList(HostCommand.SERVE_DATA), hc.getCommandQueue()); }