public Optional<Workflow> store(Workflow workflow, Consumer<Optional<Workflow>> guard) throws WorkflowInitializationException { try { return storage.runInTransaction(tx -> store(tx, workflow, guard)); } catch (IOException e) { LOG.warn("failed to write workflow {} to storage", workflow.id(), e); throw new RuntimeException(e); } }
private Response<Workflow> workflow(String componentId, String id) { try { return storage.workflow(WorkflowId.create(componentId, id)) .map(Response::forPayload) .orElse(Response.forStatus(Status.NOT_FOUND)); } catch (IOException e) { throw Throwables.propagate(e); } }
@Override public Map<WorkflowInstance, RunState> getActiveStates() { try { return storage.readActiveStates(); } catch (IOException e) { throw new RuntimeException(e); } }
final Workflow workflow; try { workflow = storage.workflow(workflowId) .orElseThrow(() -> new ResponseException( Response.forStatus(Status.NOT_FOUND.withReasonPhrase("workflow not found")))); activeWorkflowInstances = storage.readActiveStates(input.component()).keySet(); } catch (Exception e) { throw new RuntimeException(e); storage.storeBackfill(backfill); } catch (IOException e) { throw new RuntimeException(e);
private void triggerAndProgress(Backfill backfill) { final Optional<Workflow> workflowOpt; try { workflowOpt = storage.workflow(backfill.workflowId()); } catch (IOException e) { LOG.warn("Failed to read workflow {}", backfill.workflowId(), e); while (true) { try { if (!storage.runInTransaction(tx -> triggerNextPartitionAndProgress(tx, backfill.id(), workflow, initialNextTrigger, remainingCapacity, backfill.reverse()))) {
if (componentOpt.isPresent() && workflowOpt.isPresent()) { final WorkflowId workflowId = WorkflowId.create(componentOpt.get(), workflowOpt.get()); backfills = storage.backfillsForWorkflowId(showAll, workflowId).stream(); } else if (componentOpt.isPresent()) { final String component = componentOpt.get(); backfills = storage.backfillsForComponent(showAll, component).stream(); } else if (workflowOpt.isPresent()) { final String workflow = workflowOpt.get(); backfills = storage.backfillsForWorkflow(showAll, workflow).stream(); } else { backfills = storage.backfills(showAll).stream();
try { if (tail) { final Optional<Workflow> workflow = storage.workflow(workflowId); if (!workflow.isPresent()) { return Response.forStatus(Status.NOT_FOUND.withReasonPhrase("Could not find workflow.")); final WorkflowState workflowState = storage.workflowState(workflowId); if (!workflowState.nextNaturalTrigger().isPresent()) { return Response.forStatus(Status.NOT_FOUND.withReasonPhrase("No next natural trigger for workflow.")); final String tailStart = ParameterUtil.toParameter(schedule, startInstant); final String tailStop = ParameterUtil.toParameter(schedule, nextNaturalTrigger); data = storage.executionData(workflowId, tailStart, tailStop); } else if (start.isEmpty()) { data = storage.executionData(workflowId, offset, limit); } else { data = storage.executionData(workflowId, start, stop);
private Response<ByteString> deleteWorkflow(String cid, String wfid, AuthContext ac) { final WorkflowId workflowId = WorkflowId.create(cid, wfid); final Optional<Workflow> workflow; try { workflow = storage.workflow(workflowId); } catch (IOException e) { final String message = String.format("Couldn't read workflow %s. ", workflowId); LOG.warn(message, e); return Response.forStatus(Status.INTERNAL_SERVER_ERROR .withReasonPhrase("Error in internal storage")); } if (!workflow.isPresent()) { return Response.forStatus(Status.NOT_FOUND.withReasonPhrase("Workflow does not exist")); } // TODO: run in transaction workflowActionAuthorizer.authorizeWorkflowAction(ac, workflow.get()); try { storage.delete(workflowId); } catch (IOException e) { final String message = String.format("Couldn't remove workflow %s. ", workflowId); LOG.warn(message, e); return Response.forStatus(Status.INTERNAL_SERVER_ERROR .withReasonPhrase("Error in internal storage")); } workflowConsumer.accept(workflow, Optional.empty()); LOG.info("Workflow removed: {}", workflowId); return Response.forStatus(Status.NO_CONTENT); }
counter = storage.getLatestStoredCounter(workflowInstance).orElse(NO_EVENTS_PROCESSED); } catch (IOException e) { throw new RuntimeException(e); storage.runInTransaction(tx -> { final Optional<Workflow> workflow = tx.workflow(workflowInstance.workflowId()); if (!workflow.isPresent()) {
private Map<WorkflowId, Workflow> getWorkflows(final List<InstanceState> activeStates) { final Set<WorkflowId> workflowIds = activeStates.stream() .map(activeState -> activeState.workflowInstance().workflowId()) .collect(toSet()); return storage.workflows(workflowIds); }
private CompletionStage<Response<ByteString>> haltBackfill(String id, RequestContext rc, RequestAuthenticator authenticator) { final AuthContext authContext = authenticator.authenticate(rc.request()); try { // TODO: run in transction final Optional<Backfill> backfillOptional = storage.backfill(id); if (backfillOptional.isPresent()) { final Backfill backfill = backfillOptional.get(); workflowActionAuthorizer.authorizeWorkflowAction(authContext, backfill.workflowId()); storage.storeBackfill(backfill.builder().halted(true).build()); return haltActiveBackfillInstances(backfill, rc.requestScopedClient()); } else { return CompletableFuture.completedFuture( Response.forStatus(Status.NOT_FOUND.withReasonPhrase("backfill not found"))); } } catch (IOException e) { return CompletableFuture.completedFuture(Response.forStatus( Status.INTERNAL_SERVER_ERROR .withReasonPhrase("could not halt backfill: " + e.getMessage()))); } }
@Override public Map<WorkflowInstance, RunState> getActiveStatesByTriggerId(String triggerId) { try { return storage.readActiveStatesByTriggerId(triggerId); } catch (IOException e) { throw new RuntimeException(e); } }
private static SortedSet<SequenceEvent> getSequenceEvents( final WorkflowInstance workflowInstance, final Storage storage) { final SortedSet<SequenceEvent> sequenceEvents; try { sequenceEvents = storage.readEvents(workflowInstance); } catch (IOException e) { throw new RuntimeException(e); } return sequenceEvents; }
private void storeBackfill(Backfill backfill) { try { storage.storeBackfill(backfill); } catch (IOException e) { LOG.warn("Failed to store updated backfill {}", backfill.id(), e); } }
private ResourcesPayload getResources() { try { return ResourcesPayload.create(storage.resources()); } catch (IOException e) { throw new RuntimeException(e); } }
void tick0() { final Instant t0 = time.get(); final List<Backfill> backfills; try { backfills = storage.backfills(false); } catch (IOException e) { LOG.warn("Failed to get backfills", e); return; } shuffler.accept(backfills); backfills.forEach(backfill -> guard(() -> triggerAndProgress(backfill)).run()); final long durationMillis = t0.until(time.get(), ChronoUnit.MILLIS); stats.recordTickDuration(TICK_TYPE, durationMillis); }
private Response<Collection<Workflow>> workflows() { try { return Response.forPayload(storage.workflows().values()); } catch (IOException e) { return Response.forStatus(Status.INTERNAL_SERVER_ERROR.withReasonPhrase( "Failed to get workflows")); } }
activeWorkflowInstances = storage.readActiveStatesByTriggerId(backfill.id()); } catch (IOException e) { throw new RuntimeException(e);
private EventsPayload eventsForWorkflowInstance(String cid, String eid, String iid) { final WorkflowId workflowId = WorkflowId.create(cid, eid); final WorkflowInstance workflowInstance = WorkflowInstance.create(workflowId, iid); try { final Set<SequenceEvent> sequenceEvents = storage.readEvents(workflowInstance); final List<EventsPayload.TimestampedEvent> timestampedEvents = sequenceEvents.stream() .map(sequenceEvent -> EventsPayload.TimestampedEvent.create( sequenceEvent.event(), sequenceEvent.timestamp())) .collect(toList()); return EventsPayload.create(timestampedEvents); } catch (IOException e) { throw new RuntimeException(e); } } }