Search in sources :

Example 51 with Transaction

use of com.palantir.atlasdb.transaction.api.Transaction in project atlasdb by palantir.

the class StreamTest method testStreamMetadataConflictDeleteFirst.

@Test
public void testStreamMetadataConflictDeleteFirst() throws Exception {
    long streamId = timestampService.getFreshTimestamp();
    runConflictingTasksConcurrently(streamId, new TwoConflictingTasks() {

        @Override
        public void startFirstAndFail(Transaction tx, long streamId) {
            StreamTestStreamStore ss = StreamTestStreamStore.of(txManager, StreamTestTableFactory.of());
            ss.storeStreams(tx, ImmutableMap.of(streamId, new ByteArrayInputStream(new byte[1])));
        }

        @Override
        public void startSecondAndFinish(Transaction tx, long streamId) {
            DeletingStreamStore deletingStreamStore = new DeletingStreamStore(StreamTestStreamStore.of(txManager, StreamTestTableFactory.of()));
            deletingStreamStore.deleteStreams(tx, ImmutableSet.of(streamId));
        }
    });
    assertStreamDoesNotExist(streamId);
}
Also used : StreamTestStreamStore(com.palantir.atlasdb.schema.stream.generated.StreamTestStreamStore) Transaction(com.palantir.atlasdb.transaction.api.Transaction) ByteArrayInputStream(java.io.ByteArrayInputStream) DeletingStreamStore(com.palantir.atlasdb.schema.stream.generated.DeletingStreamStore) Test(org.junit.Test)

Example 52 with Transaction

use of com.palantir.atlasdb.transaction.api.Transaction in project atlasdb by palantir.

the class AbstractSerializableTransactionTest method testClassicWriteSkew2Cell.

@Test
public void testClassicWriteSkew2Cell() {
    Transaction t0 = startTransaction();
    put(t0, "row1", "col1", "100");
    put(t0, "row2", "col1", "100");
    t0.commit();
    Transaction t1 = startTransaction();
    Transaction t2 = startTransaction();
    withdrawMoney(t1, true, true);
    withdrawMoney(t2, false, true);
    t2.commit();
    try {
        t1.commit();
        fail();
    } catch (TransactionSerializableConflictException e) {
    // this is expectecd to throw because it is a write skew
    }
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) TransactionSerializableConflictException(com.palantir.atlasdb.transaction.api.TransactionSerializableConflictException) Test(org.junit.Test)

Example 53 with Transaction

use of com.palantir.atlasdb.transaction.api.Transaction in project atlasdb by palantir.

the class AbstractSerializableTransactionTest method testReadOnlySerializableTransactionsIgnoreReadWriteConflicts.

@Test
public void testReadOnlySerializableTransactionsIgnoreReadWriteConflicts() {
    Transaction t0 = startTransaction();
    put(t0, "row1", "col1", "100");
    t0.commit();
    Transaction t1 = startTransaction();
    get(t1, "row1", "col1");
    Transaction t2 = startTransaction();
    put(t2, "row1", "col1", "101");
    t2.commit();
    // Succeeds, even though t1 is serializable, because it's read-only.
    t1.commit();
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) Test(org.junit.Test)

Example 54 with Transaction

use of com.palantir.atlasdb.transaction.api.Transaction in project atlasdb by palantir.

the class AbstractSerializableTransactionTest method testPhantomReadFail.

@Test
public void testPhantomReadFail() {
    String initialValue = "100";
    Transaction t0 = startTransaction();
    put(t0, "row1", "col1", initialValue);
    put(t0, "row2", "col1", initialValue);
    t0.commit();
    Transaction t1 = startTransaction();
    RowResult<byte[]> first = BatchingVisitables.getFirst(t1.getRange(TEST_TABLE, RangeRequest.builder().build()));
    put(t1, "row22", "col1", initialValue);
    Transaction t2 = startTransaction();
    put(t2, "row0", "col1", initialValue);
    t2.commit();
    try {
        t1.commit();
        fail();
    } catch (TransactionSerializableConflictException e) {
    // this is expectecd to throw because it is a write skew
    }
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) TransactionSerializableConflictException(com.palantir.atlasdb.transaction.api.TransactionSerializableConflictException) Test(org.junit.Test)

Example 55 with Transaction

use of com.palantir.atlasdb.transaction.api.Transaction in project atlasdb by palantir.

the class AbstractSerializableTransactionTest method testConcurrentWriteSkew.

@Test(expected = TransactionFailedRetriableException.class)
public void testConcurrentWriteSkew() throws InterruptedException, BrokenBarrierException {
    Transaction t0 = startTransaction();
    put(t0, "row1", "col1", "100");
    put(t0, "row2", "col1", "100");
    t0.commit();
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final Transaction t1 = startTransaction();
    ExecutorService exec = PTExecutors.newCachedThreadPool();
    Future<?> future = exec.submit((Callable<Void>) () -> {
        withdrawMoney(t1, true, false);
        barrier.await();
        t1.commit();
        return null;
    });
    Transaction t2 = startTransaction();
    withdrawMoney(t2, false, false);
    barrier.await();
    t2.commit();
    try {
        future.get();
        fail();
    } catch (ExecutionException e) {
        throw Throwables.rewrapAndThrowUncheckedException(e.getCause());
    }
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Aggregations

Transaction (com.palantir.atlasdb.transaction.api.Transaction)60 Test (org.junit.Test)51 Cell (com.palantir.atlasdb.keyvalue.api.Cell)26 Map (java.util.Map)18 RangeRequest (com.palantir.atlasdb.keyvalue.api.RangeRequest)14 TransactionSerializableConflictException (com.palantir.atlasdb.transaction.api.TransactionSerializableConflictException)14 BatchingVisitable (com.palantir.common.base.BatchingVisitable)14 ImmutableMap (com.google.common.collect.ImmutableMap)13 TransactionConflictException (com.palantir.atlasdb.transaction.api.TransactionConflictException)13 List (java.util.List)13 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)12 PtBytes (com.palantir.atlasdb.encoding.PtBytes)10 ColumnSelection (com.palantir.atlasdb.keyvalue.api.ColumnSelection)10 RowResult (com.palantir.atlasdb.keyvalue.api.RowResult)10 ExecutionException (java.util.concurrent.ExecutionException)9 ExecutorService (java.util.concurrent.ExecutorService)9 Collectors (java.util.stream.Collectors)9 ImmutableSet (com.google.common.collect.ImmutableSet)8 Multimap (com.google.common.collect.Multimap)8 Multimaps (com.google.common.collect.Multimaps)8