From 3235b568da9fa5ad4639117f2ff502b723647bb6 Mon Sep 17 00:00:00 2001 From: niuyulin Date: Tue, 19 May 2020 10:22:28 -0500 Subject: [PATCH] HBASE-24386 TableSnapshotScanner support scan limit (#1724) Signed-off-by: Jan Hentschel Signed-off by: Viraj Jasani --- .../hbase/client/TableSnapshotScanner.java | 4 ++ .../client/TestTableSnapshotScanner.java | 37 +++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/TableSnapshotScanner.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/TableSnapshotScanner.java index 707befb9816..1e7a7d7a980 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/client/TableSnapshotScanner.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/client/TableSnapshotScanner.java @@ -82,6 +82,7 @@ public class TableSnapshotScanner extends AbstractClientScanner { private ClientSideRegionScanner currentRegionScanner = null; private int currentRegion = -1; + private int numOfCompleteRows = 0; /** * Creates a TableSnapshotScanner. * @param conf the configuration @@ -164,6 +165,9 @@ public class TableSnapshotScanner extends AbstractClientScanner { try { result = currentRegionScanner.next(); if (result != null) { + if (scan.getLimit() > 0 && ++this.numOfCompleteRows > scan.getLimit()) { + result = null; + } return result; } } finally { diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestTableSnapshotScanner.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestTableSnapshotScanner.java index da0df406a20..9f2e254e6ed 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestTableSnapshotScanner.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestTableSnapshotScanner.java @@ -40,6 +40,8 @@ import org.junit.After; import org.junit.Assert; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.junit.Rule; +import org.junit.rules.TestName; @Category(LargeTests.class) public class TestTableSnapshotScanner { @@ -54,6 +56,9 @@ public class TestTableSnapshotScanner { private FileSystem fs; private Path rootDir; + @Rule + public TestName name = new TestName(); + public void setupCluster() throws Exception { setupConf(UTIL.getConfiguration()); UTIL.startMiniCluster(NUM_REGION_SERVERS, true); @@ -159,6 +164,38 @@ public class TestTableSnapshotScanner { } } + + @Test + public void testScanLimit() throws Exception { + setupCluster(); + final TableName tableName = TableName.valueOf(name.getMethodName()); + final String snapshotName = tableName + "Snapshot"; + TableSnapshotScanner scanner = null; + try { + createTableAndSnapshot(UTIL, tableName, snapshotName, 50); + Path restoreDir = UTIL.getDataTestDirOnTestFS(snapshotName); + Scan scan = new Scan().withStartRow(bbb).setLimit(100); // limit the scan + + scanner = new TableSnapshotScanner(UTIL.getConfiguration(), restoreDir, snapshotName, scan); + int count = 0; + while (true) { + Result result = scanner.next(); + if (result == null) { + break; + } + count++; + } + Assert.assertEquals(100, count); + } finally { + if (scanner != null) { + scanner.close(); + } + UTIL.getHBaseAdmin().deleteSnapshot(snapshotName); + UTIL.deleteTable(tableName); + tearDownCluster(); + } + } + @Test public void testWithSingleRegion() throws Exception { testScanner(UTIL, "testWithSingleRegion", 1, false);