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.concurrent.Future; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.hbase.HBaseClassTestRule; 028import org.apache.hadoop.hbase.HBaseTestingUtility; 029import org.apache.hadoop.hbase.HConstants; 030import org.apache.hadoop.hbase.ServerName; 031import org.apache.hadoop.hbase.StartMiniClusterOption; 032import org.apache.hadoop.hbase.master.HMaster; 033import org.apache.hadoop.hbase.master.MasterServices; 034import org.apache.hadoop.hbase.master.assignment.AssignmentManager; 035import org.apache.hadoop.hbase.master.assignment.RegionStateNode; 036import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure; 037import org.apache.hadoop.hbase.testclassification.MediumTests; 038import org.apache.hadoop.hbase.testclassification.MiscTests; 039import org.junit.AfterClass; 040import org.junit.BeforeClass; 041import org.junit.ClassRule; 042import org.junit.Test; 043import org.junit.experimental.categories.Category; 044 045@Category({ MiscTests.class, MediumTests.class }) 046public class TestFailedMetaReplicaAssigment { 047 048 @ClassRule 049 public static final HBaseClassTestRule CLASS_RULE = 050 HBaseClassTestRule.forClass(TestFailedMetaReplicaAssigment.class); 051 052 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); 053 054 @BeforeClass 055 public static void setUp() throws Exception { 056 // using our rigged master, to force a failed meta replica assignment 057 Configuration conf = TEST_UTIL.getConfiguration(); 058 conf.setInt(HConstants.META_REPLICAS_NUM, 3); 059 StartMiniClusterOption option = StartMiniClusterOption.builder().numAlwaysStandByMasters(1) 060 .numMasters(1).numRegionServers(1).masterClass(BrokenMetaReplicaMaster.class).build(); 061 TEST_UTIL.startMiniCluster(option); 062 } 063 064 @AfterClass 065 public static void tearDown() throws IOException { 066 TEST_UTIL.shutdownMiniCluster(); 067 } 068 069 @Test 070 public void testFailedReplicaAssignment() throws InterruptedException { 071 HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster(); 072 // waiting for master to come up 073 TEST_UTIL.waitFor(30000, () -> master.isInitialized()); 074 075 AssignmentManager am = master.getAssignmentManager(); 076 // showing one of the replicas got assigned 077 RegionInfo metaReplicaHri = 078 RegionReplicaUtil.getRegionInfoForReplica(RegionInfoBuilder.FIRST_META_REGIONINFO, 1); 079 // we use assignAsync so we need to wait a bit 080 TEST_UTIL.waitFor(30000, () -> { 081 RegionStateNode metaReplicaRegionNode = 082 am.getRegionStates().getOrCreateRegionStateNode(metaReplicaHri); 083 return metaReplicaRegionNode.getRegionLocation() != null; 084 }); 085 // showing one of the replicas failed to be assigned 086 RegionInfo metaReplicaHri2 = 087 RegionReplicaUtil.getRegionInfoForReplica(RegionInfoBuilder.FIRST_META_REGIONINFO, 2); 088 RegionStateNode metaReplicaRegionNode2 = 089 am.getRegionStates().getOrCreateRegionStateNode(metaReplicaHri2); 090 // wait for several seconds to make sure that it is not assigned 091 for (int i = 0; i < 3; i++) { 092 Thread.sleep(2000); 093 assertNull(metaReplicaRegionNode2.getRegionLocation()); 094 } 095 096 // showing master is active and running 097 assertFalse(master.isStopping()); 098 assertFalse(master.isStopped()); 099 assertTrue(master.isActiveMaster()); 100 } 101 102 public static class BrokenTransitRegionStateProcedure extends TransitRegionStateProcedure { 103 protected BrokenTransitRegionStateProcedure() { 104 super(null, null, null, false, TransitionType.ASSIGN); 105 } 106 } 107 108 public static class BrokenMetaReplicaMaster extends HMaster { 109 public BrokenMetaReplicaMaster(final Configuration conf) throws IOException { 110 super(conf); 111 } 112 113 @Override 114 public AssignmentManager createAssignmentManager(MasterServices master) { 115 return new BrokenMasterMetaAssignmentManager(master); 116 } 117 } 118 119 public static class BrokenMasterMetaAssignmentManager extends AssignmentManager { 120 MasterServices master; 121 122 public BrokenMasterMetaAssignmentManager(final MasterServices master) { 123 super(master); 124 this.master = master; 125 } 126 127 public Future<byte[]> assignAsync(RegionInfo regionInfo, ServerName sn) throws IOException { 128 RegionStateNode regionNode = getRegionStates().getOrCreateRegionStateNode(regionInfo); 129 if (regionNode.getRegionInfo().getReplicaId() == 2) { 130 regionNode.setProcedure(new BrokenTransitRegionStateProcedure()); 131 } 132 return super.assignAsync(regionInfo, sn); 133 } 134 } 135}