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.junit.Assert.assertEquals;
021import static org.junit.Assert.assertFalse;
022import static org.junit.Assert.assertTrue;
023
024import java.util.List;
025import java.util.stream.Collectors;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.TableName;
028import org.apache.hadoop.hbase.procedure2.Procedure;
029import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
030import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
031import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
032import org.apache.hadoop.hbase.testclassification.MasterTests;
033import org.apache.hadoop.hbase.testclassification.MediumTests;
034import org.junit.ClassRule;
035import org.junit.Test;
036import org.junit.experimental.categories.Category;
037
038import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.SnapshotState;
039import org.apache.hadoop.hbase.shaded.protobuf.generated.SnapshotProtos;
040
041@Category({ MasterTests.class, MediumTests.class })
042public class TestSnapshotProcedureMasterRestarts extends TestSnapshotProcedure {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046    HBaseClassTestRule.forClass(TestSnapshotProcedureMasterRestarts.class);
047
048  @Test
049  public void testMasterRestarts() throws Exception {
050    ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
051    MasterProcedureEnv env = procExec.getEnvironment();
052    SnapshotProcedure sp = new SnapshotProcedure(env, snapshotProto);
053    SnapshotProcedure spySp = getDelayedOnSpecificStateSnapshotProcedure(sp,
054      procExec.getEnvironment(), SnapshotState.SNAPSHOT_SNAPSHOT_ONLINE_REGIONS);
055
056    long procId = procExec.submitProcedure(spySp);
057
058    TEST_UTIL.waitFor(2000, () -> env.getMasterServices().getProcedures().stream()
059      .map(Procedure::getProcId).collect(Collectors.toList()).contains(procId));
060    TEST_UTIL.getHBaseCluster().killMaster(master.getServerName());
061    TEST_UTIL.getHBaseCluster().waitForMasterToStop(master.getServerName(), 30000);
062    TEST_UTIL.getHBaseCluster().startMaster();
063    TEST_UTIL.getHBaseCluster().waitForActiveAndReadyMaster();
064
065    master = TEST_UTIL.getHBaseCluster().getMaster();
066    assertTrue(master.getSnapshotManager().isTakingAnySnapshot());
067    assertTrue(master.getSnapshotManager().isTableTakingAnySnapshot(TABLE_NAME));
068
069    List<SnapshotProcedure> unfinishedProcedures = master.getMasterProcedureExecutor()
070      .getProcedures().stream().filter(p -> p instanceof SnapshotProcedure)
071      .filter(p -> !p.isFinished()).map(p -> (SnapshotProcedure) p).collect(Collectors.toList());
072    assertEquals(unfinishedProcedures.size(), 1);
073    long newProcId = unfinishedProcedures.get(0).getProcId();
074    assertEquals(procId, newProcId);
075
076    ProcedureTestingUtility.waitProcedure(master.getMasterProcedureExecutor(), newProcId);
077    assertFalse(master.getSnapshotManager().isTableTakingAnySnapshot(TABLE_NAME));
078
079    List<SnapshotProtos.SnapshotDescription> snapshots =
080      master.getSnapshotManager().getCompletedSnapshots();
081    assertEquals(1, snapshots.size());
082    assertEquals(SNAPSHOT_NAME, snapshots.get(0).getName());
083    assertEquals(TABLE_NAME, TableName.valueOf(snapshots.get(0).getTable()));
084    SnapshotTestingUtils.confirmSnapshotValid(TEST_UTIL, snapshotProto, TABLE_NAME, CF);
085  }
086}