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.master; 019 020import static org.junit.Assert.assertFalse; 021import static org.junit.Assert.assertNotNull; 022import static org.junit.Assert.assertTrue; 023 024import java.io.IOException; 025import java.util.ArrayList; 026import java.util.List; 027import java.util.concurrent.ExecutionException; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.hbase.CatalogFamilyFormat; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtility; 032import org.apache.hadoop.hbase.HRegionLocation; 033import org.apache.hadoop.hbase.MetaMockingUtil; 034import org.apache.hadoop.hbase.MetaTableAccessor; 035import org.apache.hadoop.hbase.TableName; 036import org.apache.hadoop.hbase.client.Admin; 037import org.apache.hadoop.hbase.client.Connection; 038import org.apache.hadoop.hbase.client.ConnectionFactory; 039import org.apache.hadoop.hbase.client.Get; 040import org.apache.hadoop.hbase.client.RegionInfo; 041import org.apache.hadoop.hbase.client.RegionLocator; 042import org.apache.hadoop.hbase.client.Result; 043import org.apache.hadoop.hbase.client.Table; 044import org.apache.hadoop.hbase.master.assignment.AssignmentManager; 045import org.apache.hadoop.hbase.testclassification.MasterTests; 046import org.apache.hadoop.hbase.testclassification.MediumTests; 047import org.apache.hadoop.hbase.util.Bytes; 048import org.apache.hadoop.hbase.util.PairOfSameType; 049import org.apache.hadoop.hbase.util.Threads; 050import org.junit.AfterClass; 051import org.junit.BeforeClass; 052import org.junit.ClassRule; 053import org.junit.Rule; 054import org.junit.Test; 055import org.junit.experimental.categories.Category; 056import org.junit.rules.TestName; 057import org.slf4j.Logger; 058import org.slf4j.LoggerFactory; 059 060@Category({MasterTests.class, MediumTests.class}) 061public class TestCatalogJanitorInMemoryStates { 062 063 @ClassRule 064 public static final HBaseClassTestRule CLASS_RULE = 065 HBaseClassTestRule.forClass(TestCatalogJanitorInMemoryStates.class); 066 067 private static final Logger LOG = LoggerFactory.getLogger(TestCatalogJanitorInMemoryStates.class); 068 @Rule public final TestName name = new TestName(); 069 protected final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 070 private static byte [] ROW = Bytes.toBytes("testRow"); 071 private static byte [] FAMILY = Bytes.toBytes("testFamily"); 072 private static byte [] QUALIFIER = Bytes.toBytes("testQualifier"); 073 private static byte [] VALUE = Bytes.toBytes("testValue"); 074 075 /** 076 * @throws java.lang.Exception 077 */ 078 @BeforeClass 079 public static void setUpBeforeClass() throws Exception { 080 Configuration conf = TEST_UTIL.getConfiguration(); 081 TEST_UTIL.startMiniCluster(1); 082 } 083 084 /** 085 * @throws java.lang.Exception 086 */ 087 @AfterClass 088 public static void tearDownAfterClass() throws Exception { 089 TEST_UTIL.shutdownMiniCluster(); 090 } 091 092 /** 093 * Test clearing a split parent from memory. 094 */ 095 @Test 096 public void testInMemoryParentCleanup() 097 throws IOException, InterruptedException, ExecutionException { 098 final AssignmentManager am = TEST_UTIL.getHBaseCluster().getMaster().getAssignmentManager(); 099 final ServerManager sm = TEST_UTIL.getHBaseCluster().getMaster().getServerManager(); 100 final CatalogJanitor janitor = TEST_UTIL.getHBaseCluster().getMaster().getCatalogJanitor(); 101 102 Admin admin = TEST_UTIL.getAdmin(); 103 admin.catalogJanitorSwitch(false); 104 105 final TableName tableName = TableName.valueOf(name.getMethodName()); 106 Table t = TEST_UTIL.createTable(tableName, FAMILY); 107 int rowCount = TEST_UTIL.loadTable(t, FAMILY, false); 108 109 RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName); 110 List<HRegionLocation> allRegionLocations = locator.getAllRegionLocations(); 111 112 // We need to create a valid split with daughter regions 113 HRegionLocation parent = allRegionLocations.get(0); 114 List<HRegionLocation> daughters = splitRegion(parent.getRegion()); 115 LOG.info("Parent region: " + parent); 116 LOG.info("Daughter regions: " + daughters); 117 assertNotNull("Should have found daughter regions for " + parent, daughters); 118 119 assertTrue("Parent region should exist in RegionStates", 120 am.getRegionStates().isRegionInRegionStates(parent.getRegion())); 121 assertTrue("Parent region should exist in ServerManager", 122 sm.isRegionInServerManagerStates(parent.getRegion())); 123 124 // clean the parent 125 Result r = MetaMockingUtil.getMetaTableRowResult(parent.getRegion(), null, 126 daughters.get(0).getRegion(), daughters.get(1).getRegion()); 127 janitor.cleanParent(parent.getRegion(), r); 128 assertFalse("Parent region should have been removed from RegionStates", 129 am.getRegionStates().isRegionInRegionStates(parent.getRegion())); 130 assertFalse("Parent region should have been removed from ServerManager", 131 sm.isRegionInServerManagerStates(parent.getRegion())); 132 133 } 134 135 /** 136 * Splits a region 137 * @param r Region to split. 138 * @return List of region locations 139 */ 140 private List<HRegionLocation> splitRegion(final RegionInfo r) 141 throws IOException, InterruptedException, ExecutionException { 142 List<HRegionLocation> locations = new ArrayList<>(); 143 // Split this table in two. 144 Admin admin = TEST_UTIL.getAdmin(); 145 Connection connection = TEST_UTIL.getConnection(); 146 admin.splitRegionAsync(r.getEncodedNameAsBytes()).get(); 147 admin.close(); 148 PairOfSameType<RegionInfo> regions = waitOnDaughters(r); 149 if (regions != null) { 150 try (RegionLocator rl = connection.getRegionLocator(r.getTable())) { 151 locations.add(rl.getRegionLocation(regions.getFirst().getEncodedNameAsBytes())); 152 locations.add(rl.getRegionLocation(regions.getSecond().getEncodedNameAsBytes())); 153 } 154 return locations; 155 } 156 return locations; 157 } 158 159 /* 160 * Wait on region split. May return because we waited long enough on the split 161 * and it didn't happen. Caller should check. 162 * @param r 163 * @return Daughter regions; caller needs to check table actually split. 164 */ 165 private PairOfSameType<RegionInfo> waitOnDaughters(final RegionInfo r) 166 throws IOException { 167 long start = System.currentTimeMillis(); 168 PairOfSameType<RegionInfo> pair = null; 169 try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration()); 170 Table metaTable = conn.getTable(TableName.META_TABLE_NAME)) { 171 Result result = null; 172 RegionInfo region = null; 173 while ((System.currentTimeMillis() - start) < 60000) { 174 result = metaTable.get(new Get(r.getRegionName())); 175 if (result == null) { 176 break; 177 } 178 region = CatalogFamilyFormat.getRegionInfo(result); 179 if (region.isSplitParent()) { 180 LOG.debug(region.toString() + " IS a parent!"); 181 pair = MetaTableAccessor.getDaughterRegions(result); 182 break; 183 } 184 Threads.sleep(100); 185 } 186 187 if (pair.getFirst() == null || pair.getSecond() == null) { 188 throw new IOException("Failed to get daughters, for parent region: " + r); 189 } 190 return pair; 191 } 192 } 193}