use of com.palantir.atlasdb.keyvalue.api.RowResult in project atlasdb by palantir.
the class AbstractKeyValueServiceTest method doTestGetRangePaging.
private void doTestGetRangePaging(int numColumnsInMetadata, int batchSizeHint, boolean reverse) {
TableReference tableRef = createTableWithNamedColumns(numColumnsInMetadata);
Map<Cell, byte[]> values = new HashMap<Cell, byte[]>();
values.put(Cell.create(PtBytes.toBytes("00"), PtBytes.toBytes("c1")), PtBytes.toBytes("a"));
values.put(Cell.create(PtBytes.toBytes("00"), PtBytes.toBytes("c2")), PtBytes.toBytes("b"));
values.put(Cell.create(PtBytes.toBytes("01"), RangeRequests.getFirstRowName()), PtBytes.toBytes("c"));
values.put(Cell.create(PtBytes.toBytes("02"), PtBytes.toBytes("c1")), PtBytes.toBytes("d"));
values.put(Cell.create(PtBytes.toBytes("02"), PtBytes.toBytes("c2")), PtBytes.toBytes("e"));
values.put(Cell.create(PtBytes.toBytes("03"), PtBytes.toBytes("c1")), PtBytes.toBytes("f"));
values.put(Cell.create(PtBytes.toBytes("04"), PtBytes.toBytes("c1")), PtBytes.toBytes("g"));
values.put(Cell.create(PtBytes.toBytes("04"), RangeRequests.getLastRowName()), PtBytes.toBytes("h"));
values.put(Cell.create(PtBytes.toBytes("05"), PtBytes.toBytes("c1")), PtBytes.toBytes("i"));
values.put(Cell.create(RangeRequests.getLastRowName(), PtBytes.toBytes("c1")), PtBytes.toBytes("j"));
keyValueService.put(tableRef, values, TEST_TIMESTAMP);
RangeRequest request = RangeRequest.builder(reverse).batchHint(batchSizeHint).build();
try (ClosableIterator<RowResult<Value>> iter = keyValueService.getRange(tableRef, request, Long.MAX_VALUE)) {
List<RowResult<Value>> results = ImmutableList.copyOf(iter);
List<RowResult<Value>> expected = ImmutableList.of(RowResult.create(PtBytes.toBytes("00"), ImmutableSortedMap.<byte[], Value>orderedBy(UnsignedBytes.lexicographicalComparator()).put(PtBytes.toBytes("c1"), Value.create(PtBytes.toBytes("a"), TEST_TIMESTAMP)).put(PtBytes.toBytes("c2"), Value.create(PtBytes.toBytes("b"), TEST_TIMESTAMP)).build()), RowResult.create(PtBytes.toBytes("01"), ImmutableSortedMap.<byte[], Value>orderedBy(UnsignedBytes.lexicographicalComparator()).put(RangeRequests.getFirstRowName(), Value.create(PtBytes.toBytes("c"), TEST_TIMESTAMP)).build()), RowResult.create(PtBytes.toBytes("02"), ImmutableSortedMap.<byte[], Value>orderedBy(UnsignedBytes.lexicographicalComparator()).put(PtBytes.toBytes("c1"), Value.create(PtBytes.toBytes("d"), TEST_TIMESTAMP)).put(PtBytes.toBytes("c2"), Value.create(PtBytes.toBytes("e"), TEST_TIMESTAMP)).build()), RowResult.create(PtBytes.toBytes("03"), ImmutableSortedMap.<byte[], Value>orderedBy(UnsignedBytes.lexicographicalComparator()).put(PtBytes.toBytes("c1"), Value.create(PtBytes.toBytes("f"), TEST_TIMESTAMP)).build()), RowResult.create(PtBytes.toBytes("04"), ImmutableSortedMap.<byte[], Value>orderedBy(UnsignedBytes.lexicographicalComparator()).put(PtBytes.toBytes("c1"), Value.create(PtBytes.toBytes("g"), TEST_TIMESTAMP)).put(RangeRequests.getLastRowName(), Value.create(PtBytes.toBytes("h"), TEST_TIMESTAMP)).build()), RowResult.create(PtBytes.toBytes("05"), ImmutableSortedMap.<byte[], Value>orderedBy(UnsignedBytes.lexicographicalComparator()).put(PtBytes.toBytes("c1"), Value.create(PtBytes.toBytes("i"), TEST_TIMESTAMP)).build()), RowResult.create(RangeRequests.getLastRowName(), ImmutableSortedMap.<byte[], Value>orderedBy(UnsignedBytes.lexicographicalComparator()).put(PtBytes.toBytes("c1"), Value.create(PtBytes.toBytes("j"), TEST_TIMESTAMP)).build()));
assertEquals(reverse ? Lists.reverse(expected) : expected, results);
}
}
use of com.palantir.atlasdb.keyvalue.api.RowResult in project atlasdb by palantir.
the class AtlasDbServiceImpl method getRange.
@Override
public RangeToken getRange(TransactionToken token, final TableRange range) {
return runReadOnly(token, transaction -> {
int limit = range.getBatchSize() + 1;
RangeRequest request = RangeRequest.builder().startRowInclusive(range.getStartRow()).endRowExclusive(range.getEndRow()).batchHint(limit).retainColumns(range.getColumns()).build();
BatchingVisitable<RowResult<byte[]>> visitable = transaction.getRange(getTableRef(range.getTableName()), request);
List<RowResult<byte[]>> results = BatchingVisitables.limit(visitable, limit).immutableCopy();
if (results.size() == limit) {
TableRowResult data = new TableRowResult(range.getTableName(), results.subList(0, limit - 1));
RowResult<byte[]> lastResultInBatch = results.get(limit - 1);
TableRange nextRange = range.withStartRow(lastResultInBatch.getRowName());
return new RangeToken(data, nextRange);
} else {
TableRowResult data = new TableRowResult(range.getTableName(), results);
return new RangeToken(data, null);
}
});
}
use of com.palantir.atlasdb.keyvalue.api.RowResult in project atlasdb by palantir.
the class TransactionGetBenchmarks method getSingleRowWithRangeQueryInner.
private List<RowResult<byte[]>> getSingleRowWithRangeQueryInner(final ConsecutiveNarrowTable table) {
return table.getTransactionManager().runTaskThrowOnConflict(txn -> {
RangeRequest request = Iterables.getOnlyElement(table.getRangeRequests(1, 1, false));
List<RowResult<byte[]>> result = BatchingVisitables.copyToList(txn.getRange(table.getTableRef(), request));
byte[] rowName = Iterables.getOnlyElement(result).getRowName();
int rowNumber = ConsecutiveNarrowTable.rowNumber(rowName);
int expectedRowNumber = ConsecutiveNarrowTable.rowNumber(request.getStartInclusive());
Preconditions.checkState(rowNumber == expectedRowNumber, "Start Row %s, row number %s", expectedRowNumber, rowNumber);
return result;
});
}
use of com.palantir.atlasdb.keyvalue.api.RowResult in project atlasdb by palantir.
the class SnapshotTransactionTest method testTransactionAtomicity.
@Test
public void testTransactionAtomicity() throws Exception {
// This test runs multiple transactions in parallel, with KeyValueService.put calls throwing
// a RuntimeException from time to time and hanging other times. which effectively kills the
// thread. We ensure that every transaction either adds 5 rows to the table or adds 0 rows
// by checking at the end that the number of rows is a multiple of 5.
final TableReference tableRef = TABLE;
Random random = new Random(1);
final UnstableKeyValueService unstableKvs = new UnstableKeyValueService(keyValueService, random);
final TestTransactionManager unstableTransactionManager = new TestTransactionManagerImpl(unstableKvs, timestampService, lockClient, lockService, transactionService, conflictDetectionManager, sweepStrategyManager, sweepQueue);
ScheduledExecutorService service = PTExecutors.newScheduledThreadPool(20);
for (int i = 0; i < 30; i++) {
final int threadNumber = i;
service.schedule((Callable<Void>) () -> {
if (threadNumber == 10) {
unstableKvs.setRandomlyThrow(true);
}
if (threadNumber == 20) {
unstableKvs.setRandomlyHang(true);
}
Transaction transaction = unstableTransactionManager.createNewTransaction();
BatchingVisitable<RowResult<byte[]>> results = transaction.getRange(tableRef, RangeRequest.builder().build());
final MutableInt nextIndex = new MutableInt(0);
results.batchAccept(1, AbortingVisitors.batching((AbortingVisitor<RowResult<byte[]>, Exception>) row -> {
byte[] dataBytes = row.getColumns().get(PtBytes.toBytes("data"));
BigInteger dataValue = new BigInteger(dataBytes);
nextIndex.setValue(Math.max(nextIndex.toInteger(), dataValue.intValue() + 1));
return true;
}));
// rows to the table.
for (int j = 0; j < 5; j++) {
int rowNumber = nextIndex.toInteger() + j;
Cell cell = Cell.create(PtBytes.toBytes("row" + rowNumber), PtBytes.toBytes("data"));
transaction.put(tableRef, ImmutableMap.of(cell, BigInteger.valueOf(rowNumber).toByteArray()));
Thread.yield();
}
transaction.commit();
return null;
}, i * 20, TimeUnit.MILLISECONDS);
}
service.shutdown();
service.awaitTermination(1, TimeUnit.SECONDS);
// Verify each table has a number of rows that's a multiple of 5
Transaction verifyTransaction = txManager.createNewTransaction();
BatchingVisitable<RowResult<byte[]>> results = verifyTransaction.getRange(tableRef, RangeRequest.builder().build());
final MutableInt numRows = new MutableInt(0);
results.batchAccept(1, AbortingVisitors.batching((AbortingVisitor<RowResult<byte[]>, Exception>) row -> {
numRows.increment();
return true;
}));
Assert.assertEquals(0, numRows.toInteger() % 5);
}
use of com.palantir.atlasdb.keyvalue.api.RowResult in project atlasdb by palantir.
the class AbstractTransactionTest method testRangeAfterTimestamp2.
@Test
public void testRangeAfterTimestamp2() {
putDirect("row1", "col2", "", 5);
putDirect("row2", "col2", "", 0);
putDirect("row3", "col2", "", 0);
RangeRequest rangeRequest = RangeRequest.builder().batchHint(1).build();
Map<RangeRequest, TokenBackedBasicResultsPage<RowResult<Value>, byte[]>> ranges = keyValueService.getFirstBatchForRanges(TEST_TABLE, Collections.singleton(rangeRequest), 1);
assertEquals(1, ranges.keySet().size());
TokenBackedBasicResultsPage<RowResult<Value>, byte[]> page = ranges.values().iterator().next();
assertTrue(page.moreResultsAvailable());
}
Aggregations