1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.master;
20
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import java.util.Collection;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.hbase.HBaseTestingUtility;
34 import org.apache.hadoop.hbase.HConstants;
35 import org.apache.hadoop.hbase.HRegionInfo;
36 import org.apache.hadoop.hbase.MediumTests;
37 import org.apache.hadoop.hbase.MiniHBaseCluster;
38 import org.apache.hadoop.hbase.TableDescriptors;
39 import org.apache.hadoop.hbase.TableName;
40 import org.apache.hadoop.hbase.client.Durability;
41 import org.apache.hadoop.hbase.client.HTable;
42 import org.apache.hadoop.hbase.client.Put;
43 import org.apache.hadoop.hbase.client.Result;
44 import org.apache.hadoop.hbase.client.ResultScanner;
45 import org.apache.hadoop.hbase.client.Scan;
46 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
47 import org.apache.hadoop.hbase.regionserver.HRegionServer;
48 import org.apache.hadoop.hbase.util.Bytes;
49 import org.apache.hadoop.hbase.util.Threads;
50 import org.junit.AfterClass;
51 import org.junit.Assert;
52 import org.junit.Before;
53 import org.junit.BeforeClass;
54 import org.junit.Test;
55 import org.junit.experimental.categories.Category;
56 import org.mockito.Mockito;
57 import org.mockito.internal.util.reflection.Whitebox;
58
59
60
61
62 @Category(MediumTests.class)
63 public class TestZKBasedOpenCloseRegion {
64 private static final Log LOG = LogFactory.getLog(TestZKBasedOpenCloseRegion.class);
65 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
66 private static final TableName TABLENAME =
67 TableName.valueOf("TestZKBasedOpenCloseRegion");
68 private static final byte [][] FAMILIES = new byte [][] {Bytes.toBytes("a"),
69 Bytes.toBytes("b"), Bytes.toBytes("c")};
70 private static int countOfRegions;
71
72 @BeforeClass public static void beforeAllTests() throws Exception {
73 Configuration c = TEST_UTIL.getConfiguration();
74 c.setBoolean("dfs.support.append", true);
75 c.setInt("hbase.regionserver.info.port", 0);
76 TEST_UTIL.startMiniCluster(2);
77 TEST_UTIL.createTable(TABLENAME, FAMILIES);
78 HTable t = new HTable(TEST_UTIL.getConfiguration(), TABLENAME);
79 countOfRegions = TEST_UTIL.createMultiRegions(t, getTestFamily());
80 waitUntilAllRegionsAssigned();
81 addToEachStartKey(countOfRegions);
82 t.close();
83 TEST_UTIL.getHBaseCluster().getMaster().assignmentManager.initializeHandlerTrackers();
84 }
85
86 @AfterClass public static void afterAllTests() throws Exception {
87 TEST_UTIL.shutdownMiniCluster();
88 }
89
90 @Before public void setup() throws IOException {
91 if (TEST_UTIL.getHBaseCluster().getLiveRegionServerThreads().size() < 2) {
92
93 LOG.info("Started new server=" +
94 TEST_UTIL.getHBaseCluster().startRegionServer());
95
96 }
97 waitUntilAllRegionsAssigned();
98 waitOnRIT();
99 }
100
101
102
103
104
105 @Test (timeout=300000) public void testReOpenRegion()
106 throws Exception {
107 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
108 LOG.info("Number of region servers = " +
109 cluster.getLiveRegionServerThreads().size());
110
111 int rsIdx = 0;
112 HRegionServer regionServer =
113 TEST_UTIL.getHBaseCluster().getRegionServer(rsIdx);
114 HRegionInfo hri = getNonMetaRegion(ProtobufUtil.getOnlineRegions(regionServer));
115 LOG.debug("Asking RS to close region " + hri.getRegionNameAsString());
116
117 LOG.info("Unassign " + hri.getRegionNameAsString());
118 cluster.getMaster().assignmentManager.unassign(hri);
119
120 while (!cluster.getMaster().assignmentManager.wasClosedHandlerCalled(hri)) {
121 Threads.sleep(100);
122 }
123
124 while (!cluster.getMaster().assignmentManager.wasOpenedHandlerCalled(hri)) {
125 Threads.sleep(100);
126 }
127
128 LOG.info("Done with testReOpenRegion");
129 }
130
131 private HRegionInfo getNonMetaRegion(final Collection<HRegionInfo> regions) {
132 HRegionInfo hri = null;
133 for (HRegionInfo i: regions) {
134 LOG.info(i.getRegionNameAsString());
135 if (!i.isMetaRegion()) {
136 hri = i;
137 break;
138 }
139 }
140 return hri;
141 }
142
143
144
145
146
147
148 @Test
149 public void testRSAlreadyProcessingRegion() throws Exception {
150 LOG.info("starting testRSAlreadyProcessingRegion");
151 MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
152
153 HRegionServer hr0 =
154 cluster.getLiveRegionServerThreads().get(0).getRegionServer();
155 HRegionServer hr1 =
156 cluster.getLiveRegionServerThreads().get(1).getRegionServer();
157 HRegionInfo hri = getNonMetaRegion(ProtobufUtil.getOnlineRegions(hr0));
158
159
160 hr1.getRegionsInTransitionInRS().putIfAbsent(hri.getEncodedNameAsBytes(), true);
161
162
163 TEST_UTIL.getHBaseAdmin().move(hri.getEncodedNameAsBytes(),
164 Bytes.toBytes(hr1.getServerName().toString()));
165
166
167 assertEquals(hr1.getOnlineRegion(hri.getEncodedNameAsBytes()), null);
168
169
170 hr1.getRegionsInTransitionInRS().remove(hri.getEncodedNameAsBytes());
171
172
173 hri = getNonMetaRegion(ProtobufUtil.getOnlineRegions(hr1));
174
175 TEST_UTIL.getHBaseAdmin().move(hri.getEncodedNameAsBytes(),
176 Bytes.toBytes(hr0.getServerName().toString()));
177
178 while (!cluster.getMaster().assignmentManager.wasOpenedHandlerCalled(hri)) {
179 Threads.sleep(100);
180 }
181
182
183 assertTrue(hr1.getOnlineRegion(hri.getEncodedNameAsBytes()) == null);
184
185 }
186
187 private void waitOnRIT() {
188
189
190 while (TEST_UTIL.getHBaseCluster().getMaster().getAssignmentManager().
191 getRegionStates().isRegionsInTransition()) {
192 LOG.info("Waiting on regions in transition: " +
193 TEST_UTIL.getHBaseCluster().getMaster().getAssignmentManager().
194 getRegionStates().getRegionsInTransition());
195 Threads.sleep(10);
196 }
197 }
198
199
200
201
202
203
204 @Test
205 public void testRegionOpenFailsDueToIOException() throws Exception {
206 HRegionInfo REGIONINFO = new HRegionInfo(TableName.valueOf("t"),
207 HConstants.EMPTY_START_ROW, HConstants.EMPTY_START_ROW);
208 HRegionServer regionServer = TEST_UTIL.getHBaseCluster().getRegionServer(0);
209 TableDescriptors htd = Mockito.mock(TableDescriptors.class);
210 Object orizinalState = Whitebox.getInternalState(regionServer,"tableDescriptors");
211 Whitebox.setInternalState(regionServer, "tableDescriptors", htd);
212 Mockito.doThrow(new IOException()).when(htd).get((TableName) Mockito.any());
213 try {
214 ProtobufUtil.openRegion(regionServer, regionServer.getServerName(), REGIONINFO);
215 fail("It should throw IOException ");
216 } catch (IOException e) {
217 }
218 Whitebox.setInternalState(regionServer, "tableDescriptors", orizinalState);
219 assertFalse("Region should not be in RIT",
220 regionServer.getRegionsInTransitionInRS().containsKey(REGIONINFO.getEncodedNameAsBytes()));
221 }
222
223 private static void waitUntilAllRegionsAssigned()
224 throws IOException {
225 HTable meta = new HTable(TEST_UTIL.getConfiguration(), TableName.META_TABLE_NAME);
226 while (true) {
227 int rows = 0;
228 Scan scan = new Scan();
229 scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
230 ResultScanner s = meta.getScanner(scan);
231 for (Result r = null; (r = s.next()) != null;) {
232 byte [] b =
233 r.getValue(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
234 if (b == null || b.length <= 0) {
235 break;
236 }
237 rows++;
238 }
239 s.close();
240
241 if (rows >= countOfRegions) {
242 break;
243 }
244 LOG.info("Found=" + rows);
245 Threads.sleep(1000);
246 }
247 meta.close();
248 }
249
250
251
252
253
254
255
256
257 private static int addToEachStartKey(final int expected) throws IOException {
258 HTable t = new HTable(TEST_UTIL.getConfiguration(), TABLENAME);
259 HTable meta = new HTable(TEST_UTIL.getConfiguration(),
260 TableName.META_TABLE_NAME);
261 int rows = 0;
262 Scan scan = new Scan();
263 scan.addColumn(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
264 ResultScanner s = meta.getScanner(scan);
265 for (Result r = null; (r = s.next()) != null;) {
266 HRegionInfo hri = HRegionInfo.getHRegionInfo(r);
267 if (hri == null) break;
268 if(!hri.getTable().equals(TABLENAME)) {
269 continue;
270 }
271
272 byte [] row = getStartKey(hri);
273 Put p = new Put(row);
274 p.setDurability(Durability.SKIP_WAL);
275 p.add(getTestFamily(), getTestQualifier(), row);
276 t.put(p);
277 rows++;
278 }
279 s.close();
280 Assert.assertEquals(expected, rows);
281 t.close();
282 meta.close();
283 return rows;
284 }
285
286 private static byte [] getStartKey(final HRegionInfo hri) {
287 return Bytes.equals(HConstants.EMPTY_START_ROW, hri.getStartKey())?
288 Bytes.toBytes("aaa"): hri.getStartKey();
289 }
290
291 private static byte [] getTestFamily() {
292 return FAMILIES[0];
293 }
294
295 private static byte [] getTestQualifier() {
296 return getTestFamily();
297 }
298
299 }
300