/** * Aborts the given transaction, and rolls back all data set changes. If rollback fails, * the transaction is invalidated. If an exception is caught during rollback, the exception * is rethrown wrapped in a TransactionFailureException, after all remaining TransactionAwares have * completed rollback. * * @throws TransactionFailureException for any exception that is encountered. */ public void abort() throws TransactionFailureException { abort(null); }
/** * Aborts the given transaction, and rolls back all data set changes. If rollback fails, * the transaction is invalidated. If an exception is caught during rollback, the exception * is rethrown wrapped in a TransactionFailureException, after all remaining TransactionAwares have * completed rollback. * * @throws TransactionFailureException for any exception that is encountered. */ public void abort() throws TransactionFailureException { abort(null); }
@Override public void await(long timeout, TimeUnit timeUnit) throws TransactionFailureException, InterruptedException, TimeoutException { Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); long sleepTimeMicros = timeUnit.toMicros(timeout) / 10; // Have sleep time to be within 1 microsecond and 500 milliseconds sleepTimeMicros = Math.max(Math.min(sleepTimeMicros, 500 * 1000), 1); while (stopwatch.elapsedTime(timeUnit) < timeout) { txContext.start(); try { txContext.finish(); return; } catch (TransactionFailureException e) { LOG.error("Got exception waiting for fence. Sleeping for {} microseconds", sleepTimeMicros, e); txContext.abort(); TimeUnit.MICROSECONDS.sleep(sleepTimeMicros); } } throw new TimeoutException("Timeout waiting for fence"); } }
@Override public void await(long timeout, TimeUnit timeUnit) throws TransactionFailureException, InterruptedException, TimeoutException { Stopwatch stopwatch = new Stopwatch(); stopwatch.start(); long sleepTimeMicros = timeUnit.toMicros(timeout) / 10; // Have sleep time to be within 1 microsecond and 500 milliseconds sleepTimeMicros = Math.max(Math.min(sleepTimeMicros, 500 * 1000), 1); while (stopwatch.elapsedTime(timeUnit) < timeout) { txContext.start(); try { txContext.finish(); return; } catch (TransactionFailureException e) { LOG.error("Got exception waiting for fence. Sleeping for {} microseconds", sleepTimeMicros, e); txContext.abort(); TimeUnit.MICROSECONDS.sleep(sleepTimeMicros); } } throw new TimeoutException("Timeout waiting for fence"); } }
private void commit() throws TransactionFailureException { boolean commitSuccess = false; try { commitSuccess = txClient.commit(currentTx); } catch (TransactionNotInProgressException e) { String message = String.format("Transaction %d is not in progress.", currentTx.getTransactionId()); LOG.warn(message, e); abort(new TransactionFailureException(message, e)); // abort will throw that exception } catch (Throwable e) { String message = String.format("Exception from commit for transaction %d.", currentTx.getTransactionId()); LOG.warn(message, e); abort(new TransactionFailureException(message, e)); // abort will throw that exception } if (!commitSuccess) { String message = String.format("Conflict detected for transaction %d.", currentTx.getTransactionId()); abort(new TransactionConflictException(message)); // abort will throw } }
private void commit() throws TransactionFailureException { boolean commitSuccess = false; try { commitSuccess = txClient.commit(currentTx); } catch (TransactionNotInProgressException e) { String message = String.format("Transaction %d is not in progress.", currentTx.getTransactionId()); LOG.warn(message, e); abort(new TransactionFailureException(message, e)); // abort will throw that exception } catch (Throwable e) { String message = String.format("Exception from commit for transaction %d.", currentTx.getTransactionId()); LOG.warn(message, e); abort(new TransactionFailureException(message, e)); // abort will throw that exception } if (!commitSuccess) { String message = String.format("Conflict detected for transaction %d.", currentTx.getTransactionId()); abort(new TransactionConflictException(message)); // abort will throw } }
/** * Close the current consumer if there is one. */ @Override public void close() throws IOException { try { if (consumer != null && consumer instanceof Closeable) { // Call close in a new transaction. // TODO (terence): Actually need to coordinates with other flowlets to drain the queue. TransactionContext txContext = dataFabricFacade.createTransactionManager(); txContext.start(); try { ((Closeable) consumer).close(); txContext.finish(); } catch (TransactionFailureException e) { LOG.warn("Fail to commit transaction when closing consumer."); txContext.abort(); } } } catch (Exception e) { LOG.warn("Fail to close queue consumer.", e); } consumer = null; }
public Result[] get(List<Get> gets) throws IOException { try { transactionContext.start(); Result[] result = transactionAwareHTable.get(gets); transactionContext.finish(); return result; } catch (Exception e) { try { transactionContext.abort(); } catch (TransactionFailureException e1) { throw new IOException("Could not rollback transaction", e1); } } return null; }
public Result[] get(List<Get> gets) throws IOException { try { transactionContext.start(); Result[] result = transactionAwareHTable.get(gets); transactionContext.finish(); return result; } catch (Exception e) { try { transactionContext.abort(); } catch (TransactionFailureException e1) { throw new IOException("Could not rollback transaction", e1); } } return null; }
public Result[] get(List<Get> gets) throws IOException { try { transactionContext.start(); Result[] result = transactionAwareHTable.get(gets); transactionContext.finish(); return result; } catch (Exception e) { try { transactionContext.abort(); } catch (TransactionFailureException e1) { throw new IOException("Could not rollback transaction", e1); } } return null; }
public Result[] get(List<Get> gets) throws IOException { try { transactionContext.start(); Result[] result = transactionAwareHTable.get(gets); transactionContext.finish(); return result; } catch (Exception e) { try { transactionContext.abort(); } catch (TransactionFailureException e1) { throw new IOException("Could not rollback transaction", e1); } } return null; }
private void initFlowlet() throws InterruptedException { final TransactionContext txContext = flowletContext.createTransactionContext(); try { txContext.start(); try { LOG.info("Initializing flowlet: " + flowletContext); flowlet.initialize(flowletContext); LOG.info("Flowlet initialized: " + flowletContext); } catch (Throwable t) { LOG.error("User code exception. Aborting transaction.", t); txContext.abort(new TransactionFailureException("User code exception. Aborting transaction", t)); throw Throwables.propagate(t); } txContext.finish(); } catch (TransactionFailureException e) { LOG.error("Flowlet throws exception during flowlet initialize: " + flowletContext, e); throw Throwables.propagate(e); } }
public Result[] get(List<Get> gets) throws IOException { try { transactionContext.start(); Result[] result = transactionAwareHTable.get(gets); transactionContext.finish(); return result; } catch (Exception e) { try { transactionContext.abort(); } catch (TransactionFailureException e1) { throw new IOException("Could not rollback transaction", e1); } } return null; }
public Result[] get(List<Get> gets) throws IOException { try { transactionContext.start(); Result[] result = transactionAwareHTable.get(gets); transactionContext.finish(); return result; } catch (Exception e) { try { transactionContext.abort(); } catch (TransactionFailureException e1) { throw new IOException("Could not rollback transaction", e1); } } return null; }
public Result[] get(List<Get> gets) throws IOException { try { transactionContext.start(); Result[] result = transactionAwareHTable.get(gets); transactionContext.finish(); return result; } catch (Exception e) { try { transactionContext.abort(); } catch (TransactionFailureException e1) { throw new IOException("Could not rollback transaction", e1); } } return null; }
public Result[] get(List<Get> gets) throws IOException { try { transactionContext.start(); Result[] result = transactionAwareHTable.get(gets); transactionContext.finish(); return result; } catch (Exception e) { try { transactionContext.abort(); } catch (TransactionFailureException e1) { throw new IOException("Could not rollback transaction", e1); } } return null; }
private void destroyFlowlet() { final TransactionContext txContext = flowletContext.createTransactionContext(); try { txContext.start(); try { LOG.info("Destroying flowlet: " + flowletContext); flowlet.destroy(); LOG.info("Flowlet destroyed: " + flowletContext); } catch (Throwable t) { LOG.error("User code exception. Aborting transaction.", t); txContext.abort(new TransactionFailureException("User code exception. Aborting transaction", t)); // No need to propagate, as it is shutting down. } txContext.finish(); } catch (TransactionFailureException e) { LOG.error("Flowlet throws exception during flowlet destroy: " + flowletContext, e); // No need to propagate, as it is shutting down. } }
private void assertTxnConflict(TransactionContext txContext) throws Exception { try { txContext.finish(); Assert.fail("Expected transaction to fail"); } catch (TransactionConflictException e) { // Expected txContext.abort(); } }
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; } }
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; } }