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());
}
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));
}
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));
}
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();
}
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));
}
}
}
Aggregations