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.assertFalse;
021import static org.junit.Assert.assertTrue;
022
023import java.util.Optional;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.fs.Path;
026import org.apache.hadoop.hbase.HBaseClassTestRule;
027import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
028import org.apache.hadoop.hbase.regionserver.HRegion;
029import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
030import org.apache.hadoop.hbase.testclassification.MasterTests;
031import org.apache.hadoop.hbase.testclassification.MediumTests;
032import org.apache.hadoop.hbase.util.CommonFSUtils;
033import org.apache.hadoop.hdfs.DistributedFileSystem;
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;
039
040@Category({ MasterTests.class, MediumTests.class })
041public class TestSnapshotProcedureSnapshotCorrupted extends TestSnapshotProcedure {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045    HBaseClassTestRule.forClass(TestSnapshotProcedureSnapshotCorrupted.class);
046
047  @Test
048  public void testSnapshotCorruptedAndRollback() throws Exception {
049    ProcedureExecutor<MasterProcedureEnv> procExec = master.getMasterProcedureExecutor();
050    SnapshotProcedure sp = new SnapshotProcedure(procExec.getEnvironment(), snapshotProto);
051    procExec.submitProcedure(sp);
052    TEST_UTIL.waitFor(60000, 500,
053      () -> sp.getCurrentStateId() > SnapshotState.SNAPSHOT_CONSOLIDATE_SNAPSHOT_VALUE);
054    DistributedFileSystem dfs = TEST_UTIL.getDFSCluster().getFileSystem();
055    Optional<HRegion> region = TEST_UTIL.getHBaseCluster().getRegions(TABLE_NAME).stream()
056      .filter(r -> !r.getStoreFileList(new byte[][] { CF }).isEmpty()).findFirst();
057    assertTrue(region.isPresent());
058    region.get().getStoreFileList(new byte[][] { CF }).forEach(s -> {
059      try {
060        // delete real data files to trigger the CorruptedSnapshotException
061        dfs.delete(new Path(s), true);
062        LOG.info("delete {} to make snapshot corrupt", s);
063      } catch (Exception e) {
064        LOG.warn("Failed delete {} to make snapshot corrupt", s, e);
065      }
066    });
067    TEST_UTIL.waitFor(60000, () -> sp.isFailed() && sp.isFinished());
068    Configuration conf = master.getConfiguration();
069    Path workingDir = SnapshotDescriptionUtils.getWorkingSnapshotDir(snapshotProto,
070      CommonFSUtils.getRootDir(conf), conf);
071    assertFalse(dfs.exists(workingDir));
072    assertFalse(master.getSnapshotManager().isTakingSnapshot(TABLE_NAME));
073    assertFalse(master.getSnapshotManager().isTakingAnySnapshot());
074  }
075}