@Override public Transaction createNewTransaction() { return wrap(delegate.createNewTransaction()); }
@Override public void overrideConflictHandlerForTable(TableReference table, ConflictHandler conflictHandler) { delegate.overrideConflictHandlerForTable(table, conflictHandler); } }
@Override public Transaction commitAndStartNewTransaction(Transaction txn) { return wrap(delegate.commitAndStartNewTransaction(txn)); }
private long concurrentlyIncrementValueThousandTimesAndGet() throws InterruptedException, ExecutionException { CompletionService<Void> executor = new ExecutorCompletionService<Void>( PTExecutors.newFixedThreadPool(8)); final Cell cell = Cell.create(PtBytes.toBytes("row1"), PtBytes.toBytes("column1")); Transaction t1 = txManager.createNewTransaction(); t1.put(TABLE, ImmutableMap.of(cell, EncodingUtils.encodeVarLong(0L))); t1.commit(); for (int i = 0; i < 1000; i++) { executor.submit(() -> { txManager.runTaskWithRetry((TxTask) t -> { long prev = EncodingUtils.decodeVarLong( t.get(TABLE, ImmutableSet.of(cell)).values().iterator().next()); t.put(TABLE, ImmutableMap.of(cell, EncodingUtils.encodeVarLong(prev + 1))); return null; }); return null; }); } for (int i = 0; i < 1000; i++) { Future<Void> future = executor.take(); future.get(); } t1 = txManager.createNewTransaction(); return EncodingUtils.decodeVarLong(t1.get(TABLE, ImmutableSet.of(cell)).values().iterator().next()); }
private void verifyLoadStreams(PersistentStreamStore store, long id, byte[] bytesToStore) throws IOException { Map<Long, InputStream> streams = txManager.runTaskThrowOnConflict(t -> store.loadStreams(t, ImmutableSet.of(id))); assertStreamHasBytes(streams.get(id), bytesToStore); }
@Test public void testImmutableTs() throws Exception { final long firstTs = timestampService.getFreshTimestamp(); long startTs = txManager.runTaskThrowOnConflict(t -> { Assert.assertTrue(firstTs < txManager.getImmutableTimestamp()); Assert.assertTrue(txManager.getImmutableTimestamp() < t.getTimestamp()); Assert.assertTrue(t.getTimestamp() < timestampService.getFreshTimestamp()); return t.getTimestamp(); }); Assert.assertTrue(firstTs < txManager.getImmutableTimestamp()); Assert.assertTrue(startTs < txManager.getImmutableTimestamp()); }
/** * Given Pair.of("label", task), return task label iff task succeeds. */ private Optional<String> runTaskWithInvalidLocks(Pair<String, LockAwareTransactionTask<Void, Exception>> task) { try { txManager.runTaskWithLocksThrowOnConflict(ImmutableList.of(getExpiredHeldLocksToken()), task.getRight()); return Optional.of(task.getLeft()); } catch (TransactionFailedNonRetriableException expected) { return Optional.empty(); } catch (Exception e) { throw Throwables.propagate(e); } }
private StreamMetadata getStreamMetadata(long id) { return txManager.runTaskReadOnly(t -> { StreamTestWithHashStreamMetadataTable table = StreamTestTableFactory.of() .getStreamTestWithHashStreamMetadataTable(t); StreamTestWithHashStreamMetadataRow row = StreamTestWithHashStreamMetadataRow.of(id); return table.getRow(row).get().getMetadata(); }); }
@Test public void transactionDeletesAsyncOnRollback() { DeterministicScheduler executor = new DeterministicScheduler(); TestTransactionManager deleteTxManager = new TestTransactionManagerImpl( metricsManager, keyValueService, timestampService, timestampService, lockClient, lockService, transactionService, conflictDetectionManager, sweepStrategyManager, sweepQueue, executor); Supplier<PreCommitCondition> conditionSupplier = mock(Supplier.class); when(conditionSupplier.get()).thenReturn(ALWAYS_FAILS_CONDITION) .thenReturn(PreCommitConditions.NO_OP); deleteTxManager.runTaskWithConditionWithRetry(conditionSupplier, (tx, condition) -> { tx.put(TABLE, ImmutableMap.of(TEST_CELL, PtBytes.toBytes("value"))); return null; }); verify(keyValueService, times(0)).delete(any(), any()); executor.runUntilIdle(); verify(keyValueService, times(1)).delete(any(), any()); TransactionOutcomeMetricsAssert.assertThat(transactionOutcomeMetrics) .hasSuccessfulCommits(1) .hasFailedCommits(1); }
@Test public void noRetryOnExpiredLockTokens() throws InterruptedException { HeldLocksToken expiredLockToken = getExpiredHeldLocksToken(); try { txManager.runTaskWithLocksWithRetry(ImmutableList.of(expiredLockToken), () -> null, (tx, locks) -> { tx.put(TABLE, ImmutableMap.of(TEST_CELL, PtBytes.toBytes("value"))); return null; }); fail(); } catch (TransactionLockTimeoutNonRetriableException e) { LockDescriptor descriptor = Iterables.getFirst(expiredLockToken.getLockDescriptors(), null); assertThat(e.getMessage(), containsString(descriptor.toString())); assertThat(e.getMessage(), containsString("Retry is not possible.")); } }
private void verifyLoadStreamAsFile(PersistentStreamStore store, long id, byte[] bytesToStore) throws IOException { File file = txManager.runTaskThrowOnConflict(t -> store.loadStreamAsFile(t, id)); assertArrayEquals(bytesToStore, FileUtils.readFileToByteArray(file)); }
private RowResult<byte[]> readRow(byte[] defaultRow) { Transaction readTransaction = txManager.createNewTransaction(); SortedMap<byte[], RowResult<byte[]>> allRows = readTransaction.getRows(TABLE, ImmutableSet.of(defaultRow), ColumnSelection.all()); return allRows.get(defaultRow); }
protected void overrideConflictHandlerForTable(TableReference table, ConflictHandler conflictHandler) { txManager.overrideConflictHandlerForTable(table, conflictHandler); }
@Override public Transaction commitAndStartNewTransaction(Transaction txn) { return wrap(delegate.commitAndStartNewTransaction(txn)); }
private void verifyLoadSingleStream(PersistentStreamStore store, long id, byte[] toStore) throws IOException { Optional<InputStream> stream = txManager.runTaskThrowOnConflict(t -> store.loadSingleStream(t, id)); assertTrue(stream.isPresent()); assertStreamHasBytes(stream.get(), toStore); }
@Test (expected = IllegalArgumentException.class) public void disallowPutOnEmptyObject() { Transaction t1 = txManager.createNewTransaction(); t1.put(TABLE, ImmutableMap.of(TEST_CELL, PtBytes.EMPTY_BYTE_ARRAY)); }
@Override public void overrideConflictHandlerForTable(TableReference table, ConflictHandler conflictHandler) { delegate.overrideConflictHandlerForTable(table, conflictHandler); } }
@SuppressWarnings("deprecation") private void verifyLoadStream(PersistentStreamStore store, long id, byte[] bytesToStore) throws IOException { InputStream stream = txManager.runTaskThrowOnConflict(t -> store.loadStream(t, id)); assertStreamHasBytes(stream, bytesToStore); }
private void writeCells(TableReference table, ImmutableMap<Cell, byte[]> cellsToWrite) { Transaction writeTransaction = txManager.createNewTransaction(); writeTransaction.put(table, cellsToWrite); writeTransaction.commit(); }
private Optional<InputStream> getStream(long streamId) { return txManager.runTaskThrowOnConflict(t -> { StreamTestStreamStore streamStore = StreamTestStreamStore.of(txManager, StreamTestTableFactory.of()); return streamStore.loadSingleStream(t, streamId); }); }