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 MetaTableLocator mtl = new MetaTableLocator(); 115 ServerName sn = mtl.getMetaRegionLocation(zkw); 116 if (sn != null && !masterAddr.equals(sn)) { 117 return; 118 } 119 120 ProtobufUtil.openRegion(null, hrs.getRSRpcServices(), 121 hrs.getServerName(), HRegionInfo.FIRST_META_REGIONINFO); 122 while (true) { 123 sn = mtl.getMetaRegionLocation(zkw); 124 if (sn != null && sn.equals(hrs.getServerName()) 125 && hrs.onlineRegions.containsKey( 126 HRegionInfo.FIRST_META_REGIONINFO.getEncodedName())) { 127 break; 128 } 129 Thread.sleep(100); 130 } 131 } 132 133 /** Flush the given region in the mini cluster. Since no master, we cannot use HBaseAdmin.flush() */ 134 public static void flushRegion(HBaseTestingUtility HTU, HRegionInfo regionInfo) throws IOException { 135 for (RegionServerThread rst : HTU.getMiniHBaseCluster().getRegionServerThreads()) { 136 HRegion region = rst.getRegionServer().getRegionByEncodedName(regionInfo.getEncodedName()); 137 if (region != null) { 138 region.flush(true); 139 return; 140 } 141 } 142 throw new IOException("Region to flush cannot be found"); 143 } 144 145 @AfterClass 146 public static void afterClass() throws Exception { 147 HRegionServer.TEST_SKIP_REPORTING_TRANSITION = false; 148 table.close(); 149 HTU.shutdownMiniCluster(); 150 } 151 152 private static HRegionServer getRS() { 153 return HTU.getHBaseCluster().getLiveRegionServerThreads().get(0).getRegionServer(); 154 } 155 156 157 public static void openRegion(HBaseTestingUtility HTU, HRegionServer rs, HRegionInfo hri) 158 throws Exception { 159 AdminProtos.OpenRegionRequest orr = 160 RequestConverter.buildOpenRegionRequest(rs.getServerName(), hri, null); 161 AdminProtos.OpenRegionResponse responseOpen = rs.rpcServices.openRegion(null, orr); 162 163 Assert.assertTrue(responseOpen.getOpeningStateCount() == 1); 164 Assert.assertTrue(responseOpen.getOpeningState(0). 165 equals(AdminProtos.OpenRegionResponse.RegionOpeningState.OPENED)); 166 167 168 checkRegionIsOpened(HTU, rs, hri); 169 } 170 171 public static void checkRegionIsOpened(HBaseTestingUtility HTU, HRegionServer rs, 172 HRegionInfo hri) throws Exception { 173 while (!rs.getRegionsInTransitionInRS().isEmpty()) { 174 Thread.sleep(1); 175 } 176 177 Assert.assertTrue(rs.getRegion(hri.getRegionName()).isAvailable()); 178 } 179 180 public static void closeRegion(HBaseTestingUtility HTU, HRegionServer rs, HRegionInfo hri) 181 throws Exception { 182 AdminProtos.CloseRegionRequest crr = ProtobufUtil.buildCloseRegionRequest( 183 rs.getServerName(), hri.getRegionName()); 184 AdminProtos.CloseRegionResponse responseClose = rs.rpcServices.closeRegion(null, crr); 185 Assert.assertTrue(responseClose.getClosed()); 186 checkRegionIsClosed(HTU, rs, hri); 187 } 188 189 public static void checkRegionIsClosed(HBaseTestingUtility HTU, HRegionServer rs, 190 HRegionInfo hri) throws Exception { 191 while (!rs.getRegionsInTransitionInRS().isEmpty()) { 192 Thread.sleep(1); 193 } 194 195 try { 196 Assert.assertFalse(rs.getRegion(hri.getRegionName()).isAvailable()); 197 } catch (NotServingRegionException expected) { 198 // That's how it work: if the region is closed we have an exception. 199 } 200 } 201 202 /** 203 * Close the region without using ZK 204 */ 205 private void closeRegionNoZK() throws Exception { 206 // no transition in ZK 207 AdminProtos.CloseRegionRequest crr = 208 ProtobufUtil.buildCloseRegionRequest(getRS().getServerName(), regionName); 209 AdminProtos.CloseRegionResponse responseClose = getRS().rpcServices.closeRegion(null, crr); 210 Assert.assertTrue(responseClose.getClosed()); 211 212 // now waiting & checking. After a while, the transition should be done and the region closed 213 checkRegionIsClosed(HTU, getRS(), hri); 214 } 215 216 217 @Test 218 public void testCloseByRegionServer() throws Exception { 219 closeRegionNoZK(); 220 openRegion(HTU, getRS(), hri); 221 } 222 223 @Test 224 public void testMultipleCloseFromMaster() throws Exception { 225 for (int i = 0; i < 10; i++) { 226 AdminProtos.CloseRegionRequest crr = 227 ProtobufUtil.buildCloseRegionRequest(getRS().getServerName(), regionName, null); 228 try { 229 AdminProtos.CloseRegionResponse responseClose = getRS().rpcServices.closeRegion(null, crr); 230 Assert.assertTrue("request " + i + " failed", 231 responseClose.getClosed() || responseClose.hasClosed()); 232 } catch (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException se) { 233 Assert.assertTrue("The next queries may throw an exception.", i > 0); 234 } 235 } 236 237 checkRegionIsClosed(HTU, getRS(), hri); 238 239 openRegion(HTU, getRS(), hri); 240 } 241 242 /** 243 * Test that if we do a close while opening it stops the opening. 244 */ 245 @Test 246 public void testCancelOpeningWithoutZK() throws Exception { 247 // We close 248 closeRegionNoZK(); 249 checkRegionIsClosed(HTU, getRS(), hri); 250 251 // Let do the initial steps, without having a handler 252 getRS().getRegionsInTransitionInRS().put(hri.getEncodedNameAsBytes(), Boolean.TRUE); 253 254 // That's a close without ZK. 255 AdminProtos.CloseRegionRequest crr = 256 ProtobufUtil.buildCloseRegionRequest(getRS().getServerName(), regionName); 257 try { 258 getRS().rpcServices.closeRegion(null, crr); 259 Assert.assertTrue(false); 260 } catch (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException expected) { 261 } 262 263 // The state in RIT should have changed to close 264 Assert.assertEquals(Boolean.FALSE, getRS().getRegionsInTransitionInRS().get( 265 hri.getEncodedNameAsBytes())); 266 267 // Let's start the open handler 268 TableDescriptor htd = getRS().tableDescriptors.get(hri.getTable()); 269 270 getRS().executorService.submit(new OpenRegionHandler(getRS(), getRS(), hri, htd, -1)); 271 272 // The open handler should have removed the region from RIT but kept the region closed 273 checkRegionIsClosed(HTU, getRS(), hri); 274 275 openRegion(HTU, getRS(), hri); 276 } 277 278 /** 279 * Tests an on-the-fly RPC that was scheduled for the earlier RS on the same port 280 * for openRegion. The region server should reject this RPC. (HBASE-9721) 281 */ 282 @Test 283 public void testOpenCloseRegionRPCIntendedForPreviousServer() throws Exception { 284 Assert.assertTrue(getRS().getRegion(regionName).isAvailable()); 285 286 ServerName sn = getRS().getServerName(); 287 ServerName earlierServerName = ServerName.valueOf(sn.getHostname(), sn.getPort(), 1); 288 289 try { 290 CloseRegionRequest request = ProtobufUtil.buildCloseRegionRequest(earlierServerName, regionName); 291 getRS().getRSRpcServices().closeRegion(null, request); 292 Assert.fail("The closeRegion should have been rejected"); 293 } catch (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException se) { 294 Assert.assertTrue(se.getCause() instanceof IOException); 295 Assert.assertTrue(se.getCause().getMessage().contains("This RPC was intended for a different server")); 296 } 297 298 //actual close 299 closeRegionNoZK(); 300 try { 301 AdminProtos.OpenRegionRequest orr = RequestConverter.buildOpenRegionRequest( 302 earlierServerName, hri, null); 303 getRS().getRSRpcServices().openRegion(null, orr); 304 Assert.fail("The openRegion should have been rejected"); 305 } catch (org.apache.hbase.thirdparty.com.google.protobuf.ServiceException se) { 306 Assert.assertTrue(se.getCause() instanceof IOException); 307 Assert.assertTrue(se.getCause().getMessage().contains("This RPC was intended for a different server")); 308 } finally { 309 openRegion(HTU, getRS(), hri); 310 } 311 } 312}