Search in sources :

Example 91 with ProgramId

use of io.cdap.cdap.proto.id.ProgramId in project cdap by caskdata.

the class MetadataHttpHandlerTestRun method testSearchMetadataDelete.

@Test
public void testSearchMetadataDelete() throws Exception {
    NamespaceId namespace = new NamespaceId("ns1");
    namespaceClient.create(new NamespaceMeta.Builder().setName(namespace).build());
    // Deploy app
    appClient.deploy(namespace, createAppJarFile(AllProgramsApp.class, AllProgramsApp.class.getSimpleName(), "1.0"));
    ArtifactId artifact = namespace.artifact(AllProgramsApp.class.getSimpleName(), "1.0");
    ApplicationId app = namespace.app(AllProgramsApp.NAME);
    ProgramId service = app.service(AllProgramsApp.NoOpService.NAME);
    DatasetId datasetInstance = namespace.dataset(AllProgramsApp.DATASET_NAME);
    // wait for metadata to be processed
    Tasks.waitFor(false, () -> getProperties(app, MetadataScope.SYSTEM).isEmpty(), 10, TimeUnit.SECONDS);
    Tasks.waitFor(false, () -> getProperties(service, MetadataScope.SYSTEM).isEmpty(), 10, TimeUnit.SECONDS);
    Tasks.waitFor(false, () -> getProperties(datasetInstance, MetadataScope.SYSTEM).isEmpty(), 10, TimeUnit.SECONDS);
    Set<String> tags = ImmutableSet.of("tag1", "tag2");
    // Add metadata
    addTags(app, tags);
    addTags(datasetInstance, tags);
    // Assert metadata
    assertSearch(searchMetadata(namespace, AllProgramsApp.DATASET_NAME), datasetInstance);
    assertSearch(searchMetadata(namespace, "all*"), app, artifact);
    assertSearch(searchMetadata(namespace, "tag1"), app, datasetInstance);
    // Delete entities
    appClient.delete(app);
    datasetClient.delete(datasetInstance);
    artifactClient.delete(artifact);
    // Assert no metadata
    waitForSearch(() -> searchMetadata(namespace, AllProgramsApp.DATASET_NAME));
    waitForSearch(() -> searchMetadata(namespace, "all*"));
    waitForSearch(() -> searchMetadata(namespace, "tag1"));
}
Also used : ArtifactId(io.cdap.cdap.proto.id.ArtifactId) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) AllProgramsApp(io.cdap.cdap.client.app.AllProgramsApp) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) DatasetId(io.cdap.cdap.proto.id.DatasetId) Test(org.junit.Test)

Example 92 with ProgramId

use of io.cdap.cdap.proto.id.ProgramId in project cdap by caskdata.

the class UsageHandlerTestRun method testWorkerUsage.

@Test
public void testWorkerUsage() throws Exception {
    final ApplicationId app = NamespaceId.DEFAULT.app(AllProgramsApp.NAME);
    final ProgramId program = app.worker(AllProgramsApp.NoOpWorker.NAME);
    final DatasetId dataset = NamespaceId.DEFAULT.dataset(AllProgramsApp.DATASET_NAME);
    Assert.assertEquals(0, getAppDatasetUsage(app).size());
    Assert.assertEquals(0, getDatasetProgramUsage(dataset).size());
    deployApp(AllProgramsApp.class);
    try {
        startProgram(program);
        // Wait for the worker to complete
        assertProgramRuns(getProgramClient(), program, ProgramRunStatus.COMPLETED, 1, 20);
        Assert.assertTrue(getProgramDatasetUsage(program).contains(dataset));
        Assert.assertTrue(getAppDatasetUsage(app).contains(dataset));
        Assert.assertTrue(getDatasetProgramUsage(dataset).contains(program));
    } finally {
        deleteApp(app);
        Assert.assertEquals(0, getAppDatasetUsage(app).size());
        Assert.assertEquals(0, getDatasetProgramUsage(dataset).size());
    }
}
Also used : ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) DatasetId(io.cdap.cdap.proto.id.DatasetId) Test(org.junit.Test)

Example 93 with ProgramId

use of io.cdap.cdap.proto.id.ProgramId in project cdap by caskdata.

the class PreferencesHttpHandlerInternal method getProgramPreferences.

/**
 * Get program level preferences
 *
 * Note that if the given program doesn't exist, the return {@link PreferencesDetail} will be empty
 * (i.e. {@link PreferencesDetail#properties} will be an empty map). In the case of requesting resolved preferences,
 * the returned {@link PreferencesDetail} will include preferences from ancestor (i.e. preferences at application,
 * namespace and instance level)
 *
 * @param request {@link HttpRequest}
 * @param responder the responder used for sending response back to client
 * @param namespace the namespace of the application
 * @param appId the application to get preferences for
 * @param programType the type of the program
 * @param programId id of the program to get preferences for
 * @param resolved whether to return resolved preferences or not
 * @throws Exception
 */
@Path("/namespaces/{namespace-id}/apps/{application-id}/{program-type}/{program-id}/preferences")
@GET
public void getProgramPreferences(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespace, @PathParam("application-id") String appId, @PathParam("program-type") String programType, @PathParam("program-id") String programId, @QueryParam("resolved") boolean resolved) throws Exception {
    ProgramId program = new ProgramId(namespace, appId, getProgramType(programType), programId);
    // No need to check if program exists. PreferencesService returns an empty PreferencesDetail when that happens.
    PreferencesDetail detail;
    if (resolved) {
        detail = preferencesService.getResolvedPreferences(program);
    } else {
        detail = preferencesService.getPreferences(program);
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(detail, PreferencesDetail.class));
}
Also used : PreferencesDetail(io.cdap.cdap.proto.PreferencesDetail) ProgramId(io.cdap.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 94 with ProgramId

use of io.cdap.cdap.proto.id.ProgramId in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method startPrograms.

/**
 * Starts all programs that are passed into the data. The data is an array of JSON objects
 * where each object must contain the following three elements: appId, programType, and programId
 * (flow name, service name, etc.). In additional, each object can contain an optional runtimeargs element,
 * which is a map of arguments to start the program with.
 * <p>
 * Example input:
 * <pre><code>
 * [{"appId": "App1", "programType": "Service", "programId": "Service1"},
 * {"appId": "App1", "programType": "Mapreduce", "programId": "MapReduce2", "runtimeargs":{"arg1":"val1"}}]
 * </code></pre>
 * </p><p>
 * The response will be an array of JsonObjects each of which will contain the three input parameters
 * as well as a "statusCode" field which maps to the status code for the data in that JsonObjects.
 * </p><p>
 * If an error occurs in the input (for the example above, App2 does not exist), then all JsonObjects for which the
 * parameters have a valid status will have the status field but all JsonObjects for which the parameters do not have
 * a valid status will have an error message and statusCode.
 * </p><p>
 * For example, if there is no App2 in the data above, then the response would be 200 OK with following possible data:
 * </p>
 * <pre><code>
 * [{"appId": "App1", "programType": "Service", "programId": "Service1", "statusCode": 200},
 * {"appId": "App2", "programType": "Mapreduce", "programId": "Mapreduce2",
 *  "statusCode":404, "error": "App: App2 not found"}]
 * </code></pre>
 */
@POST
@Path("/start")
@AuditPolicy({ AuditDetail.REQUEST_BODY, AuditDetail.RESPONSE_BODY })
public void startPrograms(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
    List<BatchProgramStart> programs = validateAndGetBatchInput(request, BATCH_STARTS_TYPE);
    List<BatchProgramResult> output = new ArrayList<>(programs.size());
    for (BatchProgramStart program : programs) {
        ProgramId programId = new ProgramId(namespaceId, program.getAppId(), program.getProgramType(), program.getProgramId());
        try {
            String runId = lifecycleService.run(programId, program.getRuntimeargs(), false).getId();
            output.add(new BatchProgramResult(program, HttpResponseStatus.OK.code(), null, runId));
        } catch (NotFoundException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.NOT_FOUND.code(), e.getMessage()));
        } catch (BadRequestException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.BAD_REQUEST.code(), e.getMessage()));
        } catch (ConflictException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.CONFLICT.code(), e.getMessage()));
        }
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(output));
}
Also used : ConflictException(io.cdap.cdap.common.ConflictException) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult) ArrayList(java.util.ArrayList) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) BatchProgramStart(io.cdap.cdap.proto.BatchProgramStart) ProgramId(io.cdap.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 95 with ProgramId

use of io.cdap.cdap.proto.id.ProgramId in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method doAddSchedule.

private void doAddSchedule(FullHttpRequest request, HttpResponder responder, String namespace, String appName, String appVersion, String scheduleName) throws Exception {
    final ApplicationId applicationId = new ApplicationId(namespace, appName, appVersion);
    ScheduleDetail scheduleFromRequest = readScheduleDetailBody(request, scheduleName);
    if (scheduleFromRequest.getProgram() == null) {
        throw new BadRequestException("No program was specified for the schedule");
    }
    if (scheduleFromRequest.getProgram().getProgramType() == null) {
        throw new BadRequestException("No program type was specified for the schedule");
    }
    if (scheduleFromRequest.getProgram().getProgramName() == null) {
        throw new BadRequestException("No program name was specified for the schedule");
    }
    if (scheduleFromRequest.getTrigger() == null) {
        throw new BadRequestException("No trigger was specified for the schedule");
    }
    ProgramType programType = ProgramType.valueOfSchedulableType(scheduleFromRequest.getProgram().getProgramType());
    String programName = scheduleFromRequest.getProgram().getProgramName();
    ProgramId programId = applicationId.program(programType, programName);
    lifecycleService.ensureProgramExists(programId);
    String description = Objects.firstNonNull(scheduleFromRequest.getDescription(), "");
    Map<String, String> properties = Objects.firstNonNull(scheduleFromRequest.getProperties(), Collections.emptyMap());
    List<? extends Constraint> constraints = Objects.firstNonNull(scheduleFromRequest.getConstraints(), NO_CONSTRAINTS);
    long timeoutMillis = Objects.firstNonNull(scheduleFromRequest.getTimeoutMillis(), Schedulers.JOB_QUEUE_TIMEOUT_MILLIS);
    ProgramSchedule schedule = new ProgramSchedule(scheduleName, description, programId, properties, scheduleFromRequest.getTrigger(), constraints, timeoutMillis);
    programScheduleService.add(schedule);
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : BatchProgramSchedule(io.cdap.cdap.proto.BatchProgramSchedule) ProgramSchedule(io.cdap.cdap.internal.app.runtime.schedule.ProgramSchedule) BadRequestException(io.cdap.cdap.common.BadRequestException) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ProgramType(io.cdap.cdap.proto.ProgramType) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Aggregations

ProgramId (io.cdap.cdap.proto.id.ProgramId)562 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)277 Test (org.junit.Test)268 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)164 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)130 RunId (org.apache.twill.api.RunId)118 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)110 ProgramType (io.cdap.cdap.proto.ProgramType)108 HashMap (java.util.HashMap)88 HashSet (java.util.HashSet)78 ArrayList (java.util.ArrayList)76 Id (io.cdap.cdap.common.id.Id)74 IOException (java.io.IOException)74 File (java.io.File)70 RunRecord (io.cdap.cdap.proto.RunRecord)68 Path (javax.ws.rs.Path)68 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)66 NotFoundException (io.cdap.cdap.common.NotFoundException)66 Map (java.util.Map)64 Set (java.util.Set)62