Search in sources :

Example 1 with InstanceConflictException

use of co.cask.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 {
    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.getCode() == response.getResponseCode()) {
        throw new InstanceConflictException(String.format("Failed to add instance %s due to conflict, details: %s", datasetInstanceName, response));
    }
    if (HttpResponseStatus.OK.getCode() != response.getResponseCode()) {
        throw new DatasetManagementException(String.format("Failed to add instance %s, details: %s", datasetInstanceName, response));
    }
}
Also used : DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) InstanceConflictException(co.cask.cdap.api.dataset.InstanceConflictException) HttpResponse(co.cask.common.http.HttpResponse) DatasetInstanceConfiguration(co.cask.cdap.proto.DatasetInstanceConfiguration)

Example 2 with InstanceConflictException

use of co.cask.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();
    }
}
Also used : DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) InstanceConflictException(co.cask.cdap.api.dataset.InstanceConflictException) InstanceNotFoundException(co.cask.cdap.api.dataset.InstanceNotFoundException) Updatable(co.cask.cdap.api.dataset.Updatable) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) AbstractDatasetDefinition(co.cask.cdap.api.dataset.lib.AbstractDatasetDefinition) DatasetDefinition(co.cask.cdap.api.dataset.DatasetDefinition) IncompatibleUpdateException(co.cask.cdap.api.dataset.IncompatibleUpdateException)

Example 3 with InstanceConflictException

use of co.cask.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));
}
Also used : DatasetTypeId(co.cask.cdap.proto.id.DatasetTypeId) Table(co.cask.cdap.api.dataset.table.Table) FileSet(co.cask.cdap.api.dataset.lib.FileSet) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) LineageWriterDatasetFramework(co.cask.cdap.data2.metadata.writer.LineageWriterDatasetFramework) InMemoryTableModule(co.cask.cdap.data2.dataset2.module.lib.inmemory.InMemoryTableModule) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) InstanceConflictException(co.cask.cdap.api.dataset.InstanceConflictException) CoreDatasetsModule(co.cask.cdap.data2.dataset2.lib.table.CoreDatasetsModule) PartitionedFileSetModule(co.cask.cdap.data2.dataset2.lib.partitioned.PartitionedFileSetModule) FileSetModule(co.cask.cdap.data2.dataset2.lib.file.FileSetModule) File(java.io.File) Test(org.junit.Test)

Example 4 with InstanceConflictException

use of co.cask.cdap.api.dataset.InstanceConflictException in project cdap by caskdata.

the class InMemoryDatasetFramework method addInstance.

@Override
public void addInstance(String datasetType, DatasetId datasetInstanceId, DatasetProperties props) throws DatasetManagementException, IOException {
    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();
    }
}
Also used : DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) InstanceConflictException(co.cask.cdap.api.dataset.InstanceConflictException) DatasetSpecification(co.cask.cdap.api.dataset.DatasetSpecification) AbstractDatasetDefinition(co.cask.cdap.api.dataset.lib.AbstractDatasetDefinition) DatasetDefinition(co.cask.cdap.api.dataset.DatasetDefinition)

Aggregations

DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)4 InstanceConflictException (co.cask.cdap.api.dataset.InstanceConflictException)4 DatasetSpecification (co.cask.cdap.api.dataset.DatasetSpecification)3 DatasetDefinition (co.cask.cdap.api.dataset.DatasetDefinition)2 AbstractDatasetDefinition (co.cask.cdap.api.dataset.lib.AbstractDatasetDefinition)2 DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)1 IncompatibleUpdateException (co.cask.cdap.api.dataset.IncompatibleUpdateException)1 InstanceNotFoundException (co.cask.cdap.api.dataset.InstanceNotFoundException)1 Updatable (co.cask.cdap.api.dataset.Updatable)1 FileSet (co.cask.cdap.api.dataset.lib.FileSet)1 Table (co.cask.cdap.api.dataset.table.Table)1 FileSetModule (co.cask.cdap.data2.dataset2.lib.file.FileSetModule)1 PartitionedFileSetModule (co.cask.cdap.data2.dataset2.lib.partitioned.PartitionedFileSetModule)1 CoreDatasetsModule (co.cask.cdap.data2.dataset2.lib.table.CoreDatasetsModule)1 InMemoryTableModule (co.cask.cdap.data2.dataset2.module.lib.inmemory.InMemoryTableModule)1 LineageWriterDatasetFramework (co.cask.cdap.data2.metadata.writer.LineageWriterDatasetFramework)1 DatasetInstanceConfiguration (co.cask.cdap.proto.DatasetInstanceConfiguration)1 DatasetTypeId (co.cask.cdap.proto.id.DatasetTypeId)1 HttpResponse (co.cask.common.http.HttpResponse)1 File (java.io.File)1