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.HBaseTestingUtility;
031import org.apache.hadoop.hbase.HConstants;
032import org.apache.hadoop.hbase.StartMiniClusterOption;
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 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
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    StartMiniClusterOption option = StartMiniClusterOption.builder().numAlwaysStandByMasters(1)
067      .numMasters(1).numRegionServers(1).masterClass(BrokenMetaReplicaMaster.class).build();
068    TEST_UTIL.startMiniCluster(option);
069  }
070
071  @AfterClass
072  public static void tearDown() throws IOException {
073    TEST_UTIL.shutdownMiniCluster();
074  }
075
076  @Test
077  public void testFailedReplicaAssignment() throws InterruptedException {
078    HMaster master = TEST_UTIL.getMiniHBaseCluster().getMaster();
079    // waiting for master to come up
080    TEST_UTIL.waitFor(30000, () -> master.isInitialized());
081
082    AssignmentManager am = master.getAssignmentManager();
083    // showing one of the replicas got assigned
084    RegionInfo metaReplicaHri =
085      RegionReplicaUtil.getRegionInfoForReplica(RegionInfoBuilder.FIRST_META_REGIONINFO, 1);
086    // we use assignAsync so we need to wait a bit
087    TEST_UTIL.waitFor(30000, () -> {
088      RegionStateNode metaReplicaRegionNode =
089        am.getRegionStates().getOrCreateRegionStateNode(metaReplicaHri);
090      return metaReplicaRegionNode.getRegionLocation() != null;
091    });
092    // showing one of the replicas failed to be assigned
093    RegionInfo metaReplicaHri2 =
094      RegionReplicaUtil.getRegionInfoForReplica(RegionInfoBuilder.FIRST_META_REGIONINFO, 2);
095    RegionStateNode metaReplicaRegionNode2 =
096      am.getRegionStates().getOrCreateRegionStateNode(metaReplicaHri2);
097    // wait for several seconds to make sure that it is not assigned
098    for (int i = 0; i < 3; i++) {
099      Thread.sleep(2000);
100      assertNull(metaReplicaRegionNode2.getRegionLocation());
101    }
102
103    // showing master is active and running
104    assertFalse(master.isStopping());
105    assertFalse(master.isStopped());
106    assertTrue(master.isActiveMaster());
107  }
108
109  public static class BrokenTransitRegionStateProcedure extends TransitRegionStateProcedure {
110
111    public BrokenTransitRegionStateProcedure() {
112      super(null, null, null, false, TransitionType.ASSIGN);
113    }
114
115    public BrokenTransitRegionStateProcedure(MasterProcedureEnv env, RegionInfo hri) {
116      super(env, hri, null, false, TransitionType.ASSIGN);
117    }
118
119    @Override
120    protected Procedure[] execute(MasterProcedureEnv env)
121      throws ProcedureSuspendedException, ProcedureYieldException, InterruptedException {
122      throw new ProcedureSuspendedException("Never end procedure!");
123    }
124  }
125
126  public static class BrokenMetaReplicaMaster extends HMaster {
127    public BrokenMetaReplicaMaster(final Configuration conf) throws IOException {
128      super(conf);
129    }
130
131    @Override
132    public AssignmentManager createAssignmentManager(MasterServices master,
133      MasterRegion masterRegion) {
134      return new BrokenMasterMetaAssignmentManager(master, masterRegion);
135    }
136  }
137
138  public static class BrokenMasterMetaAssignmentManager extends AssignmentManager {
139    MasterServices master;
140
141    public BrokenMasterMetaAssignmentManager(final MasterServices master,
142      MasterRegion masterRegion) {
143      super(master, masterRegion);
144      this.master = master;
145    }
146
147    @Override
148    public TransitRegionStateProcedure[] createAssignProcedures(List<RegionInfo> hris) {
149      List<TransitRegionStateProcedure> procs = new ArrayList<>();
150      for (RegionInfo hri : hris) {
151        if (hri.isMetaRegion() && hri.getReplicaId() == 2) {
152          RegionStateNode regionNode = getRegionStates().getOrCreateRegionStateNode(hri);
153          regionNode.lock();
154          try {
155            procs.add(regionNode.setProcedure(new BrokenTransitRegionStateProcedure(
156              master.getMasterProcedureExecutor().getEnvironment(), hri)));
157          } finally {
158            regionNode.unlock();
159          }
160        } else {
161          procs.add(super.createAssignProcedures(Collections.singletonList(hri))[0]);
162        }
163      }
164      return procs.toArray(new TransitRegionStateProcedure[0]);
165    }
166  }
167}