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;
021import static org.junit.jupiter.api.Assertions.assertTrue;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.CellUtil;
028import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate;
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 TestMajorCompaction extends MajorCompactionTestBase {
040
041  public TestMajorCompaction(String compType) {
042    super(compType);
043  }
044
045  /**
046   * Run compaction and flushing memstore Assert deletes get cleaned up.
047   */
048  @TestTemplate
049  public void testMajorCompaction() throws Exception {
050    majorCompaction();
051  }
052
053  /**
054   * Test that on a major compaction, if all cells are expired or deleted, then we'll end up with no
055   * product. Make sure scanner over region returns right answer in this case - and that it just
056   * basically works.
057   */
058  @TestTemplate
059  public void testMajorCompactingToNoOutputWithReverseScan() throws IOException {
060    createStoreFile(r);
061    for (int i = 0; i < compactionThreshold; i++) {
062      createStoreFile(r);
063    }
064    // Now delete everything.
065    Scan scan = new Scan();
066    scan.setReversed(true);
067    try (InternalScanner s = r.getScanner(scan)) {
068      do {
069        List<Cell> results = new ArrayList<>();
070        boolean result = s.next(results);
071        assertTrue(!results.isEmpty());
072        r.delete(new Delete(CellUtil.cloneRow(results.get(0))));
073        if (!result) {
074          break;
075        }
076      } while (true);
077    }
078    // Flush
079    r.flush(true);
080    // Major compact.
081    r.compact(true);
082    scan = new Scan();
083    scan.setReversed(true);
084    int counter = 0;
085    try (InternalScanner s = r.getScanner(scan)) {
086      do {
087        List<Cell> results = new ArrayList<>();
088        boolean result = s.next(results);
089        if (!result) {
090          break;
091        }
092        counter++;
093      } while (true);
094    }
095    assertEquals(0, counter);
096  }
097}