Search in sources :

Example 1 with AdvancedScanResultConsumer

use of org.apache.hadoop.hbase.client.AdvancedScanResultConsumer in project hbase by apache.

the class TestScannerHeartbeatMessages method testEquivalenceOfScanWithHeartbeats.

/**
 * Test the equivalence of a scan versus the same scan executed when heartbeat messages are
 * necessary
 * @param scan The scan configuration being tested
 * @param rowSleepTime The time to sleep between fetches of row cells
 * @param cfSleepTime The time to sleep between fetches of column family cells
 * @param sleepBeforeCf set to true when column family sleeps should occur before the cells for
 *          that column family are fetched
 */
private void testEquivalenceOfScanWithHeartbeats(final Scan scan, int rowSleepTime, int cfSleepTime, boolean sleepBeforeCf) throws Exception {
    disableSleeping();
    AsyncTable<AdvancedScanResultConsumer> table = CONN.getTable(TABLE_NAME);
    final ResultScanner scanner = new ScanPerNextResultScanner(table, scan);
    final ResultScanner scannerWithHeartbeats = new ScanPerNextResultScanner(table, scan);
    Result r1 = null;
    Result r2 = null;
    while ((r1 = scanner.next()) != null) {
        // Enforce the specified sleep conditions during calls to the heartbeat scanner
        configureSleepTime(rowSleepTime, cfSleepTime, sleepBeforeCf);
        r2 = scannerWithHeartbeats.next();
        disableSleeping();
        assertTrue(r2 != null);
        try {
            Result.compareResults(r1, r2);
        } catch (Exception e) {
            fail(e.getMessage());
        }
    }
    assertTrue(scannerWithHeartbeats.next() == null);
    scanner.close();
    scannerWithHeartbeats.close();
}
Also used : ScanPerNextResultScanner(org.apache.hadoop.hbase.client.ScanPerNextResultScanner) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) ScanPerNextResultScanner(org.apache.hadoop.hbase.client.ScanPerNextResultScanner) ServiceException(org.apache.hbase.thirdparty.com.google.protobuf.ServiceException) IOException(java.io.IOException) AdvancedScanResultConsumer(org.apache.hadoop.hbase.client.AdvancedScanResultConsumer) Result(org.apache.hadoop.hbase.client.Result)

Example 2 with AdvancedScanResultConsumer

use of org.apache.hadoop.hbase.client.AdvancedScanResultConsumer in project hbase by apache.

the class AsyncAggregationClient method findMedian.

private static <R, S, P extends Message, Q extends Message, T extends Message> void findMedian(CompletableFuture<R> future, AsyncTable<AdvancedScanResultConsumer> table, ColumnInterpreter<R, S, P, Q, T> ci, Scan scan, NavigableMap<byte[], S> sumByRegion) {
    double halfSum = ci.divideForAvg(sumByRegion.values().stream().reduce(ci::add).get(), 2L);
    S movingSum = null;
    byte[] startRow = null;
    for (Map.Entry<byte[], S> entry : sumByRegion.entrySet()) {
        startRow = entry.getKey();
        S newMovingSum = ci.add(movingSum, entry.getValue());
        if (ci.divideForAvg(newMovingSum, 1L) > halfSum) {
            break;
        }
        movingSum = newMovingSum;
    }
    if (startRow != null) {
        scan.withStartRow(startRow);
    }
    // we can not pass movingSum directly to an anonymous class as it is not final.
    S baseSum = movingSum;
    byte[] family = scan.getFamilies()[0];
    NavigableSet<byte[]> qualifiers = scan.getFamilyMap().get(family);
    byte[] weightQualifier = qualifiers.last();
    byte[] valueQualifier = qualifiers.first();
    table.scan(scan, new AdvancedScanResultConsumer() {

        private S sum = baseSum;

        private R value = null;

        @Override
        public void onNext(Result[] results, ScanController controller) {
            try {
                for (Result result : results) {
                    Cell weightCell = result.getColumnLatestCell(family, weightQualifier);
                    R weight = ci.getValue(family, weightQualifier, weightCell);
                    sum = ci.add(sum, ci.castToReturnType(weight));
                    if (ci.divideForAvg(sum, 1L) > halfSum) {
                        if (value != null) {
                            future.complete(value);
                        } else {
                            future.completeExceptionally(new NoSuchElementException());
                        }
                        controller.terminate();
                        return;
                    }
                    Cell valueCell = result.getColumnLatestCell(family, valueQualifier);
                    value = ci.getValue(family, valueQualifier, valueCell);
                }
            } catch (IOException e) {
                future.completeExceptionally(e);
                controller.terminate();
            }
        }

        @Override
        public void onError(Throwable error) {
            future.completeExceptionally(error);
        }

        @Override
        public void onComplete() {
            if (!future.isDone()) {
                // we should not reach here as the future should be completed in onNext.
                future.completeExceptionally(new NoSuchElementException());
            }
        }
    });
}
Also used : IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) Map(java.util.Map) NavigableMap(java.util.NavigableMap) TreeMap(java.util.TreeMap) Cell(org.apache.hadoop.hbase.Cell) NoSuchElementException(java.util.NoSuchElementException) AdvancedScanResultConsumer(org.apache.hadoop.hbase.client.AdvancedScanResultConsumer)

Aggregations

IOException (java.io.IOException)2 AdvancedScanResultConsumer (org.apache.hadoop.hbase.client.AdvancedScanResultConsumer)2 Result (org.apache.hadoop.hbase.client.Result)2 Map (java.util.Map)1 NavigableMap (java.util.NavigableMap)1 NoSuchElementException (java.util.NoSuchElementException)1 TreeMap (java.util.TreeMap)1 Cell (org.apache.hadoop.hbase.Cell)1 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)1 ScanPerNextResultScanner (org.apache.hadoop.hbase.client.ScanPerNextResultScanner)1 ServiceException (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException)1