Search in sources :

Example 6 with RowColumnRangeIterator

use of com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator in project atlasdb by palantir.

the class KvsGetRowsColumnRangeBenchmarks method getAllColumnsUnaligned.

@Benchmark
@Threads(1)
@Warmup(time = 16, timeUnit = TimeUnit.SECONDS)
@Measurement(time = 160, timeUnit = TimeUnit.SECONDS)
public Object getAllColumnsUnaligned(WideRowsTable table) {
    List<byte[]> rows = IntStream.rangeClosed(0, WideRowsTable.NUM_ROWS - 1).mapToObj(WideRowsTable::getRow).collect(Collectors.toList());
    RowColumnRangeIterator rowsColumnRange = table.getKvs().getRowsColumnRange(table.getTableRef(), rows, new ColumnRangeSelection(null, null), 10017, Long.MAX_VALUE);
    int expectedNumCells = WideRowsTable.NUM_ROWS * WideRowsTable.NUM_COLS_PER_ROW;
    List<Map.Entry<Cell, Value>> loadedCells = new ArrayList<>(expectedNumCells);
    while (rowsColumnRange.hasNext()) {
        loadedCells.add(rowsColumnRange.next());
    }
    Preconditions.checkState(loadedCells.size() == expectedNumCells, "Should be %s cells, but were: %s", expectedNumCells, loadedCells.size());
    return loadedCells;
}
Also used : ArrayList(java.util.ArrayList) RowColumnRangeIterator(com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator) ColumnRangeSelection(com.palantir.atlasdb.keyvalue.api.ColumnRangeSelection) Threads(org.openjdk.jmh.annotations.Threads) Measurement(org.openjdk.jmh.annotations.Measurement) Warmup(org.openjdk.jmh.annotations.Warmup) Benchmark(org.openjdk.jmh.annotations.Benchmark)

Example 7 with RowColumnRangeIterator

use of com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator in project atlasdb by palantir.

the class CassandraKeyValueServiceImpl method getRowsColumnRangeIteratorForSingleHost.

private Map<byte[], RowColumnRangeIterator> getRowsColumnRangeIteratorForSingleHost(InetSocketAddress host, TableReference tableRef, List<byte[]> rows, BatchColumnRangeSelection batchColumnRangeSelection, long startTs) {
    try {
        RowColumnRangeExtractor.RowColumnRangeResult firstPage = getRowsColumnRangeForSingleHost(host, tableRef, rows, batchColumnRangeSelection, startTs);
        Map<byte[], LinkedHashMap<Cell, Value>> results = firstPage.getResults();
        Map<byte[], Column> rowsToLastCompositeColumns = firstPage.getRowsToLastCompositeColumns();
        Map<byte[], byte[]> incompleteRowsToNextColumns = Maps.newHashMap();
        for (Entry<byte[], Column> e : rowsToLastCompositeColumns.entrySet()) {
            byte[] row = e.getKey();
            byte[] col = CassandraKeyValueServices.decomposeName(e.getValue()).getLhSide();
            // If we read a version of the cell before our start timestamp, it will be the most recent version
            // readable to us and we can continue to the next column. Otherwise we have to continue reading
            // this column.
            Map<Cell, Value> rowResult = results.get(row);
            boolean completedCell = (rowResult != null) && rowResult.containsKey(Cell.create(row, col));
            boolean endOfRange = isEndOfColumnRange(completedCell, col, firstPage.getRowsToRawColumnCount().get(row), batchColumnRangeSelection);
            if (!endOfRange) {
                byte[] nextCol = getNextColumnRangeColumn(completedCell, col);
                incompleteRowsToNextColumns.put(row, nextCol);
            }
        }
        Map<byte[], RowColumnRangeIterator> ret = Maps.newHashMapWithExpectedSize(rows.size());
        for (byte[] row : rowsToLastCompositeColumns.keySet()) {
            Iterator<Entry<Cell, Value>> resultIterator;
            Map<Cell, Value> result = results.get(row);
            if (result != null) {
                resultIterator = result.entrySet().iterator();
            } else {
                resultIterator = Collections.emptyIterator();
            }
            byte[] nextCol = incompleteRowsToNextColumns.get(row);
            if (nextCol == null) {
                ret.put(row, new LocalRowColumnRangeIterator(resultIterator));
            } else {
                BatchColumnRangeSelection newColumnRange = BatchColumnRangeSelection.create(nextCol, batchColumnRangeSelection.getEndCol(), batchColumnRangeSelection.getBatchHint());
                ret.put(row, new LocalRowColumnRangeIterator(Iterators.concat(resultIterator, getRowColumnRange(host, tableRef, row, newColumnRange, startTs))));
            }
        }
        // We saw no Cassandra results at all for these rows, so the entire column range is empty for these rows.
        for (byte[] row : firstPage.getEmptyRows()) {
            ret.put(row, new LocalRowColumnRangeIterator(Collections.emptyIterator()));
        }
        return ret;
    } catch (Exception e) {
        throw QosAwareThrowables.unwrapAndThrowRateLimitExceededOrAtlasDbDependencyException(e);
    }
}
Also used : BatchColumnRangeSelection(com.palantir.atlasdb.keyvalue.api.BatchColumnRangeSelection) LocalRowColumnRangeIterator(com.palantir.atlasdb.keyvalue.impl.LocalRowColumnRangeIterator) RowColumnRangeIterator(com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator) InsufficientConsistencyException(com.palantir.atlasdb.keyvalue.api.InsufficientConsistencyException) CheckAndSetException(com.palantir.atlasdb.keyvalue.api.CheckAndSetException) KeyAlreadyExistsException(com.palantir.atlasdb.keyvalue.api.KeyAlreadyExistsException) FunctionCheckedException(com.palantir.common.base.FunctionCheckedException) TException(org.apache.thrift.TException) UnavailableException(org.apache.cassandra.thrift.UnavailableException) PalantirRuntimeException(com.palantir.common.exception.PalantirRuntimeException) LinkedHashMap(java.util.LinkedHashMap) Entry(java.util.Map.Entry) LocalRowColumnRangeIterator(com.palantir.atlasdb.keyvalue.impl.LocalRowColumnRangeIterator) Column(org.apache.cassandra.thrift.Column) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) Value(com.palantir.atlasdb.keyvalue.api.Value) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Example 8 with RowColumnRangeIterator

use of com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator in project atlasdb by palantir.

the class SnapshotTransaction method partitionByRow.

/**
 * Partitions a {@link RowColumnRangeIterator} into contiguous blocks that share the same row name.
 * {@link KeyValueService#getRowsColumnRange(TableReference, Iterable, ColumnRangeSelection, int, long)} guarantees
 * that all columns for a single row are adjacent, so this method will return an {@link Iterator} with exactly one
 * entry per non-empty row.
 */
private Iterator<Map.Entry<byte[], RowColumnRangeIterator>> partitionByRow(RowColumnRangeIterator rawResults) {
    PeekingIterator<Map.Entry<Cell, Value>> peekableRawResults = Iterators.peekingIterator(rawResults);
    return new AbstractIterator<Map.Entry<byte[], RowColumnRangeIterator>>() {

        byte[] prevRowName;

        @Override
        protected Map.Entry<byte[], RowColumnRangeIterator> computeNext() {
            finishConsumingPreviousRow(peekableRawResults);
            if (!peekableRawResults.hasNext()) {
                return endOfData();
            }
            byte[] nextRowName = peekableRawResults.peek().getKey().getRowName();
            Iterator<Map.Entry<Cell, Value>> columnsIterator = new AbstractIterator<Map.Entry<Cell, Value>>() {

                @Override
                protected Map.Entry<Cell, Value> computeNext() {
                    if (!peekableRawResults.hasNext() || !Arrays.equals(peekableRawResults.peek().getKey().getRowName(), nextRowName)) {
                        return endOfData();
                    }
                    return peekableRawResults.next();
                }
            };
            prevRowName = nextRowName;
            return Maps.immutableEntry(nextRowName, new LocalRowColumnRangeIterator(columnsIterator));
        }

        private void finishConsumingPreviousRow(PeekingIterator<Map.Entry<Cell, Value>> iter) {
            int numConsumed = 0;
            while (iter.hasNext() && Arrays.equals(iter.peek().getKey().getRowName(), prevRowName)) {
                iter.next();
                numConsumed++;
            }
            if (numConsumed > 0) {
                log.warn("Not all columns for row {} were read. {} columns were discarded.", UnsafeArg.of("row", Arrays.toString(prevRowName)), SafeArg.of("numColumnsDiscarded", numConsumed));
            }
        }
    };
}
Also used : Entry(java.util.Map.Entry) LocalRowColumnRangeIterator(com.palantir.atlasdb.keyvalue.impl.LocalRowColumnRangeIterator) Value(com.palantir.atlasdb.keyvalue.api.Value) AbstractIterator(com.google.common.collect.AbstractIterator) PeekingIterator(com.google.common.collect.PeekingIterator) LocalRowColumnRangeIterator(com.palantir.atlasdb.keyvalue.impl.LocalRowColumnRangeIterator) RowColumnRangeIterator(com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator) Map(java.util.Map) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) LinkedHashMap(java.util.LinkedHashMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Example 9 with RowColumnRangeIterator

use of com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator in project atlasdb by palantir.

the class SnapshotTransaction method getRowsColumnRange.

@Override
public Map<byte[], BatchingVisitable<Map.Entry<Cell, byte[]>>> getRowsColumnRange(TableReference tableRef, Iterable<byte[]> rows, BatchColumnRangeSelection columnRangeSelection) {
    checkGetPreconditions(tableRef);
    if (Iterables.isEmpty(rows)) {
        return ImmutableMap.of();
    }
    hasReads = true;
    Map<byte[], RowColumnRangeIterator> rawResults = keyValueService.getRowsColumnRange(tableRef, rows, columnRangeSelection, getStartTimestamp());
    Map<byte[], BatchingVisitable<Map.Entry<Cell, byte[]>>> postFilteredResults = Maps.newHashMapWithExpectedSize(rawResults.size());
    for (Entry<byte[], RowColumnRangeIterator> e : rawResults.entrySet()) {
        byte[] row = e.getKey();
        RowColumnRangeIterator rawIterator = e.getValue();
        Iterator<Map.Entry<Cell, byte[]>> postFilteredIterator = getPostFilteredColumns(tableRef, columnRangeSelection, row, rawIterator);
        postFilteredResults.put(row, BatchingVisitableFromIterable.create(postFilteredIterator));
    }
    return postFilteredResults;
}
Also used : AbstractBatchingVisitable(com.palantir.common.base.AbstractBatchingVisitable) BatchingVisitable(com.palantir.common.base.BatchingVisitable) Entry(java.util.Map.Entry) LocalRowColumnRangeIterator(com.palantir.atlasdb.keyvalue.impl.LocalRowColumnRangeIterator) RowColumnRangeIterator(com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator) Map(java.util.Map) ConcurrentNavigableMap(java.util.concurrent.ConcurrentNavigableMap) LinkedHashMap(java.util.LinkedHashMap) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) ImmutableMap(com.google.common.collect.ImmutableMap) SortedMap(java.util.SortedMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) Cell(com.palantir.atlasdb.keyvalue.api.Cell)

Example 10 with RowColumnRangeIterator

use of com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator in project atlasdb by palantir.

the class TracingKeyValueServiceTest method getRowsColumnRangeBatch.

@Test
public void getRowsColumnRangeBatch() throws Exception {
    RowColumnRangeIterator rowColumnIterator = mock(RowColumnRangeIterator.class);
    List<byte[]> rows = ImmutableList.of(ROW_NAME);
    Map<byte[], RowColumnRangeIterator> expectedResult = ImmutableMap.of(ROW_NAME, rowColumnIterator);
    BatchColumnRangeSelection range = BatchColumnRangeSelection.create(COL_NAME, COL_NAME, 2);
    when(delegate.getRowsColumnRange(TABLE_REF, rows, range, TIMESTAMP)).thenReturn(expectedResult);
    Map<byte[], RowColumnRangeIterator> result = kvs.getRowsColumnRange(TABLE_REF, rows, range, TIMESTAMP);
    assertThat(result, equalTo(expectedResult));
    checkSpan("atlasdb-kvs.getRowsColumnRange({table}, 1 rows, ts 1)");
    verify(delegate).getRowsColumnRange(TABLE_REF, rows, range, TIMESTAMP);
    verifyNoMoreInteractions(delegate);
}
Also used : BatchColumnRangeSelection(com.palantir.atlasdb.keyvalue.api.BatchColumnRangeSelection) RowColumnRangeIterator(com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator) Test(org.junit.Test)

Aggregations

RowColumnRangeIterator (com.palantir.atlasdb.keyvalue.api.RowColumnRangeIterator)18 BatchColumnRangeSelection (com.palantir.atlasdb.keyvalue.api.BatchColumnRangeSelection)12 ColumnRangeSelection (com.palantir.atlasdb.keyvalue.api.ColumnRangeSelection)8 Cell (com.palantir.atlasdb.keyvalue.api.Cell)6 Test (org.junit.Test)6 LocalRowColumnRangeIterator (com.palantir.atlasdb.keyvalue.impl.LocalRowColumnRangeIterator)5 LinkedHashMap (java.util.LinkedHashMap)5 Map (java.util.Map)5 Entry (java.util.Map.Entry)5 SortedMap (java.util.SortedMap)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)4 Value (com.palantir.atlasdb.keyvalue.api.Value)4 ArrayList (java.util.ArrayList)3 ConcurrentMap (java.util.concurrent.ConcurrentMap)3 ConcurrentNavigableMap (java.util.concurrent.ConcurrentNavigableMap)3 ConcurrentSkipListMap (java.util.concurrent.ConcurrentSkipListMap)3 Benchmark (org.openjdk.jmh.annotations.Benchmark)3 Measurement (org.openjdk.jmh.annotations.Measurement)3 Threads (org.openjdk.jmh.annotations.Threads)3