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.procedure;
019
020import static org.hamcrest.MatcherAssert.assertThat;
021import static org.hamcrest.Matchers.everyItem;
022import static org.junit.Assert.assertEquals;
023import static org.junit.Assert.assertFalse;
024
025import org.apache.hadoop.hbase.HBaseClassTestRule;
026import org.apache.hadoop.hbase.HBaseTestingUtil;
027import org.apache.hadoop.hbase.ServerName;
028import org.apache.hadoop.hbase.TableName;
029import org.apache.hadoop.hbase.client.RegionInfo;
030import org.apache.hadoop.hbase.master.HMaster;
031import org.apache.hadoop.hbase.master.assignment.AssignmentManager;
032import org.apache.hadoop.hbase.master.assignment.RegionStateNode;
033import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure;
034import org.apache.hadoop.hbase.procedure2.Procedure;
035import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
036import org.apache.hadoop.hbase.testclassification.MasterTests;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.hamcrest.BaseMatcher;
040import org.hamcrest.Description;
041import org.hamcrest.Matcher;
042import org.junit.AfterClass;
043import org.junit.BeforeClass;
044import org.junit.ClassRule;
045import org.junit.Test;
046import org.junit.experimental.categories.Category;
047
048import org.apache.hbase.thirdparty.com.google.common.collect.Iterables;
049
050/**
051 * Testcase for HBASE-28240.
052 */
053@Category({ MasterTests.class, MediumTests.class })
054public class TestSuspendTRSPWhenHoldingRegionStateNodeLock {
055
056  @ClassRule
057  public static final HBaseClassTestRule CLASS_RULE =
058    HBaseClassTestRule.forClass(TestSuspendTRSPWhenHoldingRegionStateNodeLock.class);
059
060  private static final HBaseTestingUtil HBTU = new HBaseTestingUtil();
061
062  private static TableName TABLE_NAME = TableName.valueOf("test");
063
064  private static byte[] FAMILY = Bytes.toBytes("family");
065
066  @BeforeClass
067  public static void setUp() throws Exception {
068    HBTU.startMiniCluster(2);
069    HBTU.createTable(TABLE_NAME, FAMILY);
070    HBTU.waitTableAvailable(TABLE_NAME);
071    HBTU.getAdmin().balancerSwitch(false, true);
072    HBTU.waitUntilNoRegionsInTransition();
073  }
074
075  @AfterClass
076  public static void tearDown() throws Exception {
077    HBTU.shutdownMiniCluster();
078  }
079
080  private <T> Matcher<Procedure<T>> notChildOf(long procId) {
081    return new BaseMatcher<Procedure<T>>() {
082
083      @Override
084      public boolean matches(Object item) {
085        if (!(item instanceof Procedure)) {
086          return false;
087        }
088        Procedure<?> proc = (Procedure<?>) item;
089        return !proc.hasParent() || proc.getRootProcId() != procId;
090      }
091
092      @Override
093      public void describeTo(Description description) {
094        description.appendText("not a child of pid=").appendValue(procId);
095      }
096    };
097  }
098
099  @Test
100  public void testSuspend() throws Exception {
101    HMaster master = HBTU.getMiniHBaseCluster().getMaster();
102    AssignmentManager am = master.getAssignmentManager();
103    RegionInfo ri = Iterables.getOnlyElement(am.getTableRegions(TABLE_NAME, true));
104    RegionStateNode rsn = am.getRegionStates().getRegionStateNode(ri);
105
106    ServerName src = rsn.getRegionLocation();
107    ServerName dst = HBTU.getMiniHBaseCluster().getRegionServerThreads().stream()
108      .map(t -> t.getRegionServer().getServerName()).filter(sn -> !sn.equals(src)).findFirst()
109      .get();
110    TransitRegionStateProcedure proc = am.createMoveRegionProcedure(ri, dst);
111    // lock the region state node manually, so later TRSP can not lock it
112    rsn.lock();
113    ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
114    long procId = procExec.submitProcedure(proc);
115    // sleep several seconds to let the procedure be scheduled
116    Thread.sleep(2000);
117    // wait until no active procedures
118    HBTU.waitFor(30000, () -> procExec.getActiveExecutorCount() == 0);
119    // the procedure should have not finished yet
120    assertFalse(proc.isFinished());
121    // the TRSP should have not scheduled any sub procedures yet
122    assertThat(procExec.getProcedures(), everyItem(notChildOf(procId)));
123    // make sure the region is still on the src region server
124    assertEquals(src, HBTU.getRSForFirstRegionInTable(TABLE_NAME).getServerName());
125
126    // unlock the region state node lock, the TRSP should be woken up and finish the execution
127    rsn.unlock();
128    HBTU.waitFor(30000, () -> proc.isFinished());
129    // make sure the region is on the dst region server
130    assertEquals(dst, HBTU.getRSForFirstRegionInTable(TABLE_NAME).getServerName());
131  }
132}