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.client; 019 020import static org.junit.Assert.assertFalse; 021import static org.junit.Assert.assertNull; 022import static org.junit.Assert.assertTrue; 023 024import java.io.IOException; 025import java.util.ArrayList; 026import java.util.Collections; 027import java.util.List; 028import org.apache.hadoop.conf.Configuration; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.HBaseTestingUtil; 031import org.apache.hadoop.hbase.HConstants; 032import org.apache.hadoop.hbase.StartTestingClusterOption; 033import org.apache.hadoop.hbase.master.HMaster; 034import org.apache.hadoop.hbase.master.MasterServices; 035import org.apache.hadoop.hbase.master.assignment.AssignmentManager; 036import org.apache.hadoop.hbase.master.assignment.RegionStateNode; 037import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure; 038import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv; 039import org.apache.hadoop.hbase.master.region.MasterRegion; 040import org.apache.hadoop.hbase.procedure2.Procedure; 041import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException; 042import org.apache.hadoop.hbase.procedure2.ProcedureYieldException; 043import org.apache.hadoop.hbase.testclassification.MediumTests; 044import org.apache.hadoop.hbase.testclassification.MiscTests; 045import org.junit.AfterClass; 046import org.junit.BeforeClass; 047import org.junit.ClassRule; 048import org.junit.Test; 049import org.junit.experimental.categories.Category; 050 051@Category({ MiscTests.class, MediumTests.class }) 052public class TestFailedMetaReplicaAssigment { 053 054 @ClassRule 055 public static final HBaseClassTestRule CLASS_RULE = 056 HBaseClassTestRule.forClass(TestFailedMetaReplicaAssigment.class); 057 058 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 059 060 @BeforeClass 061 public static void setUp() throws Exception { 062 // using our rigged master, to force a failed meta replica assignment when start up master 063 // this test can be removed once we remove the HConstants.META_REPLICAS_NUM config. 064 Configuration conf = TEST_UTIL.getConfiguration(); 065 conf.setInt(HConstants.META_REPLICAS_NUM, 3); 066 StartTestingClusterOption option = 067 StartTestingClusterOption.builder().numAlwaysStandByMasters(1).numMasters(1) 068 .numRegionServers(1).masterClass(BrokenMetaReplicaMaster.class).build(); 069 TEST_UTIL.startMiniCluster(option); 070 } 071 072 @AfterClass 073 public static void tearDown() throws IOException { 074 TEST_UTIL.shutdownMiniCluster(); 075 } 076 077 @Test 078 public void testFailedReplicaAssignment() throws InterruptedException { 079 HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster(); 080 // waiting for master to come up 081 TEST_UTIL.waitFor(30000, () -> master.isInitialized()); 082 083 AssignmentManager am = master.getAssignmentManager(); 084 // showing one of the replicas got assigned 085 RegionInfo metaReplicaHri = 086 RegionReplicaUtil.getRegionInfoForReplica(RegionInfoBuilder.FIRST_META_REGIONINFO, 1); 087 // we use assignAsync so we need to wait a bit 088 TEST_UTIL.waitFor(30000, () -> { 089 RegionStateNode metaReplicaRegionNode = 090 am.getRegionStates().getOrCreateRegionStateNode(metaReplicaHri); 091 return metaReplicaRegionNode.getRegionLocation() != null; 092 }); 093 // showing one of the replicas failed to be assigned 094 RegionInfo metaReplicaHri2 = 095 RegionReplicaUtil.getRegionInfoForReplica(RegionInfoBuilder.FIRST_META_REGIONINFO, 2); 096 RegionStateNode metaReplicaRegionNode2 = 097 am.getRegionStates().getOrCreateRegionStateNode(metaReplicaHri2); 098 // wait for several seconds to make sure that it is not assigned 099 for (int i = 0; i < 3; i++) { 100 Thread.sleep(2000); 101 assertNull(metaReplicaRegionNode2.getRegionLocation()); 102 } 103 104 // showing master is active and running 105 assertFalse(master.isStopping()); 106 assertFalse(master.isStopped()); 107 assertTrue(master.isActiveMaster()); 108 } 109 110 public static class BrokenTransitRegionStateProcedure extends TransitRegionStateProcedure { 111 112 public BrokenTransitRegionStateProcedure() { 113 super(null, null, null, false, TransitionType.ASSIGN); 114 } 115 116 public BrokenTransitRegionStateProcedure(MasterProcedureEnv env, RegionInfo hri) { 117 super(env, hri, null, false, TransitionType.ASSIGN); 118 } 119 120 @Override 121 protected Procedure[] execute(MasterProcedureEnv env) 122 throws ProcedureSuspendedException, ProcedureYieldException, InterruptedException { 123 throw new ProcedureSuspendedException("Never end procedure!"); 124 } 125 } 126 127 public static class BrokenMetaReplicaMaster extends HMaster { 128 public BrokenMetaReplicaMaster(final Configuration conf) throws IOException { 129 super(conf); 130 } 131 132 @Override 133 public AssignmentManager createAssignmentManager(MasterServices master, 134 MasterRegion masterRegion) { 135 return new BrokenMasterMetaAssignmentManager(master, masterRegion); 136 } 137 } 138 139 public static class BrokenMasterMetaAssignmentManager extends AssignmentManager { 140 MasterServices master; 141 142 public BrokenMasterMetaAssignmentManager(final MasterServices master, 143 MasterRegion masterRegion) { 144 super(master, masterRegion); 145 this.master = master; 146 } 147 148 @Override 149 public TransitRegionStateProcedure[] createAssignProcedures(List<RegionInfo> hris) { 150 List<TransitRegionStateProcedure> procs = new ArrayList<>(); 151 for (RegionInfo hri : hris) { 152 if (hri.isMetaRegion() && hri.getReplicaId() == 2) { 153 RegionStateNode regionNode = getRegionStates().getOrCreateRegionStateNode(hri); 154 regionNode.lock(); 155 try { 156 procs.add(regionNode.setProcedure(new BrokenTransitRegionStateProcedure( 157 master.getMasterProcedureExecutor().getEnvironment(), hri))); 158 } finally { 159 regionNode.unlock(); 160 } 161 } else { 162 procs.add(super.createAssignProcedures(Collections.singletonList(hri))[0]); 163 } 164 } 165 return procs.toArray(new TransitRegionStateProcedure[0]); 166 } 167 } 168}