Search in sources :

Example 6 with TransactionControl

use of io.cdap.cdap.api.annotation.TransactionControl in project cdap by cdapio.

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 defaultTxControl = context.getDefaultTxControl();
    TransactionControl txControl = mapReduce instanceof AbstractMapReduce ? Transactions.getTransactionControl(defaultTxControl, AbstractMapReduce.class, mapReduce, "initialize") : mapReduce instanceof ProgramLifecycle ? Transactions.getTransactionControl(defaultTxControl, MapReduce.class, mapReduce, "initialize", MapReduceContext.class) : defaultTxControl;
    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(io.cdap.cdap.api.ProgramLifecycle) AbstractMapReduce(io.cdap.cdap.api.mapreduce.AbstractMapReduce) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) ProgramState(io.cdap.cdap.api.ProgramState)

Example 7 with TransactionControl

use of io.cdap.cdap.api.annotation.TransactionControl in project cdap by cdapio.

the class SparkRuntimeService method initialize.

/**
 * Calls the {@link Spark#beforeSubmit(SparkClientContext)} for the pre 3.5 Spark programs, calls
 * the {@link ProgramLifecycle#initialize} otherwise.
 */
@SuppressWarnings("unchecked")
private void initialize() throws Exception {
    context.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
    // AbstractSpark implements final initialize(context) and requires subclass to
    // implement initialize(), whereas programs that directly implement Spark have
    // the option to override initialize(context) (if they implement ProgramLifeCycle)
    TransactionControl defaultTxControl = runtimeContext.getDefaultTxControl();
    final TransactionControl txControl = spark instanceof AbstractSpark ? Transactions.getTransactionControl(defaultTxControl, AbstractSpark.class, spark, "initialize") : spark instanceof ProgramLifecycle ? Transactions.getTransactionControl(defaultTxControl, Spark.class, spark, "initialize", SparkClientContext.class) : defaultTxControl;
    runtimeContext.initializeProgram(programLifecycle, txControl, false);
}
Also used : ProgramLifecycle(io.cdap.cdap.api.ProgramLifecycle) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) ProgramState(io.cdap.cdap.api.ProgramState) AbstractSpark(io.cdap.cdap.api.spark.AbstractSpark)

Example 8 with TransactionControl

use of io.cdap.cdap.api.annotation.TransactionControl in project cdap by cdapio.

the class SparkRuntimeService method destroy.

/**
 * Calls the destroy or onFinish method of {@link ProgramLifecycle}.
 */
private void destroy(final ProgramState state) {
    context.setState(state);
    TransactionControl defaultTxControl = runtimeContext.getDefaultTxControl();
    TransactionControl txControl = spark instanceof ProgramLifecycle ? Transactions.getTransactionControl(defaultTxControl, Spark.class, spark, "destroy") : defaultTxControl;
    runtimeContext.destroyProgram(programLifecycle, txControl, false);
    if (emitFieldLineage()) {
        try {
            // here we cannot call context.flushRecord() since the WorkflowNodeState will need to record and store
            // the lineage information
            FieldLineageInfo info = new FieldLineageInfo(runtimeContext.getFieldLineageOperations());
            fieldLineageWriter.write(runtimeContext.getProgramRunId(), info);
        } catch (Throwable t) {
            LOG.warn("Failed to emit the field lineage operations for Spark {}", runtimeContext.getProgramRunId(), t);
        }
    }
}
Also used : ProgramLifecycle(io.cdap.cdap.api.ProgramLifecycle) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) Spark(io.cdap.cdap.api.spark.Spark) AbstractSpark(io.cdap.cdap.api.spark.AbstractSpark) FieldLineageInfo(io.cdap.cdap.data2.metadata.lineage.field.FieldLineageInfo)

Example 9 with TransactionControl

use of io.cdap.cdap.api.annotation.TransactionControl in project cdap by cdapio.

the class ServiceHttpServer method destroyService.

@Override
protected void destroyService() throws Exception {
    super.destroyService();
    if (service == null) {
        return;
    }
    // Service is always using Explicit transaction
    TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.EXPLICIT, Service.class, service, "destroy");
    serviceContext.destroyProgram(service, txControl, false);
    serviceContext.close();
}
Also used : TransactionControl(io.cdap.cdap.api.annotation.TransactionControl)

Example 10 with TransactionControl

use of io.cdap.cdap.api.annotation.TransactionControl in project cdap by cdapio.

the class ServiceHttpServer method initializeService.

@Override
protected void initializeService() throws Exception {
    super.initializeService();
    // Instantiate service instance
    Class<?> serviceClass = program.getClassLoader().loadClass(serviceSpecification.getClassName());
    @SuppressWarnings("unchecked") TypeToken<Service> serviceType = (TypeToken<Service>) TypeToken.of(serviceClass);
    service = new InstantiatorFactory(false).get(serviceType).create();
    // Initialize service
    // Service is always using Explicit transaction
    TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.EXPLICIT, Service.class, service, "initialize", ServiceContext.class);
    serviceContext.initializeProgram(service, txControl, false);
}
Also used : InstantiatorFactory(io.cdap.cdap.common.lang.InstantiatorFactory) TypeToken(com.google.common.reflect.TypeToken) TransactionControl(io.cdap.cdap.api.annotation.TransactionControl) AbstractSystemService(io.cdap.cdap.api.service.AbstractSystemService) NettyHttpService(io.cdap.http.NettyHttpService) Service(io.cdap.cdap.api.service.Service) MessagingService(io.cdap.cdap.messaging.MessagingService) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService)

Aggregations

TransactionControl (io.cdap.cdap.api.annotation.TransactionControl)28 ProgramLifecycle (io.cdap.cdap.api.ProgramLifecycle)12 ProgramState (io.cdap.cdap.api.ProgramState)8 InstantiatorFactory (io.cdap.cdap.common.lang.InstantiatorFactory)6 MetricsFieldSetter (io.cdap.cdap.internal.app.runtime.MetricsFieldSetter)6 TypeToken (com.google.common.reflect.TypeToken)4 AbstractMapReduce (io.cdap.cdap.api.mapreduce.AbstractMapReduce)4 AbstractSpark (io.cdap.cdap.api.spark.AbstractSpark)4 PropertyFieldSetter (io.cdap.cdap.common.lang.PropertyFieldSetter)4 FieldLineageInfo (io.cdap.cdap.data2.metadata.lineage.field.FieldLineageInfo)4 Preconditions (com.google.common.base.Preconditions)2 AbstractCustomAction (io.cdap.cdap.api.customaction.AbstractCustomAction)2 CustomAction (io.cdap.cdap.api.customaction.CustomAction)2 CustomActionContext (io.cdap.cdap.api.customaction.CustomActionContext)2 MapReduce (io.cdap.cdap.api.mapreduce.MapReduce)2 MetricsCollectionService (io.cdap.cdap.api.metrics.MetricsCollectionService)2 MetricsContext (io.cdap.cdap.api.metrics.MetricsContext)2 AbstractSystemService (io.cdap.cdap.api.service.AbstractSystemService)2 Service (io.cdap.cdap.api.service.Service)2 HttpContentConsumer (io.cdap.cdap.api.service.http.HttpContentConsumer)2