Search in sources :

Example 16 with TransactionControl

use of co.cask.cdap.api.annotation.TransactionControl in project cdap by caskdata.

the class SparkRuntimeService method destroy.

/**
 * Calls the destroy or onFinish method of {@link ProgramLifecycle}.
 */
private void destroy(final ProgramState state) {
    context.setState(state);
    TransactionControl txControl = spark instanceof ProgramLifecycle ? Transactions.getTransactionControl(TransactionControl.IMPLICIT, Spark.class, spark, "destroy") : TransactionControl.IMPLICIT;
    runtimeContext.destroyProgram(programLifecycle, txControl, false);
}
Also used : ProgramLifecycle(co.cask.cdap.api.ProgramLifecycle) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) AbstractSpark(co.cask.cdap.api.spark.AbstractSpark) Spark(co.cask.cdap.api.spark.Spark)

Example 17 with TransactionControl

use of co.cask.cdap.api.annotation.TransactionControl in project cdap by caskdata.

the class MapReduceRuntimeService method beforeSubmit.

/**
 * For MapReduce programs created after 3.5, calls the initialize method of the {@link ProgramLifecycle}.
 * This method also sets up the Input/Output within the same transaction.
 */
private void beforeSubmit(final Job job) throws Exception {
    context.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
    // AbstractMapReduce implements final initialize(context) and requires subclass to
    // implement initialize(), whereas programs that directly implement MapReduce have
    // the option to override initialize(context) (if they implement ProgramLifeCycle)
    TransactionControl txControl = mapReduce instanceof AbstractMapReduce ? Transactions.getTransactionControl(TransactionControl.IMPLICIT, AbstractMapReduce.class, mapReduce, "initialize") : mapReduce instanceof ProgramLifecycle ? Transactions.getTransactionControl(TransactionControl.IMPLICIT, MapReduce.class, mapReduce, "initialize", MapReduceContext.class) : TransactionControl.IMPLICIT;
    context.initializeProgram(programLifecycle, txControl, false);
    // once the initialize method is executed, set the status of the MapReduce to RUNNING
    context.setState(new ProgramState(ProgramStatus.RUNNING, null));
    ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(context.getProgramInvocationClassLoader());
    try {
        // set input/outputs info, and get one of the configured mapper's TypeToken
        TypeToken<?> mapperTypeToken = setInputsIfNeeded(job);
        setOutputsIfNeeded(job);
        setOutputClassesIfNeeded(job, mapperTypeToken);
        setMapOutputClassesIfNeeded(job, mapperTypeToken);
    } finally {
        ClassLoaders.setContextClassLoader(oldClassLoader);
    }
}
Also used : ProgramLifecycle(co.cask.cdap.api.ProgramLifecycle) AbstractMapReduce(co.cask.cdap.api.mapreduce.AbstractMapReduce) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) ProgramState(co.cask.cdap.api.ProgramState)

Example 18 with TransactionControl

use of co.cask.cdap.api.annotation.TransactionControl in project cdap by caskdata.

the class FlowletRuntimeService method destroyFlowlet.

private void destroyFlowlet() {
    LOG.debug("Destroying flowlet: {}", flowletContext);
    TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, Flowlet.class, flowlet, "destroy");
    flowletContext.destroyProgram(flowlet, txControl, false);
    LOG.debug("Flowlet destroyed: {}", flowletContext);
}
Also used : TransactionControl(co.cask.cdap.api.annotation.TransactionControl)

Example 19 with TransactionControl

use of co.cask.cdap.api.annotation.TransactionControl in project cdap by caskdata.

the class CustomActionExecutor method execute.

void execute() throws Exception {
    try {
        customActionContext.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
        // AbstractCustomAction implements final initialize(context) and requires subclass to
        // implement initialize(), whereas programs that directly implement CustomAction can
        // override initialize(context)
        TransactionControl txControl = customAction instanceof AbstractCustomAction ? Transactions.getTransactionControl(TransactionControl.IMPLICIT, AbstractCustomAction.class, customAction, "initialize") : Transactions.getTransactionControl(TransactionControl.IMPLICIT, CustomAction.class, customAction, "initialize", CustomActionContext.class);
        customActionContext.initializeProgram(customAction, txControl, false);
        customActionContext.setState(new ProgramState(ProgramStatus.RUNNING, null));
        customActionContext.execute(customAction::run);
        customActionContext.setState(new ProgramState(ProgramStatus.COMPLETED, null));
    } catch (Throwable t) {
        customActionContext.setState(new ProgramState(ProgramStatus.FAILED, Throwables.getRootCause(t).getMessage()));
        Throwables.propagateIfPossible(t, Exception.class);
        throw Throwables.propagate(t);
    } finally {
        TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, CustomAction.class, customAction, "destroy");
        customActionContext.destroyProgram(customAction, txControl, false);
    }
}
Also used : CustomAction(co.cask.cdap.api.customaction.CustomAction) AbstractCustomAction(co.cask.cdap.api.customaction.AbstractCustomAction) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) AbstractCustomAction(co.cask.cdap.api.customaction.AbstractCustomAction) CustomActionContext(co.cask.cdap.api.customaction.CustomActionContext) BasicCustomActionContext(co.cask.cdap.internal.app.runtime.customaction.BasicCustomActionContext) ProgramState(co.cask.cdap.api.ProgramState)

Example 20 with TransactionControl

use of co.cask.cdap.api.annotation.TransactionControl in project cdap by caskdata.

the class AbstractHttpHandlerDelegator method wrapContentConsumer.

/**
 * Returns a new instance of {@link BodyConsumer} that wraps around the given {@link HttpContentConsumer}
 * and {@link DelayedHttpServiceResponder}.
 *
 * IMPORTANT: This method will also capture the context associated with the current thread, hence after
 * this method is called, no other methods on this class should be called from the current thread.
 *
 * This method is called from handler class generated by {@link HttpHandlerGenerator}.
 */
@SuppressWarnings("unused")
protected final BodyConsumer wrapContentConsumer(HttpContentConsumer consumer, DelayedHttpServiceResponder responder, TransactionControl defaultTxControl) {
    Preconditions.checkState(!responder.hasBufferedResponse(), "HttpContentConsumer may not be used after a response has already been sent.");
    // Close the provided responder since a new one will be created for the BodyConsumerAdapter to use.
    responder.close();
    ServiceTaskExecutor taskExecutor = context.getServiceTaskExecutor();
    Cancellable contextReleaser = context.capture();
    return new BodyConsumerAdapter(new DelayedHttpServiceResponder(responder, (contentProducer, txServiceContext) -> new BodyProducerAdapter(contentProducer, txServiceContext, contextReleaser, defaultTxControl)), consumer, taskExecutor, contextReleaser, defaultTxControl);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) MetricsContext(co.cask.cdap.api.metrics.MetricsContext) BodyConsumer(co.cask.http.BodyConsumer) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) HttpHandler(co.cask.http.HttpHandler) HttpContentProducer(co.cask.cdap.api.service.http.HttpContentProducer) ThrowingRunnable(co.cask.cdap.internal.app.runtime.ThrowingRunnable) HttpContentConsumer(co.cask.cdap.api.service.http.HttpContentConsumer) BodyProducer(co.cask.http.BodyProducer) HttpServiceRequest(co.cask.cdap.api.service.http.HttpServiceRequest) HttpResponder(co.cask.http.HttpResponder) Preconditions(com.google.common.base.Preconditions) Cancellable(org.apache.twill.common.Cancellable) CombineClassLoader(co.cask.cdap.common.lang.CombineClassLoader) HandlerContext(co.cask.http.HandlerContext) Cancellable(org.apache.twill.common.Cancellable)

Aggregations

TransactionControl (co.cask.cdap.api.annotation.TransactionControl)20 ProgramLifecycle (co.cask.cdap.api.ProgramLifecycle)7 ProgramState (co.cask.cdap.api.ProgramState)5 TxRunnable (co.cask.cdap.api.TxRunnable)5 DatasetContext (co.cask.cdap.api.data.DatasetContext)5 AbstractMapReduce (co.cask.cdap.api.mapreduce.AbstractMapReduce)3 MetricsFieldSetter (co.cask.cdap.internal.app.runtime.MetricsFieldSetter)3 AbstractCustomAction (co.cask.cdap.api.customaction.AbstractCustomAction)2 CustomAction (co.cask.cdap.api.customaction.CustomAction)2 CustomActionContext (co.cask.cdap.api.customaction.CustomActionContext)2 MapReduce (co.cask.cdap.api.mapreduce.MapReduce)2 AbstractSpark (co.cask.cdap.api.spark.AbstractSpark)2 CombineClassLoader (co.cask.cdap.common.lang.CombineClassLoader)2 InstantiatorFactory (co.cask.cdap.common.lang.InstantiatorFactory)2 PropertyFieldSetter (co.cask.cdap.common.lang.PropertyFieldSetter)2 BasicCustomActionContext (co.cask.cdap.internal.app.runtime.customaction.BasicCustomActionContext)2 MetricsContext (co.cask.cdap.api.metrics.MetricsContext)1 HttpContentConsumer (co.cask.cdap.api.service.http.HttpContentConsumer)1 HttpContentProducer (co.cask.cdap.api.service.http.HttpContentProducer)1 HttpServiceRequest (co.cask.cdap.api.service.http.HttpServiceRequest)1