From 68719dfc037c791c9c4d8f7235f191433af06cd4 Mon Sep 17 00:00:00 2001 From: Duo Zhang Date: Sat, 24 Feb 2024 16:20:37 +0800 Subject: [PATCH] HBASE-28398 Make sure we close all the scanners in TestHRegion (#5701) Signed-off-by: Bryan Beaudreault Signed-off-by: Rajeshbabu Chintaguntla (cherry picked from commit 63e3a43041cf89ec7c18962380c769f9a253e8d0) --- .../hbase/regionserver/TestHRegion.java | 1070 ++++++++--------- 1 file changed, 534 insertions(+), 536 deletions(-) diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHRegion.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHRegion.java index 2120f6ab856c..67221e82cc26 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHRegion.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestHRegion.java @@ -286,21 +286,26 @@ public void testSequenceId() throws IOException { HBaseTestingUtility.closeRegionAndWAL(this.region); assertEquals(HConstants.NO_SEQNUM, region.getMaxFlushedSeqId()); assertEquals(0, (long) region.getMaxStoreSeqId().get(COLUMN_FAMILY_BYTES)); - // Open region again. - region = initHRegion(tableName, method, CONF, COLUMN_FAMILY_BYTES); - byte[] value = Bytes.toBytes(method); - // Make a random put against our cf. - Put put = new Put(value); - put.addColumn(COLUMN_FAMILY_BYTES, null, value); - region.put(put); - // No flush yet so init numbers should still be in place. - assertEquals(HConstants.NO_SEQNUM, region.getMaxFlushedSeqId()); - assertEquals(0, (long) region.getMaxStoreSeqId().get(COLUMN_FAMILY_BYTES)); - region.flush(true); - long max = region.getMaxFlushedSeqId(); - HBaseTestingUtility.closeRegionAndWAL(this.region); - assertEquals(max, region.getMaxFlushedSeqId()); - this.region = null; + HRegion oldRegion = region; + try { + // Open region again. + region = initHRegion(tableName, method, CONF, COLUMN_FAMILY_BYTES); + byte[] value = Bytes.toBytes(method); + // Make a random put against our cf. + Put put = new Put(value); + put.addColumn(COLUMN_FAMILY_BYTES, null, value); + region.put(put); + // No flush yet so init numbers should still be in place. + assertEquals(HConstants.NO_SEQNUM, region.getMaxFlushedSeqId()); + assertEquals(0, (long) region.getMaxStoreSeqId().get(COLUMN_FAMILY_BYTES)); + region.flush(true); + long max = region.getMaxFlushedSeqId(); + HBaseTestingUtility.closeRegionAndWAL(this.region); + assertEquals(max, region.getMaxFlushedSeqId()); + this.region = null; + } finally { + HBaseTestingUtility.closeRegionAndWAL(oldRegion); + } } /** @@ -616,39 +621,38 @@ public void testCompactionAffectedByScanners() throws Exception { Scan scan = new Scan(); scan.setMaxVersions(3); // open the first scanner - RegionScanner scanner1 = region.getScanner(scan); - - Delete delete = new Delete(Bytes.toBytes("r1")); - region.delete(delete); - region.flush(true); - - // open the second scanner - RegionScanner scanner2 = region.getScanner(scan); - - List results = new ArrayList<>(); - - System.out.println("Smallest read point:" + region.getSmallestReadPoint()); + try (RegionScanner scanner1 = region.getScanner(scan)) { + Delete delete = new Delete(Bytes.toBytes("r1")); + region.delete(delete); + region.flush(true); + // open the second scanner + try (RegionScanner scanner2 = region.getScanner(scan)) { + List results = new ArrayList<>(); - // make a major compaction - region.compact(true); + LOG.info("Smallest read point:" + region.getSmallestReadPoint()); - // open the third scanner - RegionScanner scanner3 = region.getScanner(scan); + // make a major compaction + region.compact(true); - // get data from scanner 1, 2, 3 after major compaction - scanner1.next(results); - System.out.println(results); - assertEquals(1, results.size()); + // open the third scanner + try (RegionScanner scanner3 = region.getScanner(scan)) { + // get data from scanner 1, 2, 3 after major compaction + scanner1.next(results); + LOG.info(results.toString()); + assertEquals(1, results.size()); - results.clear(); - scanner2.next(results); - System.out.println(results); - assertEquals(0, results.size()); + results.clear(); + scanner2.next(results); + LOG.info(results.toString()); + assertEquals(0, results.size()); - results.clear(); - scanner3.next(results); - System.out.println(results); - assertEquals(0, results.size()); + results.clear(); + scanner3.next(results); + LOG.info(results.toString()); + assertEquals(0, results.size()); + } + } + } } @Test @@ -667,18 +671,18 @@ public void testToShowNPEOnRegionScannerReseek() throws Exception { Scan scan = new Scan(); scan.setMaxVersions(3); // open the first scanner - RegionScanner scanner1 = region.getScanner(scan); - - System.out.println("Smallest read point:" + region.getSmallestReadPoint()); + try (RegionScanner scanner1 = region.getScanner(scan)) { + LOG.info("Smallest read point:" + region.getSmallestReadPoint()); - region.compact(true); + region.compact(true); - scanner1.reseek(Bytes.toBytes("r2")); - List results = new ArrayList<>(); - scanner1.next(results); - Cell keyValue = results.get(0); - Assert.assertTrue(Bytes.compareTo(CellUtil.cloneRow(keyValue), Bytes.toBytes("r2")) == 0); - scanner1.close(); + scanner1.reseek(Bytes.toBytes("r2")); + List results = new ArrayList<>(); + scanner1.next(results); + Cell keyValue = results.get(0); + assertTrue(Bytes.compareTo(CellUtil.cloneRow(keyValue), Bytes.toBytes("r2")) == 0); + scanner1.close(); + } } @Test @@ -1470,37 +1474,45 @@ public void testIncrWithReadOnlyTable() throws Exception { } private void deleteColumns(HRegion r, String value, String keyPrefix) throws IOException { - InternalScanner scanner = buildScanner(keyPrefix, value, r); int count = 0; - boolean more = false; - List results = new ArrayList<>(); - do { - more = scanner.next(results); - if (results != null && !results.isEmpty()) count++; - else break; - Delete delete = new Delete(CellUtil.cloneRow(results.get(0))); - delete.addColumn(Bytes.toBytes("trans-tags"), Bytes.toBytes("qual2")); - r.delete(delete); - results.clear(); - } while (more); + try (InternalScanner scanner = buildScanner(keyPrefix, value, r)) { + boolean more = false; + List results = new ArrayList<>(); + do { + more = scanner.next(results); + if (results != null && !results.isEmpty()) { + count++; + } else { + break; + } + Delete delete = new Delete(CellUtil.cloneRow(results.get(0))); + delete.addColumn(Bytes.toBytes("trans-tags"), Bytes.toBytes("qual2")); + r.delete(delete); + results.clear(); + } while (more); + } assertEquals("Did not perform correct number of deletes", 3, count); } private int getNumberOfRows(String keyPrefix, String value, HRegion r) throws Exception { - InternalScanner resultScanner = buildScanner(keyPrefix, value, r); - int numberOfResults = 0; - List results = new ArrayList<>(); - boolean more = false; - do { - more = resultScanner.next(results); - if (results != null && !results.isEmpty()) numberOfResults++; - else break; - for (Cell kv : results) { - System.out.println("kv=" + kv.toString() + ", " + Bytes.toString(CellUtil.cloneValue(kv))); - } - results.clear(); - } while (more); - return numberOfResults; + try (InternalScanner resultScanner = buildScanner(keyPrefix, value, r)) { + int numberOfResults = 0; + List results = new ArrayList<>(); + boolean more = false; + do { + more = resultScanner.next(results); + if (results != null && !results.isEmpty()) { + numberOfResults++; + } else { + break; + } + for (Cell kv : results) { + LOG.info("kv=" + kv.toString() + ", " + Bytes.toString(CellUtil.cloneValue(kv))); + } + results.clear(); + } while (more); + return numberOfResults; + } } private InternalScanner buildScanner(String keyPrefix, String value, HRegion r) @@ -3330,14 +3342,15 @@ public void testScanner_DeleteOneFamilyNotAnother() throws IOException { Scan scan = new Scan(); scan.addFamily(fam1).addFamily(fam2); - InternalScanner s = region.getScanner(scan); - List results = new ArrayList<>(); - s.next(results); - assertTrue(CellUtil.matchingRows(results.get(0), rowA)); + try (InternalScanner s = region.getScanner(scan)) { + List results = new ArrayList<>(); + s.next(results); + assertTrue(CellUtil.matchingRows(results.get(0), rowA)); - results.clear(); - s.next(results); - assertTrue(CellUtil.matchingRows(results.get(0), rowB)); + results.clear(); + s.next(results); + assertTrue(CellUtil.matchingRows(results.get(0), rowB)); + } } @Test @@ -3459,17 +3472,17 @@ public void doTestDelete_AndPostInsert(Delete delete) throws IOException, Interr // next: Scan scan = new Scan(row); scan.addColumn(fam1, qual1); - InternalScanner s = region.getScanner(scan); - - List results = new ArrayList<>(); - assertEquals(false, s.next(results)); - assertEquals(1, results.size()); - Cell kv = results.get(0); + try (InternalScanner s = region.getScanner(scan)) { + List results = new ArrayList<>(); + assertEquals(false, s.next(results)); + assertEquals(1, results.size()); + Cell kv = results.get(0); - assertArrayEquals(value2, CellUtil.cloneValue(kv)); - assertArrayEquals(fam1, CellUtil.cloneFamily(kv)); - assertArrayEquals(qual1, CellUtil.cloneQualifier(kv)); - assertArrayEquals(row, CellUtil.cloneRow(kv)); + assertArrayEquals(value2, CellUtil.cloneValue(kv)); + assertArrayEquals(fam1, CellUtil.cloneFamily(kv)); + assertArrayEquals(qual1, CellUtil.cloneQualifier(kv)); + assertArrayEquals(row, CellUtil.cloneRow(kv)); + } } @Test @@ -3662,11 +3675,7 @@ public void testGetScanner_WithOkFamilies() throws IOException { Scan scan = new Scan(); scan.addFamily(fam1); scan.addFamily(fam2); - try { - region.getScanner(scan); - } catch (Exception e) { - assertTrue("Families could not be found in Region", false); - } + region.getScanner(scan).close(); } @Test @@ -3680,13 +3689,7 @@ public void testGetScanner_WithNotOkFamilies() throws IOException { this.region = initHRegion(tableName, method, CONF, families); Scan scan = new Scan(); scan.addFamily(fam2); - boolean ok = false; - try { - region.getScanner(scan); - } catch (Exception e) { - ok = true; - } - assertTrue("Families could not be found in Region", ok); + assertThrows(NoSuchColumnFamilyException.class, () -> region.getScanner(scan)); } @Test @@ -3710,20 +3713,20 @@ public void testGetScanner_WithNoFamilies() throws IOException { region.put(put); Scan scan = null; - RegionScannerImpl is = null; - // Testing to see how many scanners that is produced by getScanner, - // starting - // with known number, 2 - current = 1 + // Testing to see how many scanners that is produced by getScanner, starting with known number, + // 2 - current = 1 scan = new Scan(); scan.addFamily(fam2); scan.addFamily(fam4); - is = region.getScanner(scan); - assertEquals(1, is.storeHeap.getHeap().size()); + try (RegionScannerImpl is = region.getScanner(scan)) { + assertEquals(1, is.storeHeap.getHeap().size()); + } scan = new Scan(); - is = region.getScanner(scan); - assertEquals(families.length - 1, is.storeHeap.getHeap().size()); + try (RegionScannerImpl is = region.getScanner(scan)) { + assertEquals(families.length - 1, is.storeHeap.getHeap().size()); + } } /** @@ -3744,15 +3747,7 @@ public void testGetScanner_WithRegionClosed() throws IOException { fail("Got IOException during initHRegion, " + e.getMessage()); } region.closed.set(true); - try { - region.getScanner(null); - fail("Expected to get an exception during getScanner on a region that is closed"); - } catch (NotServingRegionException e) { - // this is the correct exception that is expected - } catch (IOException e) { - fail("Got wrong type of exception - should be a NotServingRegionException, " - + "but was an IOException: " + e.getMessage()); - } + assertThrows(NotServingRegionException.class, () -> region.getScanner(null)); } @Test @@ -3788,30 +3783,30 @@ public void testRegionScanner_Next() throws IOException { Scan scan = new Scan(); scan.addFamily(fam2); scan.addFamily(fam4); - InternalScanner is = region.getScanner(scan); - - List res = null; - - // Result 1 - List expected1 = new ArrayList<>(); - expected1.add(new KeyValue(row1, fam2, null, ts, KeyValue.Type.Put, null)); - expected1.add(new KeyValue(row1, fam4, null, ts, KeyValue.Type.Put, null)); - - res = new ArrayList<>(); - is.next(res); - for (int i = 0; i < res.size(); i++) { - assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected1.get(i), res.get(i))); - } + try (InternalScanner is = region.getScanner(scan)) { + List res = null; + + // Result 1 + List expected1 = new ArrayList<>(); + expected1.add(new KeyValue(row1, fam2, null, ts, KeyValue.Type.Put, null)); + expected1.add(new KeyValue(row1, fam4, null, ts, KeyValue.Type.Put, null)); + + res = new ArrayList<>(); + is.next(res); + for (int i = 0; i < res.size(); i++) { + assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected1.get(i), res.get(i))); + } - // Result 2 - List expected2 = new ArrayList<>(); - expected2.add(new KeyValue(row2, fam2, null, ts, KeyValue.Type.Put, null)); - expected2.add(new KeyValue(row2, fam4, null, ts, KeyValue.Type.Put, null)); + // Result 2 + List expected2 = new ArrayList<>(); + expected2.add(new KeyValue(row2, fam2, null, ts, KeyValue.Type.Put, null)); + expected2.add(new KeyValue(row2, fam4, null, ts, KeyValue.Type.Put, null)); - res = new ArrayList<>(); - is.next(res); - for (int i = 0; i < res.size(); i++) { - assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected2.get(i), res.get(i))); + res = new ArrayList<>(); + is.next(res); + for (int i = 0; i < res.size(); i++) { + assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected2.get(i), res.get(i))); + } } } @@ -3857,14 +3852,14 @@ public void testScanner_ExplicitColumns_FromMemStore_EnforceVersions() throws IO scan.addColumn(fam1, qf1); scan.setMaxVersions(MAX_VERSIONS); List actual = new ArrayList<>(); - InternalScanner scanner = region.getScanner(scan); - - boolean hasNext = scanner.next(actual); - assertEquals(false, hasNext); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(actual); + assertEquals(false, hasNext); - // Verify result - for (int i = 0; i < expected.size(); i++) { - assertEquals(expected.get(i), actual.get(i)); + // Verify result + for (int i = 0; i < expected.size(); i++) { + assertEquals(expected.get(i), actual.get(i)); + } } } @@ -3914,14 +3909,14 @@ public void testScanner_ExplicitColumns_FromFilesOnly_EnforceVersions() throws I scan.addColumn(fam1, qf2); scan.setMaxVersions(MAX_VERSIONS); List actual = new ArrayList<>(); - InternalScanner scanner = region.getScanner(scan); - - boolean hasNext = scanner.next(actual); - assertEquals(false, hasNext); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(actual); + assertEquals(false, hasNext); - // Verify result - for (int i = 0; i < expected.size(); i++) { - assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected.get(i), actual.get(i))); + // Verify result + for (int i = 0; i < expected.size(); i++) { + assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected.get(i), actual.get(i))); + } } } @@ -3991,14 +3986,14 @@ public void testScanner_ExplicitColumns_FromMemStoreAndFiles_EnforceVersions() int versions = 3; scan.setMaxVersions(versions); List actual = new ArrayList<>(); - InternalScanner scanner = region.getScanner(scan); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(actual); + assertEquals(false, hasNext); - boolean hasNext = scanner.next(actual); - assertEquals(false, hasNext); - - // Verify result - for (int i = 0; i < expected.size(); i++) { - assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected.get(i), actual.get(i))); + // Verify result + for (int i = 0; i < expected.size(); i++) { + assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected.get(i), actual.get(i))); + } } } @@ -4046,14 +4041,14 @@ public void testScanner_Wildcard_FromMemStore_EnforceVersions() throws IOExcepti scan.addFamily(fam1); scan.setMaxVersions(MAX_VERSIONS); List actual = new ArrayList<>(); - InternalScanner scanner = region.getScanner(scan); - - boolean hasNext = scanner.next(actual); - assertEquals(false, hasNext); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(actual); + assertEquals(false, hasNext); - // Verify result - for (int i = 0; i < expected.size(); i++) { - assertEquals(expected.get(i), actual.get(i)); + // Verify result + for (int i = 0; i < expected.size(); i++) { + assertEquals(expected.get(i), actual.get(i)); + } } } @@ -4101,14 +4096,14 @@ public void testScanner_Wildcard_FromFilesOnly_EnforceVersions() throws IOExcept scan.addFamily(fam1); scan.setMaxVersions(MAX_VERSIONS); List actual = new ArrayList<>(); - InternalScanner scanner = region.getScanner(scan); - - boolean hasNext = scanner.next(actual); - assertEquals(false, hasNext); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(actual); + assertEquals(false, hasNext); - // Verify result - for (int i = 0; i < expected.size(); i++) { - assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected.get(i), actual.get(i))); + // Verify result + for (int i = 0; i < expected.size(); i++) { + assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected.get(i), actual.get(i))); + } } } @@ -4148,11 +4143,11 @@ public void testScanner_StopRow1542() throws IOException { Scan scan = new Scan(row3, row4); scan.setMaxVersions(); scan.addColumn(family, col1); - InternalScanner s = region.getScanner(scan); - - List results = new ArrayList<>(); - assertEquals(false, s.next(results)); - assertEquals(0, results.size()); + try (InternalScanner s = region.getScanner(scan)) { + List results = new ArrayList<>(); + assertEquals(false, s.next(results)); + assertEquals(0, results.size()); + } } @Test @@ -4217,14 +4212,14 @@ public void testScanner_Wildcard_FromMemStoreAndFiles_EnforceVersions() throws I int versions = 3; scan.setMaxVersions(versions); List actual = new ArrayList<>(); - InternalScanner scanner = region.getScanner(scan); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(actual); + assertEquals(false, hasNext); - boolean hasNext = scanner.next(actual); - assertEquals(false, hasNext); - - // Verify result - for (int i = 0; i < expected.size(); i++) { - assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected.get(i), actual.get(i))); + // Verify result + for (int i = 0; i < expected.size(); i++) { + assertTrue(PrivateCellUtil.equalsIgnoreMvccVersion(expected.get(i), actual.get(i))); + } } } @@ -4272,22 +4267,22 @@ public void testScanner_JoinedScanners() throws IOException { CompareOp.NOT_EQUAL, filtered_val); scan.setFilter(filter); scan.setLoadColumnFamiliesOnDemand(true); - InternalScanner s = region.getScanner(scan); - - List results = new ArrayList<>(); - assertTrue(s.next(results)); - assertEquals(1, results.size()); - results.clear(); + try (InternalScanner s = region.getScanner(scan)) { + List results = new ArrayList<>(); + assertTrue(s.next(results)); + assertEquals(1, results.size()); + results.clear(); - assertTrue(s.next(results)); - assertEquals(3, results.size()); - assertTrue("orderCheck", CellUtil.matchingFamily(results.get(0), cf_alpha)); - assertTrue("orderCheck", CellUtil.matchingFamily(results.get(1), cf_essential)); - assertTrue("orderCheck", CellUtil.matchingFamily(results.get(2), cf_joined)); - results.clear(); + assertTrue(s.next(results)); + assertEquals(3, results.size()); + assertTrue("orderCheck", CellUtil.matchingFamily(results.get(0), cf_alpha)); + assertTrue("orderCheck", CellUtil.matchingFamily(results.get(1), cf_essential)); + assertTrue("orderCheck", CellUtil.matchingFamily(results.get(2), cf_joined)); + results.clear(); - assertFalse(s.next(results)); - assertEquals(0, results.size()); + assertFalse(s.next(results)); + assertEquals(0, results.size()); + } } /** @@ -4330,55 +4325,55 @@ public boolean isFamilyEssential(byte[] name) { }; scan.setFilter(bogusFilter); - InternalScanner s = region.getScanner(scan); - - // Our data looks like this: - // r0: first:a, first:b, second:a, second:b - // r1: first:a, first:b, second:a, second:b - // r2: first:a, first:b, second:a, second:b - // r3: first:a, first:b, second:a, second:b - // r4: first:a, first:b, second:a, second:b - // r5: first:a - // r6: first:a - // r7: first:a - // r8: first:a - // r9: first:a - - // But due to next's limit set to 3, we should get this: - // r0: first:a, first:b, second:a - // r0: second:b - // r1: first:a, first:b, second:a - // r1: second:b - // r2: first:a, first:b, second:a - // r2: second:b - // r3: first:a, first:b, second:a - // r3: second:b - // r4: first:a, first:b, second:a - // r4: second:b - // r5: first:a - // r6: first:a - // r7: first:a - // r8: first:a - // r9: first:a - - List results = new ArrayList<>(); - int index = 0; - ScannerContext scannerContext = ScannerContext.newBuilder().setBatchLimit(3).build(); - while (true) { - boolean more = s.next(results, scannerContext); - if ((index >> 1) < 5) { - if (index % 2 == 0) { - assertEquals(3, results.size()); + try (InternalScanner s = region.getScanner(scan)) { + // Our data looks like this: + // r0: first:a, first:b, second:a, second:b + // r1: first:a, first:b, second:a, second:b + // r2: first:a, first:b, second:a, second:b + // r3: first:a, first:b, second:a, second:b + // r4: first:a, first:b, second:a, second:b + // r5: first:a + // r6: first:a + // r7: first:a + // r8: first:a + // r9: first:a + + // But due to next's limit set to 3, we should get this: + // r0: first:a, first:b, second:a + // r0: second:b + // r1: first:a, first:b, second:a + // r1: second:b + // r2: first:a, first:b, second:a + // r2: second:b + // r3: first:a, first:b, second:a + // r3: second:b + // r4: first:a, first:b, second:a + // r4: second:b + // r5: first:a + // r6: first:a + // r7: first:a + // r8: first:a + // r9: first:a + + List results = new ArrayList<>(); + int index = 0; + ScannerContext scannerContext = ScannerContext.newBuilder().setBatchLimit(3).build(); + while (true) { + boolean more = s.next(results, scannerContext); + if ((index >> 1) < 5) { + if (index % 2 == 0) { + assertEquals(3, results.size()); + } else { + assertEquals(1, results.size()); + } } else { assertEquals(1, results.size()); } - } else { - assertEquals(1, results.size()); - } - results.clear(); - index++; - if (!more) { - break; + results.clear(); + index++; + if (!more) { + break; + } } } } @@ -4387,15 +4382,15 @@ public boolean isFamilyEssential(byte[] name) { public void testScannerOperationId() throws IOException { region = initHRegion(tableName, method, CONF, COLUMN_FAMILY_BYTES); Scan scan = new Scan(); - RegionScanner scanner = region.getScanner(scan); - assertNull(scanner.getOperationId()); - scanner.close(); + try (RegionScanner scanner = region.getScanner(scan)) { + assertNull(scanner.getOperationId()); + } String operationId = "test_operation_id_0101"; scan = new Scan().setId(operationId); - scanner = region.getScanner(scan); - assertEquals(operationId, scanner.getOperationId()); - scanner.close(); + try (RegionScanner scanner = region.getScanner(scan)) { + assertEquals(operationId, scanner.getOperationId()); + } HBaseTestingUtility.closeRegionAndWAL(this.region); } @@ -4463,12 +4458,14 @@ public void testFlushCacheWhileScanning() throws IOException, InterruptedExcepti if (i != 0 && i % flushAndScanInterval == 0) { res.clear(); - InternalScanner scanner = region.getScanner(scan); - if (toggle) { - flushThread.flush(); + try (InternalScanner scanner = region.getScanner(scan)) { + if (toggle) { + flushThread.flush(); + } + while (scanner.next(res)) { + // ignore + } } - while (scanner.next(res)) - ; if (!toggle) { flushThread.flush(); } @@ -4885,13 +4882,14 @@ Arrays. asList( new BinaryComparator(Bytes.toBytes(0L))), new SingleColumnValueFilter(family, qual1, CompareOp.LESS_OR_EQUAL, new BinaryComparator(Bytes.toBytes(3L)))))); - InternalScanner scanner = region.getScanner(idxScan); - List res = new ArrayList<>(); + try (InternalScanner scanner = region.getScanner(idxScan)) { + List res = new ArrayList<>(); - while (scanner.next(res)) { - // Ignore res value. + while (scanner.next(res)) { + // Ignore res value. + } + assertEquals(1L, res.size()); } - assertEquals(1L, res.size()); } // //////////////////////////////////////////////////////////////////////////// @@ -5739,8 +5737,7 @@ protected void assertScan(final HRegion r, final byte[] fs, final byte[] firstVa Scan scan = new Scan(); for (int i = 0; i < families.length; i++) scan.addFamily(families[i]); - InternalScanner s = r.getScanner(scan); - try { + try (InternalScanner s = r.getScanner(scan)) { List curVals = new ArrayList<>(); boolean first = true; OUTER_LOOP: while (s.next(curVals)) { @@ -5756,8 +5753,6 @@ protected void assertScan(final HRegion r, final byte[] fs, final byte[] firstVa } } } - } finally { - s.close(); } } @@ -5895,26 +5890,26 @@ public void testReverseScanner_FromMemStore_SingleCF_Normal() throws IOException Scan scan = new Scan(rowC); scan.setMaxVersions(5); scan.setReversed(true); - InternalScanner scanner = region.getScanner(scan); - List currRow = new ArrayList<>(); - boolean hasNext = scanner.next(currRow); - assertEquals(2, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowC, 0, rowC.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowB, 0, rowB.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowA, 0, rowA.length)); - assertFalse(hasNext); - scanner.close(); + try (InternalScanner scanner = region.getScanner(scan)) { + List currRow = new ArrayList<>(); + boolean hasNext = scanner.next(currRow); + assertEquals(2, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowC, 0, rowC.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowB, 0, rowB.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowA, 0, rowA.length)); + assertFalse(hasNext); + } } @Test @@ -5948,25 +5943,25 @@ public void testReverseScanner_FromMemStore_SingleCF_LargerKey() throws IOExcept List currRow = new ArrayList<>(); scan.setReversed(true); scan.setMaxVersions(5); - InternalScanner scanner = region.getScanner(scan); - boolean hasNext = scanner.next(currRow); - assertEquals(2, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowC, 0, rowC.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowB, 0, rowB.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowA, 0, rowA.length)); - assertFalse(hasNext); - scanner.close(); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(currRow); + assertEquals(2, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowC, 0, rowC.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowB, 0, rowB.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowA, 0, rowA.length)); + assertFalse(hasNext); + } } @Test @@ -5997,25 +5992,25 @@ public void testReverseScanner_FromMemStore_SingleCF_FullScan() throws IOExcepti Scan scan = new Scan(); List currRow = new ArrayList<>(); scan.setReversed(true); - InternalScanner scanner = region.getScanner(scan); - boolean hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowC, 0, rowC.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowB, 0, rowB.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowA, 0, rowA.length)); - assertFalse(hasNext); - scanner.close(); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowC, 0, rowC.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowB, 0, rowB.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowA, 0, rowA.length)); + assertFalse(hasNext); + } } @Test @@ -6062,36 +6057,37 @@ public void testReverseScanner_moreRowsMayExistAfter() throws IOException { scan.addColumn(families[0], col1); scan.setReversed(true); List currRow = new ArrayList<>(); - InternalScanner scanner = region.getScanner(scan); - boolean hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowD, 0, rowD.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowC, 0, rowC.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowB, 0, rowB.length)); - assertFalse(hasNext); - scanner.close(); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowD, 0, rowD.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowC, 0, rowC.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowB, 0, rowB.length)); + assertFalse(hasNext); + } scan = new Scan(rowD, rowA); scan.addColumn(families[0], col2); scan.setReversed(true); currRow.clear(); - scanner = region.getScanner(scan); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowD, 0, rowD.length)); - scanner.close(); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowD, 0, rowD.length)); + assertTrue(hasNext); + } } @Test @@ -6140,36 +6136,37 @@ public void testReverseScanner_smaller_blocksize() throws IOException { scan.addColumn(families[0], col1); scan.setReversed(true); List currRow = new ArrayList<>(); - InternalScanner scanner = region.getScanner(scan); - boolean hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowD, 0, rowD.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowC, 0, rowC.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowB, 0, rowB.length)); - assertFalse(hasNext); - scanner.close(); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowD, 0, rowD.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowC, 0, rowC.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowB, 0, rowB.length)); + assertFalse(hasNext); + } scan = new Scan(rowD, rowA); scan.addColumn(families[0], col2); scan.setReversed(true); currRow.clear(); - scanner = region.getScanner(scan); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), rowD, 0, rowD.length)); - scanner.close(); + try (InternalScanner scanner = region.getScanner(scan)) { + boolean hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), rowD, 0, rowD.length)); + assertTrue(hasNext); + } } @Test @@ -6261,60 +6258,59 @@ public void testReverseScanner_FromMemStoreAndHFiles_MultiCFs1() throws IOExcept scan.setMaxVersions(5); scan.setBatch(3); scan.setReversed(true); - InternalScanner scanner = region.getScanner(scan); - List currRow = new ArrayList<>(); - boolean hasNext = false; - // 1. scan out "row4" (5 kvs), "row5" can't be scanned out since not - // included in scan range - // "row4" takes 2 next() calls since batch=3 - hasNext = scanner.next(currRow); - assertEquals(3, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), row4, 0, row4.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(2, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), row4, 0, row4.length)); - assertTrue(hasNext); - // 2. scan out "row3" (2 kv) - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(2, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), row3, 0, row3.length)); - assertTrue(hasNext); - // 3. scan out "row2" (4 kvs) - // "row2" takes 2 next() calls since batch=3 - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(3, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), row2, 0, row2.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), row2, 0, row2.length)); - assertTrue(hasNext); - // 4. scan out "row1" (2 kv) - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(2, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), row1, 0, row1.length)); - assertTrue(hasNext); - // 5. scan out "row0" (1 kv) - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), row0, 0, row0.length)); - assertFalse(hasNext); - - scanner.close(); + try (InternalScanner scanner = region.getScanner(scan)) { + List currRow = new ArrayList<>(); + boolean hasNext = false; + // 1. scan out "row4" (5 kvs), "row5" can't be scanned out since not + // included in scan range + // "row4" takes 2 next() calls since batch=3 + hasNext = scanner.next(currRow); + assertEquals(3, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), row4, 0, row4.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(2, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), row4, 0, row4.length)); + assertTrue(hasNext); + // 2. scan out "row3" (2 kv) + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(2, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), row3, 0, row3.length)); + assertTrue(hasNext); + // 3. scan out "row2" (4 kvs) + // "row2" takes 2 next() calls since batch=3 + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(3, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), row2, 0, row2.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), row2, 0, row2.length)); + assertTrue(hasNext); + // 4. scan out "row1" (2 kv) + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(2, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), row1, 0, row1.length)); + assertTrue(hasNext); + // 5. scan out "row0" (1 kv) + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), row0, 0, row0.length)); + assertFalse(hasNext); + } } @Test @@ -6361,31 +6357,32 @@ public void testReverseScanner_FromMemStoreAndHFiles_MultiCFs2() throws IOExcept Scan scan = new Scan(row4); scan.setReversed(true); scan.setBatch(10); - InternalScanner scanner = region.getScanner(scan); - List currRow = new ArrayList<>(); - boolean hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), row4, 0, row4.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), row3, 0, row3.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), row2, 0, row2.length)); - assertTrue(hasNext); - currRow.clear(); - hasNext = scanner.next(currRow); - assertEquals(1, currRow.size()); - assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), - currRow.get(0).getRowLength(), row1, 0, row1.length)); - assertFalse(hasNext); + try (InternalScanner scanner = region.getScanner(scan)) { + List currRow = new ArrayList<>(); + boolean hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), row4, 0, row4.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), row3, 0, row3.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), row2, 0, row2.length)); + assertTrue(hasNext); + currRow.clear(); + hasNext = scanner.next(currRow); + assertEquals(1, currRow.size()); + assertTrue(Bytes.equals(currRow.get(0).getRowArray(), currRow.get(0).getRowOffset(), + currRow.get(0).getRowLength(), row1, 0, row1.length)); + assertFalse(hasNext); + } } /** @@ -6409,35 +6406,35 @@ public void testReverseScanner_StackOverflow() throws IOException { Scan scan = new Scan(Bytes.toBytes("19998")); scan.setReversed(true); - InternalScanner scanner = region.getScanner(scan); - - // create one storefile contains many rows will be skipped - // to check StoreFileScanner.seekToPreviousRow - for (int i = 10000; i < 20000; i++) { - Put p = new Put(Bytes.toBytes("" + i)); - p.addColumn(cf1, col, Bytes.toBytes("" + i)); - region.put(p); - } - region.flushcache(true, true, FlushLifeCycleTracker.DUMMY); + try (InternalScanner scanner = region.getScanner(scan)) { + // create one storefile contains many rows will be skipped + // to check StoreFileScanner.seekToPreviousRow + for (int i = 10000; i < 20000; i++) { + Put p = new Put(Bytes.toBytes("" + i)); + p.addColumn(cf1, col, Bytes.toBytes("" + i)); + region.put(p); + } + region.flushcache(true, true, FlushLifeCycleTracker.DUMMY); + + // create one memstore contains many rows will be skipped + // to check MemStoreScanner.seekToPreviousRow + for (int i = 10000; i < 20000; i++) { + Put p = new Put(Bytes.toBytes("" + i)); + p.addColumn(cf1, col, Bytes.toBytes("" + i)); + region.put(p); + } - // create one memstore contains many rows will be skipped - // to check MemStoreScanner.seekToPreviousRow - for (int i = 10000; i < 20000; i++) { - Put p = new Put(Bytes.toBytes("" + i)); - p.addColumn(cf1, col, Bytes.toBytes("" + i)); - region.put(p); + List currRow = new ArrayList<>(); + boolean hasNext; + do { + hasNext = scanner.next(currRow); + } while (hasNext); + assertEquals(2, currRow.size()); + assertEquals("19998", Bytes.toString(currRow.get(0).getRowArray(), + currRow.get(0).getRowOffset(), currRow.get(0).getRowLength())); + assertEquals("19997", Bytes.toString(currRow.get(1).getRowArray(), + currRow.get(1).getRowOffset(), currRow.get(1).getRowLength())); } - - List currRow = new ArrayList<>(); - boolean hasNext; - do { - hasNext = scanner.next(currRow); - } while (hasNext); - assertEquals(2, currRow.size()); - assertEquals("19998", Bytes.toString(currRow.get(0).getRowArray(), - currRow.get(0).getRowOffset(), currRow.get(0).getRowLength())); - assertEquals("19997", Bytes.toString(currRow.get(1).getRowArray(), - currRow.get(1).getRowOffset(), currRow.get(1).getRowLength())); } @Test @@ -6456,37 +6453,38 @@ public void testReverseScanShouldNotScanMemstoreIfReadPtLesser() throws Exceptio // create a reverse scan Scan scan = new Scan(Bytes.toBytes("19996")); scan.setReversed(true); - RegionScannerImpl scanner = region.getScanner(scan); - - // flush the cache. This will reset the store scanner - region.flushcache(true, true, FlushLifeCycleTracker.DUMMY); - - // create one memstore contains many rows will be skipped - // to check MemStoreScanner.seekToPreviousRow - for (int i = 10000; i < 20000; i++) { - Put p = new Put(Bytes.toBytes("" + i)); - p.addColumn(cf1, col, Bytes.toBytes("" + i)); - region.put(p); - } - List currRow = new ArrayList<>(); - boolean hasNext; - boolean assertDone = false; - do { - hasNext = scanner.next(currRow); - // With HBASE-15871, after the scanner is reset the memstore scanner should not be - // added here - if (!assertDone) { - StoreScanner current = (StoreScanner) (scanner.storeHeap).getCurrentForTesting(); - List scanners = current.getAllScannersForTesting(); - assertEquals("There should be only one scanner the store file scanner", 1, scanners.size()); - assertDone = true; + try (RegionScannerImpl scanner = region.getScanner(scan)) { + // flush the cache. This will reset the store scanner + region.flushcache(true, true, FlushLifeCycleTracker.DUMMY); + + // create one memstore contains many rows will be skipped + // to check MemStoreScanner.seekToPreviousRow + for (int i = 10000; i < 20000; i++) { + Put p = new Put(Bytes.toBytes("" + i)); + p.addColumn(cf1, col, Bytes.toBytes("" + i)); + region.put(p); } - } while (hasNext); - assertEquals(2, currRow.size()); - assertEquals("19996", Bytes.toString(currRow.get(0).getRowArray(), - currRow.get(0).getRowOffset(), currRow.get(0).getRowLength())); - assertEquals("19995", Bytes.toString(currRow.get(1).getRowArray(), - currRow.get(1).getRowOffset(), currRow.get(1).getRowLength())); + List currRow = new ArrayList<>(); + boolean hasNext; + boolean assertDone = false; + do { + hasNext = scanner.next(currRow); + // With HBASE-15871, after the scanner is reset the memstore scanner should not be + // added here + if (!assertDone) { + StoreScanner current = (StoreScanner) (scanner.storeHeap).getCurrentForTesting(); + List scanners = current.getAllScannersForTesting(); + assertEquals("There should be only one scanner the store file scanner", 1, + scanners.size()); + assertDone = true; + } + } while (hasNext); + assertEquals(2, currRow.size()); + assertEquals("19996", Bytes.toString(currRow.get(0).getRowArray(), + currRow.get(0).getRowOffset(), currRow.get(0).getRowLength())); + assertEquals("19995", Bytes.toString(currRow.get(1).getRowArray(), + currRow.get(1).getRowOffset(), currRow.get(1).getRowLength())); + } } @Test @@ -6507,25 +6505,25 @@ public void testReverseScanWhenPutCellsAfterOpenReverseScan() throws Exception { // Create a reverse scan Scan scan = new Scan(Bytes.toBytes("199996")); scan.setReversed(true); - RegionScannerImpl scanner = region.getScanner(scan); + try (RegionScannerImpl scanner = region.getScanner(scan)) { + // Put a lot of cells that have sequenceIDs grater than the readPt of the reverse scan + for (int i = 100000; i < 200000; i++) { + Put p = new Put(Bytes.toBytes("" + i)); + p.addColumn(cf1, col, Bytes.toBytes("" + i)); + region.put(p); + } + List currRow = new ArrayList<>(); + boolean hasNext; + do { + hasNext = scanner.next(currRow); + } while (hasNext); - // Put a lot of cells that have sequenceIDs grater than the readPt of the reverse scan - for (int i = 100000; i < 200000; i++) { - Put p = new Put(Bytes.toBytes("" + i)); - p.addColumn(cf1, col, Bytes.toBytes("" + i)); - region.put(p); + assertEquals(2, currRow.size()); + assertEquals("199996", Bytes.toString(currRow.get(0).getRowArray(), + currRow.get(0).getRowOffset(), currRow.get(0).getRowLength())); + assertEquals("199995", Bytes.toString(currRow.get(1).getRowArray(), + currRow.get(1).getRowOffset(), currRow.get(1).getRowLength())); } - List currRow = new ArrayList<>(); - boolean hasNext; - do { - hasNext = scanner.next(currRow); - } while (hasNext); - - assertEquals(2, currRow.size()); - assertEquals("199996", Bytes.toString(currRow.get(0).getRowArray(), - currRow.get(0).getRowOffset(), currRow.get(0).getRowLength())); - assertEquals("199995", Bytes.toString(currRow.get(1).getRowArray(), - currRow.get(1).getRowOffset(), currRow.get(1).getRowLength())); } @Test @@ -7531,7 +7529,7 @@ public void run() { holder.start(); latch.await(); - region.close(); + HBaseTestingUtility.closeRegionAndWAL(region); region = null; holder.join();