Search in sources :

Example 1 with NotImplementedException

use of io.cdap.cdap.common.NotImplementedException in project cdap by caskdata.

the class AppLifecycleHttpHandler method upgradeApplications.

/**
 * Upgrades a lis of existing application to use latest version of application artifact and plugin artifacts.
 *
 * <pre>
 * {@code
 * [
 *   {"name":"XYZ"},
 *   {"name":"ABC"},
 *   {"name":"FOO"},
 * ]
 * }
 * </pre>
 * The response will be an array of {@link ApplicationUpdateDetail} object, which either indicates a success (200) or
 * failure for each of the requested application in the same order as the request. The failure also indicates reason
 * for the error. The response will be sent via ChunkResponder to continuously stream upgrade result per application.
 */
@POST
@Path("/upgrade")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void upgradeApplications(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @QueryParam("artifactScope") Set<String> artifactScopes, @QueryParam("allowSnapshot") boolean allowSnapshot) throws Exception {
    // TODO: (CDAP-16910) Improve batch API performance as each application upgrade is an event independent of each
    // other.
    List<ApplicationId> appIds = decodeAndValidateBatchApplicationRecord(validateNamespace(namespaceId), request);
    Set<ArtifactScope> allowedArtifactScopes = getArtifactScopes(artifactScopes);
    try (ChunkResponder chunkResponder = responder.sendChunkStart(HttpResponseStatus.OK)) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try (JsonWriter jsonWriter = new JsonWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8))) {
            jsonWriter.beginArray();
            for (ApplicationId appId : appIds) {
                ApplicationUpdateDetail updateDetail;
                try {
                    applicationLifecycleService.upgradeApplication(appId, allowedArtifactScopes, allowSnapshot);
                    updateDetail = new ApplicationUpdateDetail(appId);
                } catch (UnsupportedOperationException e) {
                    String errorMessage = String.format("Application %s does not support upgrade.", appId);
                    updateDetail = new ApplicationUpdateDetail(appId, new NotImplementedException(errorMessage));
                } catch (InvalidArtifactException | NotFoundException e) {
                    updateDetail = new ApplicationUpdateDetail(appId, e);
                } catch (Exception e) {
                    updateDetail = new ApplicationUpdateDetail(appId, new ServiceException("Upgrade failed due to internal error.", e, HttpResponseStatus.INTERNAL_SERVER_ERROR));
                    LOG.error("Application upgrade failed with exception", e);
                }
                GSON.toJson(updateDetail, ApplicationUpdateDetail.class, jsonWriter);
                jsonWriter.flush();
                chunkResponder.sendChunk(Unpooled.wrappedBuffer(outputStream.toByteArray()));
                outputStream.reset();
                chunkResponder.flush();
            }
            jsonWriter.endArray();
        }
        chunkResponder.sendChunk(Unpooled.wrappedBuffer(outputStream.toByteArray()));
    }
}
Also used : NotImplementedException(io.cdap.cdap.common.NotImplementedException) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonWriter(com.google.gson.stream.JsonWriter) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) WriteConflictException(io.cdap.cdap.internal.app.runtime.artifact.WriteConflictException) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) ConflictException(io.cdap.cdap.common.ConflictException) NotImplementedException(io.cdap.cdap.common.NotImplementedException) ExecutionException(java.util.concurrent.ExecutionException) AccessException(io.cdap.cdap.api.security.AccessException) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) NotFoundException(io.cdap.cdap.common.NotFoundException) ServiceException(io.cdap.cdap.common.ServiceException) JsonSyntaxException(com.google.gson.JsonSyntaxException) BadRequestException(io.cdap.cdap.common.BadRequestException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) ArtifactScope(io.cdap.cdap.api.artifact.ArtifactScope) ApplicationUpdateDetail(io.cdap.cdap.proto.ApplicationUpdateDetail) ServiceException(io.cdap.cdap.common.ServiceException) OutputStreamWriter(java.io.OutputStreamWriter) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ChunkResponder(io.cdap.http.ChunkResponder) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 2 with NotImplementedException

use of io.cdap.cdap.common.NotImplementedException in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method doPerformAction.

private void doPerformAction(FullHttpRequest request, HttpResponder responder, String namespaceId, String appId, String appVersion, String type, String programId, String action) throws Exception {
    ApplicationId applicationId = new ApplicationId(namespaceId, appId, appVersion);
    if (SCHEDULES.equals(type)) {
        ScheduleId scheduleId = applicationId.schedule(programId);
        if (action.equals("disable") || action.equals("suspend")) {
            programScheduleService.suspend(scheduleId);
        } else if (action.equals("enable") || action.equals("resume")) {
            programScheduleService.resume(scheduleId);
        } else {
            throw new BadRequestException("Action for schedules may only be 'enable', 'disable', 'suspend', or 'resume' but is'" + action + "'");
        }
        responder.sendJson(HttpResponseStatus.OK, "OK");
        return;
    }
    ProgramType programType = getProgramType(type);
    ProgramId program = applicationId.program(programType, programId);
    Map<String, String> args = decodeArguments(request);
    // we have already validated that the action is valid
    switch(action.toLowerCase()) {
        case "start":
            lifecycleService.run(program, args, false);
            break;
        case "debug":
            if (!isDebugAllowed(programType)) {
                throw new NotImplementedException(String.format("debug action is not implemented for program type %s", programType));
            }
            lifecycleService.run(program, args, true);
            break;
        case "stop":
            lifecycleService.stop(program);
            break;
        default:
            throw new NotFoundException(String.format("%s action was not found", action));
    }
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : NotImplementedException(io.cdap.cdap.common.NotImplementedException) BadRequestException(io.cdap.cdap.common.BadRequestException) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramType(io.cdap.cdap.proto.ProgramType) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Example 3 with NotImplementedException

use of io.cdap.cdap.common.NotImplementedException in project cdap by caskdata.

the class TetheringServerHandler method createTethering.

/**
 * Creates a tethering with a client.
 */
@PUT
@Path("/tethering/connections/{peer}")
public void createTethering(FullHttpRequest request, HttpResponder responder, @PathParam("peer") String peer) throws NotImplementedException, IOException {
    checkTetheringServerEnabled();
    contextAccessEnforcer.enforce(InstanceId.SELF, InstancePermission.TETHER);
    String content = request.content().toString(StandardCharsets.UTF_8);
    TetheringConnectionRequest tetherRequest = GSON.fromJson(content, TetheringConnectionRequest.class);
    TopicId topicId = new TopicId(NamespaceId.SYSTEM.getNamespace(), topicPrefix + peer);
    try {
        messagingService.createTopic(new TopicMetadata(topicId, Collections.emptyMap()));
    } catch (TopicAlreadyExistsException e) {
        LOG.warn("Topic {} already exists", topicId);
    } catch (IOException e) {
        LOG.error("Failed to create topic {}", topicId, e);
        throw e;
    }
    // We don't need to keep track of the client metadata on the server side.
    PeerMetadata peerMetadata = new PeerMetadata(tetherRequest.getNamespaceAllocations(), Collections.emptyMap(), tetherRequest.getDescription());
    // We don't store the peer endpoint on the server side because the connection is initiated by the client.
    PeerInfo peerInfo = new PeerInfo(peer, null, TetheringStatus.PENDING, peerMetadata, tetherRequest.getRequestTime());
    try {
        store.addPeer(peerInfo);
    } catch (PeerAlreadyExistsException pae) {
        // Peer is already configured, treat this as a no-op.
        responder.sendStatus(HttpResponseStatus.OK);
        return;
    } catch (Exception e) {
        try {
            messagingService.deleteTopic(topicId);
        } catch (Exception ex) {
            e.addSuppressed(ex);
        }
        throw new IOException("Failed to create tethering with peer " + peer, e);
    }
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : TopicId(io.cdap.cdap.proto.id.TopicId) IOException(java.io.IOException) TopicAlreadyExistsException(io.cdap.cdap.api.messaging.TopicAlreadyExistsException) ForbiddenException(io.cdap.cdap.common.ForbiddenException) TopicAlreadyExistsException(io.cdap.cdap.api.messaging.TopicAlreadyExistsException) IOException(java.io.IOException) BadRequestException(io.cdap.cdap.common.BadRequestException) NotImplementedException(io.cdap.cdap.common.NotImplementedException) TopicNotFoundException(io.cdap.cdap.api.messaging.TopicNotFoundException) TopicMetadata(io.cdap.cdap.messaging.TopicMetadata) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Aggregations

BadRequestException (io.cdap.cdap.common.BadRequestException)3 NotImplementedException (io.cdap.cdap.common.NotImplementedException)3 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)2 NotFoundException (io.cdap.cdap.common.NotFoundException)2 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)2 IOException (java.io.IOException)2 Path (javax.ws.rs.Path)2 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 JsonWriter (com.google.gson.stream.JsonWriter)1 ArtifactScope (io.cdap.cdap.api.artifact.ArtifactScope)1 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)1 TopicAlreadyExistsException (io.cdap.cdap.api.messaging.TopicAlreadyExistsException)1 TopicNotFoundException (io.cdap.cdap.api.messaging.TopicNotFoundException)1 AccessException (io.cdap.cdap.api.security.AccessException)1 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)1 ArtifactAlreadyExistsException (io.cdap.cdap.common.ArtifactAlreadyExistsException)1 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)1 ConflictException (io.cdap.cdap.common.ConflictException)1 ForbiddenException (io.cdap.cdap.common.ForbiddenException)1 InvalidArtifactException (io.cdap.cdap.common.InvalidArtifactException)1