@Test
  public void testRemoveCompactedFilesWithException() throws Exception {
    byte[] fam = Bytes.toBytes("f");
    byte[] col = Bytes.toBytes("c");
    byte[] val = Bytes.toBytes("val");

    TableName tableName = TableName.valueOf(name.getMethodName());
    TableDescriptor htd = TableDescriptorBuilder.newBuilder(tableName)
      .setColumnFamily(ColumnFamilyDescriptorBuilder.of(fam)).build();
    RegionInfo info = RegionInfoBuilder.newBuilder(tableName).build();
    HRegion region = initHRegion(htd, info);
    RegionServerServices rss = mock(RegionServerServices.class);
    List<HRegion> regions = new ArrayList<>();
    regions.add(region);
    Mockito.doReturn(regions).when(rss).getRegions();

    // Create the cleaner object
    final CompactedHFilesDischarger cleaner =
      new CompactedHFilesDischarger(1000, (Stoppable) null, rss, false);
    // Add some data to the region and do some flushes
    int batchSize = 10;
    int fileCount = 10;
    for (int f = 0; f < fileCount; f++) {
      int start = f * batchSize;
      for (int i = start; i < start + batchSize; i++) {
        Put p = new Put(Bytes.toBytes("row" + i));
        p.addColumn(fam, col, val);
        region.put(p);
      }
      // flush them
      region.flush(true);
    }

    HStore store = region.getStore(fam);
    assertEquals(fileCount, store.getStorefilesCount());

    Collection<HStoreFile> storefiles = store.getStorefiles();
    // None of the files should be in compacted state.
    for (HStoreFile file : storefiles) {
      assertFalse(file.isCompactedAway());
    }

    StoreFileManager fileManager = store.getStoreEngine().getStoreFileManager();
    Collection<HStoreFile> initialCompactedFiles = fileManager.getCompactedfiles();
    assertTrue(initialCompactedFiles == null || initialCompactedFiles.isEmpty());

    // Do compaction
    region.compact(true);

    // all prior store files should now be compacted
    Collection<HStoreFile> compactedFilesPreClean = fileManager.getCompactedfiles();
    assertNotNull(compactedFilesPreClean);
    assertTrue(compactedFilesPreClean.size() > 0);

    // add the dummy file to the store directory
    HRegionFileSystem regionFS = region.getRegionFileSystem();
    Path errFile = regionFS.getStoreFilePath(Bytes.toString(fam), ERROR_FILE);
    FSDataOutputStream out = regionFS.getFileSystem().create(errFile);
    out.writeInt(1);
    out.close();

    StoreFileTrackerForTest storeFileTrackerForTest =
      new StoreFileTrackerForTest(store.getReadOnlyConfiguration(), true, store.getStoreContext());
    HStoreFile errStoreFile =
      new MockHStoreFile(testUtil, errFile, 1, 0, false, 1, storeFileTrackerForTest);
    fileManager.addCompactionResults(ImmutableList.of(errStoreFile), ImmutableList.of());

    // cleanup compacted files
    cleaner.chore();

    // make sure the compacted files are cleared
    Collection<HStoreFile> compactedFilesPostClean = fileManager.getCompactedfiles();
    assertEquals(1, compactedFilesPostClean.size());
    for (HStoreFile origFile : compactedFilesPreClean) {
      assertFalse(compactedFilesPostClean.contains(origFile));
    }

    // close the region
    try {
      region.close();
    } catch (FailedArchiveException e) {
      // expected due to errorfile
      assertEquals(1, e.getFailedFiles().size());
      assertEquals(ERROR_FILE, e.getFailedFiles().iterator().next().getName());
    }
  }
Tests that archiving compacted files behaves correctly when encountering exceptions.
hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestCompactionArchiveIOException.java