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 java.util.Optional;
021import java.util.concurrent.CountDownLatch;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseTestingUtility;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.RegionInfo;
026import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
027import org.apache.hadoop.hbase.coprocessor.ObserverContext;
028import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
029import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
030import org.apache.hadoop.hbase.coprocessor.RegionObserver;
031import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
032import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
033import org.apache.hadoop.hbase.regionserver.HRegionServer;
034import org.apache.hadoop.hbase.testclassification.MasterTests;
035import org.apache.hadoop.hbase.testclassification.MediumTests;
036import org.apache.hadoop.hbase.util.Bytes;
037import org.apache.hadoop.hbase.util.JVMClusterUtil;
038import org.junit.AfterClass;
039import org.junit.Assert;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044
045@Category({ MasterTests.class, MediumTests.class })
046public class TestExceptionInAssignRegion {
047
048  @ClassRule
049  public static final HBaseClassTestRule CLASS_RULE =
050    HBaseClassTestRule.forClass(TestExceptionInAssignRegion.class);
051
052  private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
053
054  private static final TableName TABLE_NAME = TableName.valueOf("test");
055
056  private static final CountDownLatch countDownLatch = new CountDownLatch(2);
057
058  private static final byte[] CF = Bytes.toBytes("cf");
059
060  @BeforeClass
061  public static void setUp() throws Exception {
062    UTIL.getConfiguration().setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
063      ThrowInOpenCP.class.getName());
064    UTIL.startMiniCluster(3);
065    UTIL.getAdmin().balancerSwitch(false, true);
066    UTIL.createTable(TABLE_NAME, CF);
067    UTIL.waitTableAvailable(TABLE_NAME);
068  }
069
070  @AfterClass
071  public static void tearDown() throws Exception {
072    UTIL.shutdownMiniCluster();
073  }
074
075  @Test
076  public void testExceptionInAssignRegion() {
077    ProcedureExecutor procedureExecutor =
078      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor();
079
080    JVMClusterUtil.RegionServerThread rsThread = null;
081    for (JVMClusterUtil.RegionServerThread t : UTIL.getMiniHBaseCluster()
082      .getRegionServerThreads()) {
083      if (!t.getRegionServer().getRegions(TABLE_NAME).isEmpty()) {
084        rsThread = t;
085        break;
086      }
087    }
088    // find the rs and hri of the table
089    HRegionServer rs = rsThread.getRegionServer();
090    RegionInfo hri = rs.getRegions(TABLE_NAME).get(0).getRegionInfo();
091    TransitRegionStateProcedure assignRegionProcedure = TransitRegionStateProcedure.move(
092      UTIL.getMiniHBaseCluster().getMaster().getMasterProcedureExecutor().getEnvironment(), hri,
093      null);
094    RegionStateNode regionNode = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager()
095      .getRegionStates().getOrCreateRegionStateNode(hri);
096    regionNode.setProcedure(assignRegionProcedure);
097    countDownLatch.countDown();
098    long prodId = procedureExecutor.submitProcedure(assignRegionProcedure);
099    ProcedureTestingUtility.waitProcedure(procedureExecutor, prodId);
100
101    Assert.assertEquals("Should be two RS since other is aborted", 2,
102      UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size());
103    Assert.assertNull("RIT Map doesn't have correct value",
104      getRegionServer(0).getRegionsInTransitionInRS().get(hri.getEncodedNameAsBytes()));
105    Assert.assertNull("RIT Map doesn't have correct value",
106      getRegionServer(1).getRegionsInTransitionInRS().get(hri.getEncodedNameAsBytes()));
107    Assert.assertNull("RIT Map doesn't have correct value",
108      getRegionServer(2).getRegionsInTransitionInRS().get(hri.getEncodedNameAsBytes()));
109  }
110
111  private HRegionServer getRegionServer(int index) {
112    return UTIL.getMiniHBaseCluster().getRegionServer(index);
113  }
114
115  public static class ThrowInOpenCP implements RegionCoprocessor, RegionObserver {
116    @Override
117    public void preOpen(ObserverContext<RegionCoprocessorEnvironment> c) {
118      if (countDownLatch.getCount() == 1) {
119        // We want to throw exception only first time in move region call
120        // After that RS aborts and we don't want to throw in any other open region
121        countDownLatch.countDown();
122        throw new RuntimeException();
123      }
124    }
125
126    @Override
127    public Optional<RegionObserver> getRegionObserver() {
128      return Optional.of(this);
129    }
130  }
131}