use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class BufferingTableTest method testTxChangePrefix.
// This test is in Buffering table because it needs to test the transaction change prefix
@Test
public void testTxChangePrefix() throws Exception {
String tableName = "same";
DatasetAdmin admin1 = getTableAdmin(CONTEXT1, tableName);
DatasetAdmin admin2 = getTableAdmin(CONTEXT2, tableName);
admin1.create();
admin2.create();
try {
BufferingTable table1 = getTable(CONTEXT1, tableName);
BufferingTable table2 = getTable(CONTEXT2, tableName);
// write some values in table1
Transaction tx1 = txClient.startShort();
table1.startTx(tx1);
table1.put(R1, a(C1), a(V1));
Collection<byte[]> tx1Changes = table1.getTxChanges();
Assert.assertTrue(txClient.canCommit(tx1, tx1Changes));
Assert.assertTrue(table1.commitTx());
Assert.assertTrue(txClient.commit(tx1));
table1.postTxCommit();
// write some values in table2
Transaction tx2 = txClient.startShort();
table2.startTx(tx2);
table2.put(R1, a(C1), a(V1));
Collection<byte[]> tx2Changes = table2.getTxChanges();
Assert.assertTrue(txClient.canCommit(tx2, tx2Changes));
Assert.assertTrue(table2.commitTx());
Assert.assertTrue(txClient.commit(tx2));
table1.postTxCommit();
String tx1ChangePrefix = new String(table1.getNameAsTxChangePrefix());
String tx2ChangePrefix = new String(table2.getNameAsTxChangePrefix());
String tx1Change = new String(((ArrayList<byte[]>) tx1Changes).get(0));
String tx2Change = new String(((ArrayList<byte[]>) tx2Changes).get(0));
Assert.assertNotEquals(tx1ChangePrefix, tx2ChangePrefix);
Assert.assertTrue(tx1ChangePrefix.contains(NAMESPACE1.getEntityName()));
Assert.assertTrue(tx2ChangePrefix.contains(NAMESPACE2.getEntityName()));
Assert.assertTrue(tx1Change.startsWith(tx1ChangePrefix));
Assert.assertTrue(tx2Change.startsWith(tx2ChangePrefix));
} finally {
admin1.drop();
admin2.drop();
}
}
use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class DatasetAdminService method createOrUpdate.
/**
* Configures and creates a Dataset
*
* @param datasetInstanceId dataset instance to be created
* @param typeMeta type meta for the dataset
* @param props dataset instance properties
* @param existing if dataset already exists (in case of update), the existing properties
* @return dataset specification
*/
public DatasetSpecification createOrUpdate(final DatasetId datasetInstanceId, final DatasetTypeMeta typeMeta, final DatasetProperties props, @Nullable final DatasetSpecification existing) throws Exception {
if (existing == null) {
LOG.info("Creating dataset instance {}, type meta: {}", datasetInstanceId, typeMeta);
} else {
LOG.info("Updating dataset instance {}, type meta: {}, existing: {}", datasetInstanceId, typeMeta, existing);
}
try (DatasetClassLoaderProvider classLoaderProvider = new DirectoryClassLoaderProvider(cConf, locationFactory)) {
final DatasetContext context = DatasetContext.from(datasetInstanceId.getNamespace());
UserGroupInformation ugi = getUgiForDataset(impersonator, datasetInstanceId);
final DatasetType type = ImpersonationUtils.doAs(ugi, new Callable<DatasetType>() {
@Override
public DatasetType call() throws Exception {
DatasetType type = dsFramework.getDatasetType(typeMeta, null, classLoaderProvider);
if (type == null) {
throw new BadRequestException(String.format("Cannot instantiate dataset type using provided type meta: %s", typeMeta));
}
return type;
}
});
DatasetSpecification spec = ImpersonationUtils.doAs(ugi, new Callable<DatasetSpecification>() {
@Override
public DatasetSpecification call() throws Exception {
DatasetSpecification spec = existing == null ? type.configure(datasetInstanceId.getEntityName(), props) : type.reconfigure(datasetInstanceId.getEntityName(), props, existing);
DatasetAdmin admin = type.getAdmin(context, spec);
if (existing != null) {
if (admin instanceof Updatable) {
((Updatable) admin).update(existing);
} else {
admin.upgrade();
}
} else {
admin.create();
}
return spec;
}
});
// Writing system metadata should be done without impersonation since user may not have access to system tables.
writeSystemMetadata(datasetInstanceId, spec, props, typeMeta, type, context, existing != null, ugi);
return spec;
} catch (Exception e) {
if (e instanceof IncompatibleUpdateException) {
// this is expected to happen if user provides bad update properties, so we log this as debug
LOG.debug("Incompatible update for dataset '{}'", datasetInstanceId, e);
} else {
LOG.error("Error {} dataset '{}': {}", existing == null ? "creating" : "updating", datasetInstanceId, e.getMessage(), e);
}
throw e;
}
}
use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class DatasetAdminService method truncate.
public void truncate(DatasetId datasetInstanceId) throws Exception {
DatasetAdmin datasetAdmin = getDatasetAdmin(datasetInstanceId);
datasetAdmin.truncate();
}
use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class DatasetAdminService method drop.
public void drop(final DatasetId datasetInstanceId, final DatasetTypeMeta typeMeta, final DatasetSpecification spec) throws Exception {
LOG.info("Dropping dataset with spec: {}, type meta: {}", spec, typeMeta);
try (DatasetClassLoaderProvider classLoaderProvider = new DirectoryClassLoaderProvider(cConf, locationFactory)) {
UserGroupInformation ugi = getUgiForDataset(impersonator, datasetInstanceId);
ImpersonationUtils.doAs(ugi, new Callable<Void>() {
@Override
public Void call() throws Exception {
DatasetType type = dsFramework.getDatasetType(typeMeta, null, classLoaderProvider);
if (type == null) {
throw new BadRequestException(String.format("Cannot instantiate dataset type using provided type meta: %s", typeMeta));
}
DatasetAdmin admin = type.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec);
admin.drop();
return null;
}
});
}
// Remove metadata for the dataset
metadataStore.removeMetadata(datasetInstanceId);
}
use of co.cask.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class InMemoryDatasetOpExecutor method drop.
@Override
public void drop(DatasetId datasetInstanceId, DatasetTypeMeta typeMeta, DatasetSpecification spec) 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);
}
DatasetAdmin admin = type.getAdmin(DatasetContext.from(datasetInstanceId.getNamespace()), spec);
admin.drop();
}
Aggregations