1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.regionserver;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
26 import org.apache.hadoop.hbase.HBaseTestingUtility;
27 import org.apache.hadoop.hbase.HColumnDescriptor;
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.HTableDescriptor;
30 import org.apache.hadoop.hbase.TableName;
31 import org.apache.hadoop.hbase.client.Admin;
32 import org.apache.hadoop.hbase.client.Put;
33 import org.apache.hadoop.hbase.client.Scan;
34 import org.apache.hadoop.hbase.client.Table;
35 import org.apache.hadoop.hbase.master.cleaner.TimeToLiveHFileCleaner;
36 import org.apache.hadoop.hbase.regionserver.compactions.CompactionConfiguration;
37 import org.apache.hadoop.hbase.testclassification.LargeTests;
38 import org.apache.hadoop.hbase.util.Bytes;
39 import org.apache.hadoop.hbase.util.JVMClusterUtil;
40 import org.junit.After;
41 import org.junit.AfterClass;
42 import org.junit.Before;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 @Category({LargeTests.class})
50 public class TestCleanupCompactedFileAfterFailover {
51
52 private static final Logger LOG =
53 LoggerFactory.getLogger(TestCleanupCompactedFileAfterFailover.class);
54
55 private static HBaseTestingUtility TEST_UTIL;
56 private static Admin admin;
57 private static Table table;
58
59 private static TableName TABLE_NAME = TableName.valueOf("TestCleanupCompactedFileAfterFailover");
60 private static byte[] ROW = Bytes.toBytes("row");
61 private static byte[] FAMILY = Bytes.toBytes("cf");
62 private static byte[] QUALIFIER = Bytes.toBytes("cq");
63 private static byte[] VALUE = Bytes.toBytes("value");
64 private static final int RS_NUMBER = 5;
65
66 @BeforeClass
67 public static void beforeClass() throws Exception {
68 TEST_UTIL = new HBaseTestingUtility();
69
70 TEST_UTIL.getConfiguration().setInt(HConstants.HBASE_CLIENT_SCANNER_TIMEOUT_PERIOD, 1200000);
71 TEST_UTIL.getConfiguration()
72 .setInt(CompactionConfiguration.HBASE_HSTORE_COMPACTION_MIN_KEY, 100);
73 TEST_UTIL.getConfiguration().set("dfs.blocksize", "64000");
74 TEST_UTIL.getConfiguration().set("dfs.namenode.fs-limits.min-block-size", "1024");
75 TEST_UTIL.getConfiguration().set(TimeToLiveHFileCleaner.TTL_CONF_KEY, "0");
76 TEST_UTIL.startMiniCluster(RS_NUMBER);
77 admin = TEST_UTIL.getHBaseAdmin();
78 }
79
80 @AfterClass
81 public static void afterClass() throws Exception {
82 TEST_UTIL.shutdownMiniCluster();
83 }
84
85 @Before
86 public void before() throws Exception {
87 HTableDescriptor htd = new HTableDescriptor(TABLE_NAME);
88 htd.addFamily(new HColumnDescriptor(FAMILY));
89 admin.createTable(htd);
90 TEST_UTIL.waitTableAvailable(TABLE_NAME, 30000);
91 table = TEST_UTIL.getConnection().getTable(TABLE_NAME);
92 }
93
94 @After
95 public void after() throws Exception {
96 admin.disableTable(TABLE_NAME);
97 admin.deleteTable(TABLE_NAME);
98 }
99
100 @Test
101 public void testCleanupAfterFailoverWithCompactOnce() throws Exception {
102 testCleanupAfterFailover(1);
103 }
104
105 @Test
106 public void testCleanupAfterFailoverWithCompactTwice() throws Exception {
107 testCleanupAfterFailover(2);
108 }
109
110 @Test
111 public void testCleanupAfterFailoverWithCompactThreeTimes() throws Exception {
112 testCleanupAfterFailover(3);
113 }
114
115 private void testCleanupAfterFailover(int compactNum) throws Exception {
116 HRegionServer rsServedTable = null;
117 List<Region> regions = new ArrayList<>();
118 for (JVMClusterUtil.RegionServerThread rsThread : TEST_UTIL.getHBaseCluster()
119 .getLiveRegionServerThreads()) {
120 HRegionServer rs = rsThread.getRegionServer();
121 if (rs.getOnlineTables().contains(TABLE_NAME)) {
122 regions.addAll(rs.getOnlineRegions(TABLE_NAME));
123 rsServedTable = rs;
124 }
125 }
126 assertNotNull(rsServedTable);
127 assertEquals("Table should only have one region", 1, regions.size());
128 HRegion region = (HRegion)regions.get(0);
129 HStore store = (HStore)region.getStore(FAMILY);
130
131 writeDataAndFlush(3, region);
132 assertEquals(3, store.getStorefilesCount());
133
134
135 store.getScanner(new Scan(), null, Long.MAX_VALUE);
136
137 region.compact(true);
138 assertEquals(1, store.getStorefilesCount());
139
140 assertEquals(3, store.getStoreEngine().getStoreFileManager().getCompactedfiles().size());
141
142 for (int i = 1; i < compactNum; i++) {
143
144 region.compact(true);
145 assertEquals(1, store.getStorefilesCount());
146 store.closeAndArchiveCompactedFiles();
147
148 assertEquals(3, store.getStoreEngine().getStoreFileManager().getCompactedfiles().size());
149 }
150
151 int walNum = rsServedTable.getWALs().size();
152
153 rsServedTable.walRoller.requestRollAll();
154
155 region.flush(true);
156
157 assertEquals("The old WAL should be archived", walNum, rsServedTable.getWALs().size());
158
159 rsServedTable.kill();
160
161 Thread.sleep(3000);
162 TEST_UTIL.waitTableAvailable(TABLE_NAME, 30000);
163
164 regions.clear();
165 for (JVMClusterUtil.RegionServerThread rsThread : TEST_UTIL.getHBaseCluster()
166 .getLiveRegionServerThreads()) {
167 HRegionServer rs = rsThread.getRegionServer();
168 if (rs != rsServedTable && rs.getOnlineTables().contains(TABLE_NAME)) {
169 regions.addAll(rs.getOnlineRegions(TABLE_NAME));
170 }
171 }
172 assertEquals("Table should only have one region", 1, regions.size());
173 region = (HRegion)regions.get(0);
174 store = (HStore)region.getStore(FAMILY);
175
176 assertEquals(1, store.getStorefilesCount());
177 }
178
179 private void writeDataAndFlush(int fileNum, HRegion region) throws Exception {
180 for (int i = 0; i < fileNum; i++) {
181 for (int j = 0; j < 100; j++) {
182 table.put(new Put(concat(ROW, j)).addColumn(FAMILY, QUALIFIER, concat(VALUE, j)));
183 }
184 region.flush(true);
185 }
186 }
187
188 private byte[] concat(byte[] base, int index) {
189 return Bytes.toBytes(Bytes.toString(base) + "-" + index);
190 }
191 }