Search in sources :

Example 51 with TransactionAware

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);
        }
    }
}
Also used : TransactionAware(org.apache.tephra.TransactionAware)

Example 52 with TransactionAware

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);
    }
}
Also used : TransactionContext(org.apache.tephra.TransactionContext) TransactionAware(org.apache.tephra.TransactionAware) TransactionFailureException(org.apache.tephra.TransactionFailureException) IOException(java.io.IOException)

Example 53 with TransactionAware

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);
}
Also used : TransactionAware(org.apache.tephra.TransactionAware) IOException(java.io.IOException) Location(org.apache.twill.filesystem.Location)

Example 54 with TransactionAware

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());
    }
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) TransactionAware(org.apache.tephra.TransactionAware) Get(co.cask.cdap.api.dataset.table.Get) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) TransactionExecutor(org.apache.tephra.TransactionExecutor) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) InMemoryTxSystemClient(org.apache.tephra.inmemory.InMemoryTxSystemClient) IOException(java.io.IOException) Put(co.cask.cdap.api.dataset.table.Put) Test(org.junit.Test)

Example 55 with TransactionAware

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());
        }
    });
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) TransactionExecutor(org.apache.tephra.TransactionExecutor) ProgramId(co.cask.cdap.proto.id.ProgramId) TransactionFailureException(org.apache.tephra.TransactionFailureException) DatasetId(co.cask.cdap.proto.id.DatasetId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TransactionAware(org.apache.tephra.TransactionAware) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) RunId(org.apache.twill.api.RunId) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) Test(org.junit.Test)

Aggregations

TransactionAware (org.apache.tephra.TransactionAware)91 Test (org.junit.Test)64 TransactionExecutor (org.apache.tephra.TransactionExecutor)47 Table (co.cask.cdap.api.dataset.table.Table)37 Transaction (org.apache.tephra.Transaction)30 IOException (java.io.IOException)27 DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)22 PartitionedFileSet (co.cask.cdap.api.dataset.lib.PartitionedFileSet)22 HBaseTable (co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable)21 PartitionKey (co.cask.cdap.api.dataset.lib.PartitionKey)17 DataSetException (co.cask.cdap.api.dataset.DataSetException)16 TransactionFailureException (org.apache.tephra.TransactionFailureException)15 List (java.util.List)13 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)11 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)10 PartitionNotFoundException (co.cask.cdap.api.dataset.PartitionNotFoundException)10 TimePartitionedFileSet (co.cask.cdap.api.dataset.lib.TimePartitionedFileSet)10 ConcurrentPartitionConsumer (co.cask.cdap.api.dataset.lib.partitioned.ConcurrentPartitionConsumer)10 PartitionConsumer (co.cask.cdap.api.dataset.lib.partitioned.PartitionConsumer)10