Search in sources :

Example 56 with NamespaceId

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

the class LogHttpHandlerTest method testLogsRunId.

private void testLogsRunId(String appId, String entityType, String entityId, String namespace, String format, List<String> suppress) throws Exception {
    ProgramId programId = new NamespaceId(namespace).app(appId).program(ProgramType.valueOfCategoryName(entityType), entityId);
    RunRecord runRecord = mockLogReader.getRunRecord(programId);
    int expectedEvents = 20;
    if (runRecord.getStatus() == ProgramRunStatus.RUNNING || runRecord.getStatus() == ProgramRunStatus.SUSPENDED) {
        expectedEvents = 30;
    }
    long startTime = MockLogReader.getMockTimeSecs(0);
    long stopTime = MockLogReader.getMockTimeSecs(100);
    String nextNoFromUrl;
    if (suppress.isEmpty()) {
        nextNoFromUrl = String.format("apps/%s/%s/%s/runs/%s/logs?format=%s&start=%s&stop=%s", appId, entityType, entityId, runRecord.getPid(), format, startTime, stopTime);
    } else {
        String fieldsToSuppress = getSuppressStr(suppress);
        nextNoFromUrl = String.format("apps/%s/%s/%s/runs/%s/logs?format=%s&start=%s&stop=%s&suppress=%s", appId, entityType, entityId, runRecord.getPid(), format, startTime, stopTime, fieldsToSuppress);
    }
    HttpResponse response = doGet(getVersionedAPIPath(nextNoFromUrl, namespace));
    verifyLogs(response, entityId, format, true, true, true, expectedEvents, 20, suppress);
}
Also used : RunRecord(io.cdap.cdap.proto.RunRecord) HttpResponse(io.cdap.common.http.HttpResponse) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Example 57 with NamespaceId

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

the class LogHttpHandlerTest method testNextRunId.

private void testNextRunId(String appId, String entityType, String entityId, String namespace, String format, List<String> suppress) throws Exception {
    ProgramId programId = new NamespaceId(namespace).app(appId).program(ProgramType.valueOfCategoryName(entityType), entityId);
    RunRecord runRecord = mockLogReader.getRunRecord(programId);
    int expectedEvents = 20;
    if (runRecord.getStatus() == ProgramRunStatus.RUNNING || runRecord.getStatus() == ProgramRunStatus.SUSPENDED) {
        expectedEvents = 30;
    }
    String nextNoFromUrl;
    if (suppress.isEmpty()) {
        nextNoFromUrl = String.format("apps/%s/%s/%s/runs/%s/logs/next?format=%s&max=100", appId, entityType, entityId, runRecord.getPid(), format);
    } else {
        String fieldsToSuppress = getSuppressStr(suppress);
        nextNoFromUrl = String.format("apps/%s/%s/%s/runs/%s/logs/next?format=%s&max=100&suppress=%s", appId, entityType, entityId, runRecord.getPid(), format, fieldsToSuppress);
    }
    HttpResponse response = doGet(getVersionedAPIPath(nextNoFromUrl, namespace));
    verifyLogs(response, entityId, format, true, false, true, expectedEvents, 20, suppress);
}
Also used : RunRecord(io.cdap.cdap.proto.RunRecord) HttpResponse(io.cdap.common.http.HttpResponse) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Example 58 with NamespaceId

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

the class ApplicationLifecycleService method deployApp.

/**
 * Deploy an application using the specified artifact and configuration. When an app is deployed, the Application
 * class is instantiated and configure() is called in order to generate an {@link ApplicationSpecification}.
 * Programs, datasets, and streams are created based on the specification before the spec is persisted in the
 * {@link Store}. This method can create a new application as well as update an existing one.
 *
 * @param namespace the namespace to deploy the app to
 * @param appName the name of the app. If null, the name will be set based on the application spec
 * @param summary the artifact summary of the app
 * @param configStr the configuration to send to the application when generating the application specification
 * @param programTerminator a program terminator that will stop programs that are removed when updating an app.
 *                          For example, if an update removes a flow, the terminator defines how to stop that flow.
 * @param ownerPrincipal the kerberos principal of the application owner
 * @param updateSchedules specifies if schedules of the workflow have to be updated,
 *                        if null value specified by the property "app.deploy.update.schedules" will be used.
 * @param isPreview whether the app deployment is for preview
 * @param userProps the user properties for the app deployment, this is basically used for preview deployment
 * @return information about the deployed application
 * @throws InvalidArtifactException if the artifact does not contain any application classes
 * @throws IOException if there was an IO error reading artifact detail from the meta store
 * @throws ArtifactNotFoundException if the specified artifact does not exist
 * @throws Exception if there was an exception during the deployment pipeline. This exception will often wrap
 *                   the actual exception
 */
public ApplicationWithPrograms deployApp(NamespaceId namespace, @Nullable String appName, @Nullable String appVersion, ArtifactSummary summary, @Nullable String configStr, ProgramTerminator programTerminator, @Nullable KerberosPrincipalId ownerPrincipal, @Nullable Boolean updateSchedules, boolean isPreview, Map<String, String> userProps) throws Exception {
    NamespaceId artifactNamespace = ArtifactScope.SYSTEM.equals(summary.getScope()) ? NamespaceId.SYSTEM : namespace;
    ArtifactRange range = new ArtifactRange(artifactNamespace.getNamespace(), summary.getName(), ArtifactVersionRange.parse(summary.getVersion()));
    // this method will not throw ArtifactNotFoundException, if no artifacts in the range, we are expecting an empty
    // collection returned.
    List<ArtifactDetail> artifactDetail = artifactRepository.getArtifactDetails(range, 1, ArtifactSortOrder.DESC);
    if (artifactDetail.isEmpty()) {
        throw new ArtifactNotFoundException(range.getNamespace(), range.getName());
    }
    return deployApp(namespace, appName, appVersion, configStr, programTerminator, artifactDetail.iterator().next(), ownerPrincipal, updateSchedules == null ? appUpdateSchedules : updateSchedules, isPreview, userProps);
}
Also used : ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) ArtifactDetail(io.cdap.cdap.internal.app.runtime.artifact.ArtifactDetail)

Example 59 with NamespaceId

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

the class ApplicationLifecycleService method scanApplications.

/**
 * Scans all applications in the specified namespace, filtered to only include application details
 * which satisfy the filters
 *
 * @param request application scan request. Must name namespace filled
 * @param consumer a {@link Consumer} to consume each ApplicationDetail being scanned
 * @return if limit was reached (true) or all items were scanned before reaching the limit (false)
 * @throws IllegalArgumentException if scan request does not have namespace specified
 */
public boolean scanApplications(ScanApplicationsRequest request, Consumer<ApplicationDetail> consumer) {
    NamespaceId namespace = request.getNamespaceId();
    if (namespace == null) {
        throw new IllegalStateException("Application scan request without namespace");
    }
    accessEnforcer.enforceOnParent(EntityType.DATASET, namespace, authenticationContext.getPrincipal(), StandardPermission.LIST);
    try (BatchingConsumer<Entry<ApplicationId, ApplicationSpecification>> batchingConsumer = new BatchingConsumer<>(list -> processApplications(list, consumer), batchSize)) {
        return store.scanApplications(request, batchSize, (appId, appSpec) -> {
            batchingConsumer.accept(new SimpleEntry<>(appId, appSpec));
        });
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) Entry(java.util.Map.Entry) SimpleEntry(java.util.AbstractMap.SimpleEntry) BatchingConsumer(io.cdap.cdap.common.utils.BatchingConsumer) NamespaceId(io.cdap.cdap.proto.id.NamespaceId)

Example 60 with NamespaceId

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

the class ApplicationLifecycleService method createAppDetailsArchive.

/**
 * Creates a ZIP archive that contains the {@link ApplicationDetail} for all applications. The archive created will
 * contain a directory entry for each of the namespace. Inside each namespace directory, it contains the
 * application detail json, the application name as the file name, with {@code ".json"} as the file extension.
 * <p/>
 * This method requires instance admin permission.
 *
 * @param zipOut the {@link ZipOutputStream} for writing out the application details
 */
public void createAppDetailsArchive(ZipOutputStream zipOut) throws Exception {
    accessEnforcer.enforce(new InstanceId(cConf.get(Constants.INSTANCE_NAME)), authenticationContext.getPrincipal(), StandardPermission.GET);
    Set<NamespaceId> namespaces = new HashSet<>();
    JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(zipOut, StandardCharsets.UTF_8));
    store.scanApplications(batchSize, (appId, appSpec) -> {
        // Skip the SYSTEM namespace apps
        if (NamespaceId.SYSTEM.equals(appId.getParent())) {
            return;
        }
        try {
            ApplicationDetail applicationDetail = enforceApplicationDetailAccess(appId, ApplicationDetail.fromSpec(appSpec, null));
            // Add a directory for the namespace
            if (namespaces.add(appId.getParent())) {
                ZipEntry entry = new ZipEntry(appId.getNamespace() + "/");
                zipOut.putNextEntry(entry);
                zipOut.closeEntry();
            }
            ZipEntry entry = new ZipEntry(appId.getNamespace() + "/" + appId.getApplication() + ".json");
            zipOut.putNextEntry(entry);
            GSON.toJson(applicationDetail, ApplicationDetail.class, jsonWriter);
            jsonWriter.flush();
            zipOut.closeEntry();
        } catch (IOException | AccessException e) {
            throw new RuntimeException("Failed to add zip entry for application " + appId, e);
        }
    });
}
Also used : ApplicationDetail(io.cdap.cdap.proto.ApplicationDetail) AccessException(io.cdap.cdap.api.security.AccessException) InstanceId(io.cdap.cdap.proto.id.InstanceId) ZipEntry(java.util.zip.ZipEntry) OutputStreamWriter(java.io.OutputStreamWriter) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) IOException(java.io.IOException) JsonIOException(com.google.gson.JsonIOException) JsonWriter(com.google.gson.stream.JsonWriter) HashSet(java.util.HashSet)

Aggregations

NamespaceId (io.cdap.cdap.proto.id.NamespaceId)648 Test (org.junit.Test)292 Path (javax.ws.rs.Path)136 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)124 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)108 IOException (java.io.IOException)102 ProgramId (io.cdap.cdap.proto.id.ProgramId)86 GET (javax.ws.rs.GET)74 DatasetId (io.cdap.cdap.proto.id.DatasetId)68 ArrayList (java.util.ArrayList)64 BadRequestException (io.cdap.cdap.common.BadRequestException)60 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)58 Principal (io.cdap.cdap.proto.security.Principal)56 Set (java.util.Set)52 Id (io.cdap.cdap.common.id.Id)50 File (java.io.File)50 HashSet (java.util.HashSet)50 NotFoundException (io.cdap.cdap.common.NotFoundException)48 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)46 HashMap (java.util.HashMap)46