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.fail;
22
23 import java.io.IOException;
24 import java.util.List;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.hadoop.conf.Configuration;
29 import org.apache.hadoop.hbase.CoordinatedStateManager;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.HConstants;
32 import org.apache.hadoop.hbase.TableName;
33 import org.apache.hadoop.hbase.Waiter.ExplainingPredicate;
34 import org.apache.hadoop.hbase.YouAreDeadException;
35 import org.apache.hadoop.hbase.client.Get;
36 import org.apache.hadoop.hbase.client.Put;
37 import org.apache.hadoop.hbase.client.Table;
38 import org.apache.hadoop.hbase.testclassification.LargeTests;
39 import org.apache.hadoop.hbase.testclassification.RegionServerTests;
40 import org.apache.hadoop.hbase.util.Bytes;
41 import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread;
42 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
43 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
44 import org.junit.After;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.experimental.categories.Category;
48
49
50
51
52
53 @Category({ RegionServerTests.class, LargeTests.class })
54 public class TestCompactionInDeadRegionServer {
55 private static final Log LOG = LogFactory.getLog(TestCompactionInDeadRegionServer.class);
56
57 private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
58
59 private static final TableName TABLE_NAME = TableName.valueOf("test");
60
61 private static final byte[] CF = Bytes.toBytes("cf");
62
63 private static final byte[] CQ = Bytes.toBytes("cq");
64
65 public static final class IgnoreYouAreDeadRS extends HRegionServer {
66
67 public IgnoreYouAreDeadRS(Configuration conf) throws IOException, InterruptedException {
68 super(conf);
69 }
70
71 public IgnoreYouAreDeadRS(Configuration conf, CoordinatedStateManager csm)
72 throws IOException, InterruptedException {
73 super(conf, csm);
74 }
75
76 @Override
77 protected void tryRegionServerReport(long reportStartTime, long reportEndTime)
78 throws IOException {
79 try {
80 super.tryRegionServerReport(reportStartTime, reportEndTime);
81 } catch (YouAreDeadException e) {
82
83 }
84 }
85 }
86
87 @Before
88 public void setUp() throws Exception {
89 UTIL.getConfiguration().setInt(HConstants.ZK_SESSION_TIMEOUT, 2000);
90 UTIL.getConfiguration().setClass(HConstants.REGION_SERVER_IMPL, IgnoreYouAreDeadRS.class,
91 HRegionServer.class);
92 UTIL.startMiniCluster(2);
93 Table table = UTIL.createTable(TABLE_NAME, CF);
94 for (int i = 0; i < 10; i++) {
95 table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
96 }
97 UTIL.getHBaseAdmin().flush(TABLE_NAME);
98 for (int i = 10; i < 20; i++) {
99 table.put(new Put(Bytes.toBytes(i)).addColumn(CF, CQ, Bytes.toBytes(i)));
100 }
101 UTIL.getHBaseAdmin().flush(TABLE_NAME);
102 }
103
104 @After
105 public void tearDown() throws Exception {
106 UTIL.shutdownMiniCluster();
107 }
108
109 @Test
110 public void test() throws Exception {
111 HRegionServer regionSvr = UTIL.getRSForFirstRegionInTable(TABLE_NAME);
112 Region region = regionSvr.getOnlineRegions(TABLE_NAME).get(0);
113 String regName = region.getRegionInfo().getEncodedName();
114 List<Region> metaRegs = regionSvr.getOnlineRegions(TableName.META_TABLE_NAME);
115 if (metaRegs != null && !metaRegs.isEmpty()) {
116 LOG.info("meta is on the same server: " + regionSvr);
117
118
119
120 HRegionServer otherRs = UTIL.getOtherRegionServer(regionSvr);
121 UTIL.moveRegionAndWait(region.getRegionInfo(), otherRs.getServerName());
122 LOG.info("Moved region: " + regName + " to " + otherRs.getServerName());
123 }
124 final HRegionServer rsToSuspend = UTIL.getRSForFirstRegionInTable(TABLE_NAME);
125 region = (HRegion) rsToSuspend.getOnlineRegions(TABLE_NAME).get(0);
126 ZooKeeperWatcher watcher = UTIL.getZooKeeperWatcher();
127 watcher.getRecoverableZooKeeper()
128 .delete(ZKUtil.joinZNode(watcher.rsZNode, rsToSuspend.getServerName().toString()), -1);
129 UTIL.waitFor(60000, 1000, new ExplainingPredicate<Exception>() {
130
131 @Override
132 public boolean evaluate() throws Exception {
133 for (RegionServerThread thread : UTIL.getHBaseCluster().getRegionServerThreads()) {
134 HRegionServer rs = thread.getRegionServer();
135 if (rs != rsToSuspend) {
136 return !rs.getOnlineRegions(TABLE_NAME).isEmpty();
137 }
138 }
139 return false;
140 }
141
142 @Override
143 public String explainFailure() throws Exception {
144 return "The region for " + TABLE_NAME + " is still on " + rsToSuspend.getServerName();
145 }
146 });
147 try {
148 ((HRegion)region).compact(true);
149 fail("Should fail as our wal file has already been closed, " +
150 "and walDir has also been renamed");
151 } catch (Exception e) {
152 LOG.debug("expected exception: ", e);
153 }
154 Table table = UTIL.getConnection().getTable(TABLE_NAME);
155
156 for (int i = 0; i < 20; i++) {
157 assertEquals(i, Bytes.toInt(table.get(new Get(Bytes.toBytes(i))).getValue(CF, CQ)));
158 }
159 }
160 }