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