Search in sources :

Example 46 with Transaction

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

the class AbstractTransactionTest method testNoNonRepeatableReads.

@Test
public void testNoNonRepeatableReads() {
    Transaction t0 = startTransaction();
    put(t0, "row1", "col1", "v1");
    t0.commit();
    Transaction t1 = startTransaction();
    assertEquals("v1", get(t1, "row1", "col1"));
    Transaction t2 = startTransaction();
    put(t2, "row1", "col1", "v2");
    t2.commit();
    // Repeated read: should see original value.
    assertEquals("v1", get(t1, "row1", "col1"));
    Transaction t3 = startTransaction();
    assertEquals("v2", get(t3, "row1", "col1"));
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) Test(org.junit.Test)

Example 47 with Transaction

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

the class AbstractTransactionTest method testReadMyWrites.

@Test
public void testReadMyWrites() {
    Transaction t = startTransaction();
    put(t, "row1", "col1", "v1");
    put(t, "row1", "col2", "v2");
    put(t, "row2", "col1", "v3");
    assertEquals("v1", get(t, "row1", "col1"));
    assertEquals("v2", get(t, "row1", "col2"));
    assertEquals("v3", get(t, "row2", "col1"));
    t.commit();
    t = startTransaction();
    assertEquals("v1", get(t, "row1", "col1"));
    assertEquals("v2", get(t, "row1", "col2"));
    assertEquals("v3", get(t, "row2", "col1"));
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) Test(org.junit.Test)

Example 48 with Transaction

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

the class AbstractTransactionTest method testReadMyWritesColumnRangePagingTransaction.

@Test
public void testReadMyWritesColumnRangePagingTransaction() {
    Transaction t = startTransaction();
    int totalPuts = 101;
    byte[] row = PtBytes.toBytes("row1");
    // Record expected results using byte ordering
    ImmutableSortedMap.Builder<Cell, byte[]> writes = ImmutableSortedMap.orderedBy(Ordering.from(UnsignedBytes.lexicographicalComparator()).onResultOf(key -> key.getColumnName()));
    for (int i = 0; i < totalPuts; i++) {
        put(t, "row1", "col" + i, "v" + i);
        if (i % 2 == 0) {
            writes.put(Cell.create(row, PtBytes.toBytes("col" + i)), PtBytes.toBytes("v" + i));
        }
    }
    t.commit();
    t = startTransaction();
    for (int i = 0; i < totalPuts; i++) {
        if (i % 2 == 1) {
            put(t, "row1", "col" + i, "t_v" + i);
            writes.put(Cell.create(row, PtBytes.toBytes("col" + i)), PtBytes.toBytes("t_v" + i));
        }
    }
    Map<byte[], BatchingVisitable<Map.Entry<Cell, byte[]>>> columnRange = t.getRowsColumnRange(TEST_TABLE, ImmutableList.of(row), BatchColumnRangeSelection.create(PtBytes.EMPTY_BYTE_ARRAY, PtBytes.EMPTY_BYTE_ARRAY, 1));
    List<Map.Entry<Cell, byte[]>> expected = ImmutableList.copyOf(writes.build().entrySet());
    verifyMatchingResult(expected, row, columnRange);
}
Also used : RangeRequests(com.palantir.atlasdb.keyvalue.api.RangeRequests) Arrays(java.util.Arrays) AtlasDbDependencyException(com.palantir.common.exception.AtlasDbDependencyException) BatchColumnRangeSelection(com.palantir.atlasdb.keyvalue.api.BatchColumnRangeSelection) Throwables(com.palantir.common.base.Throwables) ClosableIterator(com.palantir.common.base.ClosableIterator) Random(java.util.Random) MapEntries(com.palantir.common.collect.MapEntries) BatchingVisitable(com.palantir.common.base.BatchingVisitable) Map(java.util.Map) TableReference(com.palantir.atlasdb.keyvalue.api.TableReference) Assert.fail(org.junit.Assert.fail) AtlasDbConstants(com.palantir.atlasdb.AtlasDbConstants) TableMetadata(com.palantir.atlasdb.table.description.TableMetadata) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell) TransactionReadSentinelBehavior(com.palantir.atlasdb.transaction.api.TransactionReadSentinelBehavior) TableDefinition(com.palantir.atlasdb.table.description.TableDefinition) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) IterableView(com.palantir.common.collect.IterableView) ValueType(com.palantir.atlasdb.table.description.ValueType) Transaction(com.palantir.atlasdb.transaction.api.Transaction) Assert.assertFalse(org.junit.Assert.assertFalse) NoOpCleaner(com.palantir.atlasdb.cleaner.NoOpCleaner) Iterables(com.google.common.collect.Iterables) ConflictHandler(com.palantir.atlasdb.transaction.api.ConflictHandler) FutureTask(java.util.concurrent.FutureTask) Multimap(com.google.common.collect.Multimap) Iterators(com.google.common.collect.Iterators) PtBytes(com.palantir.atlasdb.encoding.PtBytes) Multimaps(com.google.common.collect.Multimaps) AtlasDbConstraintCheckingMode(com.palantir.atlasdb.transaction.api.AtlasDbConstraintCheckingMode) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) ImmutableList(com.google.common.collect.ImmutableList) LegacyTimelockService(com.palantir.lock.impl.LegacyTimelockService) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) AbortingVisitors(com.palantir.common.base.AbortingVisitors) ImmutableMultimap(com.google.common.collect.ImmutableMultimap) BatchingVisitables(com.palantir.common.base.BatchingVisitables) ExecutorService(java.util.concurrent.ExecutorService) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) TableMetadataPersistence(com.palantir.atlasdb.protos.generated.TableMetadataPersistence) UnsignedBytes(com.google.common.primitives.UnsignedBytes) Value(com.palantir.atlasdb.keyvalue.api.Value) BaseEncoding(com.google.common.io.BaseEncoding) Assert.assertTrue(org.junit.Assert.assertTrue) TimestampCache(com.palantir.atlasdb.cache.TimestampCache) Test(org.junit.Test) TokenBackedBasicResultsPage(com.palantir.util.paging.TokenBackedBasicResultsPage) Maps(com.google.common.collect.Maps) Pair(com.palantir.util.Pair) ExecutionException(java.util.concurrent.ExecutionException) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) Assert.assertNull(org.junit.Assert.assertNull) Ordering(com.google.common.collect.Ordering) TransactionTask(com.palantir.atlasdb.transaction.api.TransactionTask) MultiTableSweepQueueWriter(com.palantir.atlasdb.sweep.queue.MultiTableSweepQueueWriter) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) BatchingVisitable(com.palantir.common.base.BatchingVisitable) Transaction(com.palantir.atlasdb.transaction.api.Transaction) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Test(org.junit.Test)

Example 49 with Transaction

use of com.palantir.atlasdb.transaction.api.Transaction 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)

Example 50 with Transaction

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

the class AbstractTransactionTest method testReadMyWritesAfterGetRange.

@Test
public void testReadMyWritesAfterGetRange() throws InterruptedException, ExecutionException {
    Transaction t = startTransaction();
    // this will come first in the range
    put(t, "row0", "col1", "v0");
    put(t, "row1", "col1", "v1");
    put(t, "row1", "col2", "v2");
    put(t, "row2", "col1", "v3");
    put(t, "row4", "col1", "v4");
    t.commit();
    t = startTransaction();
    assertEquals("v1", get(t, "row1", "col1"));
    assertEquals("v2", get(t, "row1", "col2"));
    assertEquals("v3", get(t, "row2", "col1"));
    // we need a bunch of buffer writes so that we don't exhaust the local iterator right away
    // because of peeking iterators looking ahead
    put(t, "a0", "col2", "v0");
    put(t, "b1", "col3", "v0");
    put(t, "c2", "col3", "v0");
    put(t, "d3", "col3", "v0");
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final BatchingVisitable<RowResult<byte[]>> visitable = t.getRange(TEST_TABLE, RangeRequest.builder().build());
    FutureTask<Void> futureTask = new FutureTask<Void>(() -> {
        final Map<Cell, byte[]> vals = Maps.newHashMap();
        try {
            visitable.batchAccept(1, AbortingVisitors.batching((RowVisitor) item -> {
                try {
                    latch.countDown();
                    latch2.await();
                } catch (InterruptedException e) {
                    throw Throwables.throwUncheckedException(e);
                }
                MapEntries.putAll(vals, item.getCells());
                if (Arrays.equals(item.getRowName(), "row1".getBytes())) {
                    assertEquals("v5", new String(item.getColumns().get("col1".getBytes())));
                    assertEquals(3, IterableView.of(item.getCells()).size());
                }
                return true;
            }));
            assertTrue(vals.containsKey(Cell.create("row1".getBytes(), "col1".getBytes())));
            assertTrue(Arrays.equals("v5".getBytes(), vals.get(Cell.create("row1".getBytes(), "col1".getBytes()))));
            assertFalse(vals.containsKey(Cell.create("row2".getBytes(), "col1".getBytes())));
            return null;
        } catch (Throwable t1) {
            latch.countDown();
            Throwables.throwIfInstance(t1, Exception.class);
            throw Throwables.throwUncheckedException(t1);
        }
    });
    Thread thread = new Thread(futureTask);
    thread.setName("testReadMyWritesAfterGetRange");
    thread.start();
    latch.await();
    // These puts will be seen by the range scan happening on the other thread
    // this put is checked to exist
    put(t, "row1", "col1", "v5");
    // it is checked there are 3 cells for this
    put(t, "row1", "col3", "v6");
    put(t, "row3", "col1", "v7");
    // this delete is checked
    delete(t, "row2", "col1");
    put(t, "row2", "col2", "v8");
    latch2.countDown();
    futureTask.get();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AtlasDbDependencyException(com.palantir.common.exception.AtlasDbDependencyException) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) ExecutionException(java.util.concurrent.ExecutionException) RowResult(com.palantir.atlasdb.keyvalue.api.RowResult) Transaction(com.palantir.atlasdb.transaction.api.Transaction) FutureTask(java.util.concurrent.FutureTask) Cell(com.palantir.atlasdb.keyvalue.api.Cell) 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