Search in sources :

Example 11 with TransactionControl

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

the class BodyConsumerAdapter method finished.

@Override
public void finished(HttpResponder responder) {
    TransactionControl txCtrl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, HttpContentConsumer.class, delegate, "onFinish", HttpServiceResponder.class);
    try {
        if (TransactionControl.IMPLICIT == txCtrl) {
            transactional.execute(new TxRunnable() {

                @Override
                public void run(DatasetContext context) throws Exception {
                    delegate.onFinish(BodyConsumerAdapter.this.responder);
                }
            });
        } else {
            delegate.onFinish(BodyConsumerAdapter.this.responder);
        }
    } catch (Throwable t) {
        onError(t, this.responder);
        return;
    }
    // To the HttpContentConsumer, the call is completed even if it fails to send response back to client.
    completed = true;
    try {
        BodyConsumerAdapter.this.responder.execute();
    } finally {
        if (!this.responder.hasContentProducer()) {
            contextReleaser.cancel();
        }
    }
}
Also used : TxRunnable(co.cask.cdap.api.TxRunnable) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) DatasetContext(co.cask.cdap.api.data.DatasetContext)

Example 12 with TransactionControl

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

the class BodyProducerAdapter method finished.

@Override
public void finished() throws Exception {
    TransactionControl txCtrl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, HttpContentProducer.class, delegate, "onFinish");
    if (TransactionControl.IMPLICIT == txCtrl) {
        serviceContext.execute(new TxRunnable() {

            @Override
            public void run(DatasetContext context) throws Exception {
                delegate.onFinish();
            }
        });
    } else {
        delegate.onFinish();
    }
    try {
        serviceContext.dismissTransactionContext();
    } finally {
        completed = true;
        contextReleaser.cancel();
    }
}
Also used : TxRunnable(co.cask.cdap.api.TxRunnable) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) DatasetContext(co.cask.cdap.api.data.DatasetContext)

Example 13 with TransactionControl

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

the class BodyProducerAdapter method handleError.

@Override
public void handleError(final Throwable throwable) {
    if (completed) {
        return;
    }
    // To the HttpContentProducer, if there is error, no other methods will be triggered
    completed = true;
    TransactionControl txCtrl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, HttpContentProducer.class, delegate, "onError", Throwable.class);
    try {
        if (TransactionControl.IMPLICIT == txCtrl) {
            serviceContext.execute(new TxRunnable() {

                @Override
                public void run(DatasetContext context) throws Exception {
                    delegate.onError(throwable);
                }
            });
        } else {
            delegate.onError(throwable);
        }
    } catch (Throwable t) {
        throwable.addSuppressed(t);
        // nothing much can be done. Simply emit a debug log.
        LOG.warn("Exception in calling HttpContentProducer.onError.", t);
    }
    try {
        serviceContext.dismissTransactionContext();
    } finally {
        contextReleaser.cancel();
    }
}
Also used : TxRunnable(co.cask.cdap.api.TxRunnable) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) DatasetContext(co.cask.cdap.api.data.DatasetContext)

Example 14 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) throws Exception {
    final TransactionControl txControl = spark instanceof ProgramLifecycle ? Transactions.getTransactionControl(TransactionControl.IMPLICIT, Spark.class, spark, "destroy") : TransactionControl.IMPLICIT;
    TxRunnable runnable = new TxRunnable() {

        @Override
        public void run(DatasetContext ctxt) throws Exception {
            Cancellable cancellable = SparkRuntimeUtils.setContextClassLoader(new SparkClassLoader(runtimeContext));
            try {
                context.setState(state);
                if (spark instanceof ProgramLifecycle) {
                    ((ProgramLifecycle) spark).destroy();
                } else {
                    spark.onFinish(state.getStatus() == ProgramStatus.COMPLETED, context);
                }
            } finally {
                cancellable.cancel();
            }
        }
    };
    if (TransactionControl.IMPLICIT == txControl) {
        context.execute(runnable);
    } else {
        runnable.run(context);
    }
}
Also used : ProgramLifecycle(co.cask.cdap.api.ProgramLifecycle) TxRunnable(co.cask.cdap.api.TxRunnable) Cancellable(org.apache.twill.common.Cancellable) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) AbstractSpark(co.cask.cdap.api.spark.AbstractSpark) Spark(co.cask.cdap.api.spark.Spark) DatasetContext(co.cask.cdap.api.data.DatasetContext)

Aggregations

TransactionControl (co.cask.cdap.api.annotation.TransactionControl)14 TxRunnable (co.cask.cdap.api.TxRunnable)8 DatasetContext (co.cask.cdap.api.data.DatasetContext)8 ProgramLifecycle (co.cask.cdap.api.ProgramLifecycle)6 ProgramState (co.cask.cdap.api.ProgramState)3 AbstractMapReduce (co.cask.cdap.api.mapreduce.AbstractMapReduce)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 WeakReferenceDelegatorClassLoader (co.cask.cdap.common.lang.WeakReferenceDelegatorClassLoader)2 MetricsFieldSetter (co.cask.cdap.internal.app.runtime.MetricsFieldSetter)2 ProvisionException (com.google.inject.ProvisionException)2 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 TransactionConflictException (org.apache.tephra.TransactionConflictException)2 TransactionFailureException (org.apache.tephra.TransactionFailureException)2 Cancellable (org.apache.twill.common.Cancellable)2 AbstractCustomAction (co.cask.cdap.api.customaction.AbstractCustomAction)1 CustomAction (co.cask.cdap.api.customaction.CustomAction)1 CustomActionContext (co.cask.cdap.api.customaction.CustomActionContext)1