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