use of org.apache.tephra.TransactionAware in project phoenix by apache.
the class MutationState method addDMLFence.
/**
* Add an entry to the change set representing the DML operation that is starting.
* These entries will not conflict with each other, but they will conflict with a
* DDL operation of creating an index. See {@link #addDMLFence(PTable)} and TEPHRA-157
* for more information.
* @param table the table which is doing DML
* @throws SQLException
*/
private void addDMLFence(PTable table) throws SQLException {
if (table.getType() == PTableType.INDEX || !table.isTransactional()) {
return;
}
byte[] logicalKey = table.getName().getBytes();
TransactionAware logicalTxAware = VisibilityFence.create(logicalKey);
if (this.txContext == null) {
this.txAwares.add(logicalTxAware);
} else {
this.txContext.addTransactionAware(logicalTxAware);
}
byte[] physicalKey = table.getPhysicalName().getBytes();
if (Bytes.compareTo(physicalKey, logicalKey) != 0) {
TransactionAware physicalTxAware = VisibilityFence.create(physicalKey);
if (this.txContext == null) {
this.txAwares.add(physicalTxAware);
} else {
this.txContext.addTransactionAware(physicalTxAware);
}
}
}
use of org.apache.tephra.TransactionAware 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);
}
}
use of org.apache.tephra.TransactionAware in project cdap by caskdata.
the class DynamicPartitioningOutputCommitter method abortJob.
@Override
public void abortJob(JobContext context, JobStatus.State state) throws IOException {
// We want to rollback these partitions.
if (outputDataset != null) {
try {
try {
((TransactionAware) outputDataset).rollbackTx();
} catch (Throwable t) {
LOG.warn("Attempt to rollback partitions failed", t);
}
// if this is non-null, then at least some paths have been created. We need to remove them
if (relativePaths != null) {
for (String pathToDelete : relativePaths) {
Location locationToDelete = outputDataset.getEmbeddedFileSet().getLocation(pathToDelete);
try {
if (locationToDelete.exists()) {
locationToDelete.delete(true);
}
} catch (IOException e) {
// not sure how this can happen, but we want to keep going. Log and continue
LOG.warn("Attempt to clean up partition location {} failed", locationToDelete.toURI().getPath(), e);
}
}
}
} finally {
// clear this so that we only attempt rollback once in case it gets called multiple times
outputDataset = null;
relativePaths = null;
}
}
super.abortJob(context, state);
}
use of org.apache.tephra.TransactionAware in project cdap by caskdata.
the class DatasetInstanceHandlerTest method testCreateDelete.
@Test
public void testCreateDelete() throws Exception {
try {
deployModule("default-table", InMemoryTableModule.class);
deployModule("default-core", CoreDatasetsModule.class);
// cannot create instance with same name again
Assert.assertEquals(HttpStatus.SC_OK, createInstance("myTable1", "table", DatasetProperties.EMPTY).getResponseCode());
Assert.assertEquals(HttpStatus.SC_OK, createInstance("myTable2", "table", DatasetProperties.EMPTY).getResponseCode());
Assert.assertEquals(2, getInstances().getResponseObject().size());
// we want to verify that data is also gone, so we write smth to tables first
final Table table1 = dsFramework.getDataset(NamespaceId.DEFAULT.dataset("myTable1"), DatasetDefinition.NO_ARGUMENTS, null);
final Table table2 = dsFramework.getDataset(NamespaceId.DEFAULT.dataset("myTable2"), DatasetDefinition.NO_ARGUMENTS, null);
Assert.assertNotNull(table1);
Assert.assertNotNull(table2);
TransactionExecutor txExecutor = new DefaultTransactionExecutor(new InMemoryTxSystemClient(txManager), ImmutableList.of((TransactionAware) table1, (TransactionAware) table2));
txExecutor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table1.put(new Put("key1", "col1", "val1"));
table2.put(new Put("key2", "col2", "val2"));
}
});
// verify that we can read the data
txExecutor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("val1", table1.get(new Get("key1", "col1")).getString("col1"));
Assert.assertEquals("val2", table2.get(new Get("key2", "col2")).getString("col2"));
}
});
// delete table, check that it is deleted, create again and verify that it is empty
Assert.assertEquals(HttpStatus.SC_OK, deleteInstance("myTable1").getResponseCode());
ObjectResponse<List<DatasetSpecificationSummary>> instances = getInstances();
Assert.assertEquals(1, instances.getResponseObject().size());
Assert.assertEquals("myTable2", instances.getResponseObject().get(0).getName());
Assert.assertEquals(HttpStatus.SC_OK, createInstance("myTable1", "table", DatasetProperties.EMPTY).getResponseCode());
Assert.assertEquals(2, getInstances().getResponseObject().size());
// verify that table1 is empty. Note: it is ok for test purpose to re-use the table clients
txExecutor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertTrue(table1.get(new Get("key1", "col1")).isEmpty());
Assert.assertEquals("val2", table2.get(new Get("key2", "col2")).getString("col2"));
// writing smth to table1 for subsequent test
table1.put(new Put("key3", "col3", "val3"));
}
});
// delete all tables, check that they deleted, create again and verify that they are empty
deleteInstances();
Assert.assertEquals(0, getInstances().getResponseObject().size());
Assert.assertEquals(HttpStatus.SC_OK, createInstance("myTable1", "table", DatasetProperties.EMPTY).getResponseCode());
Assert.assertEquals(HttpStatus.SC_OK, createInstance("myTable2", "table", DatasetProperties.EMPTY).getResponseCode());
Assert.assertEquals(2, getInstances().getResponseObject().size());
// verify that tables are empty. Note: it is ok for test purpose to re-use the table clients
txExecutor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertTrue(table1.get(new Get("key3", "col3")).isEmpty());
Assert.assertTrue(table2.get(new Get("key2", "col2")).isEmpty());
}
});
} finally {
// cleanup
deleteInstances();
Assert.assertEquals(HttpStatus.SC_OK, deleteModules().getResponseCode());
}
}
use of org.apache.tephra.TransactionAware in project cdap by caskdata.
the class AppMetadataStoreTest method testOldRunRecordFormat.
@Test
public void testOldRunRecordFormat() throws Exception {
DatasetId storeTable = NamespaceId.DEFAULT.dataset("testOldRunRecordFormat");
datasetFramework.addInstance(Table.class.getName(), storeTable, DatasetProperties.EMPTY);
Table table = datasetFramework.getDataset(storeTable, ImmutableMap.<String, String>of(), null);
Assert.assertNotNull(table);
final AppMetadataStore metadataStoreDataset = new AppMetadataStore(table, cConf, new AtomicBoolean(false));
TransactionExecutor txnl = txExecutorFactory.createExecutor(Collections.singleton((TransactionAware) metadataStoreDataset));
ApplicationId application = NamespaceId.DEFAULT.app("app");
final ProgramId program = application.program(ProgramType.values()[ProgramType.values().length - 1], "program");
final RunId runId = RunIds.generate();
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
metadataStoreDataset.recordProgramStartOldFormat(program, runId.getId(), RunIds.getTime(runId, TimeUnit.SECONDS), null, null, null);
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Set<RunId> runIds = metadataStoreDataset.getRunningInRange(0, Long.MAX_VALUE);
Assert.assertEquals(1, runIds.size());
RunRecordMeta meta = metadataStoreDataset.getRun(program, runIds.iterator().next().getId());
Assert.assertNotNull(meta);
Assert.assertEquals(runId.getId(), meta.getPid());
}
});
txnl.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
metadataStoreDataset.recordProgramStopOldFormat(program, runId.getId(), TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis()), ProgramRunStatus.COMPLETED, null);
Map<ProgramRunId, RunRecordMeta> runRecordMap = metadataStoreDataset.getRuns(program, ProgramRunStatus.COMPLETED, 0, Long.MAX_VALUE, Integer.MAX_VALUE, null);
Assert.assertEquals(1, runRecordMap.size());
ProgramRunId programRunId = runRecordMap.keySet().iterator().next();
Assert.assertEquals(program, programRunId.getParent());
Assert.assertEquals(runId.getId(), programRunId.getRun());
}
});
}
Aggregations