Search in sources :

Example 6 with TransactionConflictException

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

the class SnapshotTransactionTest method testTransactionIsolation.

@Test
public void testTransactionIsolation() throws Exception {
    // This test creates multiple partially-done transactions and ensures that even after writes
    // and commits, the value returned by get() is consistent with either the initial value or
    // the most recently written value within the transaction.
    int numColumns = 10;
    int numTransactions = 500;
    Transaction initTransaction = txManager.createNewTransaction();
    for (int i = 0; i < numColumns; i++) {
        Cell cell = Cell.create(PtBytes.toBytes("row"), PtBytes.toBytes("column" + i));
        BigInteger cellValue = BigInteger.valueOf(i);
        initTransaction.put(TABLE, ImmutableMap.of(cell, cellValue.toByteArray()));
    }
    initTransaction.commit();
    List<Transaction> allTransactions = Lists.newArrayList();
    List<List<BigInteger>> writtenValues = Lists.newArrayList();
    for (int i = 0; i < numTransactions; i++) {
        allTransactions.add(txManager.createNewTransaction());
        List<BigInteger> initialValues = Lists.newArrayList();
        for (int j = 0; j < numColumns; j++) {
            initialValues.add(BigInteger.valueOf(j));
        }
        writtenValues.add(initialValues);
    }
    Random random = new Random(1);
    for (int i = 0; i < 10000 && !allTransactions.isEmpty(); i++) {
        int transactionIndex = random.nextInt(allTransactions.size());
        Transaction t = allTransactions.get(transactionIndex);
        int actionCode = random.nextInt(30);
        if (actionCode == 0) {
            // Commit the transaction and remove it.
            try {
                t.commit();
            } catch (TransactionConflictException e) {
            // Ignore any conflicts; the transaction just fails
            }
            allTransactions.remove(transactionIndex);
            writtenValues.remove(transactionIndex);
        } else if (actionCode < 15) {
            // Write a new value to a random column
            int columnNumber = random.nextInt(numColumns);
            Cell cell = Cell.create(PtBytes.toBytes("row"), PtBytes.toBytes("column" + columnNumber));
            BigInteger newValue = BigInteger.valueOf(random.nextInt(100000));
            t.put(TABLE, ImmutableMap.of(cell, newValue.toByteArray()));
            writtenValues.get(transactionIndex).set(columnNumber, newValue);
        } else {
            // Read and verify the value of a random column
            int columnNumber = random.nextInt(numColumns);
            Cell cell = Cell.create(PtBytes.toBytes("row"), PtBytes.toBytes("column" + columnNumber));
            byte[] storedValue = t.get(TABLE, Collections.singleton(cell)).get(cell);
            BigInteger expectedValue = writtenValues.get(transactionIndex).get(columnNumber);
            assertEquals(expectedValue, new BigInteger(storedValue));
        }
    }
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) Random(java.util.Random) BigInteger(java.math.BigInteger) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Test(org.junit.Test)

Example 7 with TransactionConflictException

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

the class AbstractTransactionTest method testWriteWriteConflict.

@Test
public void testWriteWriteConflict() {
    Transaction t1 = startTransaction();
    Transaction t2 = startTransaction();
    put(t1, "row1", "col1", "v1");
    put(t2, "row1", "col1", "v2");
    t1.commit();
    try {
        t2.commit();
        fail("Expected write-write conflict.");
    } catch (TransactionConflictException e) {
    // expected
    }
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) Test(org.junit.Test)

Example 8 with TransactionConflictException

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

the class AbstractTransactionTest method testWriteWriteConflict2.

@Test
public void testWriteWriteConflict2() {
    Transaction t2 = startTransaction();
    Transaction t1 = startTransaction();
    put(t1, "row1", "col1", "v1");
    put(t2, "row1", "col1", "v2");
    t1.commit();
    try {
        t2.commit();
        fail("Expected write-write conflict.");
    } catch (TransactionConflictException e) {
    // expected
    }
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) Test(org.junit.Test)

Aggregations

Transaction (com.palantir.atlasdb.transaction.api.Transaction)8 TransactionConflictException (com.palantir.atlasdb.transaction.api.TransactionConflictException)8 Test (org.junit.Test)8 Cell (com.palantir.atlasdb.keyvalue.api.Cell)5 Throwables (com.google.common.base.Throwables)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableMultimap (com.google.common.collect.ImmutableMultimap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 Iterables (com.google.common.collect.Iterables)1 Multimap (com.google.common.collect.Multimap)1 Sets (com.google.common.collect.Sets)1 Futures (com.google.common.util.concurrent.Futures)1 ByteString (com.google.protobuf.ByteString)1 AtlasDbTestCase (com.palantir.atlasdb.AtlasDbTestCase)1 PtBytes (com.palantir.atlasdb.encoding.PtBytes)1 StreamPersistence (com.palantir.atlasdb.protos.generated.StreamPersistence)1 StreamMetadata (com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata)1 DeletingStreamStore (com.palantir.atlasdb.schema.stream.generated.DeletingStreamStore)1 KeyValueTable (com.palantir.atlasdb.schema.stream.generated.KeyValueTable)1