001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.regionserver; 019 020import java.io.IOException; 021import org.apache.hadoop.hbase.HBaseClassTestRule; 022import org.apache.hadoop.hbase.HBaseTestingUtility; 023import org.apache.hadoop.hbase.HConstants; 024import org.apache.hadoop.hbase.HRegionInfo; 025import org.apache.hadoop.hbase.NotServingRegionException; 026import org.apache.hadoop.hbase.ServerName; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.client.Put; 029import org.apache.hadoop.hbase.client.RegionLocator; 030import org.apache.hadoop.hbase.client.Table; 031import org.apache.hadoop.hbase.client.TableDescriptor; 032import org.apache.hadoop.hbase.master.HMaster; 033import org.apache.hadoop.hbase.regionserver.handler.OpenRegionHandler; 034import org.apache.hadoop.hbase.testclassification.MediumTests; 035import org.apache.hadoop.hbase.testclassification.RegionServerTests; 036import org.apache.hadoop.hbase.util.Bytes; 037import org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread; 038import org.apache.hadoop.hbase.util.Threads; 039import org.apache.hadoop.hbase.zookeeper.MetaTableLocator; 040import org.apache.hadoop.hbase.zookeeper.ZKWatcher; 041import org.junit.AfterClass; 042import org.junit.Assert; 043import org.junit.BeforeClass; 044import org.junit.ClassRule; 045import org.junit.Test; 046import org.junit.experimental.categories.Category; 047import org.slf4j.Logger; 048import org.slf4j.LoggerFactory; 049 050import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; 051import org.apache.hadoop.hbase.shaded.protobuf.RequestConverter; 052import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos; 053import org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.CloseRegionRequest; 054 055/** 056 * Tests on the region server, without the master. 057 */ 058@Category({RegionServerTests.class, MediumTests.class}) 059public class TestRegionServerNoMaster { 060 061 @ClassRule 062 public static final HBaseClassTestRule CLASS_RULE = 063 HBaseClassTestRule.forClass(TestRegionServerNoMaster.class); 064 065 private static final Logger LOG = LoggerFactory.getLogger(TestRegionServerNoMaster.class); 066 private static final int NB_SERVERS = 1; 067 private static Table table; 068 private static final byte[] row = Bytes.toBytes("ee"); 069 070 private static HRegionInfo hri; 071 072 private static byte[] regionName; 073 private static final HBaseTestingUtility HTU = new HBaseTestingUtility(); 074 075 076 @BeforeClass 077 public static void before() throws Exception { 078 HTU.startMiniCluster(NB_SERVERS); 079 final TableName tableName = TableName.valueOf(TestRegionServerNoMaster.class.getSimpleName()); 080 081 // Create table then get the single region for our new table. 082 table = HTU.createTable(tableName,HConstants.CATALOG_FAMILY); 083 Put p = new Put(row); 084 p.addColumn(HConstants.CATALOG_FAMILY, row, row); 085 table.put(p); 086 087 try (RegionLocator locator = HTU.getConnection().getRegionLocator(tableName)) { 088 hri = locator.getRegionLocation(row, false).getRegionInfo(); 089 } 090 regionName = hri.getRegionName(); 091 092 stopMasterAndAssignMeta(HTU); 093 } 094 095 public static void stopMasterAndAssignMeta(HBaseTestingUtility HTU) 096 throws IOException, InterruptedException { 097 // Stop master 098 HMaster master = HTU.getHBaseCluster().getMaster(); 099 Thread masterThread = HTU.getHBaseCluster().getMasterThread(); 100 ServerName masterAddr = master.getServerName(); 101 master.stopMaster(); 102 103 LOG.info("Waiting until master thread exits"); 104 while (masterThread != null && masterThread.isAlive()) { 105 Threads.sleep(100); 106 } 107 108 HRegionServer.TEST_SKIP_REPORTING_TRANSITION = true; 109 // Master is down, so is the meta. We need to assign it somewhere 110 // so that regions can be assigned during the mocking phase. 111 HRegionServer hrs = HTU.getHBaseCluster() 112 .getLiveRegionServerThreads().get(0).getRegionServer(); 113 ZKWatcher zkw = hrs.getZooKeeper(); 114 ServerName sn = MetaTableLocator.getMetaRegionLocation(zkw); 115 if (sn != null && !masterAddr.equals(sn)) { 116 return; 117 } 118 119 ProtobufUtil.openRegion(null, hrs.getRSRpcServices(), 120 hrs.getServerName(), HRegionInfo.FIRST_META_REGIONINFO); 121 while (true) { 122 sn = MetaTableLocator.getMetaRegionLocation(zkw); 123 if (sn != null && sn.equals(hrs.getServerName()) 124 && hrs.onlineRegions.containsKey( 125 HRegionInfo.FIRST_META_REGIONINFO.getEncodedName())) { 126 break; 127 } 128 Thread.sleep(100); 129 } 130 } 131 132 /** Flush the given region in the mini cluster. Since no master, we cannot use HBaseAdmin.flush() */ 133 public static void flushRegion(HBaseTestingUtility HTU, HRegionInfo regionInfo) throws IOException { 134 for (RegionServerThread rst : HTU.getMiniHBaseCluster().getRegionServerThreads()) { 135 HRegion region = rst.getRegionServer().getRegionByEncodedName(regionInfo.getEncodedName()); 136 if (region != null) { 137 region.flush(true); 138 return; 139 } 140 } 141 throw new IOException("Region to flush cannot be found"); 142 } 143 144 @AfterClass 145 public static void afterClass() throws Exception { 146 HRegionServer.TEST_SKIP_REPORTING_TRANSITION = false; 147 table.close(); 148 HTU.shutdownMiniCluster(); 149 } 150 151 private static HRegionServer getRS() { 152 return HTU.getHBaseCluster().getLiveRegionServerThreads().get(0).getRegionServer(); 153 } 154 155 156 public static void openRegion(HBaseTestingUtility HTU, HRegionServer rs, HRegionInfo hri) 157 throws Exception { 158 AdminProtos.OpenRegionRequest orr = 159 RequestConverter.buildOpenRegionRequest(rs.getServerName(), hri, null); 160 AdminProtos.OpenRegionResponse responseOpen = rs.rpcServices.openRegion(null, orr); 161 162 Assert.assertTrue(responseOpen.getOpeningStateCount() == 1); 163 Assert.assertTrue(responseOpen.getOpeningState(0). 164 equals(AdminProtos.OpenRegionResponse.RegionOpeningState.OPENED)); 165 166 167 checkRegionIsOpened(HTU, rs, hri); 168 } 169 170 public static void checkRegionIsOpened(HBaseTestingUtility HTU, HRegionServer rs, 171 HRegionInfo hri) throws Exception { 172 while (!rs.getRegionsInTransitionInRS().isEmpty()) { 173 Thread.sleep(1); 174 } 175 176 Assert.assertTrue(rs.getRegion(hri.getRegionName()).isAvailable()); 177 } 178 179 public static void closeRegion(HBaseTestingUtility HTU, HRegionServer rs, HRegionInfo hri) 180 throws Exception { 181 AdminProtos.CloseRegionRequest crr = ProtobufUtil.buildCloseRegionRequest( 182 rs.getServerName(), hri.getRegionName()); 183 AdminProtos.CloseRegionResponse responseClose = rs.rpcServices.closeRegion(null, crr); 184 Assert.assertTrue(responseClose.getClosed()); 185 checkRegionIsClosed(HTU, rs, hri); 186 } 187 188 public static void checkRegionIsClosed(HBaseTestingUtility HTU, HRegionServer rs, 189 HRegionInfo hri) throws Exception { 190 while (!rs.getRegionsInTransitionInRS().isEmpty()) { 191 Thread.sleep(1); 192 } 193 194 try { 195 Assert.assertFalse(rs.getRegion(hri.getRegionName()).isAvailable()); 196 } catch (NotServingRegionException expected) { 197 // That's how it work: if the region is closed we have an exception. 198 } 199 } 200 201 /** 202 * Close the region without using ZK 203 */ 204 private void closeRegionNoZK() throws Exception { 205 // no transition in ZK 206 AdminProtos.CloseRegionRequest crr = 207 ProtobufUtil.buildCloseRegionRequest(getRS().getServerName(), regionName); 208 AdminProtos.CloseRegionResponse responseClose = getRS().rpcServices.closeRegion(null, crr); 209 Assert.assertTrue(responseClose.getClosed()); 210 211 // now waiting & checking. After a while, the transition should be done and the region closed 212 checkRegionIsClosed(HTU, getRS(), hri); 213 } 214 215 216 @Test 217 public void testCloseByRegionServer() throws Exception { 218 closeRegionNoZK(); 219 openRegion(HTU, getRS(), hri); 220 } 221 222 @Test 223 public void testMultipleCloseFromMaster() throws Exception { 224 for (int i = 0; i < 10; i++) { 225 AdminProtos.CloseRegionRequest crr = 226 ProtobufUtil.buildCloseRegionRequest(getRS().getServerName(), regionName, null); 227 try { 228 AdminProtos.CloseRegionResponse responseClose = getRS().rpcServices.closeRegion(null, crr); 229 Assert.assertTrue("request " + i + " failed", 230 responseClose.getClosed() || responseClose.hasClosed()); 231 } catch (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException se) { 232 Assert.assertTrue("The next queries may throw an exception.", i > 0); 233 } 234 } 235 236 checkRegionIsClosed(HTU, getRS(), hri); 237 238 openRegion(HTU, getRS(), hri); 239 } 240 241 /** 242 * Test that if we do a close while opening it stops the opening. 243 */ 244 @Test 245 public void testCancelOpeningWithoutZK() throws Exception { 246 // We close 247 closeRegionNoZK(); 248 checkRegionIsClosed(HTU, getRS(), hri); 249 250 // Let do the initial steps, without having a handler 251 getRS().getRegionsInTransitionInRS().put(hri.getEncodedNameAsBytes(), Boolean.TRUE); 252 253 // That's a close without ZK. 254 AdminProtos.CloseRegionRequest crr = 255 ProtobufUtil.buildCloseRegionRequest(getRS().getServerName(), regionName); 256 try { 257 getRS().rpcServices.closeRegion(null, crr); 258 Assert.assertTrue(false); 259 } catch (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException expected) { 260 } 261 262 // The state in RIT should have changed to close 263 Assert.assertEquals(Boolean.FALSE, getRS().getRegionsInTransitionInRS().get( 264 hri.getEncodedNameAsBytes())); 265 266 // Let's start the open handler 267 TableDescriptor htd = getRS().tableDescriptors.get(hri.getTable()); 268 269 getRS().executorService.submit(new OpenRegionHandler(getRS(), getRS(), hri, htd, -1)); 270 271 // The open handler should have removed the region from RIT but kept the region closed 272 checkRegionIsClosed(HTU, getRS(), hri); 273 274 openRegion(HTU, getRS(), hri); 275 } 276 277 /** 278 * Tests an on-the-fly RPC that was scheduled for the earlier RS on the same port 279 * for openRegion. The region server should reject this RPC. (HBASE-9721) 280 */ 281 @Test 282 public void testOpenCloseRegionRPCIntendedForPreviousServer() throws Exception { 283 Assert.assertTrue(getRS().getRegion(regionName).isAvailable()); 284 285 ServerName sn = getRS().getServerName(); 286 ServerName earlierServerName = ServerName.valueOf(sn.getHostname(), sn.getPort(), 1); 287 288 try { 289 CloseRegionRequest request = ProtobufUtil.buildCloseRegionRequest(earlierServerName, regionName); 290 getRS().getRSRpcServices().closeRegion(null, request); 291 Assert.fail("The closeRegion should have been rejected"); 292 } catch (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException se) { 293 Assert.assertTrue(se.getCause() instanceof IOException); 294 Assert.assertTrue(se.getCause().getMessage().contains("This RPC was intended for a different server")); 295 } 296 297 //actual close 298 closeRegionNoZK(); 299 try { 300 AdminProtos.OpenRegionRequest orr = RequestConverter.buildOpenRegionRequest( 301 earlierServerName, hri, null); 302 getRS().getRSRpcServices().openRegion(null, orr); 303 Assert.fail("The openRegion should have been rejected"); 304 } catch (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException se) { 305 Assert.assertTrue(se.getCause() instanceof IOException); 306 Assert.assertTrue(se.getCause().getMessage().contains("This RPC was intended for a different server")); 307 } finally { 308 openRegion(HTU, getRS(), hri); 309 } 310 } 311}