use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class NotificationTest method useTransactionTest.
@Test
public void useTransactionTest() throws Exception {
// Performing admin operations to create dataset instance
// keyValueTable is a system dataset module
namespaceAdmin.create(new NamespaceMeta.Builder().setName(namespace).build());
DatasetId myTableInstance = namespace.dataset("myTable");
dsFramework.addInstance("keyValueTable", myTableInstance, DatasetProperties.EMPTY);
final CountDownLatch receivedLatch = new CountDownLatch(1);
Assert.assertTrue(feedManager.createFeed(FEED1_INFO));
try {
Cancellable cancellable = notificationService.subscribe(FEED1, new NotificationHandler<String>() {
private int received = 0;
@Override
public Type getNotificationType() {
return String.class;
}
@Override
public void received(final String notification, NotificationContext notificationContext) {
notificationContext.execute(new TxRunnable() {
@Override
public void run(DatasetContext context) throws Exception {
KeyValueTable table = context.getDataset("myTable");
table.write("foo", String.format("%s-%d", notification, received++));
receivedLatch.countDown();
}
}, TxRetryPolicy.maxRetries(5));
}
});
// Short delay for the subscriber to setup the subscription.
TimeUnit.MILLISECONDS.sleep(500);
try {
notificationService.publish(FEED1, "foobar");
// Waiting for the subscriber to receive that notification
Assert.assertTrue(receivedLatch.await(5, TimeUnit.SECONDS));
// Read the KeyValueTable for the value updated from the subscriber.
// Need to poll it couple times since after the received method returned,
// the tx may not yet committed when we try to read it here.
final KeyValueTable table = dsFramework.getDataset(myTableInstance, DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table);
final TransactionContext txContext = new TransactionContext(txClient, table);
Tasks.waitFor(true, new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
txContext.start();
try {
return "foobar-0".equals(Bytes.toString(table.read("foo")));
} finally {
txContext.finish();
}
}
}, 5, TimeUnit.SECONDS);
} finally {
cancellable.cancel();
}
} finally {
dsFramework.deleteInstance(myTableInstance);
feedManager.deleteFeed(FEED1);
namespaceAdmin.delete(namespace);
}
}
use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class UnitTestManager method getDataset.
@Override
public <T> DataSetManager<T> getDataset(DatasetId datasetInstanceId) throws Exception {
@SuppressWarnings("unchecked") final T dataSet = datasetFramework.getDataset(datasetInstanceId, new HashMap<String, String>(), null);
try {
final TransactionContext txContext;
// not every dataset is TransactionAware. FileSets for example, are not transactional.
if (dataSet instanceof TransactionAware) {
TransactionAware txAwareDataset = (TransactionAware) dataSet;
txContext = new TransactionContext(txSystemClient, Lists.newArrayList(txAwareDataset));
txContext.start();
} else {
txContext = null;
}
return new UnitTestDatasetManager<>(dataSet, txContext);
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
Aggregations