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 org.apache.hadoop.hbase.HBaseClassTestRule;
021import org.apache.hadoop.hbase.ServerName;
022import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
023import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
024import org.apache.hadoop.hbase.regionserver.HRegionServer;
025import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
026import org.apache.hadoop.hbase.testclassification.MasterTests;
027import org.apache.hadoop.hbase.testclassification.MediumTests;
028import org.junit.ClassRule;
029import org.junit.Test;
030import org.junit.experimental.categories.Category;
031
032@Category({ MasterTests.class, MediumTests.class })
033public class TestSnapshotProcedureRSCrashes extends TestSnapshotProcedure {
034
035  @ClassRule
036  public static final HBaseClassTestRule CLASS_RULE =
037    HBaseClassTestRule.forClass(TestSnapshotProcedureRSCrashes.class);
038
039  @Test
040  public void testRegionServerCrashWhileTakingSnapshot() throws Exception {
041    ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
042    MasterProcedureEnv env = procExec.getEnvironment();
043    SnapshotProcedure sp = new SnapshotProcedure(env, snapshotProto);
044    long procId = procExec.submitProcedure(sp);
045
046    SnapshotRegionProcedure snp =
047      waitProcedureRunnableAndGetFirst(SnapshotRegionProcedure.class, 60000);
048    ServerName targetServer = env.getAssignmentManager().getRegionStates()
049      .getRegionStateNode(snp.getRegion()).getRegionLocation();
050    TEST_UTIL.getHBaseCluster().killRegionServer(targetServer);
051
052    TEST_UTIL.waitFor(60000, () -> snp.inRetrying());
053    ProcedureTestingUtility.waitProcedure(procExec, procId);
054
055    SnapshotTestingUtils.assertOneSnapshotThatMatches(TEST_UTIL.getAdmin(), snapshotProto);
056    SnapshotTestingUtils.confirmSnapshotValid(TEST_UTIL, snapshotProto, TABLE_NAME, CF);
057  }
058
059  @Test
060  public void testRegionServerCrashWhileVerifyingSnapshot() throws Exception {
061    ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
062    MasterProcedureEnv env = procExec.getEnvironment();
063    SnapshotProcedure sp = new SnapshotProcedure(env, snapshotProto);
064    long procId = procExec.submitProcedure(sp);
065
066    SnapshotVerifyProcedure svp =
067      waitProcedureRunnableAndGetFirst(SnapshotVerifyProcedure.class, 60000);
068    TEST_UTIL.waitFor(10000, () -> svp.getServerName() != null);
069    ServerName previousTargetServer = svp.getServerName();
070
071    HRegionServer rs = TEST_UTIL.getHBaseCluster().getRegionServer(previousTargetServer);
072    TEST_UTIL.getHBaseCluster().killRegionServer(rs.getServerName());
073    TEST_UTIL.waitFor(60000,
074      () -> svp.getServerName() != null && !svp.getServerName().equals(previousTargetServer));
075    ProcedureTestingUtility.waitProcedure(procExec, procId);
076
077    SnapshotTestingUtils.assertOneSnapshotThatMatches(TEST_UTIL.getAdmin(), snapshotProto);
078    SnapshotTestingUtils.confirmSnapshotValid(TEST_UTIL, snapshotProto, TABLE_NAME, CF);
079  }
080}