Search in sources :

Example 1 with Program

use of io.cdap.cdap.app.program.Program in project cdap by caskdata.

the class AbstractProgramRuntimeService method run.

@Override
public final RuntimeInfo run(ProgramDescriptor programDescriptor, ProgramOptions options, RunId runId) {
    ProgramId programId = programDescriptor.getProgramId();
    ProgramRunId programRunId = programId.run(runId);
    ClusterMode clusterMode = ProgramRunners.getClusterMode(options);
    // Creates the ProgramRunner based on the cluster mode
    ProgramRunner runner = (clusterMode == ClusterMode.ON_PREMISE ? programRunnerFactory : Optional.ofNullable(remoteProgramRunnerFactory).orElseThrow(UnsupportedOperationException::new)).create(programId.getType());
    File tempDir = createTempDirectory(programId, runId);
    AtomicReference<Runnable> cleanUpTaskRef = new AtomicReference<>(createCleanupTask(tempDir, runner));
    DelayedProgramController controller = new DelayedProgramController(programRunId);
    RuntimeInfo runtimeInfo = createRuntimeInfo(controller, programId, () -> cleanUpTaskRef.get().run());
    updateRuntimeInfo(runtimeInfo);
    executor.execute(() -> {
        try {
            // Get the artifact details and save it into the program options.
            ArtifactId artifactId = programDescriptor.getArtifactId();
            ArtifactDetail artifactDetail = getArtifactDetail(artifactId);
            ApplicationSpecification appSpec = programDescriptor.getApplicationSpecification();
            ProgramDescriptor newProgramDescriptor = programDescriptor;
            boolean isPreview = Boolean.valueOf(options.getArguments().getOption(ProgramOptionConstants.IS_PREVIEW, "false"));
            // for preview we already have a resolved app spec, so no need to regenerate the app spec again
            if (!isPreview && appSpec != null && ClusterMode.ON_PREMISE.equals(clusterMode)) {
                try {
                    ApplicationSpecification generatedAppSpec = regenerateAppSpec(artifactDetail, programId, artifactId, appSpec, options);
                    appSpec = generatedAppSpec != null ? generatedAppSpec : appSpec;
                    newProgramDescriptor = new ProgramDescriptor(programDescriptor.getProgramId(), appSpec);
                } catch (Exception e) {
                    LOG.warn("Failed to regenerate the app spec for program {}, using the existing app spec", programId);
                }
            }
            ProgramOptions runtimeProgramOptions = updateProgramOptions(artifactId, programId, options, runId, clusterMode, Iterables.getFirst(artifactDetail.getMeta().getClasses().getApps(), null));
            // Take a snapshot of all the plugin artifacts used by the program
            ProgramOptions optionsWithPlugins = createPluginSnapshot(runtimeProgramOptions, programId, tempDir, newProgramDescriptor.getApplicationSpecification());
            // Create and run the program
            Program executableProgram = createProgram(cConf, runner, newProgramDescriptor, artifactDetail, tempDir);
            cleanUpTaskRef.set(createCleanupTask(cleanUpTaskRef.get(), executableProgram));
            controller.setProgramController(runner.run(executableProgram, optionsWithPlugins));
        } catch (Exception e) {
            controller.failed(e);
            programStateWriter.error(programRunId, e);
            LOG.error("Exception while trying to run program", e);
        }
    });
    return runtimeInfo;
}
Also used : ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) Program(io.cdap.cdap.app.program.Program) AppDeploymentRuntimeInfo(io.cdap.cdap.internal.app.deploy.pipeline.AppDeploymentRuntimeInfo) SimpleRuntimeInfo(io.cdap.cdap.internal.app.runtime.service.SimpleRuntimeInfo) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ClusterMode(io.cdap.cdap.app.guice.ClusterMode) AtomicReference(java.util.concurrent.atomic.AtomicReference) ProgramId(io.cdap.cdap.proto.id.ProgramId) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) ExecutionException(java.util.concurrent.ExecutionException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ProgramDescriptor(io.cdap.cdap.app.program.ProgramDescriptor) File(java.io.File) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)

Example 2 with Program

use of io.cdap.cdap.app.program.Program in project cdap by caskdata.

the class AbstractProgramRuntimeService method list.

@Override
public Map<RunId, RuntimeInfo> list(ProgramType type) {
    Map<RunId, RuntimeInfo> result = new HashMap<>();
    Lock lock = runtimeInfosLock.readLock();
    lock.lock();
    try {
        result.putAll(runtimeInfos.row(type));
    } finally {
        lock.unlock();
    }
    // Add any missing RuntimeInfo from the remote twill runner
    if (remoteTwillRunnerService == null) {
        return Collections.unmodifiableMap(result);
    }
    for (TwillRunner.LiveInfo liveInfo : remoteTwillRunnerService.lookupLive()) {
        ProgramId programId = TwillAppNames.fromTwillAppName(liveInfo.getApplicationName(), false);
        if (programId == null || !programId.getType().equals(type)) {
            continue;
        }
        for (TwillController controller : liveInfo.getControllers()) {
            // For remote twill runner, the twill run id and cdap run id are the same
            RunId runId = controller.getRunId();
            if (result.computeIfAbsent(runId, rid -> createRuntimeInfo(programId, runId, controller)) == null) {
                LOG.warn("Unable to create runtime info for program {} with run id {}", programId, runId);
            }
        }
    }
    return Collections.unmodifiableMap(result);
}
Also used : TwillController(org.apache.twill.api.TwillController) ConfigResponse(io.cdap.cdap.app.deploy.ConfigResponse) Arrays(java.util.Arrays) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) ClusterMode(io.cdap.cdap.app.guice.ClusterMode) TimeoutException(java.util.concurrent.TimeoutException) HashBasedTable(com.google.common.collect.HashBasedTable) TwillAppNames(io.cdap.cdap.common.twill.TwillAppNames) AppDeploymentRuntimeInfo(io.cdap.cdap.internal.app.deploy.pipeline.AppDeploymentRuntimeInfo) ResourceReport(org.apache.twill.api.ResourceReport) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) Map(java.util.Map) Closeables(com.google.common.io.Closeables) RunId(org.apache.twill.api.RunId) EnumSet(java.util.EnumSet) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) TwillController(org.apache.twill.api.TwillController) Threads(org.apache.twill.common.Threads) Artifacts(io.cdap.cdap.internal.app.runtime.artifact.Artifacts) ImmutableMap(com.google.common.collect.ImmutableMap) ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) Set(java.util.Set) InMemoryProgramLiveInfo(io.cdap.cdap.proto.InMemoryProgramLiveInfo) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) AppDeploymentInfo(io.cdap.cdap.internal.app.deploy.pipeline.AppDeploymentInfo) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Sets(com.google.common.collect.Sets) Id(io.cdap.cdap.common.id.Id) List(java.util.List) Optional(java.util.Optional) Constants(io.cdap.cdap.common.conf.Constants) ClassLoaderFolder(io.cdap.cdap.common.lang.jar.ClassLoaderFolder) ProgramOptionConstants(io.cdap.cdap.internal.app.runtime.ProgramOptionConstants) Configurator(io.cdap.cdap.app.deploy.Configurator) DirUtils(io.cdap.cdap.common.utils.DirUtils) AppSpecInfo(io.cdap.cdap.internal.app.deploy.pipeline.AppSpecInfo) Joiner(com.google.common.base.Joiner) ProgramRunners(io.cdap.cdap.internal.app.runtime.ProgramRunners) ThreadFactoryBuilder(com.google.common.util.concurrent.ThreadFactoryBuilder) Iterables(com.google.common.collect.Iterables) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Location(org.apache.twill.filesystem.Location) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) SimpleRuntimeInfo(io.cdap.cdap.internal.app.runtime.service.SimpleRuntimeInfo) Program(io.cdap.cdap.app.program.Program) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Programs(io.cdap.cdap.app.program.Programs) ProgramType(io.cdap.cdap.proto.ProgramType) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) NotRunningProgramLiveInfo(io.cdap.cdap.proto.NotRunningProgramLiveInfo) Strings(com.google.common.base.Strings) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ImmutableList(com.google.common.collect.ImmutableList) TwillRunner(org.apache.twill.api.TwillRunner) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) Locations(io.cdap.cdap.common.io.Locations) TwillRunnerService(org.apache.twill.api.TwillRunnerService) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) ProgramLiveInfo(io.cdap.cdap.proto.ProgramLiveInfo) Logger(org.slf4j.Logger) ConfiguratorFactory(io.cdap.cdap.internal.app.deploy.ConfiguratorFactory) Files(java.nio.file.Files) RunIds(io.cdap.cdap.common.app.RunIds) RuntimeArguments(io.cdap.cdap.api.common.RuntimeArguments) ProgramId(io.cdap.cdap.proto.id.ProgramId) Throwables(com.google.common.base.Throwables) ProgramDescriptor(io.cdap.cdap.app.program.ProgramDescriptor) IOException(java.io.IOException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) Maps(com.google.common.collect.Maps) File(java.io.File) AbstractListener(io.cdap.cdap.internal.app.runtime.AbstractListener) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Lock(java.util.concurrent.locks.Lock) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) Closeable(java.io.Closeable) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) BundleJarUtil(io.cdap.cdap.common.lang.jar.BundleJarUtil) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Table(com.google.common.collect.Table) Plugin(io.cdap.cdap.api.plugin.Plugin) Collections(java.util.Collections) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) AppDeploymentRuntimeInfo(io.cdap.cdap.internal.app.deploy.pipeline.AppDeploymentRuntimeInfo) SimpleRuntimeInfo(io.cdap.cdap.internal.app.runtime.service.SimpleRuntimeInfo) HashMap(java.util.HashMap) TwillRunner(org.apache.twill.api.TwillRunner) RunId(org.apache.twill.api.RunId) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) ProgramId(io.cdap.cdap.proto.id.ProgramId) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Example 3 with Program

use of io.cdap.cdap.app.program.Program in project cdap by caskdata.

the class DefaultProgramWorkflowRunner method create.

@Override
public Runnable create(String name) {
    ProgramRunner programRunner = programRunnerFactory.create(programType);
    try {
        ProgramId programId = workflowProgram.getId().getParent().program(programType, name);
        Program program = Programs.create(cConf, workflowProgram, programId, programRunner);
        return getProgramRunnable(name, programRunner, program);
    } catch (Exception e) {
        closeProgramRunner(programRunner);
        throw Throwables.propagate(e);
    }
}
Also used : Program(io.cdap.cdap.app.program.Program) ProgramRunner(io.cdap.cdap.app.runtime.ProgramRunner) ProgramId(io.cdap.cdap.proto.id.ProgramId) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with Program

use of io.cdap.cdap.app.program.Program in project cdap by caskdata.

the class MapReduceTaskContextProvider method createCacheLoader.

/**
 * Creates a {@link CacheLoader} for the task context cache.
 */
private CacheLoader<ContextCacheKey, BasicMapReduceTaskContext> createCacheLoader(final Injector injector) {
    DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
    DatasetFramework datasetFramework = injector.getInstance(DatasetFramework.class);
    SecureStore secureStore = injector.getInstance(SecureStore.class);
    SecureStoreManager secureStoreManager = injector.getInstance(SecureStoreManager.class);
    MessagingService messagingService = injector.getInstance(MessagingService.class);
    // Multiple instances of BasicMapReduceTaskContext can share the same program.
    AtomicReference<Program> programRef = new AtomicReference<>();
    MetadataReader metadataReader = injector.getInstance(MetadataReader.class);
    MetadataPublisher metadataPublisher = injector.getInstance(MetadataPublisher.class);
    FieldLineageWriter fieldLineageWriter = injector.getInstance(FieldLineageWriter.class);
    RemoteClientFactory remoteClientFactory = injector.getInstance(RemoteClientFactory.class);
    return new CacheLoader<ContextCacheKey, BasicMapReduceTaskContext>() {

        @Override
        public BasicMapReduceTaskContext load(ContextCacheKey key) throws Exception {
            TaskAttemptID taskAttemptId = key.getTaskAttemptID();
            // taskAttemptId could be null if used from a org.apache.hadoop.mapreduce.Partitioner or
            // from a org.apache.hadoop.io.RawComparator, in which case we can get the JobId from the conf. Note that the
            // JobId isn't in the conf for the OutputCommitter#setupJob method, in which case we use the taskAttemptId
            Path txFile = MainOutputCommitter.getTxFile(key.getConfiguration(), taskAttemptId != null ? taskAttemptId.getJobID() : null);
            FileSystem fs = txFile.getFileSystem(key.getConfiguration());
            Transaction transaction = null;
            if (fs.exists(txFile)) {
                try (FSDataInputStream txFileInputStream = fs.open(txFile)) {
                    transaction = new TransactionCodec().decode(ByteStreams.toByteArray(txFileInputStream));
                }
            }
            MapReduceContextConfig contextConfig = new MapReduceContextConfig(key.getConfiguration());
            MapReduceClassLoader classLoader = MapReduceClassLoader.getFromConfiguration(key.getConfiguration());
            Program program = programRef.get();
            if (program == null) {
                // Creation of program is relatively cheap, so just create and do compare and set.
                programRef.compareAndSet(null, createProgram(contextConfig, classLoader.getProgramClassLoader()));
                program = programRef.get();
            }
            WorkflowProgramInfo workflowInfo = contextConfig.getWorkflowProgramInfo();
            DatasetFramework programDatasetFramework = workflowInfo == null ? datasetFramework : NameMappedDatasetFramework.createFromWorkflowProgramInfo(datasetFramework, workflowInfo, program.getApplicationSpecification());
            // Setup dataset framework context, if required
            if (programDatasetFramework instanceof ProgramContextAware) {
                ProgramRunId programRunId = program.getId().run(ProgramRunners.getRunId(contextConfig.getProgramOptions()));
                ((ProgramContextAware) programDatasetFramework).setContext(new BasicProgramContext(programRunId));
            }
            MapReduceSpecification spec = program.getApplicationSpecification().getMapReduce().get(program.getName());
            MetricsCollectionService metricsCollectionService = null;
            MapReduceMetrics.TaskType taskType = null;
            String taskId = null;
            ProgramOptions options = contextConfig.getProgramOptions();
            // from a org.apache.hadoop.io.RawComparator
            if (taskAttemptId != null) {
                taskId = taskAttemptId.getTaskID().toString();
                if (MapReduceMetrics.TaskType.hasType(taskAttemptId.getTaskType())) {
                    taskType = MapReduceMetrics.TaskType.from(taskAttemptId.getTaskType());
                    // if this is not for a mapper or a reducer, we don't need the metrics collection service
                    metricsCollectionService = injector.getInstance(MetricsCollectionService.class);
                    options = new SimpleProgramOptions(options.getProgramId(), options.getArguments(), new BasicArguments(RuntimeArguments.extractScope("task", taskType.toString().toLowerCase(), contextConfig.getProgramOptions().getUserArguments().asMap())), options.isDebug());
                }
            }
            CConfiguration cConf = injector.getInstance(CConfiguration.class);
            TransactionSystemClient txClient = injector.getInstance(TransactionSystemClient.class);
            NamespaceQueryAdmin namespaceQueryAdmin = injector.getInstance(NamespaceQueryAdmin.class);
            return new BasicMapReduceTaskContext(program, options, cConf, taskType, taskId, spec, workflowInfo, discoveryServiceClient, metricsCollectionService, txClient, transaction, programDatasetFramework, classLoader.getPluginInstantiator(), contextConfig.getLocalizedResources(), secureStore, secureStoreManager, accessEnforcer, authenticationContext, messagingService, mapReduceClassLoader, metadataReader, metadataPublisher, namespaceQueryAdmin, fieldLineageWriter, remoteClientFactory);
        }
    };
}
Also used : RemoteClientFactory(io.cdap.cdap.common.internal.remote.RemoteClientFactory) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) TaskAttemptID(org.apache.hadoop.mapreduce.TaskAttemptID) DatasetFramework(io.cdap.cdap.data2.dataset2.DatasetFramework) NameMappedDatasetFramework(io.cdap.cdap.internal.app.runtime.workflow.NameMappedDatasetFramework) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) FileSystem(org.apache.hadoop.fs.FileSystem) NamespaceQueryAdmin(io.cdap.cdap.common.namespace.NamespaceQueryAdmin) SecureStoreManager(io.cdap.cdap.api.security.store.SecureStoreManager) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) MapReduceMetrics(io.cdap.cdap.app.metrics.MapReduceMetrics) Path(org.apache.hadoop.fs.Path) DefaultProgram(io.cdap.cdap.app.program.DefaultProgram) Program(io.cdap.cdap.app.program.Program) MetricsCollectionService(io.cdap.cdap.api.metrics.MetricsCollectionService) MapReduceSpecification(io.cdap.cdap.api.mapreduce.MapReduceSpecification) MetadataReader(io.cdap.cdap.api.metadata.MetadataReader) MetadataPublisher(io.cdap.cdap.data2.metadata.writer.MetadataPublisher) AtomicReference(java.util.concurrent.atomic.AtomicReference) BasicProgramContext(io.cdap.cdap.internal.app.runtime.BasicProgramContext) SecureStore(io.cdap.cdap.api.security.store.SecureStore) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) ProgramOptions(io.cdap.cdap.app.runtime.ProgramOptions) MessagingService(io.cdap.cdap.messaging.MessagingService) Transaction(org.apache.tephra.Transaction) WorkflowProgramInfo(io.cdap.cdap.internal.app.runtime.workflow.WorkflowProgramInfo) TransactionCodec(org.apache.tephra.TransactionCodec) FSDataInputStream(org.apache.hadoop.fs.FSDataInputStream) CacheLoader(com.google.common.cache.CacheLoader) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) ProgramContextAware(io.cdap.cdap.data.ProgramContextAware) FieldLineageWriter(io.cdap.cdap.data2.metadata.writer.FieldLineageWriter)

Example 5 with Program

use of io.cdap.cdap.app.program.Program in project cdap by caskdata.

the class AbstractProgramRuntimeServiceTest method testDeadlock.

@Test(timeout = 5000)
public void testDeadlock() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // This test is for testing condition in (CDAP-3579)
    // The race condition is if a program finished very fast such that inside the AbstractProgramRuntimeService is
    // still in the run method, it holds the object lock, making the callback from the listener block forever.
    ProgramRunnerFactory runnerFactory = createProgramRunnerFactory();
    Program program = createDummyProgram();
    ProgramRuntimeService runtimeService = new TestProgramRuntimeService(CConfiguration.create(), runnerFactory, program, null, null);
    runtimeService.startAndWait();
    try {
        ProgramDescriptor descriptor = new ProgramDescriptor(program.getId(), null, NamespaceId.DEFAULT.artifact("test", "1.0"));
        ProgramController controller = runtimeService.run(descriptor, new SimpleProgramOptions(program.getId()), RunIds.generate()).getController();
        Tasks.waitFor(ProgramController.State.COMPLETED, controller::getState, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        Tasks.waitFor(true, () -> runtimeService.list(ProgramType.WORKER).isEmpty(), 5, TimeUnit.SECONDS, 100, TimeUnit.MICROSECONDS);
    } finally {
        runtimeService.stopAndWait();
    }
}
Also used : Program(io.cdap.cdap.app.program.Program) ProgramDescriptor(io.cdap.cdap.app.program.ProgramDescriptor) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) Test(org.junit.Test)

Aggregations

Program (io.cdap.cdap.app.program.Program)12 SimpleProgramOptions (io.cdap.cdap.internal.app.runtime.SimpleProgramOptions)10 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)8 BasicArguments (io.cdap.cdap.internal.app.runtime.BasicArguments)8 ProgramDescriptor (io.cdap.cdap.app.program.ProgramDescriptor)7 ProgramId (io.cdap.cdap.proto.id.ProgramId)7 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)7 IOException (java.io.IOException)7 File (java.io.File)6 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)5 AbstractIdleService (com.google.common.util.concurrent.AbstractIdleService)4 ClusterMode (io.cdap.cdap.app.guice.ClusterMode)4 ProgramOptions (io.cdap.cdap.app.runtime.ProgramOptions)4 ProgramRunner (io.cdap.cdap.app.runtime.ProgramRunner)4 ExecutionException (java.util.concurrent.ExecutionException)4 ProgramRunnerFactory (io.cdap.cdap.app.runtime.ProgramRunnerFactory)3 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)3 ArtifactDetail (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)3 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)3 Closeable (java.io.Closeable)3