joshelser commented on a change in pull request #1230: Backport HBASE-23553 to 
branch-2.1
URL: https://github.com/apache/hbase/pull/1230#discussion_r386504683
 
 

 ##########
 File path: 
hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestTableSnapshotScanner.java
 ##########
 @@ -303,4 +312,131 @@ private static void verifyRow(Result result) throws 
IOException {
     }
   }
 
+  @Test
+  public void testMergeRegion() throws Exception {
+    setupCluster();
+    TableName tableName = TableName.valueOf("testMergeRegion");
+    String snapshotName = tableName.getNameAsString() + "_snapshot";
+    Configuration conf = UTIL.getConfiguration();
+    Path rootDir = 
UTIL.getHBaseCluster().getMaster().getMasterFileSystem().getRootDir();
+    long timeout = 20000; // 20s
+    try (Admin admin = UTIL.getAdmin()) {
+      List<String> serverList = admin.getRegionServers().stream().map(sn -> 
sn.getServerName())
+          .collect(Collectors.toList());
+      // create table with 3 regions
+      Table table = UTIL.createTable(tableName, FAMILIES, 1, bbb, yyy, 3);
+      List<RegionInfo> regions = admin.getRegions(tableName);
+      Assert.assertEquals(3, regions.size());
+      RegionInfo region0 = regions.get(0);
+      RegionInfo region1 = regions.get(1);
+      RegionInfo region2 = regions.get(2);
+      // put some data in the table
+      UTIL.loadTable(table, FAMILIES);
+      admin.flush(tableName);
+      // wait flush is finished
+      UTIL.waitFor(timeout, () -> {
+        try {
+          Path tableDir = FSUtils.getTableDir(rootDir, tableName);
+          for (RegionInfo region : regions) {
+            Path regionDir = new Path(tableDir, region.getEncodedName());
+            for (Path familyDir : FSUtils.getFamilyDirs(fs, regionDir)) {
+              if (fs.listStatus(familyDir).length != 1) {
+                return false;
+              }
+            }
+          }
+          return true;
+        } catch (IOException e) {
+          LOG.warn("Failed check if flush is finished", e);
+          return false;
+        }
+      });
+      // merge 2 regions
+      admin.mergeRegionsAsync(region0.getEncodedNameAsBytes(), 
region1.getEncodedNameAsBytes(),
+        true);
+      UTIL.waitFor(timeout, () -> admin.getRegions(tableName).size() == 2);
+      List<RegionInfo> mergedRegions = admin.getRegions(tableName);
+      RegionInfo mergedRegion =
+          
mergedRegions.get(0).getEncodedName().equals(region2.getEncodedName())
+              ? mergedRegions.get(1)
+              : mergedRegions.get(0);
+      // snapshot
+      admin.snapshot(snapshotName, tableName);
+      Assert.assertEquals(1, admin.listSnapshots().size());
+      // major compact
+      admin.majorCompactRegion(mergedRegion.getRegionName());
+      // wait until merged region has no reference
+      UTIL.waitFor(timeout, () -> {
+        try {
+          for (RegionServerThread regionServerThread : 
UTIL.getMiniHBaseCluster()
+              .getRegionServerThreads()) {
+            HRegionServer regionServer = regionServerThread.getRegionServer();
+            for (HRegion subRegion : regionServer.getRegions(tableName)) {
+              if (subRegion.getRegionInfo().getEncodedName()
+                  .equals(mergedRegion.getEncodedName())) {
+                regionServer.getCompactedHFilesDischarger().chore();
+              }
+            }
+          }
+          Path tableDir = FSUtils.getTableDir(rootDir, tableName);
+          HRegionFileSystem regionFs = HRegionFileSystem
+              .openRegionFromFileSystem(UTIL.getConfiguration(), fs, tableDir, 
mergedRegion, true);
+          return !regionFs.hasReferences(admin.getDescriptor(tableName));
+        } catch (IOException e) {
+          LOG.warn("Failed check merged region has no reference", e);
+          return false;
+        }
+      });
+      // run catalog janitor to clean and wait for parent regions are archived
+      
UTIL.getMiniHBaseCluster().getMaster().getCatalogJanitor().choreForTesting();
+      UTIL.waitFor(timeout, () -> {
+        try {
+          Path tableDir = FSUtils.getTableDir(rootDir, tableName);
+          for (FileStatus fileStatus : fs.listStatus(tableDir)) {
+            String name = fileStatus.getPath().getName();
+            if (name.equals(region0.getEncodedName()) || 
name.equals(region1.getEncodedName())) {
+              return false;
+            }
+          }
+          return true;
+        } catch (IOException e) {
+          LOG.warn("Check if parent regions are archived error", e);
+          return false;
+        }
+      });
+      // set file modify time and then run cleaner
+      long time = System.currentTimeMillis() - 
TimeToLiveHFileCleaner.DEFAULT_TTL * 1000;
+      traverseAndSetFileTime(HFileArchiveUtil.getArchivePath(conf), time);
+      UTIL.getMiniHBaseCluster().getMaster().getHFileCleaner().runCleaner();
+      // scan snapshot
+      try (TableSnapshotScanner scanner = new TableSnapshotScanner(conf,
+          UTIL.getDataTestDirOnTestFS(snapshotName), snapshotName, new 
Scan(bbb, yyy))) {
+        verifyScanner(scanner, bbb, yyy);
+      }
+    } catch (Exception e) {
+      LOG.error("scan snapshot error", e);
+      Assert.fail("Should not throw FileNotFoundException");
 
 Review comment:
   Don't you really want to make sure just the `TableSnapshotScanner` doesnt' 
throw a `FileNotFoundException`? Like, maybe lift this catch up into that try 
block specifically?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to