use of io.cdap.cdap.api.dataset.InstanceConflictException in project cdap by caskdata.
the class DatasetServiceClient method addInstance.
public void addInstance(String datasetInstanceName, String datasetType, DatasetProperties props, @Nullable KerberosPrincipalId owner) throws DatasetManagementException, UnauthorizedException {
String ownerPrincipal = owner == null ? null : owner.getPrincipal();
DatasetInstanceConfiguration creationProperties = new DatasetInstanceConfiguration(datasetType, props.getProperties(), props.getDescription(), ownerPrincipal);
HttpResponse response = doPut("datasets/" + datasetInstanceName, GSON.toJson(creationProperties));
if (HttpResponseStatus.CONFLICT.code() == response.getResponseCode()) {
throw new InstanceConflictException(String.format("Failed to add instance %s due to conflict, details: %s", datasetInstanceName, response));
}
if (HttpResponseStatus.FORBIDDEN.code() == response.getResponseCode()) {
throw new DatasetManagementException(String.format("Failed to add instance %s, details: %s", datasetInstanceName, response), new UnauthorizedException(response.getResponseBodyAsString()));
}
if (HttpResponseStatus.OK.code() != response.getResponseCode()) {
throw new DatasetManagementException(String.format("Failed to add instance %s, details: %s", datasetInstanceName, response));
}
}
use of io.cdap.cdap.api.dataset.InstanceConflictException in project cdap by caskdata.
the class InMemoryDatasetFramework method updateInstance.
@Override
public void updateInstance(DatasetId datasetInstanceId, DatasetProperties props) throws DatasetManagementException, IOException {
writeLock.lock();
try {
DatasetSpecification oldSpec = instances.get(datasetInstanceId.getParent(), datasetInstanceId);
if (oldSpec == null) {
throw new InstanceNotFoundException(datasetInstanceId.getEntityName());
}
DatasetDefinition def = getDefinitionForType(datasetInstanceId.getParent(), oldSpec.getType());
if (def == null) {
throw new DatasetManagementException(String.format("Dataset type '%s' is neither registered in the '%s' namespace nor in the system namespace", oldSpec.getType(), datasetInstanceId.getParent()));
}
DatasetSpecification spec = AbstractDatasetDefinition.reconfigure(def, datasetInstanceId.getEntityName(), props, oldSpec).setOriginalProperties(props);
if (props.getDescription() != null) {
spec = spec.setDescription(props.getDescription());
}
instances.put(datasetInstanceId.getParent(), datasetInstanceId, spec);
DatasetAdmin admin = def.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec, null);
if (admin instanceof Updatable) {
((Updatable) admin).update(oldSpec);
} else {
admin.upgrade();
}
publishAudit(datasetInstanceId, AuditType.UPDATE);
} catch (IncompatibleUpdateException e) {
throw new InstanceConflictException("Update failed for dataset instance " + datasetInstanceId, e);
} finally {
writeLock.unlock();
}
}
use of io.cdap.cdap.api.dataset.InstanceConflictException in project cdap by caskdata.
the class ExternalDatasets method registerLineage.
/**
* Register lineage for this Spark program using the given reference name
*
* @param referenceName reference name used for source
* @param accessType the access type of the lineage
* @throws DatasetManagementException thrown if there was an error in creating reference dataset
*/
public static void registerLineage(Admin admin, String referenceName, AccessType accessType, @Nullable Schema schema, Supplier<Dataset> datasetSupplier) throws DatasetManagementException {
DatasetProperties datasetProperties;
if (schema == null) {
datasetProperties = DatasetProperties.EMPTY;
} else {
datasetProperties = DatasetProperties.of(Collections.singletonMap(DatasetProperties.SCHEMA, schema.toString()));
}
try {
if (!admin.datasetExists(referenceName)) {
admin.createDataset(referenceName, EXTERNAL_DATASET_TYPE, datasetProperties);
}
} catch (InstanceConflictException ex) {
// Might happen if this is executed in parallel across multiple pipeline runs.
}
// we cannot instantiate ExternalDataset here - it is in CDAP data-fabric,
// and this code (the pipeline app) cannot depend on that. Thus, use reflection
// to invoke a method on the dataset.
Dataset ds = datasetSupplier.get();
Class<? extends Dataset> dsClass = ds.getClass();
switch(accessType) {
case READ:
invokeMethod(referenceName, ds, dsClass, "recordRead", accessType);
break;
case WRITE:
invokeMethod(referenceName, ds, dsClass, "recordWrite", accessType);
break;
default:
LOG.warn("Failed to register lineage because of unknown access type {}", accessType);
}
}
use of io.cdap.cdap.api.dataset.InstanceConflictException in project cdap by caskdata.
the class InMemoryDatasetFramework method addInstance.
@Override
public void addInstance(String datasetType, DatasetId datasetInstanceId, DatasetProperties props, @Nullable KerberosPrincipalId ownerPrincipal) throws DatasetManagementException, IOException {
if (ownerPrincipal != null) {
throw new UnsupportedOperationException("Creating dataset with owner is not supported");
}
writeLock.lock();
try {
if (instances.contains(datasetInstanceId.getParent(), datasetInstanceId)) {
throw new InstanceConflictException(String.format("Dataset instance '%s' already exists.", datasetInstanceId));
}
DatasetDefinition def = getDefinitionForType(datasetInstanceId.getParent(), datasetType);
if (def == null) {
throw new DatasetManagementException(String.format("Dataset type '%s' is neither registered in the '%s' namespace nor in the system namespace", datasetType, datasetInstanceId.getParent()));
}
DatasetSpecification spec = def.configure(datasetInstanceId.getEntityName(), props);
spec = spec.setOriginalProperties(props);
if (props.getDescription() != null) {
spec = spec.setDescription(props.getDescription());
}
def.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec, null).create();
instances.put(datasetInstanceId.getParent(), datasetInstanceId, spec);
publishAudit(datasetInstanceId, AuditType.CREATE);
LOG.info("Created dataset {} of type {}", datasetInstanceId, datasetType);
} finally {
writeLock.unlock();
}
}
use of io.cdap.cdap.api.dataset.InstanceConflictException in project cdap by caskdata.
the class AbstractDatasetFrameworkTest method testBasicManagement.
@Test
public void testBasicManagement() throws Exception {
DatasetTypeId tableType = NAMESPACE_ID.datasetType(Table.class.getName());
// Adding modules
DatasetFramework framework = getFramework();
framework.addModule(IN_MEMORY, new InMemoryTableModule());
framework.addModule(CORE, new CoreDatasetsModule());
framework.addModule(FILE, new FileSetModule());
framework.addModule(KEY_VALUE, new SingleTypeModule(SimpleKVTable.class));
// keyvalue has been added in the system namespace
Assert.assertTrue(framework.hasSystemType(Table.class.getName()));
Assert.assertFalse(framework.hasSystemType(SimpleKVTable.class.getName()));
Assert.assertTrue(framework.hasType(tableType));
Assert.assertTrue(framework.hasType(SIMPLE_KV_TYPE));
// Creating instances
framework.addInstance(Table.class.getName(), MY_TABLE, DatasetProperties.EMPTY);
Assert.assertTrue(framework.hasInstance(MY_TABLE));
DatasetSpecification spec = framework.getDatasetSpec(MY_TABLE);
Assert.assertNotNull(spec);
Assert.assertEquals(MY_TABLE.getEntityName(), spec.getName());
Assert.assertEquals(Table.class.getName(), spec.getType());
framework.addInstance(Table.class.getName(), MY_TABLE2, DatasetProperties.EMPTY);
Assert.assertTrue(framework.hasInstance(MY_TABLE2));
// Update instances
File baseDir = TMP_FOLDER.newFolder();
framework.addInstance(FileSet.class.getName(), MY_DS, FileSetProperties.builder().setBasePath(baseDir.getPath()).setDataExternal(true).build());
// this should fail because it would "internalize" external data
try {
framework.updateInstance(MY_DS, DatasetProperties.EMPTY);
Assert.fail("update should have thrown instance conflict");
} catch (InstanceConflictException e) {
// expected
}
baseDir = TMP_FOLDER.newFolder();
// this should succeed because it simply changes the external path
framework.updateInstance(MY_DS, FileSetProperties.builder().setBasePath(baseDir.getPath()).setDataExternal(true).build());
spec = framework.getDatasetSpec(MY_DS);
Assert.assertNotNull(spec);
Assert.assertEquals(baseDir.getPath(), FileSetProperties.getBasePath(spec.getProperties()));
// cleanup
try {
framework.deleteAllModules(NAMESPACE_ID);
Assert.fail("should not delete modules: there are datasets using their types");
} catch (DatasetManagementException e) {
// expected
}
// types are still there
Assert.assertTrue(framework.hasType(tableType));
Assert.assertTrue(framework.hasType(SIMPLE_KV_TYPE));
framework.deleteAllInstances(NAMESPACE_ID);
Assert.assertEquals(0, framework.getInstances(NAMESPACE_ID).size());
Assert.assertFalse(framework.hasInstance(MY_TABLE));
Assert.assertNull(framework.getDatasetSpec(MY_TABLE));
Assert.assertFalse(framework.hasInstance(MY_TABLE2));
Assert.assertNull(framework.getDatasetSpec(MY_TABLE2));
// now it should succeed
framework.deleteAllModules(NAMESPACE_ID);
Assert.assertTrue(framework.hasSystemType(Table.class.getName()));
Assert.assertFalse(framework.hasType(tableType));
Assert.assertFalse(framework.hasType(SIMPLE_KV_TYPE));
}
Aggregations