Search in sources :

Example 1 with ProgramLiveInfo

use of io.cdap.cdap.proto.ProgramLiveInfo in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method getInstanceCount.

/**
 * Returns the number of instances currently running for different runnables for different programs
 */
private int getInstanceCount(ProgramId programId, String runnableId) {
    ProgramLiveInfo info = runtimeService.getLiveInfo(programId);
    int count = 0;
    if (info instanceof NotRunningProgramLiveInfo) {
        return count;
    }
    if (info instanceof Containers) {
        Containers containers = (Containers) info;
        for (Containers.ContainerInfo container : containers.getContainers()) {
            if (container.getName().equals(runnableId)) {
                count++;
            }
        }
        return count;
    }
    // The get instances contract for both flowlets and services should be re-thought and fixed as part of CDAP-1091
    if (programId.getType() == ProgramType.SERVICE) {
        return getRequestedServiceInstances(programId);
    }
    // Not running on YARN default 1
    return 1;
}
Also used : NotRunningProgramLiveInfo(io.cdap.cdap.proto.NotRunningProgramLiveInfo) Containers(io.cdap.cdap.proto.Containers) ProgramLiveInfo(io.cdap.cdap.proto.ProgramLiveInfo) NotRunningProgramLiveInfo(io.cdap.cdap.proto.NotRunningProgramLiveInfo) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint)

Example 2 with ProgramLiveInfo

use of io.cdap.cdap.proto.ProgramLiveInfo in project cdap by caskdata.

the class AbstractProgramRuntimeServiceTest method testScopingRuntimeArguments.

@Test
public void testScopingRuntimeArguments() throws Exception {
    Map<ProgramId, Arguments> argumentsMap = new ConcurrentHashMap<>();
    ProgramRunnerFactory runnerFactory = createProgramRunnerFactory(argumentsMap);
    final Program program = createDummyProgram();
    final ProgramRuntimeService runtimeService = new AbstractProgramRuntimeService(CConfiguration.create(), runnerFactory, null, new NoOpProgramStateWriter(), null) {

        @Override
        public ProgramLiveInfo getLiveInfo(ProgramId programId) {
            return new ProgramLiveInfo(programId, "runtime") {
            };
        }

        @Override
        protected Program createProgram(CConfiguration cConf, ProgramRunner programRunner, ProgramDescriptor programDescriptor, ArtifactDetail artifactDetail, File tempDir) throws IOException {
            return program;
        }

        @Override
        protected ArtifactDetail getArtifactDetail(ArtifactId artifactId) throws IOException, ArtifactNotFoundException {
            io.cdap.cdap.api.artifact.ArtifactId id = new io.cdap.cdap.api.artifact.ArtifactId("dummy", new ArtifactVersion("1.0"), ArtifactScope.USER);
            return new ArtifactDetail(new ArtifactDescriptor(NamespaceId.DEFAULT.getEntityName(), id, Locations.toLocation(TEMP_FOLDER.newFile())), new ArtifactMeta(ArtifactClasses.builder().build()));
        }
    };
    runtimeService.startAndWait();
    try {
        try {
            ProgramDescriptor descriptor = new ProgramDescriptor(program.getId(), null, NamespaceId.DEFAULT.artifact("test", "1.0"));
            // Set of scopes to test
            String programScope = program.getType().getScope();
            String clusterName = "c1";
            List<String> scopes = Arrays.asList("cluster.*.", "cluster." + clusterName + ".", "cluster." + clusterName + ".app.*.", "app.*.", "app." + program.getApplicationId() + ".", "app." + program.getApplicationId() + "." + programScope + ".*.", "app." + program.getApplicationId() + "." + programScope + "." + program.getName() + ".", programScope + ".*.", programScope + "." + program.getName() + ".", "");
            for (String scope : scopes) {
                ProgramOptions programOptions = new SimpleProgramOptions(program.getId(), new BasicArguments(Collections.singletonMap(Constants.CLUSTER_NAME, clusterName)), new BasicArguments(Collections.singletonMap(scope + "size", Integer.toString(scope.length()))));
                final ProgramController controller = runtimeService.run(descriptor, programOptions, RunIds.generate()).getController();
                Tasks.waitFor(ProgramController.State.COMPLETED, controller::getState, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
                // Should get an argument
                Arguments args = argumentsMap.get(program.getId());
                Assert.assertNotNull(args);
                Assert.assertEquals(scope.length(), Integer.parseInt(args.getOption("size")));
            }
        } finally {
            runtimeService.stopAndWait();
        }
    } finally {
        runtimeService.stopAndWait();
    }
}
Also used : ArtifactId(io.cdap.cdap.proto.id.ArtifactId) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) ArtifactDescriptor(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor) ProgramDescriptor(io.cdap.cdap.app.program.ProgramDescriptor) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArtifactMeta(io.cdap.cdap.internal.app.runtime.artifact.ArtifactMeta) Program(io.cdap.cdap.app.program.Program) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) ProgramId(io.cdap.cdap.proto.id.ProgramId) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) ProgramLiveInfo(io.cdap.cdap.proto.ProgramLiveInfo) File(java.io.File) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail) Test(org.junit.Test)

Aggregations

ProgramLiveInfo (io.cdap.cdap.proto.ProgramLiveInfo)2 ArtifactVersion (io.cdap.cdap.api.artifact.ArtifactVersion)1 Program (io.cdap.cdap.app.program.Program)1 ProgramDescriptor (io.cdap.cdap.app.program.ProgramDescriptor)1 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)1 BasicArguments (io.cdap.cdap.internal.app.runtime.BasicArguments)1 SimpleProgramOptions (io.cdap.cdap.internal.app.runtime.SimpleProgramOptions)1 ArtifactDescriptor (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDescriptor)1 ArtifactDetail (io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)1 ArtifactMeta (io.cdap.cdap.internal.app.runtime.artifact.ArtifactMeta)1 Constraint (io.cdap.cdap.internal.schedule.constraint.Constraint)1 Containers (io.cdap.cdap.proto.Containers)1 NotRunningProgramLiveInfo (io.cdap.cdap.proto.NotRunningProgramLiveInfo)1 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)1 ProgramId (io.cdap.cdap.proto.id.ProgramId)1 File (java.io.File)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Test (org.junit.Test)1