@Test
  public void test() throws Exception {
    // set flush size to 10MB
    CONF.setInt("hbase.hregion.memstore.flush.size", 1024 * 1024 * 10);
    // set the report interval to a very small value
    CONF.setInt("hbase.hstore.report.interval.edits", 1);
    CONF.setInt("hbase.hstore.report.period", 0);
    // mock a RegionServerServices
    final RegionServerAccounting rsAccounting = new RegionServerAccounting(CONF);
    RegionServerServices rs = Mockito.mock(RegionServerServices.class);
    ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null,
      MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT);
    Mockito.when(rs.getRegionServerAccounting()).thenReturn(rsAccounting);
    Mockito.when(rs.isAborted()).thenReturn(false);
    Mockito.when(rs.getNonceManager()).thenReturn(null);
    Mockito.when(rs.getServerName()).thenReturn(ServerName.valueOf("test", 0, 111));
    Mockito.when(rs.getConfiguration()).thenReturn(CONF);
    // create a region
    TableName testTable = TableName.valueOf("testRecoveredEidtsReplayAndAbort");
    TableDescriptor htd = TableDescriptorBuilder.newBuilder(testTable)
      .setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(fam1).build()).build();
    RegionInfo info = RegionInfoBuilder.newBuilder(htd.getTableName()).build();
    Path logDir = TEST_UTIL.getDataTestDirOnTestFS("TestRecoveredEidtsReplayAndAbort.log");
    final WAL wal = HBaseTestingUtil.createWal(CONF, logDir, info);
    Path rootDir = TEST_UTIL.getDataTestDir();
    Path tableDir = CommonFSUtils.getTableDir(rootDir, info.getTable());
    HRegionFileSystem.createRegionOnFileSystem(CONF, TEST_UTIL.getTestFileSystem(), tableDir, info);
    region = HRegion.newHRegion(tableDir, wal, TEST_UTIL.getTestFileSystem(), CONF, info, htd, rs);
    // create some recovered.edits
    final WALFactory wals = new WALFactory(CONF, method);
    try {
      Path regiondir = region.getRegionFileSystem().getRegionDir();
      FileSystem fs = region.getRegionFileSystem().getFileSystem();
      byte[] regionName = region.getRegionInfo().getEncodedNameAsBytes();

      Path recoveredEditsDir = WALSplitUtil.getRegionDirRecoveredEditsDir(regiondir);
      long maxSeqId = 1200;
      long minSeqId = 1000;
      long totalEdits = maxSeqId - minSeqId;
      for (long i = minSeqId; i <= maxSeqId; i += 100) {
        Path recoveredEdits = new Path(recoveredEditsDir, String.format("%019d", i));
        LOG.info("Begin to write recovered.edits : " + recoveredEdits);
        fs.create(recoveredEdits);
        WALProvider.Writer writer = wals.createRecoveredEditsWriter(fs, recoveredEdits);
        for (long j = i; j < i + 100; j++) {
          long time = System.nanoTime();
          WALEdit edit = new WALEdit();
          // 200KB kv
          byte[] value = new byte[200 * 1024];
          Bytes.random(value);
          WALEditInternalHelper.addExtendedCell(edit,
            new KeyValue(row, fam1, Bytes.toBytes(j), time, KeyValue.Type.Put, value));
          writer.append(new WAL.Entry(
            new WALKeyImpl(regionName, tableName, j, time, HConstants.DEFAULT_CLUSTER_ID), edit));
        }
        writer.close();
      }
      TaskMonitor.get().createStatus(method);
      // try to replay the edits
      try {
        region.initialize(new CancelableProgressable() {
          private long replayedEdits = 0;

          @Override
          public boolean progress() {
            replayedEdits++;
            // during replay, rsAccounting should align with global memstore, because
            // there is only one memstore here
            Assert.assertEquals(rsAccounting.getGlobalMemStoreDataSize(),
              region.getMemStoreDataSize());
            Assert.assertEquals(rsAccounting.getGlobalMemStoreHeapSize(),
              region.getMemStoreHeapSize());
            Assert.assertEquals(rsAccounting.getGlobalMemStoreOffHeapSize(),
              region.getMemStoreOffHeapSize());
            // abort the replay before finishing, leaving some edits in the memory
            return replayedEdits < totalEdits - 10;
          }
        });
        Assert.fail("Should not reach here");
      } catch (IOException t) {
        LOG.info("Current memstore: " + region.getMemStoreDataSize() + ", "
          + region.getMemStoreHeapSize() + ", " + region.getMemStoreOffHeapSize());
      }
      // After aborting replay, there should be no data in the memory
      Assert.assertEquals(0, rsAccounting.getGlobalMemStoreDataSize());
      Assert.assertEquals(0, region.getMemStoreDataSize());
      // All the chunk in the MSLAB should be recycled, otherwise, there might be
      // a memory leak.
      Assert.assertEquals(0, ChunkCreator.getInstance().numberOfMappedChunks());
    } finally {
      HBaseTestingUtil.closeRegionAndWAL(this.region);
      this.region = null;
      wals.close();
    }
  }
HBASE-21031 If replay edits fails, we need to make sure memstore is rollbacked And if MSLAB is
used, all chunk is released too.
hbase-server/src/test/java/org/apache/hadoop/hbase/regionserver/TestRecoveredEditsReplayAndAbort.java