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.Assert.assertEquals;
021import static org.junit.Assert.assertTrue;
022
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.List;
026import java.util.Random;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtil;
029import org.apache.hadoop.hbase.TableName;
030import org.apache.hadoop.hbase.client.Admin;
031import org.apache.hadoop.hbase.client.CompactionState;
032import org.apache.hadoop.hbase.client.Put;
033import org.apache.hadoop.hbase.client.Table;
034import org.apache.hadoop.hbase.master.HMaster;
035import org.apache.hadoop.hbase.testclassification.LargeTests;
036import org.apache.hadoop.hbase.testclassification.VerySlowRegionServerTests;
037import org.apache.hadoop.hbase.util.Bytes;
038import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
039import org.junit.AfterClass;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Rule;
043import org.junit.Test;
044import org.junit.experimental.categories.Category;
045import org.junit.rules.TestName;
046import org.slf4j.Logger;
047import org.slf4j.LoggerFactory;
048
049/** Unit tests to test retrieving table/region compaction state*/
050@Category({VerySlowRegionServerTests.class, LargeTests.class})
051public class TestCompactionState {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055      HBaseClassTestRule.forClass(TestCompactionState.class);
056
057  private static final Logger LOG = LoggerFactory.getLogger(TestCompactionState.class);
058  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
059  private final static Random random = new Random();
060
061  @Rule
062  public TestName name = new TestName();
063
064  @BeforeClass
065  public static void setUpBeforeClass() throws Exception {
066    TEST_UTIL.startMiniCluster();
067  }
068
069  @AfterClass
070  public static void tearDownAfterClass() throws Exception {
071    TEST_UTIL.shutdownMiniCluster();
072  }
073
074  enum StateSource {
075    ADMIN, MASTER
076  }
077
078  @Test
079  public void testMajorCompactionStateFromAdmin() throws IOException, InterruptedException {
080    compaction(name.getMethodName(), 8, CompactionState.MAJOR, false, StateSource.ADMIN);
081  }
082
083  @Test
084  public void testMinorCompactionStateFromAdmin() throws IOException, InterruptedException {
085    compaction(name.getMethodName(), 15, CompactionState.MINOR, false, StateSource.ADMIN);
086  }
087
088  @Test
089  public void testMajorCompactionOnFamilyStateFromAdmin() throws IOException, InterruptedException {
090    compaction(name.getMethodName(), 8, CompactionState.MAJOR, true, StateSource.ADMIN);
091  }
092
093  @Test
094  public void testMinorCompactionOnFamilyStateFromAdmin() throws IOException, InterruptedException {
095    compaction(name.getMethodName(), 15, CompactionState.MINOR, true, StateSource.ADMIN);
096  }
097
098  @Test
099  public void testMajorCompactionStateFromMaster() throws IOException, InterruptedException {
100    compaction(name.getMethodName(), 8, CompactionState.MAJOR, false, StateSource.MASTER);
101  }
102
103  @Test
104  public void testMinorCompactionStateFromMaster() throws IOException, InterruptedException {
105    compaction(name.getMethodName(), 15, CompactionState.MINOR, false, StateSource.MASTER);
106  }
107
108  @Test
109  public void testMajorCompactionOnFamilyStateFromMaster()
110      throws IOException, InterruptedException {
111    compaction(name.getMethodName(), 8, CompactionState.MAJOR, true, StateSource.MASTER);
112  }
113
114  @Test
115  public void testMinorCompactionOnFamilyStateFromMaster()
116      throws IOException, InterruptedException {
117    compaction(name.getMethodName(), 15, CompactionState.MINOR, true, StateSource.MASTER);
118  }
119
120  @Test
121  public void testInvalidColumnFamily() throws IOException, InterruptedException {
122    final TableName tableName = TableName.valueOf(name.getMethodName());
123    byte [] family = Bytes.toBytes("family");
124    byte [] fakecf = Bytes.toBytes("fakecf");
125    boolean caughtMinorCompact = false;
126    boolean caughtMajorCompact = false;
127    Table ht = null;
128    try {
129      ht = TEST_UTIL.createTable(tableName, family);
130      Admin admin = TEST_UTIL.getAdmin();
131      try {
132        admin.compact(tableName, fakecf);
133      } catch (IOException ioe) {
134        caughtMinorCompact = true;
135      }
136      try {
137        admin.majorCompact(tableName, fakecf);
138      } catch (IOException ioe) {
139        caughtMajorCompact = true;
140      }
141    } finally {
142      if (ht != null) {
143        TEST_UTIL.deleteTable(tableName);
144      }
145      assertTrue(caughtMinorCompact);
146      assertTrue(caughtMajorCompact);
147    }
148  }
149
150  /**
151   * Load data to a table, flush it to disk, trigger compaction,
152   * confirm the compaction state is right and wait till it is done.
153   *
154   * @param tableName
155   * @param flushes
156   * @param expectedState
157   * @param singleFamily otherwise, run compaction on all cfs
158   * @param stateSource get the state by Admin or Master
159   * @throws IOException
160   * @throws InterruptedException
161   */
162  private void compaction(final String tableName, final int flushes,
163      final CompactionState expectedState, boolean singleFamily, StateSource stateSource)
164      throws IOException, InterruptedException {
165    // Create a table with regions
166    TableName table = TableName.valueOf(tableName);
167    byte [] family = Bytes.toBytes("family");
168    byte [][] families =
169      {family, Bytes.add(family, Bytes.toBytes("2")), Bytes.add(family, Bytes.toBytes("3"))};
170    Table ht = null;
171    try {
172      ht = TEST_UTIL.createTable(table, families);
173      loadData(ht, families, 3000, flushes);
174      HRegionServer rs = TEST_UTIL.getMiniHBaseCluster().getRegionServer(0);
175      HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
176      List<HRegion> regions = rs.getRegions(table);
177      int countBefore = countStoreFilesInFamilies(regions, families);
178      int countBeforeSingleFamily = countStoreFilesInFamily(regions, family);
179      assertTrue(countBefore > 0); // there should be some data files
180      Admin admin = TEST_UTIL.getAdmin();
181      if (expectedState == CompactionState.MINOR) {
182        if (singleFamily) {
183          admin.compact(table, family);
184        } else {
185          admin.compact(table);
186        }
187      } else {
188        if (singleFamily) {
189          admin.majorCompact(table, family);
190        } else {
191          admin.majorCompact(table);
192        }
193      }
194      long curt = EnvironmentEdgeManager.currentTime();
195      long waitTime = 5000;
196      long endt = curt + waitTime;
197      CompactionState state = getCompactionState(stateSource, master, admin, table);
198      while (state == CompactionState.NONE && curt < endt) {
199        Thread.sleep(10);
200        state = getCompactionState(stateSource, master, admin, table);
201        curt = EnvironmentEdgeManager.currentTime();
202      }
203      // Now, should have the right compaction state,
204      // otherwise, the compaction should have already been done
205      if (expectedState != state) {
206        for (Region region: regions) {
207          state = CompactionState.valueOf(region.getCompactionState().toString());
208          assertEquals(CompactionState.NONE, state);
209        }
210      } else {
211        // Wait until the compaction is done
212        state = getCompactionState(stateSource, master, admin, table);
213        while (state != CompactionState.NONE && curt < endt) {
214          Thread.sleep(10);
215          state = getCompactionState(stateSource, master, admin, table);
216        }
217        // Now, compaction should be done.
218        assertEquals(CompactionState.NONE, state);
219      }
220      int countAfter = countStoreFilesInFamilies(regions, families);
221      int countAfterSingleFamily = countStoreFilesInFamily(regions, family);
222      assertTrue(countAfter < countBefore);
223      if (!singleFamily) {
224        if (expectedState == CompactionState.MAJOR) assertTrue(families.length == countAfter);
225        else assertTrue(families.length < countAfter);
226      } else {
227        int singleFamDiff = countBeforeSingleFamily - countAfterSingleFamily;
228        // assert only change was to single column family
229        assertTrue(singleFamDiff == (countBefore - countAfter));
230        if (expectedState == CompactionState.MAJOR) {
231          assertTrue(1 == countAfterSingleFamily);
232        } else {
233          assertTrue(1 < countAfterSingleFamily);
234        }
235      }
236    } finally {
237      if (ht != null) {
238        TEST_UTIL.deleteTable(table);
239      }
240    }
241  }
242
243  private static CompactionState getCompactionState(StateSource stateSource, HMaster master,
244      Admin admin, TableName table) throws IOException {
245    CompactionState state = stateSource == StateSource.ADMIN ?
246      admin.getCompactionState(table) :
247      master.getCompactionState(table);
248    return state;
249  }
250
251  private static int countStoreFilesInFamily(
252      List<HRegion> regions, final byte[] family) {
253    return countStoreFilesInFamilies(regions, new byte[][]{family});
254  }
255
256  private static int countStoreFilesInFamilies(List<HRegion> regions, final byte[][] families) {
257    int count = 0;
258    for (HRegion region: regions) {
259      count += region.getStoreFileList(families).size();
260    }
261    return count;
262  }
263
264  private static void loadData(final Table ht, final byte[][] families,
265      final int rows, final int flushes) throws IOException {
266    List<Put> puts = new ArrayList<>(rows);
267    byte[] qualifier = Bytes.toBytes("val");
268    for (int i = 0; i < flushes; i++) {
269      for (int k = 0; k < rows; k++) {
270        byte[] row = Bytes.toBytes(random.nextLong());
271        Put p = new Put(row);
272        for (int j = 0; j < families.length; ++j) {
273          p.addColumn(families[j], qualifier, row);
274        }
275        puts.add(p);
276      }
277      ht.put(puts);
278      TEST_UTIL.flush();
279      puts.clear();
280    }
281  }
282}