001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.regionserver;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.CellUtil;
027import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate;
028import org.apache.hadoop.hbase.KeepDeletedCells;
029import org.apache.hadoop.hbase.client.Delete;
030import org.apache.hadoop.hbase.client.Scan;
031import org.apache.hadoop.hbase.testclassification.LargeTests;
032import org.apache.hadoop.hbase.testclassification.RegionServerTests;
033import org.junit.jupiter.api.Tag;
034import org.junit.jupiter.api.TestTemplate;
035
036@Tag(RegionServerTests.TAG)
037@Tag(LargeTests.TAG)
038@HBaseParameterizedTestTemplate(name = "{index}: compType={0}")
039public class TestMajorCompactionWithDeletes extends MajorCompactionTestBase {
040
041  public TestMajorCompactionWithDeletes(String compType) {
042    super(compType);
043  }
044
045  /**
046   * Test that on a major compaction, if all cells are expired or deleted, then we'll end up with no
047   * product. Make sure scanner over region returns right answer in this case - and that it just
048   * basically works.
049   * @throws IOException exception encountered
050   */
051  @TestTemplate
052  public void testMajorCompactingToNoOutput() throws IOException {
053    testMajorCompactingWithDeletes(KeepDeletedCells.FALSE);
054  }
055
056  /**
057   * Test that on a major compaction,Deleted cells are retained if keep deleted cells is set to true
058   * @throws IOException exception encountered
059   */
060  @TestTemplate
061  public void testMajorCompactingWithKeepDeletedCells() throws IOException {
062    testMajorCompactingWithDeletes(KeepDeletedCells.TRUE);
063  }
064
065  private void testMajorCompactingWithDeletes(KeepDeletedCells keepDeletedCells)
066    throws IOException {
067    createStoreFile(r);
068    for (int i = 0; i < compactionThreshold; i++) {
069      createStoreFile(r);
070    }
071    // Now delete everything.
072    InternalScanner s = r.getScanner(new Scan());
073    int originalCount = 0;
074    do {
075      List<Cell> results = new ArrayList<>();
076      boolean result = s.next(results);
077      r.delete(new Delete(CellUtil.cloneRow(results.get(0))));
078      if (!result) break;
079      originalCount++;
080    } while (true);
081    s.close();
082    // Flush
083    r.flush(true);
084
085    for (HStore store : this.r.stores.values()) {
086      ScanInfo old = store.getScanInfo();
087      ScanInfo si = old.customize(old.getMaxVersions(), old.getTtl(), keepDeletedCells);
088      store.setScanInfo(si);
089    }
090    // Major compact.
091    r.compact(true);
092    s = r.getScanner(new Scan().setRaw(true));
093    int counter = 0;
094    do {
095      List<Cell> results = new ArrayList<>();
096      boolean result = s.next(results);
097      if (!result) break;
098      counter++;
099    } while (true);
100    assertEquals(keepDeletedCells == KeepDeletedCells.TRUE ? originalCount : 0, counter);
101  }
102}