use of com.palantir.atlasdb.transaction.api.TransactionConflictException in project atlasdb by palantir.
the class SnapshotTransactionTest method testWriteChangedConflictsThrow.
@Test
public void testWriteChangedConflictsThrow() {
overrideConflictHandlerForTable(TABLE, ConflictHandler.RETRY_ON_VALUE_CHANGED);
final Cell cell = Cell.create(PtBytes.toBytes("row1"), PtBytes.toBytes("column1"));
Transaction t1 = txManager.createNewTransaction();
Transaction t2 = txManager.createNewTransaction();
t1.delete(TABLE, ImmutableSet.of(cell));
t2.put(TABLE, ImmutableMap.of(cell, new byte[1]));
t1.commit();
try {
t2.commit();
fail();
} catch (TransactionConflictException e) {
// good
}
t1 = txManager.createNewTransaction();
t2 = txManager.createNewTransaction();
t1.delete(TABLE, ImmutableSet.of(cell));
t2.put(TABLE, ImmutableMap.of(cell, new byte[1]));
t2.commit();
try {
t1.commit();
fail();
} catch (TransactionConflictException e) {
// good
}
t1 = txManager.createNewTransaction();
t2 = txManager.createNewTransaction();
t2.delete(TABLE, ImmutableSet.of(cell));
t1.put(TABLE, ImmutableMap.of(cell, new byte[1]));
t2.commit();
try {
t1.commit();
fail();
} catch (TransactionConflictException e) {
// good
}
t1 = txManager.createNewTransaction();
t2 = txManager.createNewTransaction();
t2.delete(TABLE, ImmutableSet.of(cell));
t1.put(TABLE, ImmutableMap.of(cell, new byte[1]));
t1.commit();
try {
t2.commit();
fail();
} catch (TransactionConflictException e) {
// good
}
}
use of com.palantir.atlasdb.transaction.api.TransactionConflictException in project atlasdb by palantir.
the class SnapshotTransactionTest method testTransactionWriteWriteConflicts.
@Test
public void testTransactionWriteWriteConflicts() throws Exception {
// This test creates various types of conflicting writes and makes sure that write-write
// conflicts are thrown when necessary, and not thrown when there actually isn't a conflict.
Cell row1Column1 = Cell.create(PtBytes.toBytes("row1"), PtBytes.toBytes("column1"));
Cell row1Column2 = Cell.create(PtBytes.toBytes("row1"), PtBytes.toBytes("column2"));
Cell row2Column1 = Cell.create(PtBytes.toBytes("row2"), PtBytes.toBytes("column1"));
// First transaction commits first, second tries to commit same write
Transaction t1 = txManager.createNewTransaction();
Transaction t2 = txManager.createNewTransaction();
t1.put(TABLE1, ImmutableMap.of(row1Column1, BigInteger.valueOf(1).toByteArray()));
t2.put(TABLE1, ImmutableMap.of(row1Column1, BigInteger.valueOf(1).toByteArray()));
t1.commit();
try {
t2.commit();
assertTrue(false);
} catch (TransactionConflictException e) {
// We expect to catch this exception
}
// Second transaction commits first, first tries to commit same write
t1 = txManager.createNewTransaction();
t2 = txManager.createNewTransaction();
t1.put(TABLE1, ImmutableMap.of(row1Column1, BigInteger.valueOf(1).toByteArray()));
t2.put(TABLE1, ImmutableMap.of(row1Column1, BigInteger.valueOf(1).toByteArray()));
t2.commit();
try {
t1.commit();
assertTrue(false);
} catch (TransactionConflictException e) {
// We expect to catch this exception
}
// Transactions committing to different rows
t1 = txManager.createNewTransaction();
t2 = txManager.createNewTransaction();
t1.put(TABLE1, ImmutableMap.of(row1Column1, BigInteger.valueOf(1).toByteArray()));
t2.put(TABLE1, ImmutableMap.of(row2Column1, BigInteger.valueOf(1).toByteArray()));
t1.commit();
t2.commit();
// Transactions committing to different tables
t1 = txManager.createNewTransaction();
t2 = txManager.createNewTransaction();
t1.put(TABLE1, ImmutableMap.of(row1Column1, BigInteger.valueOf(1).toByteArray()));
t2.put(TABLE2, ImmutableMap.of(row1Column1, BigInteger.valueOf(1).toByteArray()));
t1.commit();
t2.commit();
// Transactions committing to different columns in the same row
t1 = txManager.createNewTransaction();
t2 = txManager.createNewTransaction();
t1.put(TABLE1, ImmutableMap.of(row1Column1, BigInteger.valueOf(1).toByteArray()));
t2.put(TABLE1, ImmutableMap.of(row1Column2, BigInteger.valueOf(1).toByteArray()));
t1.commit();
t2.commit();
}
use of com.palantir.atlasdb.transaction.api.TransactionConflictException in project atlasdb by palantir.
the class SnapshotTransactionTest method noWritesAddedToSweepQueueOnConflict.
@Test
public void noWritesAddedToSweepQueueOnConflict() {
Cell cell = Cell.create("foo".getBytes(), "bar".getBytes());
byte[] value = new byte[1];
Transaction t1 = txManager.createNewTransaction();
Transaction t2 = txManager.createNewTransaction();
t1.put(TABLE, ImmutableMap.of(cell, value));
t2.put(TABLE, ImmutableMap.of(cell, new byte[1]));
t1.commit();
verify(sweepQueue).enqueue(any(), any());
try {
t2.commit();
fail();
} catch (TransactionConflictException e) {
// expected
}
verifyNoMoreInteractions(sweepQueue);
}
use of com.palantir.atlasdb.transaction.api.TransactionConflictException in project atlasdb by palantir.
the class SnapshotTransactionTest method testWriteWriteConflictsDeletedThrow.
@Test
public void testWriteWriteConflictsDeletedThrow() {
overrideConflictHandlerForTable(TABLE, ConflictHandler.RETRY_ON_WRITE_WRITE);
final Cell cell = Cell.create(PtBytes.toBytes("row1"), PtBytes.toBytes("column1"));
Transaction t1 = txManager.createNewTransaction();
Transaction t2 = txManager.createNewTransaction();
t1.delete(TABLE, ImmutableSet.of(cell));
t2.delete(TABLE, ImmutableSet.of(cell));
t1.commit();
try {
t2.commit();
fail();
} catch (TransactionConflictException e) {
// good
}
}
use of com.palantir.atlasdb.transaction.api.TransactionConflictException 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);
}
Aggregations