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