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.util.List;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.hbase.HBaseTestingUtil;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.Waiter;
027import org.apache.hadoop.hbase.client.Admin;
028import org.apache.hadoop.hbase.client.CompactionState;
029import org.apache.hadoop.hbase.client.Put;
030import org.apache.hadoop.hbase.client.Table;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
034import org.junit.jupiter.api.AfterAll;
035import org.junit.jupiter.api.AfterEach;
036import org.junit.jupiter.api.BeforeAll;
037import org.junit.jupiter.api.Tag;
038import org.junit.jupiter.api.Test;
039
040/**
041 * This class tests the scenario where a store refresh happens due to a file not found during scan,
042 * after a compaction but before the compacted files are archived. At this state we test for a split
043 * and compaction
044 */
045@Tag(MediumTests.TAG)
046public class TestCompactionFileNotFound {
047
048  private static final HBaseTestingUtil util = new HBaseTestingUtil();
049
050  private static final TableName TEST_TABLE = TableName.valueOf("test");
051  private static final byte[] TEST_FAMILY = Bytes.toBytes("f1");
052
053  private static final byte[] ROW_A = Bytes.toBytes("aaa");
054  private static final byte[] ROW_B = Bytes.toBytes("bbb");
055  private static final byte[] ROW_C = Bytes.toBytes("ccc");
056
057  private static final byte[] qualifierCol1 = Bytes.toBytes("col1");
058
059  private static final byte[] bytes1 = Bytes.toBytes(1);
060  private static final byte[] bytes2 = Bytes.toBytes(2);
061  private static final byte[] bytes3 = Bytes.toBytes(3);
062
063  private Table table;
064
065  @BeforeAll
066  public static void setupBeforeClass() throws Exception {
067    Configuration conf = util.getConfiguration();
068    conf.setInt("hbase.hfile.compaction.discharger.interval", Integer.MAX_VALUE);
069    util.startMiniCluster(3);
070  }
071
072  @AfterAll
073  public static void tearDownAfterClass() throws Exception {
074    util.shutdownMiniCluster();
075  }
076
077  @AfterEach
078  public void after() throws Exception {
079    try {
080      if (table != null) {
081        table.close();
082      }
083    } finally {
084      util.deleteTable(TEST_TABLE);
085    }
086  }
087
088  @Test
089  public void testSplitAfterRefresh() throws Exception {
090    Admin admin = util.getAdmin();
091    table = util.createTable(TEST_TABLE, TEST_FAMILY);
092
093    try {
094      // Create Multiple store files
095      Put puta = new Put(ROW_A);
096      puta.addColumn(TEST_FAMILY, qualifierCol1, bytes1);
097      table.put(puta);
098      admin.flush(TEST_TABLE);
099
100      Put putb = new Put(ROW_B);
101      putb.addColumn(TEST_FAMILY, qualifierCol1, bytes2);
102      table.put(putb);
103      admin.flush(TEST_TABLE);
104
105      Put putc = new Put(ROW_C);
106      putc.addColumn(TEST_FAMILY, qualifierCol1, bytes3);
107      table.put(putc);
108      admin.flush(TEST_TABLE);
109
110      admin.compact(TEST_TABLE);
111      while (admin.getCompactionState(TEST_TABLE) != CompactionState.NONE) {
112        Thread.sleep(1000);
113      }
114      table.put(putb);
115      HRegion hr1 = (HRegion) util.getRSForFirstRegionInTable(TEST_TABLE)
116        .getRegionByEncodedName(admin.getRegions(TEST_TABLE).get(0).getEncodedName());
117      // Refresh store files post compaction, this should not open already compacted files
118      hr1.refreshStoreFiles(true);
119      int numRegionsBeforeSplit = admin.getRegions(TEST_TABLE).size();
120      // Check if we can successfully split after compaction
121      admin.splitRegionAsync(admin.getRegions(TEST_TABLE).get(0).getEncodedNameAsBytes(), ROW_C)
122        .get();
123      util.waitFor(20000, new Waiter.Predicate<Exception>() {
124        @Override
125        public boolean evaluate() throws Exception {
126          int numRegionsAfterSplit = 0;
127          List<RegionServerThread> rst = util.getMiniHBaseCluster().getLiveRegionServerThreads();
128          for (RegionServerThread t : rst) {
129            numRegionsAfterSplit += t.getRegionServer().getRegions(TEST_TABLE).size();
130          }
131          // Make sure that the split went through and all the regions are assigned
132          return (numRegionsAfterSplit == numRegionsBeforeSplit + 1
133            && admin.isTableAvailable(TEST_TABLE));
134        }
135      });
136      // Split at this point should not result in the RS being aborted
137      assertEquals(3, util.getMiniHBaseCluster().getLiveRegionServerThreads().size());
138    } finally {
139      if (admin != null) {
140        admin.close();
141      }
142    }
143  }
144
145  @Test
146  public void testCompactionAfterRefresh() throws Exception {
147    Admin admin = util.getAdmin();
148    table = util.createTable(TEST_TABLE, TEST_FAMILY);
149    try {
150      // Create Multiple store files
151      Put puta = new Put(ROW_A);
152      puta.addColumn(TEST_FAMILY, qualifierCol1, bytes1);
153      table.put(puta);
154      admin.flush(TEST_TABLE);
155
156      Put putb = new Put(ROW_B);
157      putb.addColumn(TEST_FAMILY, qualifierCol1, bytes2);
158      table.put(putb);
159      admin.flush(TEST_TABLE);
160
161      Put putc = new Put(ROW_C);
162      putc.addColumn(TEST_FAMILY, qualifierCol1, bytes3);
163      table.put(putc);
164      admin.flush(TEST_TABLE);
165
166      admin.compact(TEST_TABLE);
167      while (admin.getCompactionState(TEST_TABLE) != CompactionState.NONE) {
168        Thread.sleep(1000);
169      }
170      table.put(putb);
171      HRegion hr1 = (HRegion) util.getRSForFirstRegionInTable(TEST_TABLE)
172        .getRegionByEncodedName(admin.getRegions(TEST_TABLE).get(0).getEncodedName());
173      // Refresh store files post compaction, this should not open already compacted files
174      hr1.refreshStoreFiles(true);
175      // Archive the store files and try another compaction to see if all is good
176      for (HStore store : hr1.getStores()) {
177        store.closeAndArchiveCompactedFiles();
178      }
179      hr1.compact(false);
180    } finally {
181      if (admin != null) {
182        admin.close();
183      }
184    }
185  }
186}