use of org.apache.twill.api.RunId in project cdap by caskdata.
the class DefaultStoreTest method testConcurrentStopStart.
@Test
public void testConcurrentStopStart() throws Exception {
// Two programs that start/stop at same time
// Should have two run history.
ProgramId programId = new ProgramId("account1", "concurrentApp", ProgramType.FLOW, "concurrentFlow");
long now = System.currentTimeMillis();
long nowSecs = TimeUnit.MILLISECONDS.toSeconds(now);
RunId run1 = RunIds.generate(now - 10000);
store.setStart(programId, run1.getId(), runIdToSecs(run1));
RunId run2 = RunIds.generate(now - 10000);
store.setStart(programId, run2.getId(), runIdToSecs(run2));
store.setStop(programId, run1.getId(), nowSecs, ProgramController.State.COMPLETED.getRunStatus());
store.setStop(programId, run2.getId(), nowSecs, ProgramController.State.COMPLETED.getRunStatus());
Map<ProgramRunId, RunRecordMeta> historymap = store.getRuns(programId, ProgramRunStatus.ALL, 0, Long.MAX_VALUE, Integer.MAX_VALUE);
Assert.assertEquals(2, historymap.size());
}
use of org.apache.twill.api.RunId in project cdap by caskdata.
the class DefaultStoreTest method testHistoryDeletion.
@Test
public void testHistoryDeletion() throws Exception {
// Deploy two apps, write some history for programs
// Remove application using accountId, AppId and verify
// Remove all from accountId and verify
ApplicationSpecification spec = Specifications.from(new AllProgramsApp());
NamespaceId namespaceId = new NamespaceId("testDeleteAll");
ApplicationId appId1 = namespaceId.app(spec.getName());
store.addApplication(appId1, spec);
spec = Specifications.from(new WordCountApp());
ApplicationId appId2 = namespaceId.app(spec.getName());
store.addApplication(appId2, spec);
ProgramId flowProgramId1 = appId1.flow("NoOpFlow");
ProgramId mapreduceProgramId1 = appId1.mr("NoOpMR");
ProgramId workflowProgramId1 = appId1.workflow("NoOpWorkflow");
ProgramId flowProgramId2 = appId2.flow("WordCountFlow");
Assert.assertNotNull(store.getApplication(appId1));
Assert.assertNotNull(store.getApplication(appId2));
long now = System.currentTimeMillis();
store.setStart(flowProgramId1, "flowRun1", now - 1000);
store.setStop(flowProgramId1, "flowRun1", now, ProgramController.State.COMPLETED.getRunStatus());
store.setStart(mapreduceProgramId1, "mrRun1", now - 1000);
store.setStop(mapreduceProgramId1, "mrRun1", now, ProgramController.State.COMPLETED.getRunStatus());
RunId runId = RunIds.generate(System.currentTimeMillis() - TimeUnit.SECONDS.toMillis(1000));
store.setStart(workflowProgramId1, runId.getId(), now - 1000);
store.setStop(workflowProgramId1, runId.getId(), now, ProgramController.State.COMPLETED.getRunStatus());
store.setStart(flowProgramId2, "flowRun2", now - 1000);
store.setStop(flowProgramId2, "flowRun2", now, ProgramController.State.COMPLETED.getRunStatus());
verifyRunHistory(flowProgramId1, 1);
verifyRunHistory(mapreduceProgramId1, 1);
verifyRunHistory(workflowProgramId1, 1);
verifyRunHistory(flowProgramId2, 1);
// removing application
store.removeApplication(appId1);
Assert.assertNull(store.getApplication(appId1));
Assert.assertNotNull(store.getApplication(appId2));
verifyRunHistory(flowProgramId1, 0);
verifyRunHistory(mapreduceProgramId1, 0);
verifyRunHistory(workflowProgramId1, 0);
// Check to see if the flow history of second app is not deleted
verifyRunHistory(flowProgramId2, 1);
// remove all
store.removeAll(namespaceId);
verifyRunHistory(flowProgramId2, 0);
}
use of org.apache.twill.api.RunId in project cdap by caskdata.
the class DefaultStoreTest method testCompareAndSetStatus.
@Test
public void testCompareAndSetStatus() throws Exception {
ProgramId programId = NamespaceId.DEFAULT.app("app1").flow("flow1");
long now = System.currentTimeMillis();
RunId run1 = RunIds.generate(now - 20000);
store.setStart(programId, run1.getId(), runIdToSecs(run1));
// this should succeed, because the program status is as expected (RUNNING)
Assert.assertTrue(store.compareAndSetStatus(programId, run1.getId(), ProgramRunStatus.RUNNING, ProgramRunStatus.SUSPENDED));
Assert.assertEquals(ProgramRunStatus.SUSPENDED, store.getRun(programId, run1.getId()).getStatus());
// this will not change the status of the program, because the expected is no longer RUNNING
Assert.assertFalse(store.compareAndSetStatus(programId, run1.getId(), ProgramRunStatus.RUNNING, ProgramRunStatus.FAILED));
Assert.assertEquals(ProgramRunStatus.SUSPENDED, store.getRun(programId, run1.getId()).getStatus());
}
use of org.apache.twill.api.RunId in project cdap by caskdata.
the class WorkerProgramRunner method run.
@Override
public ProgramController run(Program program, ProgramOptions options) {
ApplicationSpecification appSpec = program.getApplicationSpecification();
Preconditions.checkNotNull(appSpec, "Missing application specification.");
int instanceId = Integer.parseInt(options.getArguments().getOption(ProgramOptionConstants.INSTANCE_ID, "-1"));
Preconditions.checkArgument(instanceId >= 0, "Missing instance Id");
int instanceCount = Integer.parseInt(options.getArguments().getOption(ProgramOptionConstants.INSTANCES, "0"));
Preconditions.checkArgument(instanceCount > 0, "Invalid or missing instance count");
RunId runId = ProgramRunners.getRunId(options);
ProgramType programType = program.getType();
Preconditions.checkNotNull(programType, "Missing processor type.");
Preconditions.checkArgument(programType == ProgramType.WORKER, "Only Worker process type is supported.");
WorkerSpecification workerSpec = appSpec.getWorkers().get(program.getName());
Preconditions.checkArgument(workerSpec != null, "Missing Worker specification for %s", program.getId());
String instances = options.getArguments().getOption(ProgramOptionConstants.INSTANCES, String.valueOf(workerSpec.getInstances()));
WorkerSpecification newWorkerSpec = new WorkerSpecification(workerSpec.getClassName(), workerSpec.getName(), workerSpec.getDescription(), workerSpec.getProperties(), workerSpec.getDatasets(), workerSpec.getResources(), Integer.valueOf(instances));
// Setup dataset framework context, if required
if (datasetFramework instanceof ProgramContextAware) {
ProgramId programId = program.getId();
((ProgramContextAware) datasetFramework).setContext(new BasicProgramContext(programId.run(runId)));
}
final PluginInstantiator pluginInstantiator = createPluginInstantiator(options, program.getClassLoader());
try {
BasicWorkerContext context = new BasicWorkerContext(newWorkerSpec, program, options, cConf, instanceId, instanceCount, metricsCollectionService, datasetFramework, txClient, discoveryServiceClient, streamWriterFactory, pluginInstantiator, secureStore, secureStoreManager, messagingService);
WorkerDriver worker = new WorkerDriver(program, newWorkerSpec, context);
// Add a service listener to make sure the plugin instantiator is closed when the worker driver finished.
worker.addListener(new ServiceListenerAdapter() {
@Override
public void terminated(Service.State from) {
Closeables.closeQuietly(pluginInstantiator);
}
@Override
public void failed(Service.State from, Throwable failure) {
Closeables.closeQuietly(pluginInstantiator);
}
}, Threads.SAME_THREAD_EXECUTOR);
ProgramController controller = new WorkerControllerServiceAdapter(worker, program.getId(), runId, workerSpec.getName() + "-" + instanceId);
worker.start();
return controller;
} catch (Throwable t) {
Closeables.closeQuietly(pluginInstantiator);
throw t;
}
}
use of org.apache.twill.api.RunId in project cdap by caskdata.
the class ProgramLifecycleService method findRuntimeInfo.
private List<ProgramRuntimeService.RuntimeInfo> findRuntimeInfo(ProgramId programId, @Nullable String runId) throws BadRequestException {
if (runId != null) {
RunId run;
try {
run = RunIds.fromString(runId);
} catch (IllegalArgumentException e) {
throw new BadRequestException("Error parsing run-id.", e);
}
ProgramRuntimeService.RuntimeInfo runtimeInfo = runtimeService.lookup(programId, run);
return runtimeInfo == null ? Collections.<RuntimeInfo>emptyList() : Collections.singletonList(runtimeInfo);
}
return new ArrayList<>(runtimeService.list(programId).values());
}
Aggregations