public TephraTransactionContext(PhoenixConnection connection) throws SQLException { PhoenixTransactionClient client = connection.getQueryServices().initTransactionClient(getProvider()); assert (client instanceof TephraTransactionClient); this.txServiceClient = ((TephraTransactionClient)client).getTransactionClient(); this.txAwares = Collections.emptyList(); this.txContext = new TransactionContext(txServiceClient); }
/** * Handy method to create {@link TransactionExecutor} (See TEPHRA-71). */ public static TransactionExecutor createTransactionExecutor(TransactionExecutorFactory factory, final TransactionSystemClient txClient, final Iterable<? extends TransactionAware> txAwares) { return factory.createExecutor( () -> new TransactionContext(txClient, Iterables.transform(txAwares, Functions.<TransactionAware>identity()))); }
/** * Handy method to create {@link TransactionExecutor} (See TEPHRA-71). */ public static TransactionExecutor createTransactionExecutor(TransactionExecutorFactory factory, final TransactionSystemClient txClient, final Iterable<? extends TransactionAware> txAwares) { return factory.createExecutor( () -> new TransactionContext(txClient, Iterables.transform(txAwares, Functions.<TransactionAware>identity()))); }
/** * Used by a writer to wait on a fence so that changes are visible to all readers with in-progress transactions. * * @param fenceId uniquely identifies the data that this fence is used to synchronize. * If the data is a table cell then this id can be composed of the table name, row key * and column key for the data. * @return {@link FenceWait} object */ public static FenceWait prepareWait(byte[] fenceId, TransactionSystemClient txClient) throws TransactionFailureException, InterruptedException, TimeoutException { return new DefaultFenceWait(new TransactionContext(txClient, new WriteFence(fenceId))); } }
@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); } }
@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); } }
public void execute(Runnable runnable) throws TransactionFailureException { TransactionContext txCtx = new TransactionContext(txSystemClient, (TransactionAware) dataset); txCtx.start(); try { runnable.run(); } catch (Throwable t) { txCtx.abort(new TransactionFailureException("runnable failed", t)); } txCtx.finish(); } }
public void execute(Runnable runnable) throws TransactionFailureException { TransactionContext txCtx = new TransactionContext(txSystemClient, (TransactionAware) dataset); txCtx.start(); try { runnable.run(); } catch (Throwable t) { txCtx.abort(new TransactionFailureException("runnable failed", t)); } txCtx.finish(); } }
public TephraTransactionContext(PhoenixConnection connection) { PhoenixTransactionClient client = connection.getQueryServices().initTransactionClient(getProvider()); assert (client instanceof TephraTransactionClient); this.txServiceClient = ((TephraTransactionClient)client).getTransactionClient(); this.txAwares = Collections.emptyList(); this.txContext = new TransactionContext(txServiceClient); }
public TephraTransactionContext(PhoenixConnection connection) throws SQLException { PhoenixTransactionClient client = connection.getQueryServices().initTransactionClient(getProvider()); assert (client instanceof TephraTransactionClient); this.txServiceClient = ((TephraTransactionClient)client).getTransactionClient(); this.txAwares = Collections.emptyList(); this.txContext = new TransactionContext(txServiceClient); }
private <I, O> O executeOnce(Function<I, O> function, I input) throws TransactionFailureException { TransactionContext txContext = new TransactionContext(txClient, txAwares); txContext.start(); O o = null; try { o = function.apply(input); } catch (Throwable e) { txContext.abort(new TransactionFailureException("Transaction function failure for transaction. ", e)); // abort will throw } // will throw if smth goes wrong txContext.finish(); return o; } }
public SecondaryIndexTable(TransactionServiceClient transactionServiceClient, Table hTable, byte[] secondaryIndex) throws IOException { secondaryIndexTableName = TableName.valueOf(hTable.getName().getNameAsString() + ".idx"); Table secondaryIndexHTable = null; try { conn = ConnectionFactory.createConnection(hTable.getConfiguration()); Admin hBaseAdmin = conn.getAdmin(); if (!hBaseAdmin.tableExists(secondaryIndexTableName)) { hBaseAdmin.createTable(new HTableDescriptor(secondaryIndexTableName)); } secondaryIndexHTable = conn.getTable(secondaryIndexTableName); } catch (Exception e) { Throwables.propagate(e); } this.secondaryIndex = secondaryIndex; this.transactionAwareHTable = new TransactionAwareHTable(hTable); this.secondaryIndexTable = new TransactionAwareHTable(secondaryIndexHTable); this.transactionContext = new TransactionContext(transactionServiceClient, transactionAwareHTable, secondaryIndexTable); }
public SecondaryIndexTable(TransactionServiceClient transactionServiceClient, HTableInterface hTable, byte[] secondaryIndex) { secondaryIndexTableName = TableName.valueOf(hTable.getName().getNameAsString() + ".idx"); HTable secondaryIndexHTable = null; HBaseAdmin hBaseAdmin = null; try { hBaseAdmin = new HBaseAdmin(hTable.getConfiguration()); if (!hBaseAdmin.tableExists(secondaryIndexTableName)) { hBaseAdmin.createTable(new HTableDescriptor(secondaryIndexTableName)); } secondaryIndexHTable = new HTable(hTable.getConfiguration(), secondaryIndexTableName); } catch (Exception e) { Throwables.propagate(e); } finally { try { hBaseAdmin.close(); } catch (Exception e) { Throwables.propagate(e); } } this.secondaryIndex = secondaryIndex; this.transactionAwareHTable = new TransactionAwareHTable(hTable); this.secondaryIndexTable = new TransactionAwareHTable(secondaryIndexHTable); this.transactionContext = new TransactionContext(transactionServiceClient, transactionAwareHTable, secondaryIndexTable); }
public SecondaryIndexTable(TransactionServiceClient transactionServiceClient, HTableInterface hTable, byte[] secondaryIndex) { secondaryIndexTableName = TableName.valueOf(hTable.getName().getNameAsString() + ".idx"); HTable secondaryIndexHTable = null; HBaseAdmin hBaseAdmin = null; try { hBaseAdmin = new HBaseAdmin(hTable.getConfiguration()); if (!hBaseAdmin.tableExists(secondaryIndexTableName)) { hBaseAdmin.createTable(new HTableDescriptor(secondaryIndexTableName)); } secondaryIndexHTable = new HTable(hTable.getConfiguration(), secondaryIndexTableName); } catch (Exception e) { Throwables.propagate(e); } finally { try { hBaseAdmin.close(); } catch (Exception e) { Throwables.propagate(e); } } this.secondaryIndex = secondaryIndex; this.transactionAwareHTable = new TransactionAwareHTable(hTable); this.secondaryIndexTable = new TransactionAwareHTable(secondaryIndexHTable); this.transactionContext = new TransactionContext(transactionServiceClient, transactionAwareHTable, secondaryIndexTable); }
public SecondaryIndexTable(TransactionServiceClient transactionServiceClient, HTableInterface hTable, byte[] secondaryIndex) { secondaryIndexTableName = TableName.valueOf(hTable.getName().getNameAsString() + ".idx"); HTable secondaryIndexHTable = null; HBaseAdmin hBaseAdmin = null; try { hBaseAdmin = new HBaseAdmin(hTable.getConfiguration()); if (!hBaseAdmin.tableExists(secondaryIndexTableName)) { hBaseAdmin.createTable(new HTableDescriptor(secondaryIndexTableName)); } secondaryIndexHTable = new HTable(hTable.getConfiguration(), secondaryIndexTableName); } catch (Exception e) { Throwables.propagate(e); } finally { try { hBaseAdmin.close(); } catch (Exception e) { Throwables.propagate(e); } } this.secondaryIndex = secondaryIndex; this.transactionAwareHTable = new TransactionAwareHTable(hTable); this.secondaryIndexTable = new TransactionAwareHTable(secondaryIndexHTable); this.transactionContext = new TransactionContext(transactionServiceClient, transactionAwareHTable, secondaryIndexTable); }
public SecondaryIndexTable(TransactionServiceClient transactionServiceClient, HTableInterface hTable, byte[] secondaryIndex) { secondaryIndexTableName = TableName.valueOf(hTable.getName().getNameAsString() + ".idx"); HTable secondaryIndexHTable = null; HBaseAdmin hBaseAdmin = null; try { hBaseAdmin = new HBaseAdmin(hTable.getConfiguration()); if (!hBaseAdmin.tableExists(secondaryIndexTableName)) { hBaseAdmin.createTable(new HTableDescriptor(secondaryIndexTableName)); } secondaryIndexHTable = new HTable(hTable.getConfiguration(), secondaryIndexTableName); } catch (Exception e) { Throwables.propagate(e); } finally { try { hBaseAdmin.close(); } catch (Exception e) { Throwables.propagate(e); } } this.secondaryIndex = secondaryIndex; this.transactionAwareHTable = new TransactionAwareHTable(hTable); this.secondaryIndexTable = new TransactionAwareHTable(secondaryIndexHTable); this.transactionContext = new TransactionContext(transactionServiceClient, transactionAwareHTable, secondaryIndexTable); }
@Test public void testRollbackOfPartitionCreateThenDelete() throws Exception { PartitionedFileSet pfs = dsFrameworkUtil.getInstance(pfsInstance); TransactionContext txContext = new TransactionContext(txClient, (TransactionAware) pfs); txContext.start(); Assert.assertNull(pfs.getPartition(PARTITION_KEY)); Location outputLocation = createPartition(pfs, PARTITION_KEY, "file"); Assert.assertNotNull(pfs.getPartition(PARTITION_KEY)); pfs.dropPartition(PARTITION_KEY); txContext.abort(); // the file shouldn't exist because the transaction was aborted (AND because it was dropped at the end of the tx) Assert.assertFalse(outputLocation.exists()); }
@Test public void testMetadataForNonexistentPartition() throws Exception { PartitionedFileSet pfs = dsFrameworkUtil.getInstance(pfsInstance); PartitionKey key = generateUniqueKey(); TransactionContext txContext = new TransactionContext(txClient, (TransactionAware) pfs); txContext.start(); try { // didn't add any partitions to the dataset, so any partition key should throw a PartitionNotFoundException pfs.addMetadata(key, "metaKey", "metaValue"); Assert.fail("Expected not to find key: " + key); } catch (PartitionNotFoundException e) { Assert.assertEquals(pfsInstance.getEntityName(), e.getPartitionedFileSetName()); Assert.assertEquals(key, e.getPartitionKey()); } finally { txContext.abort(); } }
@Test public void testRollbackOnJobFailure() throws Exception { // tests the logic of #onFailure method Map<String, String> args = new HashMap<>(); FileSetArguments.setOutputPath(args, "custom/output/path"); PartitionedFileSetArguments.setOutputPartitionKey(args, PARTITION_KEY); PartitionedFileSet pfs = dsFrameworkUtil.getInstance(pfsInstance, args); TransactionContext txContext = new TransactionContext(txClient, (TransactionAware) pfs); txContext.start(); Location outputLocation = pfs.getEmbeddedFileSet().getOutputLocation(); Assert.assertFalse(outputLocation.exists()); outputLocation.mkdirs(); Assert.assertTrue(outputLocation.exists()); ((PartitionedFileSetDataset) pfs).onFailure(); txContext.abort(); // because the previous transaction aborted, the partition as well as the directory for it will not exist txContext.start(); Assert.assertNull(pfs.getPartition(PARTITION_KEY)); Assert.assertFalse(outputLocation.exists()); txContext.finish(); }
@Test public void testRollbackOnTransactionAbort() throws Exception { PartitionedFileSet pfs = dsFrameworkUtil.getInstance(pfsInstance); TransactionContext txContext = new TransactionContext(txClient, (TransactionAware) pfs); txContext.start(); Location outputLocation = createPartition(pfs, PARTITION_KEY, "file");; Assert.assertNotNull(pfs.getPartition(PARTITION_KEY)); Assert.assertTrue(pfs.getPartition(PARTITION_KEY).getLocation().exists()); txContext.abort(); // because the previous transaction aborted, the partition as well as the file will not exist txContext.start(); Assert.assertNull(pfs.getPartition(PARTITION_KEY)); Assert.assertFalse(outputLocation.exists()); txContext.finish(); }