use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.
the class AbstractContext method execute.
/**
* Execute in a transaction with optional retry on conflict.
*/
public void execute(final TxRunnable runnable, boolean retryOnConflict) throws TransactionFailureException {
ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(getClass().getClassLoader());
try {
Transactional txnl = retryOnConflict ? Transactions.createTransactionalWithRetry(transactional, RetryStrategies.retryOnConflict(20, 100)) : transactional;
txnl.execute(new TxRunnable() {
@Override
public void run(DatasetContext context) throws Exception {
ClassLoader oldClassLoader = setContextCombinedClassLoader();
try {
runnable.run(context);
} finally {
ClassLoaders.setContextClassLoader(oldClassLoader);
}
}
});
} finally {
ClassLoaders.setContextClassLoader(oldClassLoader);
}
}
use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.
the class DefaultStore method getDeletedProgramSpecifications.
// todo: this method should be moved into DeletedProgramHandlerState, bad design otherwise
@Override
public List<ProgramSpecification> getDeletedProgramSpecifications(final ApplicationId id, ApplicationSpecification appSpec) {
ApplicationMeta existing = Transactions.executeUnchecked(transactional, new TxCallable<ApplicationMeta>() {
@Override
public ApplicationMeta call(DatasetContext context) throws Exception {
return getAppMetadataStore(context).getApplication(id.getNamespace(), id.getApplication(), id.getVersion());
}
});
List<ProgramSpecification> deletedProgramSpecs = Lists.newArrayList();
if (existing != null) {
ApplicationSpecification existingAppSpec = existing.getSpec();
Map<String, ProgramSpecification> existingSpec = ImmutableMap.<String, ProgramSpecification>builder().putAll(existingAppSpec.getMapReduce()).putAll(existingAppSpec.getSpark()).putAll(existingAppSpec.getWorkflows()).putAll(existingAppSpec.getFlows()).putAll(existingAppSpec.getServices()).putAll(existingAppSpec.getWorkers()).build();
Map<String, ProgramSpecification> newSpec = ImmutableMap.<String, ProgramSpecification>builder().putAll(appSpec.getMapReduce()).putAll(appSpec.getSpark()).putAll(appSpec.getWorkflows()).putAll(appSpec.getFlows()).putAll(appSpec.getServices()).putAll(appSpec.getWorkers()).build();
MapDifference<String, ProgramSpecification> mapDiff = Maps.difference(existingSpec, newSpec);
deletedProgramSpecs.addAll(mapDiff.entriesOnlyOnLeft().values());
}
return deletedProgramSpecs;
}
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 setFlowletInstances.
@Override
public FlowSpecification setFlowletInstances(final ProgramId id, final String flowletId, final int count) {
Preconditions.checkArgument(count > 0, "Cannot change number of flowlet instances to %s", count);
LOG.trace("Setting flowlet instances: namespace: {}, application: {}, flow: {}, flowlet: {}, " + "new instances count: {}", id.getNamespace(), id.getApplication(), id.getProgram(), flowletId, count);
FlowSpecification flowSpec = Transactions.executeUnchecked(transactional, new TxCallable<FlowSpecification>() {
@Override
public FlowSpecification call(DatasetContext context) throws Exception {
AppMetadataStore metaStore = getAppMetadataStore(context);
ApplicationSpecification appSpec = getAppSpecOrFail(metaStore, id);
ApplicationSpecification newAppSpec = updateFlowletInstancesInAppSpec(appSpec, id, flowletId, count);
metaStore.updateAppSpec(id.getNamespace(), id.getApplication(), id.getVersion(), newAppSpec);
return appSpec.getFlows().get(id.getProgram());
}
});
LOG.trace("Set flowlet instances: namespace: {}, application: {}, flow: {}, flowlet: {}, instances now: {}", id.getNamespaceId(), id.getApplication(), id.getProgram(), flowletId, count);
return flowSpec;
}
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