public void awaitForState(final RunningState targetState, long timeoutMs) throws InterruptedException, TimeoutException { awaitForStatePredicate(new Predicate<JobExecutionState>() { @Override public boolean apply(JobExecutionState state) { return null != state.getRunningState() && state.getRunningState().equals(targetState); } @Override public String toString() { return "runningState == " + targetState; } }, timeoutMs); }
public void setRunningState(JobState.RunningState runningState) { doRunningStateChange(runningState); }
@Override public boolean apply(JobExecutionState state) { return null != state.getRunningState() && state.getRunningState().equals(targetState); } @Override public String toString() {
@Override public void onJobPrepare(JobContext jobContext) throws Exception { super.onJobPrepare(jobContext); _jobContext = jobContext; if (_jobState.getRunningState() == null) { _jobState.switchToPending(); } _jobState.switchToRunning(); if (_instrumentationEnabled && null != _launcherMetrics) { _launcherMetrics.getNumJobsLaunched().inc(); } }
@Override public void onJobCompletion(JobContext jobContext) throws Exception { Preconditions.checkArgument(jobContext.getJobState().getState() == RunningState.SUCCESSFUL || jobContext.getJobState().getState() == RunningState.COMMITTED || jobContext.getJobState().getState() == RunningState.FAILED, "Unexpected state: " + jobContext.getJobState().getState() + " in " + jobContext); super.onJobCompletion(jobContext); if (_instrumentationEnabled && null != _launcherMetrics) { _launcherMetrics.getNumJobsCompleted().inc(); } if (jobContext.getJobState().getState() == RunningState.FAILED) { if (_instrumentationEnabled && null != _launcherMetrics) { _launcherMetrics.getNumJobsFailed().inc(); } _jobState.switchToFailed(); } else { // TODO Remove next line once the JobLauncher starts sending notifications for success _jobState.switchToSuccessful(); _jobState.switchToCommitted(); if (_instrumentationEnabled && null != _launcherMetrics) { _launcherMetrics.getNumJobsCommitted().inc(); } } }
/** {@inheritDoc} */ @Override public void onStatusChange(JobExecutionState state, RunningState previousStatus, RunningState newStatus) { if (_log.isPresent()) { _log.get().info("JobExection status change for " + state.getJobSpec().toShortString() + ": " + previousStatus + " --> " + newStatus); } }
private static String getEmailBody(JobExecutionState state, RunningState previousStatus, RunningState newStatus) { return new StringBuilder().append("RunningState: ").append(newStatus.toString()).append("\n") .append("JobExecutionState: ").append(state.getJobSpec().toLongString()).append("\n") .append("ExecutionMetadata: ").append(state.getExecutionMetadata()).toString(); }
public static JobExecutionResult createFromState(JobExecutionState state) { Preconditions.checkArgument(null != state.getRunningState()); Preconditions.checkArgument(state.getRunningState().isDone()); if (state.getRunningState().isSuccess()) { return JobExecutionResult.createSuccessResult(); } else if (state.getRunningState().isCancelled()) { return JobExecutionResult.createCancelledResult(); } else { // FIXME we need to capture error(s) return JobExecutionResult.createFailureResult( new RuntimeException("Gobblin job failed:" + state.getJobExecution())); } } }
@Override public JobExecutionResult call() throws JobException, InterruptedException, TimeoutException { jobLauncher.launchJob(bridge); jobState.awaitForDone(Long.MAX_VALUE); return JobExecutionResult.createFromState(jobState); } }
@Override public void onJobCancellation(JobContext jobContext) throws Exception { super.onJobCancellation(jobContext); _jobState.switchToCancelled(); if (_instrumentationEnabled && null != _launcherMetrics) { _launcherMetrics.getNumJobsCancelled().inc(); } } }
/** * Creates a new JobExecutionDriver which acts as an adapter to the legacy {@link JobLauncher} API. * @param sysConfig the system/environment config * @param jobSpec the JobSpec to be executed * @param jobLauncherType an optional jobLauncher type; the value follows the convention of * {@link JobLauncherFactory#newJobLauncher(java.util.Properties, java.util.Properties, String). * If absent, {@link JobLauncherFactory#newJobLauncher(java.util.Properties, java.util.Properties)} * will be used which looks for the {@link ConfigurationKeys#JOB_LAUNCHER_TYPE_KEY} * in the system configuration. * @param jobExecStateListener an optional listener to listen for state changes in the execution. * @param log an optional logger to be used; if none is specified, a default one * will be instantiated. */ public static JobLauncherExecutionDriver create(Configurable sysConfig, JobSpec jobSpec, Optional<JobLauncherFactory.JobLauncherType> jobLauncherType, Optional<Logger> log, boolean instrumentationEnabled, JobExecutionLauncher.StandardMetrics launcherMetrics, SharedResourcesBroker<GobblinScopeTypes> instanceBroker) { Logger actualLog = log.isPresent() ? log.get() : LoggerFactory.getLogger(JobLauncherExecutionDriver.class); JobExecutionStateListeners callbackDispatcher = new JobExecutionStateListeners(actualLog); JobExecutionUpdatable jobExec = JobExecutionUpdatable.createFromJobSpec(jobSpec); JobExecutionState jobState = new JobExecutionState(jobSpec, jobExec, Optional.<JobExecutionStateListener>of(callbackDispatcher)); JobLauncher jobLauncher = createLauncher(sysConfig, jobSpec, actualLog, jobLauncherType.isPresent() ? Optional.of(jobLauncherType.get().toString()) : Optional.<String>absent(), instanceBroker); JobListenerToJobStateBridge bridge = new JobListenerToJobStateBridge(actualLog, jobState, instrumentationEnabled, launcherMetrics); DriverRunnable runnable = new DriverRunnable(jobLauncher, bridge, jobState, callbackDispatcher, jobExec); return new JobLauncherExecutionDriver(jobSpec, actualLog, runnable); }
/** {@inheritDoc} */ @Override public void onMetadataChange(JobExecutionState state, String key, Object oldValue, Object newValue) { if (this.filter.apply(state.getJobSpec())) { this.delegate.onMetadataChange(state, key, oldValue, newValue); } }
/** {@inheritDoc} */ @Override public void onStatusChange(JobExecutionState state, RunningState previousStatus, RunningState newStatus) { if (_log.isPresent()) { _log.get().info("JobExection status change for " + state.getJobSpec().toShortString() + ": " + previousStatus + " --> " + newStatus); } }
public void switchToRunning() { doRunningStateChange(RunningState.RUNNING); }
@Override public boolean apply(@Nonnull JobExecutionState state) { return null != state.getRunningState() && state.getRunningState().isDone(); } @Override public String toString() {
public void awaitForDone(long timeoutMs) throws InterruptedException, TimeoutException { awaitForStatePredicate(EXECUTION_DONE_PREDICATE, timeoutMs); }
/** {@inheritDoc} */ @Override public void onMetadataChange(JobExecutionState state, String key, Object oldValue, Object newValue) { if (_log.isPresent()) { _log.get().info("JobExection metadata change for " + state.getJobSpec().toShortString() + key + ": '" + oldValue + "' --> '" + newValue + "'"); } }
public void switchToCommitted() { doRunningStateChange(RunningState.COMMITTED); }
/** {@inheritDoc} */ @Override public void onStatusChange(JobExecutionState state, RunningState previousStatus, RunningState newStatus) { if (this.filter.apply(state.getJobSpec())) { this.delegate.onStatusChange(state, previousStatus, newStatus); } }
public void switchToFailed() { doRunningStateChange(RunningState.FAILED); }