use of io.cdap.cdap.internal.app.runtime.AbstractListener in project cdap by cdapio.
the class AbstractProgramRuntimeService method updateRuntimeInfo.
/**
* Updates the runtime info cache by adding the given {@link RuntimeInfo} if it does not exist.
*
* @param info information about the running program
*/
@VisibleForTesting
void updateRuntimeInfo(RuntimeInfo info) {
// Add the runtime info if it does not exist in the cache.
Lock lock = runtimeInfosLock.writeLock();
lock.lock();
try {
if (runtimeInfos.contains(info.getType(), info.getController().getRunId())) {
LOG.debug("RuntimeInfo already exists: {}", info.getController().getProgramRunId());
cleanupRuntimeInfo(info);
return;
}
runtimeInfos.put(info.getType(), info.getController().getRunId(), info);
} finally {
lock.unlock();
}
LOG.debug("Added RuntimeInfo: {}", info.getController().getProgramRunId());
ProgramController controller = info.getController();
controller.addListener(new AbstractListener() {
@Override
public void init(ProgramController.State currentState, @Nullable Throwable cause) {
if (COMPLETED_STATES.contains(currentState)) {
remove(info);
}
}
@Override
public void completed() {
remove(info);
}
@Override
public void killed() {
remove(info);
}
@Override
public void error(Throwable cause) {
remove(info);
}
}, Threads.SAME_THREAD_EXECUTOR);
}
use of io.cdap.cdap.internal.app.runtime.AbstractListener in project cdap by cdapio.
the class DefaultPreviewRunner method startPreview.
@Override
public Future<PreviewRequest> startPreview(PreviewRequest previewRequest) throws Exception {
ProgramId programId = previewRequest.getProgram();
long submitTimeMillis = RunIds.getTime(programId.getApplication(), TimeUnit.MILLISECONDS);
previewStarted(programId);
AppRequest<?> request = previewRequest.getAppRequest();
if (request == null) {
// This shouldn't happen
throw new IllegalStateException("Preview request shouldn't have an empty application request");
}
ArtifactSummary artifactSummary = request.getArtifact();
ApplicationId preview = programId.getParent();
try {
namespaceAdmin.create(new NamespaceMeta.Builder().setName(programId.getNamespaceId()).build());
} catch (NamespaceAlreadyExistsException e) {
LOG.debug("Namespace {} already exists.", programId.getNamespaceId());
}
DataTracerFactoryProvider.setDataTracerFactory(preview, dataTracerFactory);
String config = request.getConfig() == null ? null : GSON.toJson(request.getConfig());
PreviewConfig previewConfig = previewRequest.getAppRequest().getPreview();
PreferencesDetail preferences = preferencesFetcher.get(programId, true);
Map<String, String> userProps = new HashMap<>(preferences.getProperties());
if (previewConfig != null) {
userProps.putAll(previewConfig.getRuntimeArgs());
}
try {
LOG.debug("Deploying preview application for {}", programId);
applicationLifecycleService.deployApp(preview.getParent(), preview.getApplication(), preview.getVersion(), artifactSummary, config, NOOP_PROGRAM_TERMINATOR, null, request.canUpdateSchedules(), true, userProps);
} catch (Exception e) {
PreviewStatus previewStatus = new PreviewStatus(PreviewStatus.Status.DEPLOY_FAILED, submitTimeMillis, new BasicThrowable(e), null, null);
previewTerminated(programId, previewStatus);
throw e;
}
LOG.debug("Starting preview for {}", programId);
ProgramController controller = programLifecycleService.start(programId, userProps, false, true);
long startTimeMillis = System.currentTimeMillis();
AtomicBoolean timeout = new AtomicBoolean();
CompletableFuture<PreviewRequest> resultFuture = new CompletableFuture<>();
controller.addListener(new AbstractListener() {
@Override
public void init(ProgramController.State currentState, @Nullable Throwable cause) {
switch(currentState) {
case STARTING:
case ALIVE:
case STOPPING:
setStatus(programId, new PreviewStatus(PreviewStatus.Status.RUNNING, submitTimeMillis, null, startTimeMillis, null));
break;
case COMPLETED:
terminated(PreviewStatus.Status.COMPLETED, null);
break;
case KILLED:
terminated(PreviewStatus.Status.KILLED, null);
break;
case ERROR:
terminated(PreviewStatus.Status.RUN_FAILED, cause);
break;
}
}
@Override
public void completed() {
terminated(PreviewStatus.Status.COMPLETED, null);
}
@Override
public void killed() {
terminated(timeout.get() ? PreviewStatus.Status.KILLED_BY_TIMER : PreviewStatus.Status.KILLED, null);
}
@Override
public void error(Throwable cause) {
terminated(PreviewStatus.Status.RUN_FAILED, cause);
}
/**
* Handle termination of program run.
*
* @param status the termination status
* @param failureCause if the program was terminated due to error, this carries the failure cause
*/
private void terminated(PreviewStatus.Status status, @Nullable Throwable failureCause) {
PreviewStatus previewStatus = new PreviewStatus(status, submitTimeMillis, failureCause == null ? null : new BasicThrowable(failureCause), startTimeMillis, System.currentTimeMillis());
previewTerminated(programId, previewStatus);
if (failureCause == null) {
resultFuture.complete(previewRequest);
} else {
resultFuture.completeExceptionally(failureCause);
}
}
}, Threads.SAME_THREAD_EXECUTOR);
trackPreviewTimeout(previewRequest, timeout, resultFuture);
PreviewMessage message = new PreviewMessage(PreviewMessage.Type.PROGRAM_RUN_ID, programId.getParent(), GSON.toJsonTree(controller.getProgramRunId()));
previewDataPublisher.publish(programId.getParent(), message);
return resultFuture;
}
use of io.cdap.cdap.internal.app.runtime.AbstractListener in project cdap by cdapio.
the class MapReduceRunnerTestBase method waitForCompletion.
private boolean waitForCompletion(ProgramController controller) throws InterruptedException {
final AtomicBoolean success = new AtomicBoolean(false);
final CountDownLatch completion = new CountDownLatch(1);
controller.addListener(new AbstractListener() {
@Override
public void completed() {
success.set(true);
completion.countDown();
}
@Override
public void error(Throwable cause) {
completion.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
// MR tests can run for long time.
completion.await(10, TimeUnit.MINUTES);
return success.get();
}
use of io.cdap.cdap.internal.app.runtime.AbstractListener in project cdap by caskdata.
the class MapReduceRunnerTestBase method waitForCompletion.
private boolean waitForCompletion(ProgramController controller) throws InterruptedException {
final AtomicBoolean success = new AtomicBoolean(false);
final CountDownLatch completion = new CountDownLatch(1);
controller.addListener(new AbstractListener() {
@Override
public void completed() {
success.set(true);
completion.countDown();
}
@Override
public void error(Throwable cause) {
completion.countDown();
}
}, Threads.SAME_THREAD_EXECUTOR);
// MR tests can run for long time.
completion.await(10, TimeUnit.MINUTES);
return success.get();
}
use of io.cdap.cdap.internal.app.runtime.AbstractListener in project cdap by caskdata.
the class WorkflowTest method testOneActionWorkflow.
@Test(timeout = 120 * 1000L)
public void testOneActionWorkflow() throws Exception {
final ApplicationWithPrograms app = AppFabricTestHelper.deployApplicationWithManager(OneActionWorkflowApp.class, TEMP_FOLDER_SUPPLIER);
final Injector injector = AppFabricTestHelper.getInjector();
final ProgramDescriptor programDescriptor = Iterators.filter(app.getPrograms().iterator(), input -> input.getProgramId().getType() == ProgramType.WORKFLOW).next();
final SettableFuture<String> completion = SettableFuture.create();
final ProgramController controller = AppFabricTestHelper.submit(app, programDescriptor.getSpecification().getClassName(), new BasicArguments(), TEMP_FOLDER_SUPPLIER);
controller.addListener(new AbstractListener() {
@Override
public void init(ProgramController.State currentState, @Nullable Throwable cause) {
LOG.info("Initializing");
ArtifactId artifactId = controller.getProgramRunId().getNamespaceId().artifact("test", "1.0").toApiArtifactId();
setStartAndRunning(injector.getInstance(Store.class), controller.getProgramRunId().getParent(), controller.getProgramRunId().getRun(), artifactId);
}
@Override
public void completed() {
LOG.info("Completed");
completion.set("Completed");
}
@Override
public void error(Throwable cause) {
LOG.info("Error", cause);
completion.setException(cause);
}
}, Threads.SAME_THREAD_EXECUTOR);
String run = completion.get();
Assert.assertEquals("Completed", run);
}
Aggregations