Search in sources :

Example 1 with ConflictException

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

the class SystemProgramManagementService method startPrograms.

private void startPrograms(Map<ProgramId, Arguments> enabledProgramsMap) {
    for (ProgramId programId : enabledProgramsMap.keySet()) {
        Map<String, String> overrides = enabledProgramsMap.get(programId).asMap();
        LOG.debug("Starting program {} with args {}", programId, overrides);
        try {
            programLifecycleService.start(programId, overrides, false, false);
        } catch (ConflictException ex) {
            // Ignore if the program is already running.
            LOG.debug("Program {} is already running.", programId);
        } catch (Exception ex) {
            LOG.warn("Could not start program {} , will be retried in next run.", programId, ex);
        }
    }
}
Also used : ConflictException(io.cdap.cdap.common.ConflictException) ProgramId(io.cdap.cdap.proto.id.ProgramId) ConflictException(io.cdap.cdap.common.ConflictException)

Example 2 with ConflictException

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

the class InMemoryDatasetOpExecutor method update.

@Override
public DatasetCreationResponse update(DatasetId datasetInstanceId, DatasetTypeMeta typeMeta, DatasetProperties props, DatasetSpecification existing) throws Exception {
    DatasetType type = client.getDatasetType(typeMeta, null, new ConstantClassLoaderProvider());
    if (type == null) {
        throw new IllegalArgumentException("Dataset type cannot be instantiated for provided type meta: " + typeMeta);
    }
    try {
        DatasetSpecification spec = type.reconfigure(datasetInstanceId.getEntityName(), props, existing);
        DatasetAdmin admin = type.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec);
        if (admin instanceof Updatable) {
            ((Updatable) admin).update(existing);
        } else {
            admin.create();
        }
        if (spec.getDescription() == null && existing.getDescription() != null) {
            spec.setDescription(existing.getDescription());
        }
        return new DatasetCreationResponse(spec, null);
    } catch (IncompatibleUpdateException e) {
        throw new ConflictException(e.getMessage());
    }
}
Also used : ConflictException(io.cdap.cdap.common.ConflictException) Updatable(io.cdap.cdap.api.dataset.Updatable) ConstantClassLoaderProvider(io.cdap.cdap.data2.datafabric.dataset.type.ConstantClassLoaderProvider) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) DatasetType(io.cdap.cdap.data2.datafabric.dataset.DatasetType) IncompatibleUpdateException(io.cdap.cdap.api.dataset.IncompatibleUpdateException)

Example 3 with ConflictException

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

the class AppLifecycleHttpHandler method deployAppFromArtifact.

// normally we wouldn't want to use a body consumer but would just want to read the request body directly
// since it wont be big. But the deploy app API has one path with different behavior based on content type
// the other behavior requires a BodyConsumer and only have one method per path is allowed,
// so we have to use a BodyConsumer
private BodyConsumer deployAppFromArtifact(final ApplicationId appId) throws IOException {
    // Perform auth checks outside BodyConsumer as only the first http request containing auth header
    // to populate SecurityRequestContext while http chunk doesn't. BodyConsumer runs in the thread
    // that processes the last http chunk.
    accessEnforcer.enforce(appId, authenticationContext.getPrincipal(), StandardPermission.CREATE);
    // createTempFile() needs a prefix of at least 3 characters
    return new AbstractBodyConsumer(File.createTempFile("apprequest-" + appId, ".json", tmpDir)) {

        @Override
        protected void onFinish(HttpResponder responder, File uploadedFile) {
            try (FileReader fileReader = new FileReader(uploadedFile)) {
                AppRequest<?> appRequest = DECODE_GSON.fromJson(fileReader, AppRequest.class);
                ArtifactSummary artifactSummary = appRequest.getArtifact();
                KerberosPrincipalId ownerPrincipalId = appRequest.getOwnerPrincipal() == null ? null : new KerberosPrincipalId(appRequest.getOwnerPrincipal());
                // if we don't null check, it gets serialized to "null"
                Object config = appRequest.getConfig();
                String configString = config == null ? null : config instanceof String ? (String) config : GSON.toJson(config);
                try {
                    applicationLifecycleService.deployApp(appId.getParent(), appId.getApplication(), appId.getVersion(), artifactSummary, configString, createProgramTerminator(), ownerPrincipalId, appRequest.canUpdateSchedules(), false, Collections.emptyMap());
                } catch (DatasetManagementException e) {
                    if (e.getCause() instanceof UnauthorizedException) {
                        throw (UnauthorizedException) e.getCause();
                    } else {
                        throw e;
                    }
                }
                responder.sendString(HttpResponseStatus.OK, "Deploy Complete");
            } catch (ArtifactNotFoundException e) {
                responder.sendString(HttpResponseStatus.NOT_FOUND, e.getMessage());
            } catch (ConflictException e) {
                responder.sendString(HttpResponseStatus.CONFLICT, e.getMessage());
            } catch (UnauthorizedException e) {
                responder.sendString(HttpResponseStatus.FORBIDDEN, e.getMessage());
            } catch (InvalidArtifactException e) {
                responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
            } catch (IOException e) {
                LOG.error("Error reading request body for creating app {}.", appId);
                responder.sendString(HttpResponseStatus.INTERNAL_SERVER_ERROR, String.format("Error while reading json request body for app %s.", appId));
            } catch (Exception e) {
                LOG.error("Deploy failure", e);
                responder.sendString(HttpResponseStatus.BAD_REQUEST, e.getMessage());
            }
        }
    };
}
Also used : HttpResponder(io.cdap.http.HttpResponder) WriteConflictException(io.cdap.cdap.internal.app.runtime.artifact.WriteConflictException) ConflictException(io.cdap.cdap.common.ConflictException) IOException(java.io.IOException) 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) DatasetManagementException(io.cdap.cdap.api.dataset.DatasetManagementException) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) AbstractBodyConsumer(io.cdap.cdap.common.http.AbstractBodyConsumer) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) FileReader(java.io.FileReader) JsonObject(com.google.gson.JsonObject) File(java.io.File) KerberosPrincipalId(io.cdap.cdap.proto.id.KerberosPrincipalId) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException)

Example 4 with ConflictException

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

the class SystemAppEnableExecutor method startProgram.

private void startProgram(ProgramId programId) throws Exception {
    try {
        ProgramStatus currentStatus = programLifecycleService.getProgramStatus(programId);
        // checking if artifact version has changed would be a good start.
        try {
            if (currentStatus == ProgramStatus.RUNNING) {
                programLifecycleService.stop(programId);
            }
        } catch (ConflictException e) {
        // Will reach here if the program is already stopped, which means it tried to stop after the status check above.
        // ignore this, as it means the program is stopped as we wanted.
        }
        programLifecycleService.run(programId, Collections.emptyMap(), false);
    } catch (ConflictException e) {
    // thrown if the program is already running, which means it was started after the status check above.
    // ignore this, as it means the program is running as expected
    } catch (NotFoundException e) {
        // use a nicer error message
        throw new IllegalArgumentException(String.format("Cannot start %s because it does not exist.", programId), e);
    }
}
Also used : ConflictException(io.cdap.cdap.common.ConflictException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramStatus(io.cdap.cdap.proto.ProgramStatus)

Example 5 with ConflictException

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

the class ProgramStarter method execute.

@Override
public void execute(Arguments arguments) throws Exception {
    ProgramId programId = arguments.getId();
    Preconditions.checkArgument(programLifecycleService.getProgramSpecification(programId) != null, "Cannot start %s because it does not exist.", programId);
    try {
        // do nothing if the program is already running
        ProgramStatus currentStatus = programLifecycleService.getProgramStatus(programId);
        if (currentStatus != ProgramStatus.STOPPED) {
            LOG.info("Program {} is in the {} state, skipping start program bootstrap step.", programId, currentStatus);
            return;
        }
        programLifecycleService.run(programId, Collections.emptyMap(), false);
    } catch (ConflictException e) {
    // thrown if the program is already running, which means it was started after the status check above.
    // ignore this, as it means the program is running as expected
    } catch (NotFoundException e) {
        // use a nicer error message
        throw new IllegalArgumentException(String.format("Cannot start %s because it does not exist.", programId), e);
    }
}
Also used : ConflictException(io.cdap.cdap.common.ConflictException) NotFoundException(io.cdap.cdap.common.NotFoundException) ProgramId(io.cdap.cdap.proto.id.ProgramId) ProgramStatus(io.cdap.cdap.proto.ProgramStatus)

Aggregations

ConflictException (io.cdap.cdap.common.ConflictException)18 NotFoundException (io.cdap.cdap.common.NotFoundException)7 IOException (java.io.IOException)5 POST (javax.ws.rs.POST)5 Path (javax.ws.rs.Path)5 BadRequestException (io.cdap.cdap.common.BadRequestException)4 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)4 ProgramId (io.cdap.cdap.proto.id.ProgramId)4 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)3 AccessException (io.cdap.cdap.api.security.AccessException)3 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)3 ArtifactAlreadyExistsException (io.cdap.cdap.common.ArtifactAlreadyExistsException)3 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)3 InvalidArtifactException (io.cdap.cdap.common.InvalidArtifactException)3 NotImplementedException (io.cdap.cdap.common.NotImplementedException)3 ServiceException (io.cdap.cdap.common.ServiceException)3 WriteConflictException (io.cdap.cdap.internal.app.runtime.artifact.WriteConflictException)3 UnauthorizedException (io.cdap.cdap.security.spi.authorization.UnauthorizedException)3 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)2