use of co.cask.cdap.proto.BasicThrowable in project cdap by caskdata.
the class MapReduceProgramRunner method createRuntimeServiceListener.
/**
* Creates a service listener to reactor on state changes on {@link MapReduceRuntimeService}.
*/
private Service.Listener createRuntimeServiceListener(final ProgramId programId, final RunId runId, final Iterable<Closeable> closeables, final Arguments arguments, final Arguments userArgs) {
final String twillRunId = arguments.getOption(ProgramOptionConstants.TWILL_RUN_ID);
return new ServiceListenerAdapter() {
@Override
public void starting() {
//Get start time from RunId
long startTimeInSeconds = RunIds.getTime(runId, TimeUnit.SECONDS);
if (startTimeInSeconds == -1) {
// If RunId is not time-based, use current time as start time
startTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
}
final long finalStartTimeInSeconds = startTimeInSeconds;
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
runtimeStore.setStart(programId, runId.getId(), finalStartTimeInSeconds, twillRunId, userArgs.asMap(), arguments.asMap());
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
@Override
public void terminated(Service.State from) {
closeAllQuietly(closeables);
ProgramRunStatus runStatus = ProgramController.State.COMPLETED.getRunStatus();
if (from == Service.State.STOPPING) {
// Service was killed
runStatus = ProgramController.State.KILLED.getRunStatus();
}
final ProgramRunStatus finalRunStatus = runStatus;
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
runtimeStore.setStop(programId, runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), finalRunStatus);
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
@Override
public void failed(Service.State from, @Nullable final Throwable failure) {
closeAllQuietly(closeables);
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
runtimeStore.setStop(programId, runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramController.State.ERROR.getRunStatus(), new BasicThrowable(failure));
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
};
}
use of co.cask.cdap.proto.BasicThrowable in project cdap by caskdata.
the class WorkflowDriver method executeCustomAction.
private void executeCustomAction(final WorkflowActionNode node, InstantiatorFactory instantiator, final ClassLoader classLoader, WorkflowToken token) throws Exception {
CustomActionExecutor customActionExecutor;
if (node.getActionSpecification() != null) {
// Node has WorkflowActionSpecification, so it must represent the deprecated WorkflowAction
// Create instance of the CustomActionExecutor using BasicWorkflowContext
BasicWorkflowContext context = createWorkflowContext(node.getActionSpecification(), token);
customActionExecutor = new CustomActionExecutor(workflowRunId, context, instantiator, classLoader);
} else {
// Node has CustomActionSpecification, so it must represent the CustomAction added in 3.5.0
// Create instance of the CustomActionExecutor using CustomActionContext
WorkflowProgramInfo info = new WorkflowProgramInfo(workflowSpec.getName(), node.getNodeId(), workflowRunId.getRun(), node.getNodeId(), (BasicWorkflowToken) token);
ProgramOptions actionOptions = new SimpleProgramOptions(programOptions.getName(), programOptions.getArguments(), new BasicArguments(RuntimeArguments.extractScope(ACTION_SCOPE, node.getNodeId(), programOptions.getUserArguments().asMap())));
BasicCustomActionContext context = new BasicCustomActionContext(program, actionOptions, cConf, node.getCustomActionSpecification(), info, metricsCollectionService, datasetFramework, txClient, discoveryServiceClient, pluginInstantiator, secureStore, secureStoreManager, messagingService);
customActionExecutor = new CustomActionExecutor(workflowRunId, context, instantiator, classLoader);
}
status.put(node.getNodeId(), node);
runtimeStore.addWorkflowNodeState(workflowRunId, new WorkflowNodeStateDetail(node.getNodeId(), NodeStatus.RUNNING));
Throwable failureCause = null;
try {
customActionExecutor.execute();
} catch (Throwable t) {
failureCause = t;
throw t;
} finally {
status.remove(node.getNodeId());
runtimeStore.updateWorkflowToken(workflowRunId, token);
NodeStatus status = failureCause == null ? NodeStatus.COMPLETED : NodeStatus.FAILED;
nodeStates.put(node.getNodeId(), new WorkflowNodeState(node.getNodeId(), status, null, failureCause));
BasicThrowable defaultThrowable = failureCause == null ? null : new BasicThrowable(failureCause);
runtimeStore.addWorkflowNodeState(workflowRunId, new WorkflowNodeStateDetail(node.getNodeId(), status, null, defaultThrowable));
}
}
use of co.cask.cdap.proto.BasicThrowable in project cdap by caskdata.
the class WorkflowProgramRunner method run.
@Override
public ProgramController run(final Program program, final ProgramOptions options) {
// Extract and verify options
ApplicationSpecification appSpec = program.getApplicationSpecification();
Preconditions.checkNotNull(appSpec, "Missing application specification.");
ProgramType processorType = program.getType();
Preconditions.checkNotNull(processorType, "Missing processor type.");
Preconditions.checkArgument(processorType == ProgramType.WORKFLOW, "Only WORKFLOW process type is supported.");
WorkflowSpecification workflowSpec = appSpec.getWorkflows().get(program.getName());
Preconditions.checkNotNull(workflowSpec, "Missing WorkflowSpecification for %s", program.getName());
final RunId runId = ProgramRunners.getRunId(options);
// Setup dataset framework context, if required
if (datasetFramework instanceof ProgramContextAware) {
ProgramId programId = program.getId();
((ProgramContextAware) datasetFramework).setContext(new BasicProgramContext(programId.run(runId)));
}
// List of all Closeable resources that needs to be cleanup
final List<Closeable> closeables = new ArrayList<>();
try {
PluginInstantiator pluginInstantiator = createPluginInstantiator(options, program.getClassLoader());
if (pluginInstantiator != null) {
closeables.add(pluginInstantiator);
}
WorkflowDriver driver = new WorkflowDriver(program, options, hostname, workflowSpec, programRunnerFactory, metricsCollectionService, datasetFramework, discoveryServiceClient, txClient, runtimeStore, cConf, pluginInstantiator, secureStore, secureStoreManager, messagingService);
// Controller needs to be created before starting the driver so that the state change of the driver
// service can be fully captured by the controller.
final ProgramController controller = new WorkflowProgramController(program, driver, serviceAnnouncer, runId);
final String twillRunId = options.getArguments().getOption(ProgramOptionConstants.TWILL_RUN_ID);
controller.addListener(new AbstractListener() {
@Override
public void init(ProgramController.State state, @Nullable Throwable cause) {
// Get start time from RunId
long startTimeInSeconds = RunIds.getTime(controller.getRunId(), TimeUnit.SECONDS);
if (startTimeInSeconds == -1) {
// If RunId is not time-based, use current time as start time
startTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
}
final long finalStartTimeInSeconds = startTimeInSeconds;
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
runtimeStore.setStart(program.getId(), runId.getId(), finalStartTimeInSeconds, twillRunId, options.getUserArguments().asMap(), options.getArguments().asMap());
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
// This can happen if there is a delay in calling the init listener
if (state == ProgramController.State.COMPLETED) {
completed();
}
// This can happen if there is a delay in calling the init listener
if (state == ProgramController.State.ERROR) {
error(controller.getFailureCause());
}
}
@Override
public void completed() {
LOG.debug("Program {} with run id {} completed successfully.", program.getId(), runId.getId());
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
runtimeStore.setStop(program.getId(), runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramController.State.COMPLETED.getRunStatus());
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
@Override
public void killed() {
LOG.debug("Program {} with run id {} killed.", program.getId(), runId.getId());
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
runtimeStore.setStop(program.getId(), runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramController.State.KILLED.getRunStatus());
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
@Override
public void suspended() {
LOG.debug("Suspending Program {} with run id {}.", program.getId(), runId.getId());
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
runtimeStore.setSuspend(program.getId(), runId.getId());
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
@Override
public void resuming() {
LOG.debug("Resuming Program {} {}.", program.getId(), runId.getId());
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
runtimeStore.setResume(program.getId(), runId.getId());
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
@Override
public void error(final Throwable cause) {
LOG.info("Program {} with run id {} stopped because of error {}.", program.getId(), runId.getId(), cause);
closeAllQuietly(closeables);
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
runtimeStore.setStop(program.getId(), runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramController.State.ERROR.getRunStatus(), new BasicThrowable(cause));
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
}, Threads.SAME_THREAD_EXECUTOR);
driver.start();
return controller;
} catch (Exception e) {
closeAllQuietly(closeables);
throw Throwables.propagate(e);
}
}
use of co.cask.cdap.proto.BasicThrowable in project cdap by caskdata.
the class DefaultStore method compareAndSetStatus.
@Override
public boolean compareAndSetStatus(final ProgramId id, final String pid, final ProgramRunStatus expectedStatus, final ProgramRunStatus newStatus) {
Preconditions.checkArgument(expectedStatus != null, "Expected of program run should be defined");
Preconditions.checkArgument(newStatus != null, "New state of program run should be defined");
return Transactions.executeUnchecked(transactional, new TxCallable<Boolean>() {
@Override
public Boolean call(DatasetContext context) throws Exception {
AppMetadataStore mds = getAppMetadataStore(context);
RunRecordMeta target = mds.getRun(id, pid);
if (target.getStatus() == expectedStatus) {
long now = System.currentTimeMillis();
long nowSecs = TimeUnit.MILLISECONDS.toSeconds(now);
switch(newStatus) {
case RUNNING:
Map<String, String> runtimeArgs = GSON.fromJson(target.getProperties().get("runtimeArgs"), STRING_MAP_TYPE);
Map<String, String> systemArgs = GSON.fromJson(target.getProperties().get("systemArgs"), STRING_MAP_TYPE);
if (runtimeArgs == null) {
runtimeArgs = EMPTY_STRING_MAP;
}
if (systemArgs == null) {
systemArgs = EMPTY_STRING_MAP;
}
mds.recordProgramStart(id, pid, nowSecs, target.getTwillRunId(), runtimeArgs, systemArgs);
break;
case SUSPENDED:
mds.recordProgramSuspend(id, pid);
break;
case COMPLETED:
case KILLED:
case FAILED:
BasicThrowable failureCause = newStatus == ProgramRunStatus.FAILED ? new BasicThrowable(new Throwable("Marking run record as failed since no running program found.")) : null;
mds.recordProgramStop(id, pid, nowSecs, newStatus, failureCause);
break;
default:
break;
}
return true;
}
return false;
}
});
}
use of co.cask.cdap.proto.BasicThrowable in project cdap by caskdata.
the class ProgramLifecycleService method start.
/**
* Start a Program.
*
* @param programId the {@link ProgramId program} to start
* @param systemArgs system arguments
* @param userArgs user arguments
* @param debug enable debug mode
* @return {@link ProgramRuntimeService.RuntimeInfo}
* @throws IOException if there is an error starting the program
* @throws ProgramNotFoundException if program is not found
* @throws UnauthorizedException if the logged in user is not authorized to start the program. To start a program,
* a user requires {@link Action#EXECUTE} on the program
* @throws Exception if there were other exceptions checking if the current user is authorized to start the program
*/
public ProgramRuntimeService.RuntimeInfo start(final ProgramId programId, final Map<String, String> systemArgs, final Map<String, String> userArgs, boolean debug) throws Exception {
authorizationEnforcer.enforce(programId, authenticationContext.getPrincipal(), Action.EXECUTE);
ProgramDescriptor programDescriptor = store.loadProgram(programId);
BasicArguments systemArguments = new BasicArguments(systemArgs);
BasicArguments userArguments = new BasicArguments(userArgs);
ProgramRuntimeService.RuntimeInfo runtimeInfo = runtimeService.run(programDescriptor, new SimpleProgramOptions(programId.getProgram(), systemArguments, userArguments, debug));
final ProgramController controller = runtimeInfo.getController();
final String runId = controller.getRunId().getId();
final String twillRunId = runtimeInfo.getTwillRunId() == null ? null : runtimeInfo.getTwillRunId().getId();
if (programId.getType() != ProgramType.MAPREDUCE && programId.getType() != ProgramType.SPARK && programId.getType() != ProgramType.WORKFLOW) {
// MapReduce state recording is done by the MapReduceProgramRunner, Spark state recording
// is done by SparkProgramRunner, and Workflow state recording is done by WorkflowProgramRunner.
// TODO [JIRA: CDAP-2013] Same needs to be done for other programs as well
controller.addListener(new AbstractListener() {
@Override
public void init(ProgramController.State state, @Nullable Throwable cause) {
// Get start time from RunId
long startTimeInSeconds = RunIds.getTime(controller.getRunId(), TimeUnit.SECONDS);
if (startTimeInSeconds == -1) {
// If RunId is not time-based, use current time as start time
startTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
}
final long finalStartTimeInSeconds = startTimeInSeconds;
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
store.setStart(programId, runId, finalStartTimeInSeconds, twillRunId, userArgs, systemArgs);
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
if (state == ProgramController.State.COMPLETED) {
completed();
}
if (state == ProgramController.State.ERROR) {
error(controller.getFailureCause());
}
}
@Override
public void completed() {
LOG.debug("Program {} completed successfully.", programId);
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
store.setStop(programId, runId, TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramController.State.COMPLETED.getRunStatus());
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
@Override
public void killed() {
LOG.debug("Program {} killed.", programId);
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
store.setStop(programId, runId, TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramController.State.KILLED.getRunStatus());
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
@Override
public void suspended() {
LOG.debug("Suspending Program {} {}.", programId, runId);
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
store.setSuspend(programId, runId);
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
@Override
public void resuming() {
LOG.debug("Resuming Program {} {}.", programId, runId);
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
store.setResume(programId, runId);
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
@Override
public void error(final Throwable cause) {
LOG.info("Program stopped with error {}, {}", programId, runId, cause);
Retries.supplyWithRetries(new Supplier<Void>() {
@Override
public Void get() {
store.setStop(programId, runId, TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramController.State.ERROR.getRunStatus(), new BasicThrowable(cause));
return null;
}
}, RetryStrategies.fixDelay(Constants.Retry.RUN_RECORD_UPDATE_RETRY_DELAY_SECS, TimeUnit.SECONDS));
}
}, Threads.SAME_THREAD_EXECUTOR);
}
return runtimeInfo;
}
Aggregations