Search in sources :

Example 1 with TransactionConflictException

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
    }
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Test(org.junit.Test)

Example 2 with TransactionConflictException

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();
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Test(org.junit.Test)

Example 3 with TransactionConflictException

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);
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Test(org.junit.Test)

Example 4 with TransactionConflictException

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
    }
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Test(org.junit.Test)

Example 5 with TransactionConflictException

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);
}
Also used : StreamTestWithHashStreamMetadataRow(com.palantir.atlasdb.schema.stream.generated.StreamTestWithHashStreamMetadataTable.StreamTestWithHashStreamMetadataRow) Arrays(java.util.Arrays) Matchers.not(org.hamcrest.Matchers.not) TestHashComponentsStreamValueRow(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamValueTable.TestHashComponentsStreamValueRow) Random(java.util.Random) Assert.assertThat(org.junit.Assert.assertThat) Future(java.util.concurrent.Future) TestHashComponentsStreamIdxRow(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamIdxTable.TestHashComponentsStreamIdxRow) ByteArrayInputStream(java.io.ByteArrayInputStream) PTExecutors(com.palantir.common.concurrent.PTExecutors) StreamTestTableFactory(com.palantir.atlasdb.schema.stream.generated.StreamTestTableFactory) Map(java.util.Map) Assert.fail(org.junit.Assert.fail) StreamTestWithHashStreamMetadataTable(com.palantir.atlasdb.schema.stream.generated.StreamTestWithHashStreamMetadataTable) StreamTestWithHashStreamStore(com.palantir.atlasdb.schema.stream.generated.StreamTestWithHashStreamStore) Parameterized(org.junit.runners.Parameterized) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) Set(java.util.Set) StreamTestStreamHashAidxTable(com.palantir.atlasdb.schema.stream.generated.StreamTestStreamHashAidxTable) Sets(com.google.common.collect.Sets) ByteString(com.google.protobuf.ByteString) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) CountDownLatch(java.util.concurrent.CountDownLatch) IOUtils(org.apache.commons.io.IOUtils) Transaction(com.palantir.atlasdb.transaction.api.Transaction) Assert.assertFalse(org.junit.Assert.assertFalse) StreamTestStreamStore(com.palantir.atlasdb.schema.stream.generated.StreamTestStreamStore) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) TestHashComponentsStreamStore(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamStore) Iterables(com.google.common.collect.Iterables) StreamMetadata(com.palantir.atlasdb.protos.generated.StreamPersistence.StreamMetadata) StreamTestStreamMetadataTable(com.palantir.atlasdb.schema.stream.generated.StreamTestStreamMetadataTable) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) Schemas(com.palantir.atlasdb.table.description.Schemas) Multimap(com.google.common.collect.Multimap) AtlasDbTestCase(com.palantir.atlasdb.AtlasDbTestCase) PtBytes(com.palantir.atlasdb.encoding.PtBytes) StreamTestStreamValueTable(com.palantir.atlasdb.schema.stream.generated.StreamTestStreamValueTable) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TestHashComponentsStreamMetadataRow(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamMetadataTable.TestHashComponentsStreamMetadataRow) StreamTestWithHashStreamIdxRow(com.palantir.atlasdb.schema.stream.generated.StreamTestWithHashStreamIdxTable.StreamTestWithHashStreamIdxRow) StreamTestWithHashStreamValueRow(com.palantir.atlasdb.schema.stream.generated.StreamTestWithHashStreamValueTable.StreamTestWithHashStreamValueRow) PersistentStreamStore(com.palantir.atlasdb.stream.PersistentStreamStore) TestHashComponentsStreamMetadataTable(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamMetadataTable) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) Sha256Hash(com.palantir.util.crypto.Sha256Hash) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Assert.assertNotNull(org.junit.Assert.assertNotNull) TestHashComponentsStreamHashAidxTable(com.palantir.atlasdb.schema.stream.generated.TestHashComponentsStreamHashAidxTable) Assert.assertTrue(org.junit.Assert.assertTrue) Throwables(com.google.common.base.Throwables) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) File(java.io.File) DeletingStreamStore(com.palantir.atlasdb.schema.stream.generated.DeletingStreamStore) Pair(com.palantir.util.Pair) Futures(com.google.common.util.concurrent.Futures) StreamPersistence(com.palantir.atlasdb.protos.generated.StreamPersistence) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) TransactionTask(com.palantir.atlasdb.transaction.api.TransactionTask) StreamTestMaxMemStreamStore(com.palantir.atlasdb.schema.stream.generated.StreamTestMaxMemStreamStore) Assert.assertEquals(org.junit.Assert.assertEquals) TemporaryFolder(org.junit.rules.TemporaryFolder) InputStream(java.io.InputStream) KeyValueTable(com.palantir.atlasdb.schema.stream.generated.KeyValueTable) ExecutorService(java.util.concurrent.ExecutorService) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) CountDownLatch(java.util.concurrent.CountDownLatch)

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