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.io.IOException;
021import java.io.UncheckedIOException;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.HBaseTestingUtil;
024import org.apache.hadoop.hbase.ServerName;
025import org.apache.hadoop.hbase.TableName;
026import org.apache.hadoop.hbase.client.RegionInfo;
027import org.apache.hadoop.hbase.master.HMaster;
028import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure.TransitionType;
029import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
030import org.apache.hadoop.hbase.master.procedure.ServerCrashProcedure;
031import org.apache.hadoop.hbase.procedure2.Procedure;
032import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
033import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
034import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
035import org.apache.hadoop.hbase.procedure2.ProcedureYieldException;
036import org.apache.hadoop.hbase.testclassification.MasterTests;
037import org.apache.hadoop.hbase.testclassification.MediumTests;
038import org.apache.hadoop.hbase.util.Bytes;
039import org.junit.AfterClass;
040import org.junit.BeforeClass;
041import org.junit.ClassRule;
042import org.junit.Test;
043import org.junit.experimental.categories.Category;
044
045import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos.ProcedureState;
046
047/**
048 * Testcase for HBASE-29259
049 */
050@Category({ MasterTests.class, MediumTests.class })
051public class TestTRSPPersistUninitializedSubProc {
052
053  @ClassRule
054  public static final HBaseClassTestRule CLASS_RULE =
055    HBaseClassTestRule.forClass(TestTRSPPersistUninitializedSubProc.class);
056
057  private static HBaseTestingUtil UTIL = new HBaseTestingUtil();
058
059  private static byte[] CF = Bytes.toBytes("cf");
060
061  private static TableName TN = TableName.valueOf("tn");
062
063  public static class TRSPForTest extends TransitRegionStateProcedure {
064
065    private boolean injected = false;
066
067    public TRSPForTest() {
068    }
069
070    public TRSPForTest(MasterProcedureEnv env, RegionInfo hri, ServerName assignCandidate,
071      boolean forceNewPlan, TransitionType type) {
072      super(env, hri, assignCandidate, forceNewPlan, type);
073    }
074
075    @Override
076    protected Procedure[] execute(MasterProcedureEnv env)
077      throws ProcedureSuspendedException, ProcedureYieldException, InterruptedException {
078      Procedure[] subProcs = super.execute(env);
079      if (!injected && subProcs != null && subProcs[0] instanceof CloseRegionProcedure) {
080        injected = true;
081        ServerName sn = ((CloseRegionProcedure) subProcs[0]).targetServer;
082        env.getMasterServices().getServerManager().expireServer(sn);
083        try {
084          UTIL.waitFor(15000, () -> env.getMasterServices().getProcedures().stream().anyMatch(
085            p -> p instanceof ServerCrashProcedure && p.getState() != ProcedureState.INITIALIZING));
086        } catch (IOException e) {
087          throw new UncheckedIOException(e);
088        }
089        // sleep 10 seconds to let the SCP interrupt the TRSP, where we will call TRSP.serverCrashed
090        Thread.sleep(10000);
091      }
092      return subProcs;
093    }
094  }
095
096  @BeforeClass
097  public static void setUpBeforeClass() throws Exception {
098    UTIL.startMiniCluster(2);
099    UTIL.getAdmin().balancerSwitch(false, true);
100    UTIL.createTable(TN, CF);
101    UTIL.waitTableAvailable(TN);
102  }
103
104  @AfterClass
105  public static void tearDownAfterClass() throws Exception {
106    UTIL.shutdownMiniCluster();
107  }
108
109  @Test
110  public void testServerCrash() throws Exception {
111    HMaster master = UTIL.getHBaseCluster().getMaster();
112    ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
113    RegionInfo region = UTIL.getAdmin().getRegions(TN).get(0);
114    RegionStateNode rsn =
115      master.getAssignmentManager().getRegionStates().getRegionStateNode(region);
116    TRSPForTest trsp =
117      new TRSPForTest(procExec.getEnvironment(), region, null, false, TransitionType.REOPEN);
118    // attach it to RegionStateNode, to simulate normal reopen
119    rsn.setProcedure(trsp);
120    procExec.submitProcedure(trsp);
121    ProcedureTestingUtility.waitProcedure(procExec, trsp);
122    // make sure we do not store invalid procedure to procedure store
123    ProcedureTestingUtility.restart(procExec);
124  }
125}