Search in sources :

Example 1 with ScanMetrics

use of org.apache.hadoop.hbase.client.metrics.ScanMetrics in project hbase by apache.

the class TestFromClientSide method testScanMetrics.

/**
  * Test ScanMetrics
  * @throws Exception
  */
@Test
@SuppressWarnings("unused")
public void testScanMetrics() throws Exception {
    final TableName tableName = TableName.valueOf(name.getMethodName());
    // Set up test table:
    // Create table:
    Table ht = TEST_UTIL.createMultiRegionTable(tableName, FAMILY);
    int numOfRegions = -1;
    try (RegionLocator r = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
        numOfRegions = r.getStartKeys().length;
    }
    // Create 3 rows in the table, with rowkeys starting with "zzz*" so that
    // scan are forced to hit all the regions.
    Put put1 = new Put(Bytes.toBytes("zzz1"));
    put1.addColumn(FAMILY, QUALIFIER, VALUE);
    Put put2 = new Put(Bytes.toBytes("zzz2"));
    put2.addColumn(FAMILY, QUALIFIER, VALUE);
    Put put3 = new Put(Bytes.toBytes("zzz3"));
    put3.addColumn(FAMILY, QUALIFIER, VALUE);
    ht.put(Arrays.asList(put1, put2, put3));
    Scan scan1 = new Scan();
    int numRecords = 0;
    ResultScanner scanner = ht.getScanner(scan1);
    for (Result result : scanner) {
        numRecords++;
    }
    scanner.close();
    LOG.info("test data has " + numRecords + " records.");
    // by default, scan metrics collection is turned off
    assertEquals(null, scan1.getScanMetrics());
    // turn on scan metrics
    Scan scan2 = new Scan();
    scan2.setScanMetricsEnabled(true);
    scan2.setCaching(numRecords + 1);
    scanner = ht.getScanner(scan2);
    for (Result result : scanner.next(numRecords - 1)) {
    }
    scanner.close();
    // closing the scanner will set the metrics.
    assertNotNull(scan2.getScanMetrics());
    // set caching to 1, because metrics are collected in each roundtrip only
    scan2 = new Scan();
    scan2.setScanMetricsEnabled(true);
    scan2.setCaching(1);
    scanner = ht.getScanner(scan2);
    // the end of the scanner. So this is asking for 2 of the 3 rows we inserted.
    for (Result result : scanner.next(numRecords - 1)) {
    }
    scanner.close();
    ScanMetrics scanMetrics = scan2.getScanMetrics();
    assertEquals("Did not access all the regions in the table", numOfRegions, scanMetrics.countOfRegions.get());
    // check byte counters
    scan2 = new Scan();
    scan2.setScanMetricsEnabled(true);
    scan2.setCaching(1);
    scanner = ht.getScanner(scan2);
    int numBytes = 0;
    for (Result result : scanner.next(1)) {
        for (Cell cell : result.listCells()) {
            numBytes += CellUtil.estimatedSerializedSizeOf(cell);
        }
    }
    scanner.close();
    scanMetrics = scan2.getScanMetrics();
    assertEquals("Did not count the result bytes", numBytes, scanMetrics.countOfBytesInResults.get());
    // check byte counters on a small scan
    scan2 = new Scan();
    scan2.setScanMetricsEnabled(true);
    scan2.setCaching(1);
    scan2.setSmall(true);
    scanner = ht.getScanner(scan2);
    numBytes = 0;
    for (Result result : scanner.next(1)) {
        for (Cell cell : result.listCells()) {
            numBytes += CellUtil.estimatedSerializedSizeOf(cell);
        }
    }
    scanner.close();
    scanMetrics = scan2.getScanMetrics();
    assertEquals("Did not count the result bytes", numBytes, scanMetrics.countOfBytesInResults.get());
    // now, test that the metrics are still collected even if you don't call close, but do
    // run past the end of all the records
    /** There seems to be a timing issue here.  Comment out for now. Fix when time.
    Scan scanWithoutClose = new Scan();
    scanWithoutClose.setCaching(1);
    scanWithoutClose.setScanMetricsEnabled(true);
    ResultScanner scannerWithoutClose = ht.getScanner(scanWithoutClose);
    for (Result result : scannerWithoutClose.next(numRecords + 1)) {
    }
    ScanMetrics scanMetricsWithoutClose = getScanMetrics(scanWithoutClose);
    assertEquals("Did not access all the regions in the table", numOfRegions,
        scanMetricsWithoutClose.countOfRegions.get());
    */
    // finally, test that the metrics are collected correctly if you both run past all the records,
    // AND close the scanner
    Scan scanWithClose = new Scan();
    // make sure we can set caching up to the number of a scanned values
    scanWithClose.setCaching(numRecords);
    scanWithClose.setScanMetricsEnabled(true);
    ResultScanner scannerWithClose = ht.getScanner(scanWithClose);
    for (Result result : scannerWithClose.next(numRecords + 1)) {
    }
    scannerWithClose.close();
    ScanMetrics scanMetricsWithClose = getScanMetrics(scanWithClose);
    assertEquals("Did not access all the regions in the table", numOfRegions, scanMetricsWithClose.countOfRegions.get());
}
Also used : TableName(org.apache.hadoop.hbase.TableName) ScanMetrics(org.apache.hadoop.hbase.client.metrics.ScanMetrics) Cell(org.apache.hadoop.hbase.Cell) MultiRowMutationEndpoint(org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint) Test(org.junit.Test)

Example 2 with ScanMetrics

use of org.apache.hadoop.hbase.client.metrics.ScanMetrics in project hbase by apache.

the class ProtobufUtil method toScanMetrics.

public static ScanMetrics toScanMetrics(final byte[] bytes) {
    Parser<MapReduceProtos.ScanMetrics> parser = MapReduceProtos.ScanMetrics.PARSER;
    MapReduceProtos.ScanMetrics pScanMetrics = null;
    try {
        pScanMetrics = parser.parseFrom(bytes);
    } catch (InvalidProtocolBufferException e) {
    //Ignored there are just no key values to add.
    }
    ScanMetrics scanMetrics = new ScanMetrics();
    if (pScanMetrics != null) {
        for (HBaseProtos.NameInt64Pair pair : pScanMetrics.getMetricsList()) {
            if (pair.hasName() && pair.hasValue()) {
                scanMetrics.setCounter(pair.getName(), pair.getValue());
            }
        }
    }
    return scanMetrics;
}
Also used : MapReduceProtos(org.apache.hadoop.hbase.protobuf.generated.MapReduceProtos) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ScanMetrics(org.apache.hadoop.hbase.client.metrics.ScanMetrics) HBaseProtos(org.apache.hadoop.hbase.protobuf.generated.HBaseProtos)

Example 3 with ScanMetrics

use of org.apache.hadoop.hbase.client.metrics.ScanMetrics in project hbase by apache.

the class ScanPerformanceEvaluation method testSnapshotScan.

public void testSnapshotScan() throws IOException {
    Stopwatch snapshotRestoreTimer = Stopwatch.createUnstarted();
    Stopwatch scanOpenTimer = Stopwatch.createUnstarted();
    Stopwatch scanTimer = Stopwatch.createUnstarted();
    Path restoreDir = new Path(this.restoreDir);
    snapshotRestoreTimer.start();
    restoreDir.getFileSystem(conf).delete(restoreDir, true);
    snapshotRestoreTimer.stop();
    Scan scan = getScan();
    scanOpenTimer.start();
    TableSnapshotScanner scanner = new TableSnapshotScanner(conf, restoreDir, snapshotName, scan);
    scanOpenTimer.stop();
    long numRows = 0;
    long numCells = 0;
    scanTimer.start();
    while (true) {
        Result result = scanner.next();
        if (result == null) {
            break;
        }
        numRows++;
        numCells += result.rawCells().length;
    }
    scanTimer.stop();
    scanner.close();
    ScanMetrics metrics = scanner.getScanMetrics();
    long totalBytes = metrics.countOfBytesInResults.get();
    double throughput = (double) totalBytes / scanTimer.elapsed(TimeUnit.SECONDS);
    double throughputRows = (double) numRows / scanTimer.elapsed(TimeUnit.SECONDS);
    double throughputCells = (double) numCells / scanTimer.elapsed(TimeUnit.SECONDS);
    System.out.println("HBase scan snapshot: ");
    System.out.println("total time to restore snapshot: " + snapshotRestoreTimer.elapsed(TimeUnit.MILLISECONDS) + " ms");
    System.out.println("total time to open scanner: " + scanOpenTimer.elapsed(TimeUnit.MILLISECONDS) + " ms");
    System.out.println("total time to scan: " + scanTimer.elapsed(TimeUnit.MILLISECONDS) + " ms");
    System.out.println("Scan metrics:\n" + metrics.getMetricsMap());
    System.out.println("total bytes: " + totalBytes + " bytes (" + StringUtils.humanReadableInt(totalBytes) + ")");
    System.out.println("throughput  : " + StringUtils.humanReadableInt((long) throughput) + "B/s");
    System.out.println("total rows  : " + numRows);
    System.out.println("throughput  : " + StringUtils.humanReadableInt((long) throughputRows) + " rows/s");
    System.out.println("total cells : " + numCells);
    System.out.println("throughput  : " + StringUtils.humanReadableInt((long) throughputCells) + " cells/s");
}
Also used : Path(org.apache.hadoop.fs.Path) TableSnapshotScanner(org.apache.hadoop.hbase.client.TableSnapshotScanner) Stopwatch(org.apache.hbase.thirdparty.com.google.common.base.Stopwatch) Scan(org.apache.hadoop.hbase.client.Scan) ScanMetrics(org.apache.hadoop.hbase.client.metrics.ScanMetrics) Result(org.apache.hadoop.hbase.client.Result)

Example 4 with ScanMetrics

use of org.apache.hadoop.hbase.client.metrics.ScanMetrics in project hbase by apache.

the class ScanPerformanceEvaluation method testScan.

public void testScan() throws IOException {
    Stopwatch tableOpenTimer = Stopwatch.createUnstarted();
    Stopwatch scanOpenTimer = Stopwatch.createUnstarted();
    Stopwatch scanTimer = Stopwatch.createUnstarted();
    tableOpenTimer.start();
    Connection connection = ConnectionFactory.createConnection(getConf());
    Table table = connection.getTable(TableName.valueOf(tablename));
    tableOpenTimer.stop();
    Scan scan = getScan();
    scanOpenTimer.start();
    ResultScanner scanner = table.getScanner(scan);
    scanOpenTimer.stop();
    long numRows = 0;
    long numCells = 0;
    scanTimer.start();
    while (true) {
        Result result = scanner.next();
        if (result == null) {
            break;
        }
        numRows++;
        numCells += result.rawCells().length;
    }
    scanTimer.stop();
    scanner.close();
    table.close();
    connection.close();
    ScanMetrics metrics = scanner.getScanMetrics();
    long totalBytes = metrics.countOfBytesInResults.get();
    double throughput = (double) totalBytes / scanTimer.elapsed(TimeUnit.SECONDS);
    double throughputRows = (double) numRows / scanTimer.elapsed(TimeUnit.SECONDS);
    double throughputCells = (double) numCells / scanTimer.elapsed(TimeUnit.SECONDS);
    System.out.println("HBase scan: ");
    System.out.println("total time to open table: " + tableOpenTimer.elapsed(TimeUnit.MILLISECONDS) + " ms");
    System.out.println("total time to open scanner: " + scanOpenTimer.elapsed(TimeUnit.MILLISECONDS) + " ms");
    System.out.println("total time to scan: " + scanTimer.elapsed(TimeUnit.MILLISECONDS) + " ms");
    System.out.println("Scan metrics:\n" + metrics.getMetricsMap());
    System.out.println("total bytes: " + totalBytes + " bytes (" + StringUtils.humanReadableInt(totalBytes) + ")");
    System.out.println("throughput  : " + StringUtils.humanReadableInt((long) throughput) + "B/s");
    System.out.println("total rows  : " + numRows);
    System.out.println("throughput  : " + StringUtils.humanReadableInt((long) throughputRows) + " rows/s");
    System.out.println("total cells : " + numCells);
    System.out.println("throughput  : " + StringUtils.humanReadableInt((long) throughputCells) + " cells/s");
}
Also used : Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) Stopwatch(org.apache.hbase.thirdparty.com.google.common.base.Stopwatch) Connection(org.apache.hadoop.hbase.client.Connection) Scan(org.apache.hadoop.hbase.client.Scan) ScanMetrics(org.apache.hadoop.hbase.client.metrics.ScanMetrics) Result(org.apache.hadoop.hbase.client.Result)

Example 5 with ScanMetrics

use of org.apache.hadoop.hbase.client.metrics.ScanMetrics in project hbase by apache.

the class TableRecordReaderImpl method updateCounters.

/**
 * If hbase runs on new version of mapreduce, RecordReader has access to
 * counters thus can update counters based on scanMetrics.
 * If hbase runs on old version of mapreduce, it won't be able to get
 * access to counters and TableRecorderReader can't update counter values.
 */
private void updateCounters() {
    ScanMetrics scanMetrics = scanner.getScanMetrics();
    if (scanMetrics == null) {
        return;
    }
    updateCounters(scanMetrics, numRestarts, context, numStale);
}
Also used : ScanMetrics(org.apache.hadoop.hbase.client.metrics.ScanMetrics)

Aggregations

ScanMetrics (org.apache.hadoop.hbase.client.metrics.ScanMetrics)10 TableName (org.apache.hadoop.hbase.TableName)3 Test (org.junit.Test)3 Cell (org.apache.hadoop.hbase.Cell)2 Result (org.apache.hadoop.hbase.client.Result)2 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)2 Scan (org.apache.hadoop.hbase.client.Scan)2 MultiRowMutationEndpoint (org.apache.hadoop.hbase.coprocessor.MultiRowMutationEndpoint)2 Stopwatch (org.apache.hbase.thirdparty.com.google.common.base.Stopwatch)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 List (java.util.List)1 ForkJoinPool (java.util.concurrent.ForkJoinPool)1 Path (org.apache.hadoop.fs.Path)1 HBaseClassTestRule (org.apache.hadoop.hbase.HBaseClassTestRule)1 HBaseTestingUtil (org.apache.hadoop.hbase.HBaseTestingUtil)1 PrivateCellUtil (org.apache.hadoop.hbase.PrivateCellUtil)1 Connection (org.apache.hadoop.hbase.client.Connection)1