Search in sources :

Example 21 with Transaction

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

the class AbstractSerializableTransactionTest method testColumnRangeReadWriteEmptyRange.

@Test
public void testColumnRangeReadWriteEmptyRange() {
    byte[] row = PtBytes.toBytes("row1");
    Transaction t1 = startTransaction();
    Map<byte[], BatchingVisitable<Map.Entry<Cell, byte[]>>> columnRange = t1.getRowsColumnRange(TEST_TABLE, ImmutableList.of(row), BatchColumnRangeSelection.create(PtBytes.toBytes("col"), PtBytes.toBytes("col0"), 1));
    assertNull(BatchingVisitables.getFirst(Iterables.getOnlyElement(columnRange.values())));
    // Write to avoid the read only path.
    put(t1, "row1_1", "col0", "v0");
    Transaction t2 = startTransaction();
    put(t2, "row1", "col", "v0");
    t2.commit();
    try {
        t1.commit();
        fail();
    } catch (TransactionSerializableConflictException e) {
    // expected
    }
}
Also used : BatchingVisitable(com.palantir.common.base.BatchingVisitable) Transaction(com.palantir.atlasdb.transaction.api.Transaction) TransactionSerializableConflictException(com.palantir.atlasdb.transaction.api.TransactionSerializableConflictException) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Test(org.junit.Test)

Example 22 with Transaction

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

the class AbstractSerializableTransactionTest method testConcurrentWriteSkewCell.

@Test(expected = TransactionFailedRetriableException.class)
public void testConcurrentWriteSkewCell() throws InterruptedException, BrokenBarrierException {
    Transaction t0 = startTransaction();
    put(t0, "row1", "col1", "100");
    put(t0, "row2", "col1", "100");
    t0.commit();
    final CyclicBarrier barrier = new CyclicBarrier(2);
    final Transaction t1 = startTransaction();
    ExecutorService exec = PTExecutors.newCachedThreadPool();
    Future<?> future = exec.submit((Callable<Void>) () -> {
        withdrawMoney(t1, true, true);
        barrier.await();
        t1.commit();
        return null;
    });
    Transaction t2 = startTransaction();
    withdrawMoney(t2, false, true);
    barrier.await();
    t2.commit();
    try {
        future.get();
        fail();
    } catch (ExecutionException e) {
        throw Throwables.rewrapAndThrowUncheckedException(e.getCause());
    }
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) ExecutorService(java.util.concurrent.ExecutorService) ExecutionException(java.util.concurrent.ExecutionException) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 23 with Transaction

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

the class AbstractSerializableTransactionTest method testPhantomReadFail2.

@Test
public void testPhantomReadFail2() {
    String initialValue = "100";
    Transaction t0 = startTransaction();
    put(t0, "row1", "col1", initialValue);
    put(t0, "row2", "col1", initialValue);
    t0.commit();
    Transaction t1 = startTransaction();
    BatchingVisitables.copyToList(t1.getRange(TEST_TABLE, RangeRequest.builder().build()));
    put(t1, "row22", "col1", initialValue);
    Transaction t2 = startTransaction();
    put(t2, "row3", "col1", initialValue);
    t2.commit();
    try {
        t1.commit();
        fail();
    } catch (TransactionSerializableConflictException e) {
    // this is expectecd to throw because it is a write skew
    }
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) TransactionSerializableConflictException(com.palantir.atlasdb.transaction.api.TransactionSerializableConflictException) Test(org.junit.Test)

Example 24 with Transaction

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

the class AbstractSerializableTransactionTest method writeColumns.

private void writeColumns() {
    Transaction t1 = 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(Cell::getColumnName));
    for (int i = 0; i < totalPuts; i++) {
        put(t1, "row1", "col" + i, "v" + i);
        writes.put(Cell.create(row, PtBytes.toBytes("col" + i)), PtBytes.toBytes("v" + i));
    }
    t1.commit();
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Example 25 with Transaction

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

the class SchemaApiTestImpl method getRangeSecondColumnOnlyFirstTwoResults.

@Override
protected Map<String, StringValue> getRangeSecondColumnOnlyFirstTwoResults(Transaction transaction, String startRowKey, String endRowKey) {
    SchemaApiTestTable table = tableFactory.getSchemaApiTestTable(transaction);
    ColumnSelection secondColSelection = SchemaApiTestTable.getColumnSelection(SchemaApiTestTable.SchemaApiTestNamedColumn.COLUMN2);
    RangeRequest rangeRequest = RangeRequest.builder().startRowInclusive(SchemaApiTestRow.of(startRowKey).persistToBytes()).endRowExclusive(SchemaApiTestRow.of(endRowKey).persistToBytes()).retainColumns(secondColSelection).batchHint(2).build();
    BatchingVisitableView<SchemaApiTestRowResult> rangeRequestResult = table.getRange(rangeRequest);
    return BatchingVisitables.take(rangeRequestResult, 2).stream().collect(Collectors.toMap(entry -> entry.getRowName().getComponent1(), SchemaApiTestTable.SchemaApiTestRowResult::getColumn2));
}
Also used : StringValue(com.palantir.atlasdb.table.description.test.StringValue) EncodingUtils(com.palantir.atlasdb.ptobject.EncodingUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashComponentsTestTable(com.palantir.atlasdb.table.description.generated.HashComponentsTestTable) ApiTestTableFactory(com.palantir.atlasdb.table.description.generated.ApiTestTableFactory) Test(org.junit.Test) Hashing(com.google.common.hash.Hashing) Collectors(java.util.stream.Collectors) PtBytes(com.palantir.atlasdb.encoding.PtBytes) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) SchemaApiTestRow(com.palantir.atlasdb.table.description.generated.SchemaApiTestTable.SchemaApiTestRow) List(java.util.List) Transaction(com.palantir.atlasdb.transaction.api.Transaction) Map(java.util.Map) SchemaApiTestRowResult(com.palantir.atlasdb.table.description.generated.SchemaApiTestTable.SchemaApiTestRowResult) BatchingVisitableView(com.palantir.common.base.BatchingVisitableView) Optional(java.util.Optional) SchemaApiTestTable(com.palantir.atlasdb.table.description.generated.SchemaApiTestTable) BatchingVisitables(com.palantir.common.base.BatchingVisitables) ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) SchemaApiTestTable(com.palantir.atlasdb.table.description.generated.SchemaApiTestTable) SchemaApiTestRowResult(com.palantir.atlasdb.table.description.generated.SchemaApiTestTable.SchemaApiTestRowResult)

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