diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ClientScanner.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ClientScanner.java index ebab79921ae..362a36f267d 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ClientScanner.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/ClientScanner.java @@ -64,7 +64,8 @@ public class ClientScanner extends AbstractClientScanner { private final HConnection connection; private final byte[] tableName; private final int scannerTimeout; - + private boolean scanMetricsPublished = false; + /** * Create a new ClientScanner for the specified table. An HConnection will be * retrieved using the passed Configuration. @@ -243,12 +244,13 @@ public class ClientScanner extends AbstractClientScanner { * * scan.setAttribute(SCAN_ATTRIBUTES_METRICS_ENABLE, Bytes.toBytes(Boolean.TRUE)) */ - private void writeScanMetrics() throws IOException { - if (this.scanMetrics == null) { + private void writeScanMetrics() { + if (this.scanMetrics == null || scanMetricsPublished) { return; } MapReduceProtos.ScanMetrics pScanMetrics = ProtobufUtil.toScanMetrics(scanMetrics); scan.setAttribute(Scan.SCAN_ATTRIBUTES_METRICS_DATA, pScanMetrics.toByteArray()); + scanMetricsPublished = true; } public Result next() throws IOException { @@ -390,6 +392,7 @@ public class ClientScanner extends AbstractClientScanner { } public void close() { + if (!scanMetricsPublished) writeScanMetrics(); if (callable != null) { callable.setClose(); try { @@ -399,13 +402,6 @@ public class ClientScanner extends AbstractClientScanner { // have since decided that it's not nice for a scanner's close to // throw exceptions. Chances are it was just an UnknownScanner // exception due to lease time out. - } finally { - // we want to output the scan metrics even if an error occurred on close - try { - writeScanMetrics(); - } catch (IOException e) { - // As above, we still don't want the scanner close() method to throw. - } } callable = null; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java index ce94325b6bb..7e4ff0c418b 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide.java @@ -4618,9 +4618,8 @@ public class TestFromClientSide { for (Result result : scanner.next(numRecords - 1)) { } scanner.close(); - // need to have at one next roundtrip in order to collect metrics - // here we have less than +1 KVs, so no metrics were collected - assertNull(scan.getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_DATA)); + // closing the scanner will set the metrics. + assertNotNull(scan.getAttribute(Scan.SCAN_ATTRIBUTES_METRICS_DATA)); // set caching to 1, becasue metrics are collected in each roundtrip only scan = new Scan();