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