private void submit(final FlowScript flow) { LOG.debug("Submitting jobflow \"{}\": {}", flow.getId(), batchId); FlowScriptTask task = new FlowScriptTask( flow, doneQueue, new Callable<Void>() { @Override public Void call() throws InterruptedException, IOException { if (Thread.interrupted()) { throw new InterruptedException(); } String executionId = UUID.randomUUID().toString(); LOG.debug("Generated execution ID for \"{}\": {}", flow.getId(), executionId); lock.beginFlow(flow.getId(), executionId); executeFlow(batchId, flow, executionId); lock.endFlow(flow.getId(), executionId); LOG.debug("Completing jobflow \"{}\": {}", flow.getId(), batchId); return null; } }); YSLOG.info("I01003", batchId, flow.getId()); executor.execute(task); running.put(flow.getId(), task); }
private void waitForComplete() throws InterruptedException, IOException { LOG.debug("Waiting for running jobflows complete: {}", batchId); FlowScriptTask done = doneQueue.take(); assert done.isDone(); FlowScript flow = done.script; try { done.get(); boolean blocked = blocking.remove(flow.getId()); assert blocked; } catch (CancellationException e) { YSLOG.info(e, "I01005", batchId, flow.getId()); } catch (ExecutionException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else if (e.getCause() instanceof InterruptedException) { throw (InterruptedException) e.getCause(); } else if (e.getCause() instanceof Error) { throw (Error) e.getCause(); } else { throw new IOException("Flow execution failed by unknown error", e); } } finally { FlowScriptTask ran = running.remove(flow.getId()); assert ran != null; } }
public void run() throws InterruptedException, IOException { try { while (flows.isEmpty() == false) { boolean submitted = submit(); if (submitted == false) { if (running.isEmpty()) { throw new IOException( MessageFormat.format("Deadlock was detected in \"{0}\": {1}", batchId, blocking)); } waitForComplete(); } } while (running.isEmpty() == false) { waitForComplete(); } } finally { YSLOG.info("I01004", batchId); for (FlowScriptTask task : running.values()) { task.cancel(true); } while (running.isEmpty() == false) { try { waitForComplete(); } catch (IOException e) { YSLOG.warn(e, "W01002", batchId); } } } }