Search in sources :

Example 31 with Transaction

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

the class RangeVisitor method visitRange.

private void visitRange(Visitor visitor, MutableRange range) {
    do {
        final RangeRequest request = range.getRangeRequest();
        try {
            long startTime = System.currentTimeMillis();
            long numVisited = txManager.runTaskWithLocksWithRetry(lockTokens, Suppliers.ofInstance(null), new LockAwareTransactionTask<Long, RuntimeException>() {

                @Override
                public Long execute(Transaction tx, Iterable<HeldLocksToken> heldLocks) {
                    return visitInternal(tx, visitor, request, range);
                }

                @Override
                public String toString() {
                    return "visitRange(" + request + ")";
                }
            });
            counter.addAndGet(numVisited);
            log.info("Visited {} rows from {} in {} ms.", numVisited, tableRef.getQualifiedName(), System.currentTimeMillis() - startTime);
        } catch (InterruptedException e) {
            throw Throwables.rewrapAndThrowUncheckedException(e);
        }
    } while (!range.isComplete());
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) HeldLocksToken(com.palantir.lock.HeldLocksToken) RangeRequest(com.palantir.atlasdb.keyvalue.api.RangeRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong)

Example 32 with Transaction

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

the class SnapshotTransactionTest method disallowPutOnEmptyObject.

@Test(expected = IllegalArgumentException.class)
public void disallowPutOnEmptyObject() {
    Transaction t1 = txManager.createNewTransaction();
    t1.put(TABLE, ImmutableMap.of(TEST_CELL, PtBytes.EMPTY_BYTE_ARRAY));
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) Test(org.junit.Test)

Example 33 with Transaction

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

the class SnapshotTransactionTest method testGetRowsLocalWritesWithColumnSelection.

@Test
public void testGetRowsLocalWritesWithColumnSelection() {
    // This test ensures getRows correctly applies columnSelection when there are local writes
    byte[] row1 = PtBytes.toBytes("row1");
    Cell row1Column1 = Cell.create(row1, PtBytes.toBytes("column1"));
    Cell row1Column2 = Cell.create(row1, PtBytes.toBytes("column2"));
    byte[] row1Column1Value = BigInteger.valueOf(1).toByteArray();
    byte[] row1Column2Value = BigInteger.valueOf(2).toByteArray();
    Transaction snapshotTx = serializableTxManager.createNewTransaction();
    snapshotTx.put(TABLE, ImmutableMap.of(row1Column1, row1Column1Value, row1Column2, row1Column2Value));
    ColumnSelection column1Selection = ColumnSelection.create(ImmutableList.of(row1Column1.getColumnName()));
    // local writes still apply columnSelection
    RowResult<byte[]> rowResult1 = snapshotTx.getRows(TABLE, ImmutableList.of(row1), column1Selection).get(row1);
    assertThat(rowResult1.getColumns(), hasEntry(row1Column1.getColumnName(), row1Column1Value));
    assertThat(rowResult1.getColumns(), not(hasEntry(row1Column2.getColumnName(), row1Column2Value)));
    RowResult<byte[]> rowResult2 = snapshotTx.getRows(TABLE, ImmutableList.of(row1), ColumnSelection.all()).get(row1);
    assertThat(rowResult2.getColumns(), hasEntry(row1Column1.getColumnName(), row1Column1Value));
    assertThat(rowResult2.getColumns(), hasEntry(row1Column2.getColumnName(), row1Column2Value));
}
Also used : ColumnSelection(com.palantir.atlasdb.keyvalue.api.ColumnSelection) Transaction(com.palantir.atlasdb.transaction.api.Transaction) Cell(com.palantir.atlasdb.keyvalue.api.Cell) Test(org.junit.Test)

Example 34 with Transaction

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

the class SnapshotTransactionTest method writeCells.

private void writeCells(TableReference table, ImmutableMap<Cell, byte[]> cellsToWrite) {
    Transaction writeTransaction = txManager.createNewTransaction();
    writeTransaction.put(table, cellsToWrite);
    writeTransaction.commit();
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction)

Example 35 with Transaction

use of com.palantir.atlasdb.transaction.api.Transaction 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));
        }
    }
}
Also used : Transaction(com.palantir.atlasdb.transaction.api.Transaction) Random(java.util.Random) BigInteger(java.math.BigInteger) TransactionConflictException(com.palantir.atlasdb.transaction.api.TransactionConflictException) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) 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