use of io.cdap.cdap.api.dataset.DatasetManagementException in project cdap by caskdata.
the class ExternalDatasets method makeTrackable.
/**
* If the input is an external source then an external dataset is created for tracking purpose and returned.
* If the input is a regular dataset or a stream then it is already trackable, hence same input is returned.
*
* @param admin {@link Admin} used to create external dataset
* @param input input to be tracked
* @return an external dataset if input is an external source, otherwise the same input that is passed-in is returned
*/
public static Input makeTrackable(Admin admin, Input input) {
// If input is not an external source, return the same input as it can be tracked by itself.
if (!(input instanceof Input.InputFormatProviderInput)) {
return input;
}
// Input is an external source, create an external dataset so that it can be tracked.
String inputName = input.getName();
InputFormatProvider inputFormatProvider = ((Input.InputFormatProviderInput) input).getInputFormatProvider();
Map<String, String> inputFormatConfiguration = inputFormatProvider.getInputFormatConfiguration();
// this too can be tracked by itself without creating an external dataset
if (inputFormatProvider instanceof Dataset) {
return input;
}
try {
// Create an external dataset for the input format for lineage tracking
Map<String, String> arguments = new HashMap<>();
arguments.put("input.format.class", inputFormatProvider.getInputFormatClassName());
arguments.putAll(inputFormatConfiguration);
if (!admin.datasetExists(inputName)) {
// Note: the dataset properties are the same as the arguments since we cannot identify them separately
// since they are mixed up in a single configuration object (CDAP-5674)
// Also, the properties of the external dataset created will contain runtime arguments for the same reason.
admin.createDataset(inputName, EXTERNAL_DATASET_TYPE, DatasetProperties.of(arguments));
} else {
// Check if the external dataset name clashes with an existing CDAP Dataset
String datasetType = admin.getDatasetType(inputName);
if (!EXTERNAL_DATASET_TYPE.equals(datasetType)) {
throw new IllegalArgumentException("An external source cannot have the same name as an existing CDAP Dataset instance " + inputName);
}
}
return Input.ofDataset(inputName, Collections.unmodifiableMap(arguments)).alias(input.getAlias());
} catch (DatasetManagementException e) {
throw Throwables.propagate(e);
}
}
use of io.cdap.cdap.api.dataset.DatasetManagementException in project cdap by caskdata.
the class ExternalDatasets method makeTrackable.
/**
* If the output is an external sink then an external dataset is created for tracking purpose and returned.
* If the output is a regular dataset then it is already trackable, hence same output is returned.
*
* @param admin {@link Admin} used to create external dataset
* @param output output to be tracked
* @return an external dataset if output is an external sink, otherwise the same output is returned
*/
public static Output makeTrackable(Admin admin, Output output) {
// If output is not an external sink, return the same output as it can be tracked by itself.
if (!(output instanceof Output.OutputFormatProviderOutput)) {
return output;
}
// Output is an external sink, create an external dataset so that it can be tracked.
String outputName = output.getName();
OutputFormatProvider outputFormatProvider = ((Output.OutputFormatProviderOutput) output).getOutputFormatProvider();
Map<String, String> outputFormatConfiguration = outputFormatProvider.getOutputFormatConfiguration();
// this can be tracked by itself without creating an external dataset
if (outputFormatProvider instanceof Dataset) {
return output;
}
// Output is an external sink, create an external dataset so that it can be tracked.
try {
// Create an external dataset for the output format for lineage tracking
Map<String, String> arguments = new HashMap<>();
arguments.put("output.format.class", outputFormatProvider.getOutputFormatClassName());
arguments.putAll(outputFormatConfiguration);
if (!admin.datasetExists(outputName)) {
// Note: the dataset properties are the same as the arguments since we cannot identify them separately
// since they are mixed up in a single configuration object (CDAP-5674)
// Also, the properties of the external dataset created will contain runtime arguments for the same reason.
admin.createDataset(outputName, EXTERNAL_DATASET_TYPE, DatasetProperties.of(arguments));
} else {
// Check if the external dataset name clashes with an existing CDAP Dataset
String datasetType = admin.getDatasetType(outputName);
if (!EXTERNAL_DATASET_TYPE.equals(datasetType)) {
throw new IllegalArgumentException("An external sink cannot have the same name as an existing CDAP Dataset instance " + outputName);
}
}
return Output.ofDataset(outputName, Collections.unmodifiableMap(arguments)).alias(output.getAlias());
} catch (DatasetManagementException e) {
throw Throwables.propagate(e);
}
}
use of io.cdap.cdap.api.dataset.DatasetManagementException in project cdap by caskdata.
the class InMemoryDatasetFrameworkTest method getFramework.
@Override
protected DatasetFramework getFramework() throws DatasetManagementException {
InMemoryDatasetFramework framework = new InMemoryDatasetFramework(registryFactory, DEFAULT_MODULES);
framework.setAuditPublisher(inMemoryAuditPublisher);
try {
namespacePathLocator.get(NAMESPACE_ID).mkdirs();
} catch (IOException e) {
throw new DatasetManagementException(e.getMessage());
}
return framework;
}
use of io.cdap.cdap.api.dataset.DatasetManagementException 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.api.dataset.DatasetManagementException in project cdap by caskdata.
the class AppCreator method execute.
@Override
public void execute(Arguments arguments) throws Exception {
ApplicationId appId = arguments.getId();
ArtifactSummary artifactSummary = arguments.getArtifact();
if (appExists(appId) && !arguments.overwrite) {
return;
}
KerberosPrincipalId ownerPrincipalId = arguments.getOwnerPrincipal() == null ? null : new KerberosPrincipalId(arguments.getOwnerPrincipal());
// if we don't null check, it gets serialized to "null"
String configString = arguments.getConfig() == null ? null : GSON.toJson(arguments.getConfig());
try {
appLifecycleService.deployApp(appId.getParent(), appId.getApplication(), appId.getVersion(), artifactSummary, configString, x -> {
}, ownerPrincipalId, arguments.canUpdateSchedules(), false, Collections.emptyMap());
} catch (NotFoundException | UnauthorizedException | InvalidArtifactException e) {
// up to the default time limit
throw e;
} catch (DatasetManagementException e) {
if (e.getCause() instanceof UnauthorizedException) {
throw (UnauthorizedException) e.getCause();
} else {
throw new RetryableException(e);
}
} catch (Exception e) {
throw new RetryableException(e);
}
}
Aggregations