1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
64 Thread.sleep(10 * 1000);
65
66 int numberOfRegions = utility.getHBaseAdmin().getTableRegions(tableName).size();
67 int numHFiles = utility.getNumHFiles(tableName, FAMILY);
68
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
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
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
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 }