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