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));
}
}
}
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
}
}
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
}
}
Aggregations