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);
}
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();
}
}
}
}
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();
}
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
}
});
}
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;
}
});
}
Aggregations