Search in sources :

Example 1 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class DefaultCheckpointManager method saveCheckpoints.

@Override
public void saveCheckpoints(final Map<Integer, ? extends Checkpoint> checkpoints) throws Exception {
    // if the checkpoints have not changed, we skip writing to table and return.
    if (lastCheckpoint.equals(checkpoints)) {
        return;
    }
    try {
        lastCheckpoint = Transactions.execute(transactional, new TxCallable<Map<Integer, Checkpoint>>() {

            @Override
            public Map<Integer, Checkpoint> call(DatasetContext context) throws Exception {
                Map<Integer, Checkpoint> result = new HashMap<>();
                Table table = getCheckpointTable(context);
                for (Map.Entry<Integer, ? extends Checkpoint> entry : checkpoints.entrySet()) {
                    byte[] key = Bytes.add(rowKeyPrefix, Bytes.toBytes(entry.getKey()));
                    Checkpoint checkpoint = entry.getValue();
                    table.put(key, OFFSET_COL_NAME, Bytes.toBytes(checkpoint.getNextOffset()));
                    table.put(key, NEXT_TIME_COL_NAME, Bytes.toBytes(checkpoint.getNextEventTime()));
                    table.put(key, MAX_TIME_COL_NAME, Bytes.toBytes(checkpoint.getMaxEventTime()));
                    result.put(entry.getKey(), new Checkpoint(checkpoint.getNextOffset(), checkpoint.getNextEventTime(), checkpoint.getMaxEventTime()));
                }
                return result;
            }
        });
    } catch (TransactionFailureException e) {
        throw Transactions.propagate(e, ServiceUnavailableException.class);
    }
    LOG.trace("Saved checkpoints for partitions {}", checkpoints);
}
Also used : TransactionFailureException(org.apache.tephra.TransactionFailureException) Table(co.cask.cdap.api.dataset.table.Table) HashMap(java.util.HashMap) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) DatasetContext(co.cask.cdap.api.data.DatasetContext) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) TxCallable(co.cask.cdap.data2.transaction.TxCallable)

Example 2 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class BodyConsumerAdapter method onError.

/**
   * Calls the {@link HttpContentConsumer#onError(HttpServiceResponder, Throwable)} method from a transaction.
   */
private void onError(final Throwable cause, final DelayedHttpServiceResponder responder) {
    if (completed) {
        return;
    }
    // To the HttpContentConsumer, once onError is called, no other methods will be triggered
    completed = true;
    TransactionControl txCtrl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, HttpContentConsumer.class, delegate, "onError", HttpServiceResponder.class, Throwable.class);
    try {
        if (TransactionControl.IMPLICIT == txCtrl) {
            transactional.execute(new TxRunnable() {

                @Override
                public void run(DatasetContext context) throws Exception {
                    delegate.onError(responder, cause);
                }
            });
        } else {
            delegate.onError(responder, cause);
        }
    } catch (Throwable t) {
        responder.setTransactionFailureResponse(t);
        LOG.warn("Exception in calling HttpContentConsumer.onError", t);
    } finally {
        try {
            responder.execute(false);
        } finally {
            if (!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 3 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class Transactions method execute.

/**
   * Executes the given {@link TxCallable} using the given {@link Transactional}.
   *
   * @param transactional the {@link Transactional} to use for transactional execution.
   * @param callable the {@link TxCallable} to be executed inside a transaction
   * @param <V> type of the result
   * @return value returned by the given {@link TxCallable}.
   * @throws TransactionFailureException if failed to execute the given {@link TxRunnable} in a transaction
   *
   * TODO: CDAP-6103 Move this to {@link Transactional} when revamping tx supports in program.
   */
public static <V> V execute(Transactional transactional, final TxCallable<V> callable) throws TransactionFailureException {
    final AtomicReference<V> result = new AtomicReference<>();
    transactional.execute(new TxRunnable() {

        @Override
        public void run(DatasetContext context) throws Exception {
            result.set(callable.call(context));
        }
    });
    return result.get();
}
Also used : TxRunnable(co.cask.cdap.api.TxRunnable) AtomicReference(java.util.concurrent.atomic.AtomicReference) DatasetContext(co.cask.cdap.api.data.DatasetContext) TransactionFailureException(org.apache.tephra.TransactionFailureException)

Example 4 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class DefaultStore method setStop.

@Override
public void setStop(final ProgramId id, final String pid, final long endTime, final ProgramRunStatus runStatus, final BasicThrowable failureCause) {
    Preconditions.checkArgument(runStatus != null, "Run state of program run should be defined");
    Transactions.executeUnchecked(transactional, new TxRunnable() {

        @Override
        public void run(DatasetContext context) throws Exception {
            AppMetadataStore metaStore = getAppMetadataStore(context);
            metaStore.recordProgramStop(id, pid, endTime, runStatus, failureCause);
            // This block has been added so that completed workflow runs can be logged to the workflow dataset
            WorkflowId workflowId = new WorkflowId(id.getParent(), id.getProgram());
            if (id.getType() == ProgramType.WORKFLOW && runStatus == ProgramRunStatus.COMPLETED) {
                recordCompletedWorkflow(metaStore, getWorkflowDataset(context), workflowId, pid);
            }
        // todo: delete old history data
        }
    });
}
Also used : TxRunnable(co.cask.cdap.api.TxRunnable) DatasetContext(co.cask.cdap.api.data.DatasetContext) WorkflowId(co.cask.cdap.proto.id.WorkflowId) TransactionFailureException(org.apache.tephra.TransactionFailureException) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) TransactionNotInProgressException(org.apache.tephra.TransactionNotInProgressException) TransactionConflictException(org.apache.tephra.TransactionConflictException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Example 5 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext 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;
        }
    });
}
Also used : BasicThrowable(co.cask.cdap.proto.BasicThrowable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DatasetContext(co.cask.cdap.api.data.DatasetContext) BasicThrowable(co.cask.cdap.proto.BasicThrowable) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) TransactionFailureException(org.apache.tephra.TransactionFailureException) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) TransactionNotInProgressException(org.apache.tephra.TransactionNotInProgressException) TransactionConflictException(org.apache.tephra.TransactionConflictException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Aggregations

DatasetContext (co.cask.cdap.api.data.DatasetContext)36 TxRunnable (co.cask.cdap.api.TxRunnable)33 IOException (java.io.IOException)12 TransactionFailureException (org.apache.tephra.TransactionFailureException)12 Table (co.cask.cdap.api.dataset.table.Table)7 TransactionConflictException (org.apache.tephra.TransactionConflictException)7 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)6 TransactionControl (co.cask.cdap.api.annotation.TransactionControl)5 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)5 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)5 PipelineRuntime (co.cask.cdap.etl.common.PipelineRuntime)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 NoSuchElementException (java.util.NoSuchElementException)5 TransactionNotInProgressException (org.apache.tephra.TransactionNotInProgressException)5 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)4 Put (co.cask.cdap.api.dataset.table.Put)4 SparkExecutionPluginContext (co.cask.cdap.etl.api.batch.SparkExecutionPluginContext)4 ArrayList (java.util.ArrayList)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4