use of com.palantir.atlasdb.transaction.api.TransactionTask in project atlasdb by palantir.
the class StreamTest method testAddDelete.
@Test
public void testAddDelete() throws Exception {
final byte[] data = PtBytes.toBytes("streamed");
final long streamId = txManager.runTaskWithRetry((TransactionTask<Long, Exception>) t -> {
Sha256Hash hash = Sha256Hash.computeHash(data);
byte[] reference = "ref".getBytes();
return defaultStore.getByHashOrStoreStreamAndMarkAsUsed(t, hash, new ByteArrayInputStream(data), reference);
});
txManager.runTaskWithRetry((TransactionTask<Void, Exception>) t -> {
Optional<InputStream> inputStream = defaultStore.loadSingleStream(t, streamId);
assertTrue(inputStream.isPresent());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(inputStream.get());
assertArrayEquals(data, outputStream.toByteArray());
return null;
});
}
use of com.palantir.atlasdb.transaction.api.TransactionTask in project atlasdb by palantir.
the class StreamTest method runConflictingTasksConcurrently.
private void runConflictingTasksConcurrently(long streamId, TwoConflictingTasks tasks) throws InterruptedException {
final CountDownLatch firstLatch = new CountDownLatch(1);
final CountDownLatch secondLatch = new CountDownLatch(1);
ExecutorService exec = PTExecutors.newFixedThreadPool(2);
Future<?> firstFuture = exec.submit(() -> {
try {
txManager.runTaskThrowOnConflict(t -> {
tasks.startFirstAndFail(t, streamId);
letOtherTaskFinish(firstLatch, secondLatch);
return null;
});
fail("Because we concurrently wrote, we should have failed with TransactionConflictException.");
} catch (TransactionConflictException e) {
// expected
}
});
firstLatch.await();
Future<?> secondFuture = exec.submit((Runnable) () -> txManager.runTaskThrowOnConflict((TransactionTask<Void, RuntimeException>) t -> {
tasks.startSecondAndFinish(t, streamId);
return null;
}));
exec.shutdown();
Futures.getUnchecked(secondFuture);
secondLatch.countDown();
Futures.getUnchecked(firstFuture);
}
use of com.palantir.atlasdb.transaction.api.TransactionTask in project atlasdb by palantir.
the class SweeperTestSetup method mockTxManager.
public static LockAwareTransactionManager mockTxManager() {
LockAwareTransactionManager txManager = mock(LockAwareTransactionManager.class);
Answer runTaskAnswer = inv -> {
Object[] args = inv.getArguments();
TransactionTask<?, ?> task = (TransactionTask<?, ?>) args[0];
return task.execute(mock(Transaction.class));
};
doAnswer(runTaskAnswer).when(txManager).runTaskReadOnly(any());
doAnswer(runTaskAnswer).when(txManager).runTaskWithRetry(any());
return txManager;
}
use of com.palantir.atlasdb.transaction.api.TransactionTask in project atlasdb by palantir.
the class TableMigratorTest method testMigrationToDifferentKvs.
// Table/IndexDefinition syntax
@SuppressWarnings({ "checkstyle:Indentation", "checkstyle:RightCurly" })
@Test
public void testMigrationToDifferentKvs() throws TableMappingNotFoundException {
final TableReference tableRef = TableReference.create(Namespace.DEFAULT_NAMESPACE, "table");
final TableReference namespacedTableRef = TableReference.createFromFullyQualifiedName("namespace." + tableRef.getTablename());
TableDefinition definition = new TableDefinition() {
{
rowName();
rowComponent("r", ValueType.BLOB);
columns();
column("c", "c", ValueType.BLOB);
}
};
keyValueService.createTable(tableRef, definition.toTableMetadata().persistToBytes());
keyValueService.createTable(namespacedTableRef, definition.toTableMetadata().persistToBytes());
keyValueService.putMetadataForTable(namespacedTableRef, definition.toTableMetadata().persistToBytes());
final Cell theCell = Cell.create(PtBytes.toBytes("r1"), PtBytes.toBytes("c"));
final byte[] theValue = PtBytes.toBytes("v1");
txManager.runTaskWithRetry((TransactionTask<Void, RuntimeException>) txn -> {
Map<Cell, byte[]> values = ImmutableMap.of(theCell, theValue);
txn.put(tableRef, values);
txn.put(namespacedTableRef, values);
return null;
});
final InMemoryKeyValueService kvs2 = new InMemoryKeyValueService(false);
final ConflictDetectionManager cdm2 = ConflictDetectionManagers.createWithNoConflictDetection();
final SweepStrategyManager ssm2 = SweepStrategyManagers.completelyConservative(kvs2);
final TestTransactionManagerImpl txManager2 = new TestTransactionManagerImpl(kvs2, timestampService, lockClient, lockService, transactionService, cdm2, ssm2, MultiTableSweepQueueWriter.NO_OP);
kvs2.createTable(tableRef, definition.toTableMetadata().persistToBytes());
kvs2.createTable(namespacedTableRef, definition.toTableMetadata().persistToBytes());
TableReference checkpointTable = TableReference.create(Namespace.DEFAULT_NAMESPACE, "checkpoint");
GeneralTaskCheckpointer checkpointer = new GeneralTaskCheckpointer(checkpointTable, kvs2, txManager2);
for (final TableReference name : Lists.newArrayList(tableRef, namespacedTableRef)) {
TransactionRangeMigrator rangeMigrator = new TransactionRangeMigratorBuilder().srcTable(name).readTxManager(txManager).txManager(txManager2).checkpointer(checkpointer).build();
TableMigratorBuilder builder = new TableMigratorBuilder().srcTable(name).partitions(1).executor(PTExecutors.newSingleThreadExecutor()).checkpointer(checkpointer).rangeMigrator(rangeMigrator);
TableMigrator migrator = builder.build();
migrator.migrate();
}
checkpointer.deleteCheckpoints();
final ConflictDetectionManager verifyCdm = ConflictDetectionManagers.createWithNoConflictDetection();
final SweepStrategyManager verifySsm = SweepStrategyManagers.completelyConservative(kvs2);
final TestTransactionManagerImpl verifyTxManager = new TestTransactionManagerImpl(kvs2, timestampService, lockClient, lockService, transactionService, verifyCdm, verifySsm, MultiTableSweepQueueWriter.NO_OP);
final MutableLong count = new MutableLong();
for (final TableReference name : Lists.newArrayList(tableRef, namespacedTableRef)) {
verifyTxManager.runTaskReadOnly((TransactionTask<Void, RuntimeException>) txn -> {
BatchingVisitable<RowResult<byte[]>> bv = txn.getRange(name, RangeRequest.all());
bv.batchAccept(1000, AbortingVisitors.batching(new AbortingVisitor<RowResult<byte[]>, RuntimeException>() {
@Override
public boolean visit(RowResult<byte[]> item) throws RuntimeException {
Iterable<Entry<Cell, byte[]>> cells = item.getCells();
Entry<Cell, byte[]> entry = Iterables.getOnlyElement(cells);
Assert.assertEquals(theCell, entry.getKey());
Assert.assertArrayEquals(theValue, entry.getValue());
count.increment();
return true;
}
}));
return null;
});
}
Assert.assertEquals(2L, count.longValue());
}
use of com.palantir.atlasdb.transaction.api.TransactionTask in project atlasdb by palantir.
the class ProfileStoreTest method testDeleteImage.
@Test
public void testDeleteImage() {
final UUID userId = storeUser();
storeImage(userId);
runWithRetry(Transaction.TransactionType.AGGRESSIVE_HARD_DELETE, store -> {
store.deleteImage(userId);
return userId;
});
txnMgr.runTaskWithRetry((TransactionTask<Void, RuntimeException>) txn -> {
ProfileTableFactory tables = ProfileTableFactory.of();
UserPhotosStreamValueTable streams = tables.getUserPhotosStreamValueTable(txn);
Assert.assertTrue(streams.getAllRowsUnordered().isEmpty());
return null;
});
}
Aggregations