1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.List;
26 import java.util.concurrent.CountDownLatch;
27 import java.util.concurrent.atomic.AtomicLong;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.fs.FileSystem;
33 import org.apache.hadoop.fs.Path;
34 import org.apache.hadoop.hbase.client.Admin;
35 import org.apache.hadoop.hbase.client.HBaseAdmin;
36 import org.apache.hadoop.hbase.client.Table;
37 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
38 import org.apache.hadoop.hbase.protobuf.generated.WALProtos.CompactionDescriptor;
39 import org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy;
40 import org.apache.hadoop.hbase.regionserver.HRegion;
41 import org.apache.hadoop.hbase.regionserver.HRegionServer;
42 import org.apache.hadoop.hbase.regionserver.HStore;
43 import org.apache.hadoop.hbase.regionserver.Region;
44 import org.apache.hadoop.hbase.regionserver.RegionServerServices;
45 import org.apache.hadoop.hbase.regionserver.Store;
46 import org.apache.hadoop.hbase.regionserver.StoreFile;
47 import org.apache.hadoop.hbase.regionserver.compactions.CompactionContext;
48 import org.apache.hadoop.hbase.testclassification.MediumTests;
49 import org.apache.hadoop.hbase.regionserver.compactions.CompactionThroughputController;
50 import org.apache.hadoop.hbase.wal.WAL;
51 import org.apache.hadoop.hbase.regionserver.wal.WALUtil;
52 import org.apache.hadoop.hbase.util.Bytes;
53 import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
54 import org.junit.Test;
55 import org.junit.experimental.categories.Category;
56
57 import com.google.common.collect.Lists;
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 @Category(MediumTests.class)
78 public class TestIOFencing {
79 static final Log LOG = LogFactory.getLog(TestIOFencing.class);
80 static {
81
82
83
84
85
86
87
88
89 }
90
91 public abstract static class CompactionBlockerRegion extends HRegion {
92 volatile int compactCount = 0;
93 volatile CountDownLatch compactionsBlocked = new CountDownLatch(0);
94 volatile CountDownLatch compactionsWaiting = new CountDownLatch(0);
95
96 @SuppressWarnings("deprecation")
97 public CompactionBlockerRegion(Path tableDir, WAL log,
98 FileSystem fs, Configuration confParam, HRegionInfo info,
99 HTableDescriptor htd, RegionServerServices rsServices) {
100 super(tableDir, log, fs, confParam, info, htd, rsServices);
101 }
102
103 public void stopCompactions() {
104 compactionsBlocked = new CountDownLatch(1);
105 compactionsWaiting = new CountDownLatch(1);
106 }
107
108 public void allowCompactions() {
109 LOG.debug("allowing compactions");
110 compactionsBlocked.countDown();
111 }
112 public void waitForCompactionToBlock() throws IOException {
113 try {
114 LOG.debug("waiting for compaction to block");
115 compactionsWaiting.await();
116 LOG.debug("compaction block reached");
117 } catch (InterruptedException ex) {
118 throw new IOException(ex);
119 }
120 }
121
122 @Override
123 public boolean compact(CompactionContext compaction, Store store,
124 CompactionThroughputController throughputController) throws IOException {
125 try {
126 return super.compact(compaction, store, throughputController);
127 } finally {
128 compactCount++;
129 }
130 }
131
132 public int countStoreFiles() {
133 int count = 0;
134 for (Store store : stores.values()) {
135 count += store.getStorefilesCount();
136 }
137 return count;
138 }
139 }
140
141
142
143
144
145 public static class BlockCompactionsInPrepRegion extends CompactionBlockerRegion {
146
147 public BlockCompactionsInPrepRegion(Path tableDir, WAL log,
148 FileSystem fs, Configuration confParam, HRegionInfo info,
149 HTableDescriptor htd, RegionServerServices rsServices) {
150 super(tableDir, log, fs, confParam, info, htd, rsServices);
151 }
152 @Override
153 protected void doRegionCompactionPrep() throws IOException {
154 compactionsWaiting.countDown();
155 try {
156 compactionsBlocked.await();
157 } catch (InterruptedException ex) {
158 throw new IOException();
159 }
160 super.doRegionCompactionPrep();
161 }
162 }
163
164
165
166
167
168
169 public static class BlockCompactionsInCompletionRegion extends CompactionBlockerRegion {
170 public BlockCompactionsInCompletionRegion(Path tableDir, WAL log,
171 FileSystem fs, Configuration confParam, HRegionInfo info,
172 HTableDescriptor htd, RegionServerServices rsServices) {
173 super(tableDir, log, fs, confParam, info, htd, rsServices);
174 }
175 @Override
176 protected HStore instantiateHStore(final HColumnDescriptor family) throws IOException {
177 return new BlockCompactionsInCompletionHStore(this, family, this.conf);
178 }
179 }
180
181 public static class BlockCompactionsInCompletionHStore extends HStore {
182 CompactionBlockerRegion r;
183 protected BlockCompactionsInCompletionHStore(HRegion region, HColumnDescriptor family,
184 Configuration confParam) throws IOException {
185 super(region, family, confParam);
186 r = (CompactionBlockerRegion) region;
187 }
188
189 @Override
190 protected void completeCompaction(final Collection<StoreFile> compactedFiles,
191 boolean removeFiles) throws IOException {
192 try {
193 r.compactionsWaiting.countDown();
194 r.compactionsBlocked.await();
195 } catch (InterruptedException ex) {
196 throw new IOException(ex);
197 }
198 super.completeCompaction(compactedFiles, removeFiles);
199 }
200 @Override
201 protected void completeCompaction(Collection<StoreFile> compactedFiles) throws IOException {
202 try {
203 r.compactionsWaiting.countDown();
204 r.compactionsBlocked.await();
205 } catch (InterruptedException ex) {
206 throw new IOException(ex);
207 }
208 super.completeCompaction(compactedFiles);
209 }
210 }
211
212 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
213 private final static TableName TABLE_NAME =
214 TableName.valueOf("tabletest");
215 private final static byte[] FAMILY = Bytes.toBytes("family");
216 private static final int FIRST_BATCH_COUNT = 4000;
217 private static final int SECOND_BATCH_COUNT = FIRST_BATCH_COUNT;
218
219
220
221
222
223
224
225 @Test
226 public void testFencingAroundCompaction() throws Exception {
227 doTest(BlockCompactionsInPrepRegion.class, false);
228 doTest(BlockCompactionsInPrepRegion.class, true);
229 }
230
231
232
233
234
235
236
237 @Test
238 public void testFencingAroundCompactionAfterWALSync() throws Exception {
239 doTest(BlockCompactionsInCompletionRegion.class, false);
240 doTest(BlockCompactionsInCompletionRegion.class, true);
241 }
242
243 public void doTest(Class<?> regionClass, boolean distributedLogReplay) throws Exception {
244 Configuration c = TEST_UTIL.getConfiguration();
245 c.setBoolean(HConstants.DISTRIBUTED_LOG_REPLAY_KEY, distributedLogReplay);
246
247 c.setClass(HConstants.REGION_IMPL, regionClass, HRegion.class);
248 c.setBoolean("dfs.support.append", true);
249
250 c.setLong("hbase.hregion.memstore.flush.size", 200000);
251 c.set(HConstants.HBASE_REGION_SPLIT_POLICY_KEY, ConstantSizeRegionSplitPolicy.class.getName());
252
253 c.setInt("hbase.hstore.compactionThreshold", 1000);
254 c.setLong("hbase.hstore.blockingStoreFiles", 1000);
255
256 c.setInt("hbase.regionserver.thread.splitcompactcheckfrequency", 1000);
257 LOG.info("Starting mini cluster");
258 TEST_UTIL.startMiniCluster(1);
259 CompactionBlockerRegion compactingRegion = null;
260 Admin admin = null;
261 try {
262 LOG.info("Creating admin");
263 admin = TEST_UTIL.getConnection().getAdmin();
264 LOG.info("Creating table");
265 TEST_UTIL.createTable(TABLE_NAME, FAMILY);
266 Table table = TEST_UTIL.getConnection().getTable(TABLE_NAME);
267 LOG.info("Loading test table");
268
269 List<HRegion> testRegions = TEST_UTIL.getMiniHBaseCluster().findRegionsForTable(TABLE_NAME);
270 assertEquals(1, testRegions.size());
271 compactingRegion = (CompactionBlockerRegion)testRegions.get(0);
272 LOG.info("Blocking compactions");
273 compactingRegion.stopCompactions();
274 long lastFlushTime = compactingRegion.getEarliestFlushTimeForAllStores();
275
276 TEST_UTIL.loadNumericRows(table, FAMILY, 0, FIRST_BATCH_COUNT);
277
278
279
280 HRegionInfo oldHri = new HRegionInfo(table.getName(),
281 HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW);
282 CompactionDescriptor compactionDescriptor = ProtobufUtil.toCompactionDescriptor(oldHri,
283 FAMILY, Lists.newArrayList(new Path("/a")), Lists.newArrayList(new Path("/b")),
284 new Path("store_dir"));
285 WALUtil.writeCompactionMarker(compactingRegion.getWAL(), table.getTableDescriptor(),
286 oldHri, compactionDescriptor, new AtomicLong(Long.MAX_VALUE-100));
287
288
289 long startWaitTime = System.currentTimeMillis();
290 while (compactingRegion.getEarliestFlushTimeForAllStores() <= lastFlushTime ||
291 compactingRegion.countStoreFiles() <= 1) {
292 LOG.info("Waiting for the region to flush " +
293 compactingRegion.getRegionInfo().getRegionNameAsString());
294 Thread.sleep(1000);
295 assertTrue("Timed out waiting for the region to flush",
296 System.currentTimeMillis() - startWaitTime < 30000);
297 }
298 assertTrue(compactingRegion.countStoreFiles() > 1);
299 final byte REGION_NAME[] = compactingRegion.getRegionInfo().getRegionName();
300 LOG.info("Asking for compaction");
301 ((HBaseAdmin)admin).majorCompact(TABLE_NAME.getName());
302 LOG.info("Waiting for compaction to be about to start");
303 compactingRegion.waitForCompactionToBlock();
304 LOG.info("Starting a new server");
305 RegionServerThread newServerThread = TEST_UTIL.getMiniHBaseCluster().startRegionServer();
306 final HRegionServer newServer = newServerThread.getRegionServer();
307 LOG.info("Killing region server ZK lease");
308 TEST_UTIL.expireRegionServerSession(0);
309 CompactionBlockerRegion newRegion = null;
310 startWaitTime = System.currentTimeMillis();
311 LOG.info("Waiting for the new server to pick up the region " + Bytes.toString(REGION_NAME));
312
313
314 Waiter.waitFor(c, 60000, new Waiter.Predicate<Exception>() {
315 @Override
316 public boolean evaluate() throws Exception {
317 Region newRegion = newServer.getOnlineRegion(REGION_NAME);
318 return newRegion != null && !newRegion.isRecovering();
319 }
320 });
321
322 newRegion = (CompactionBlockerRegion)newServer.getOnlineRegion(REGION_NAME);
323
324 LOG.info("Allowing compaction to proceed");
325 compactingRegion.allowCompactions();
326 while (compactingRegion.compactCount == 0) {
327 Thread.sleep(1000);
328 }
329
330
331 LOG.info("Compaction finished");
332
333
334 FileSystem fs = newRegion.getFilesystem();
335 for (String f: newRegion.getStoreFileList(new byte [][] {FAMILY})) {
336 assertTrue("After compaction, does not exist: " + f, fs.exists(new Path(f)));
337 }
338
339
340 TEST_UTIL.loadNumericRows(table, FAMILY, FIRST_BATCH_COUNT, FIRST_BATCH_COUNT + SECOND_BATCH_COUNT);
341 ((HBaseAdmin)admin).majorCompact(TABLE_NAME.getName());
342 startWaitTime = System.currentTimeMillis();
343 while (newRegion.compactCount == 0) {
344 Thread.sleep(1000);
345 assertTrue("New region never compacted", System.currentTimeMillis() - startWaitTime < 180000);
346 }
347 assertEquals(FIRST_BATCH_COUNT + SECOND_BATCH_COUNT, TEST_UTIL.countRows(table));
348 } finally {
349 if (compactingRegion != null) {
350 compactingRegion.allowCompactions();
351 }
352 admin.close();
353 TEST_UTIL.shutdownMiniCluster();
354 }
355 }
356 }