View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.util.compaction;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.IOException;
24  
25  import org.apache.hadoop.hbase.HBaseTestingUtility;
26  import org.apache.hadoop.hbase.HColumnDescriptor;
27  import org.apache.hadoop.hbase.HTableDescriptor;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.client.Connection;
30  import org.apache.hadoop.hbase.client.Table;
31  import org.apache.hadoop.hbase.testclassification.MediumTests;
32  import org.apache.hadoop.hbase.testclassification.MiscTests;
33  import org.junit.After;
34  import org.junit.Before;
35  import org.junit.Rule;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  import org.junit.rules.TestName;
39  
40  @Category({ MiscTests.class, MediumTests.class })
41  public class TestMajorCompactorTTL extends MajorCompactorTest {
42  
43    @Rule
44    public TestName name = new TestName();
45  
46    @Before
47    public void setUp() throws Exception {
48      utility = new HBaseTestingUtility();
49      utility.getConfiguration().setInt("hbase.hfile.compaction.discharger.interval", 10);
50      utility.startMiniCluster();
51      admin = utility.getHBaseAdmin();
52    }
53  
54    @After
55    public void tearDown() throws Exception {
56      utility.shutdownMiniCluster();
57    }
58  
59    @Test
60    public void testCompactingATable() throws Exception {
61      TableName tableName = createTable(name.getMethodName());
62  
63      // Delay a bit, so we can set the table TTL to 5 seconds
64      Thread.sleep(10 * 1000);
65  
66      int numberOfRegions = utility.getHBaseAdmin().getTableRegions(tableName).size();
67      int numHFiles = utility.getNumHFiles(tableName, FAMILY);
68      // we should have a table with more store files than we would before we major compacted.
69      assertTrue(numberOfRegions < numHFiles);
70      modifyTTL(tableName);
71  
72      MajorCompactorTTL compactor = new MajorCompactorTTL(utility.getConfiguration(),
73          admin.getTableDescriptor(tableName), 1, 200);
74      compactor.initializeWorkQueues();
75      compactor.compactAllRegions();
76      compactor.shutdown();
77  
78      // verify that the store has been completely major compacted.
79      numberOfRegions = utility.getHBaseAdmin().getTableRegions(tableName).size();
80      numHFiles = utility.getNumHFiles(tableName, FAMILY);
81      assertEquals(numberOfRegions, numHFiles);
82    }
83  
84    protected void modifyTTL(TableName tableName) throws IOException, InterruptedException {
85      // Set the TTL to 5 secs, so all the files just written above will get cleaned up on compact.
86      admin.disableTable(tableName);
87      utility.waitTableDisabled(tableName.getName());
88      HTableDescriptor descriptor = admin.getTableDescriptor(tableName);
89      HColumnDescriptor colDesc = descriptor.getFamily(FAMILY);
90      colDesc.setTimeToLive(5);
91      admin.modifyColumn(tableName, colDesc);
92      admin.enableTable(tableName);
93      utility.waitTableEnabled(tableName);
94    }
95  
96    protected TableName createTable(String name) throws IOException, InterruptedException {
97      TableName tableName = TableName.valueOf(name);
98      utility.createMultiRegionTable(tableName, FAMILY, 5);
99      utility.waitTableAvailable(tableName);
100     Connection connection = utility.getConnection();
101     Table table = connection.getTable(tableName);
102     // write data and flush multiple store files:
103     for (int i = 0; i < 5; i++) {
104       loadRandomRows(table, FAMILY, 50, 100);
105       utility.flush(tableName);
106     }
107     table.close();
108     return tableName;
109   }
110 }