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.assignment; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertNull; 022import static org.junit.Assert.assertTrue; 023 024import java.util.concurrent.Executors; 025import java.util.concurrent.Future; 026import org.apache.hadoop.hbase.HBaseClassTestRule; 027import org.apache.hadoop.hbase.HBaseTestingUtility; 028import org.apache.hadoop.hbase.MetaTableAccessor; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.client.RegionInfo; 031import org.apache.hadoop.hbase.client.RegionInfoBuilder; 032import org.apache.hadoop.hbase.master.RegionState.State; 033import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility; 034import org.apache.hadoop.hbase.procedure2.util.StringUtils; 035import org.apache.hadoop.hbase.testclassification.LargeTests; 036import org.apache.hadoop.hbase.testclassification.MasterTests; 037import org.junit.ClassRule; 038import org.junit.Test; 039import org.junit.experimental.categories.Category; 040import org.slf4j.Logger; 041import org.slf4j.LoggerFactory; 042 043@Category({ MasterTests.class, LargeTests.class }) 044public class TestAssignmentManager extends TestAssignmentManagerBase { 045 046 @ClassRule 047 public static final HBaseClassTestRule CLASS_RULE = 048 HBaseClassTestRule.forClass(TestAssignmentManager.class); 049 050 private static final Logger LOG = LoggerFactory.getLogger(TestAssignmentManager.class); 051 052 @Test 053 public void testAssignWithGoodExec() throws Exception { 054 // collect AM metrics before test 055 collectAssignmentManagerMetrics(); 056 057 testAssign(new GoodRsExecutor()); 058 059 assertEquals(assignSubmittedCount + NREGIONS, 060 assignProcMetrics.getSubmittedCounter().getCount()); 061 assertEquals(assignFailedCount, assignProcMetrics.getFailedCounter().getCount()); 062 } 063 064 @Test 065 public void testAssignAndCrashBeforeResponse() throws Exception { 066 TableName tableName = TableName.valueOf("testAssignAndCrashBeforeResponse"); 067 RegionInfo hri = createRegionInfo(tableName, 1); 068 rsDispatcher.setMockRsExecutor(new HangThenRSCrashExecutor()); 069 TransitRegionStateProcedure proc = createAssignProcedure(hri); 070 waitOnFuture(submitProcedure(proc)); 071 } 072 073 @Test 074 public void testUnassignAndCrashBeforeResponse() throws Exception { 075 TableName tableName = TableName.valueOf("testAssignAndCrashBeforeResponse"); 076 RegionInfo hri = createRegionInfo(tableName, 1); 077 rsDispatcher.setMockRsExecutor(new HangOnCloseThenRSCrashExecutor()); 078 for (int i = 0; i < HangOnCloseThenRSCrashExecutor.TYPES_OF_FAILURE; i++) { 079 TransitRegionStateProcedure assign = createAssignProcedure(hri); 080 waitOnFuture(submitProcedure(assign)); 081 TransitRegionStateProcedure unassign = createUnassignProcedure(hri); 082 waitOnFuture(submitProcedure(unassign)); 083 } 084 } 085 086 @Test 087 public void testAssignSocketTimeout() throws Exception { 088 TableName tableName = TableName.valueOf(this.name.getMethodName()); 089 RegionInfo hri = createRegionInfo(tableName, 1); 090 091 // collect AM metrics before test 092 collectAssignmentManagerMetrics(); 093 094 rsDispatcher.setMockRsExecutor(new SocketTimeoutRsExecutor(20)); 095 waitOnFuture(submitProcedure(createAssignProcedure(hri))); 096 097 // we crashed a rs, so it is possible that there are other regions on the rs which will also be 098 // reassigned, so here we just assert greater than, not the exact number. 099 assertTrue(assignProcMetrics.getSubmittedCounter().getCount() > assignSubmittedCount); 100 assertEquals(assignFailedCount, assignProcMetrics.getFailedCounter().getCount()); 101 } 102 103 @Test 104 public void testAssignQueueFullOnce() throws Exception { 105 TableName tableName = TableName.valueOf(this.name.getMethodName()); 106 RegionInfo hri = createRegionInfo(tableName, 1); 107 108 // collect AM metrics before test 109 collectAssignmentManagerMetrics(); 110 111 rsDispatcher.setMockRsExecutor(new CallQueueTooBigOnceRsExecutor()); 112 waitOnFuture(submitProcedure(createAssignProcedure(hri))); 113 114 assertEquals(assignSubmittedCount + 1, assignProcMetrics.getSubmittedCounter().getCount()); 115 assertEquals(assignFailedCount, assignProcMetrics.getFailedCounter().getCount()); 116 } 117 118 @Test 119 public void testTimeoutThenQueueFull() throws Exception { 120 TableName tableName = TableName.valueOf(this.name.getMethodName()); 121 RegionInfo hri = createRegionInfo(tableName, 1); 122 123 // collect AM metrics before test 124 collectAssignmentManagerMetrics(); 125 126 rsDispatcher.setMockRsExecutor(new TimeoutThenCallQueueTooBigRsExecutor(10)); 127 waitOnFuture(submitProcedure(createAssignProcedure(hri))); 128 rsDispatcher.setMockRsExecutor(new TimeoutThenCallQueueTooBigRsExecutor(15)); 129 waitOnFuture(submitProcedure(createUnassignProcedure(hri))); 130 131 assertEquals(assignSubmittedCount + 1, assignProcMetrics.getSubmittedCounter().getCount()); 132 assertEquals(assignFailedCount, assignProcMetrics.getFailedCounter().getCount()); 133 assertEquals(unassignSubmittedCount + 1, unassignProcMetrics.getSubmittedCounter().getCount()); 134 assertEquals(unassignFailedCount, unassignProcMetrics.getFailedCounter().getCount()); 135 } 136 137 private void testAssign(final MockRSExecutor executor) throws Exception { 138 testAssign(executor, NREGIONS); 139 } 140 141 private void testAssign(MockRSExecutor executor, int nRegions) throws Exception { 142 rsDispatcher.setMockRsExecutor(executor); 143 144 TransitRegionStateProcedure[] assignments = new TransitRegionStateProcedure[nRegions]; 145 146 long st = System.currentTimeMillis(); 147 bulkSubmit(assignments); 148 149 for (int i = 0; i < assignments.length; ++i) { 150 ProcedureTestingUtility.waitProcedure(master.getMasterProcedureExecutor(), assignments[i]); 151 assertTrue(assignments[i].toString(), assignments[i].isSuccess()); 152 } 153 long et = System.currentTimeMillis(); 154 float sec = ((et - st) / 1000.0f); 155 LOG.info(String.format("[T] Assigning %dprocs in %s (%.2fproc/sec)", assignments.length, 156 StringUtils.humanTimeDiff(et - st), assignments.length / sec)); 157 } 158 159 @Test 160 public void testAssignAnAssignedRegion() throws Exception { 161 final TableName tableName = TableName.valueOf("testAssignAnAssignedRegion"); 162 final RegionInfo hri = createRegionInfo(tableName, 1); 163 164 // collect AM metrics before test 165 collectAssignmentManagerMetrics(); 166 167 rsDispatcher.setMockRsExecutor(new GoodRsExecutor()); 168 169 Future<byte[]> futureA = submitProcedure(createAssignProcedure(hri)); 170 171 // wait first assign 172 waitOnFuture(futureA); 173 am.getRegionStates().isRegionInState(hri, State.OPEN); 174 // Second should be a noop. We should recognize region is already OPEN internally 175 // and skip out doing nothing. 176 // wait second assign 177 Future<byte[]> futureB = submitProcedure(createAssignProcedure(hri)); 178 waitOnFuture(futureB); 179 am.getRegionStates().isRegionInState(hri, State.OPEN); 180 // TODO: What else can we do to ensure just a noop. 181 182 // TODO: Though second assign is noop, it's considered success, can noop be handled in a 183 // better way? 184 assertEquals(assignSubmittedCount + 2, assignProcMetrics.getSubmittedCounter().getCount()); 185 assertEquals(assignFailedCount, assignProcMetrics.getFailedCounter().getCount()); 186 } 187 188 @Test 189 public void testUnassignAnUnassignedRegion() throws Exception { 190 final TableName tableName = TableName.valueOf("testUnassignAnUnassignedRegion"); 191 final RegionInfo hri = createRegionInfo(tableName, 1); 192 193 // collect AM metrics before test 194 collectAssignmentManagerMetrics(); 195 196 rsDispatcher.setMockRsExecutor(new GoodRsExecutor()); 197 198 // assign the region first 199 waitOnFuture(submitProcedure(createAssignProcedure(hri))); 200 201 final Future<byte[]> futureA = submitProcedure(createUnassignProcedure(hri)); 202 203 // Wait first unassign. 204 waitOnFuture(futureA); 205 am.getRegionStates().isRegionInState(hri, State.CLOSED); 206 // Second should be a noop. We should recognize region is already CLOSED internally 207 // and skip out doing nothing. 208 final Future<byte[]> futureB = submitProcedure(createUnassignProcedure(hri)); 209 waitOnFuture(futureB); 210 // Ensure we are still CLOSED. 211 am.getRegionStates().isRegionInState(hri, State.CLOSED); 212 // TODO: What else can we do to ensure just a noop. 213 214 assertEquals(assignSubmittedCount + 1, assignProcMetrics.getSubmittedCounter().getCount()); 215 assertEquals(assignFailedCount, assignProcMetrics.getFailedCounter().getCount()); 216 // TODO: Though second unassign is noop, it's considered success, can noop be handled in a 217 // better way? 218 assertEquals(unassignSubmittedCount + 2, unassignProcMetrics.getSubmittedCounter().getCount()); 219 assertEquals(unassignFailedCount, unassignProcMetrics.getFailedCounter().getCount()); 220 } 221 222 /** 223 * It is possible that when AM send assign meta request to a RS successfully, but RS can not send 224 * back any response, which cause master startup hangs forever 225 */ 226 @Test 227 public void testAssignMetaAndCrashBeforeResponse() throws Exception { 228 tearDown(); 229 // See setUp(), start HBase until set up meta 230 util = new HBaseTestingUtility(); 231 this.executor = Executors.newSingleThreadScheduledExecutor(); 232 setupConfiguration(util.getConfiguration()); 233 master = new MockMasterServices(util.getConfiguration(), this.regionsToRegionServers); 234 rsDispatcher = new MockRSProcedureDispatcher(master); 235 master.start(NSERVERS, rsDispatcher); 236 am = master.getAssignmentManager(); 237 238 // Assign meta 239 rsDispatcher.setMockRsExecutor(new HangThenRSRestartExecutor()); 240 am.assign(RegionInfoBuilder.FIRST_META_REGIONINFO); 241 assertEquals(true, am.isMetaAssigned()); 242 243 // set it back as default, see setUpMeta() 244 am.wakeMetaLoadedEvent(); 245 } 246 247 private void assertCloseThenOpen() { 248 assertEquals(closeSubmittedCount + 1, closeProcMetrics.getSubmittedCounter().getCount()); 249 assertEquals(closeFailedCount, closeProcMetrics.getFailedCounter().getCount()); 250 assertEquals(openSubmittedCount + 1, openProcMetrics.getSubmittedCounter().getCount()); 251 assertEquals(openFailedCount, openProcMetrics.getFailedCounter().getCount()); 252 } 253 254 @Test 255 public void testMove() throws Exception { 256 TableName tableName = TableName.valueOf("testMove"); 257 RegionInfo hri = createRegionInfo(tableName, 1); 258 rsDispatcher.setMockRsExecutor(new GoodRsExecutor()); 259 am.assign(hri); 260 261 // collect AM metrics before test 262 collectAssignmentManagerMetrics(); 263 264 am.move(hri); 265 266 assertEquals(moveSubmittedCount + 1, moveProcMetrics.getSubmittedCounter().getCount()); 267 assertEquals(moveFailedCount, moveProcMetrics.getFailedCounter().getCount()); 268 assertCloseThenOpen(); 269 } 270 271 @Test 272 public void testReopen() throws Exception { 273 TableName tableName = TableName.valueOf("testReopen"); 274 RegionInfo hri = createRegionInfo(tableName, 1); 275 rsDispatcher.setMockRsExecutor(new GoodRsExecutor()); 276 am.assign(hri); 277 278 // collect AM metrics before test 279 collectAssignmentManagerMetrics(); 280 281 TransitRegionStateProcedure proc = 282 TransitRegionStateProcedure.reopen(master.getMasterProcedureExecutor().getEnvironment(), hri); 283 am.getRegionStates().getRegionStateNode(hri).setProcedure(proc); 284 waitOnFuture(submitProcedure(proc)); 285 286 assertEquals(reopenSubmittedCount + 1, reopenProcMetrics.getSubmittedCounter().getCount()); 287 assertEquals(reopenFailedCount, reopenProcMetrics.getFailedCounter().getCount()); 288 assertCloseThenOpen(); 289 } 290 291 @Test 292 public void testLoadRegionFromMetaAfterRegionManuallyAdded() throws Exception { 293 try { 294 this.util.startMiniCluster(); 295 final AssignmentManager am = this.util.getHBaseCluster().getMaster().getAssignmentManager(); 296 final TableName tableName = TableName. 297 valueOf("testLoadRegionFromMetaAfterRegionManuallyAdded"); 298 this.util.createTable(tableName, "f"); 299 RegionInfo hri = createRegionInfo(tableName, 1); 300 assertNull("RegionInfo was just instantiated by the test, but " 301 + "shouldn't be in AM regionStates yet.", am.getRegionStates().getRegionState(hri)); 302 MetaTableAccessor.addRegionToMeta(this.util.getConnection(), hri); 303 assertNull("RegionInfo was manually added in META, but " 304 + "shouldn't be in AM regionStates yet.", am.getRegionStates().getRegionState(hri)); 305 hri = am.loadRegionFromMeta(hri.getEncodedName()); 306 assertEquals(hri.getEncodedName(), 307 am.getRegionStates().getRegionState(hri).getRegion().getEncodedName()); 308 }finally { 309 this.util.killMiniHBaseCluster(); 310 } 311 } 312 313 @Test 314 public void testLoadRegionFromMetaRegionNotInMeta() throws Exception { 315 try { 316 this.util.startMiniCluster(); 317 final AssignmentManager am = this.util.getHBaseCluster().getMaster().getAssignmentManager(); 318 final TableName tableName = TableName.valueOf("testLoadRegionFromMetaRegionNotInMeta"); 319 this.util.createTable(tableName, "f"); 320 final RegionInfo hri = createRegionInfo(tableName, 1); 321 assertNull("RegionInfo was just instantiated by the test, but " 322 + "shouldn't be in AM regionStates yet.", am.getRegionStates().getRegionState(hri)); 323 assertNull("RegionInfo was never added in META, should had returned null.", 324 am.loadRegionFromMeta(hri.getEncodedName())); 325 }finally { 326 this.util.killMiniHBaseCluster(); 327 } 328 } 329}