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);
}
}
}
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());
}
}
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());
}
}
};
}
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);
}
}
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);
}
}
Aggregations