private boolean wantsPod(RunState runState) { switch (runState.state()) { // Be conservative and only let the pod go when we really know it is not needed anymore. case TERMINATED: case FAILED: case ERROR: case DONE: return false; default: return true; } }
private static List<Event> handleError(RunState state, Event event) { switch (state.state()) { case PREPARE: case SUBMITTED: case RUNNING: return ImmutableList.of(event); default: return ImmutableList.of(); } }
private static List<Event> handleStarted(WorkflowInstance workflowInstance, RunState state) { switch (state.state()) { case PREPARE: case SUBMITTED: return ImmutableList.of(Event.started(workflowInstance)); default: return ImmutableList.of(); } }
private IllegalStateException illegalTransition(String event) { return new IllegalStateException(workflowInstance() + " received " + event + " while in " + state()); }
@Override public void transitionInto(RunState state) { final String name = state.state().name().toLowerCase(); LOG.info("{}{} transition -> {} {}", prefix, state.workflowInstance(), name, stateInfo(state)); }
public static Set<WorkflowInstance> getTimedOutInstances(Map<WorkflowId, Workflow> workflows, List<InstanceState> activeStates, Instant instant, TimeoutConfig ttl) { return activeStates.parallelStream() .filter(entry -> { final Optional<Workflow> workflowOpt = Optional.ofNullable(workflows.get(entry.workflowInstance().workflowId())); return hasTimedOut(workflowOpt, entry.runState(), instant, ttl.ttlOf(entry.runState().state())); }) .map(InstanceState::workflowInstance) .collect(toSet()); }
private void sendTimeout(WorkflowInstance workflowInstance, RunState runState) { LOG.info("Found stale state {} since {} for workflow {}; Issuing a timeout", runState.state(), Instant.ofEpochMilli(runState.timestamp()), workflowInstance); stateManager.receiveIgnoreClosed(Event.timeout(workflowInstance), runState.counter()); } }
private boolean shouldExecute(RunState runState) { if (runState.state() != State.QUEUED) { return false; } final Instant now = time.get(); final Instant deadline = Instant .ofEpochMilli(runState.timestamp()) .plusMillis(runState.data().retryDelayMillis().orElse(0L)); return !deadline.isAfter(now); }
@Deprecated @Override public RunState timeTrigger(WorkflowInstance workflowInstance) { switch (state()) { case NEW: return state( // for backwards compatibility SUBMITTED, data().builder() .trigger(Trigger.unknown("UNKNOWN")) .triggerId("UNKNOWN") // for backwards compatibility .build()); default: throw illegalTransition("timeTrigger"); } }
@Override public void transitionInto(RunState state) { switch (state.state()) { case TERMINATED: if (state.data().lastExit().isPresent()) { stats.recordExitCode(state.data().lastExit().get()); } break; default: // do nothing } } }
private RunState transitionUpdates(Instant instant) { return new AutoValue_RunState( workflowInstance(), state(), instant.toEpochMilli(), data(), counter() + 1); }
@Override public RunState triggerExecution(WorkflowInstance workflowInstance, Trigger trigger, TriggerParameters parameters) { switch (state()) { case NEW: return state( QUEUED, data().builder() .trigger(trigger) .triggerId(TriggerUtil.triggerId(trigger)) // for backwards compatibility .triggerParameters(parameters) .build()); default: throw illegalTransition("triggerExecution"); } }
@Override public RunState submitted(WorkflowInstance workflowInstance, String executionId) { switch (state()) { case SUBMITTING: return state( SUBMITTED, data().builder() .tries(data().tries() + 1) // backwards compatibility .executionId(data().executionId().orElse(executionId)) .build()); default: throw illegalTransition("submitted"); } }
private RunStateData runStateToRunStateData(RunState state) { return RunStateData.newBuilder() .workflowInstance(state.workflowInstance()) .state(state.state().name()) .stateData(state.data()) .latestTimestamp(state.timestamp()) .build(); }