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.snapshot; 019 020import static org.junit.Assert.assertFalse; 021import static org.junit.Assert.assertTrue; 022 023import java.io.IOException; 024import java.util.Collection; 025import java.util.HashSet; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.fs.FileSystem; 028import org.apache.hadoop.fs.Path; 029import org.apache.hadoop.hbase.HBaseClassTestRule; 030import org.apache.hadoop.hbase.HBaseTestingUtil; 031import org.apache.hadoop.hbase.HConstants; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.client.RegionInfo; 034import org.apache.hadoop.hbase.client.RegionInfoBuilder; 035import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils; 036import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil; 037import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils; 038import org.apache.hadoop.hbase.testclassification.MasterTests; 039import org.apache.hadoop.hbase.testclassification.SmallTests; 040import org.apache.hadoop.hbase.util.CommonFSUtils; 041import org.junit.AfterClass; 042import org.junit.BeforeClass; 043import org.junit.ClassRule; 044import org.junit.Rule; 045import org.junit.Test; 046import org.junit.experimental.categories.Category; 047import org.junit.rules.TestName; 048 049/** 050 * Test that the snapshot hfile cleaner finds hfiles referenced in a snapshot 051 */ 052@Category({ MasterTests.class, SmallTests.class }) 053public class TestSnapshotHFileCleaner { 054 055 @ClassRule 056 public static final HBaseClassTestRule CLASS_RULE = 057 HBaseClassTestRule.forClass(TestSnapshotHFileCleaner.class); 058 059 private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 060 private static final String TABLE_NAME_STR = "testSnapshotManifest"; 061 private static final String SNAPSHOT_NAME_STR = "testSnapshotManifest-snapshot"; 062 private static Path rootDir; 063 private static FileSystem fs; 064 private static Configuration conf; 065 066 @Rule 067 public TestName name = new TestName(); 068 069 /** 070 * Setup the test environment 071 */ 072 @BeforeClass 073 public static void setup() throws Exception { 074 conf = TEST_UTIL.getConfiguration(); 075 rootDir = CommonFSUtils.getRootDir(conf); 076 fs = FileSystem.get(conf); 077 } 078 079 @AfterClass 080 public static void cleanup() throws IOException { 081 // cleanup 082 fs.delete(rootDir, true); 083 } 084 085 @Test 086 public void testFindsSnapshotFilesWhenCleaning() throws IOException { 087 CommonFSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir()); 088 Path rootDir = CommonFSUtils.getRootDir(conf); 089 Path archivedHfileDir = 090 new Path(TEST_UTIL.getDataTestDir(), HConstants.HFILE_ARCHIVE_DIRECTORY); 091 092 FileSystem fs = FileSystem.get(conf); 093 SnapshotHFileCleaner cleaner = new SnapshotHFileCleaner(); 094 cleaner.setConf(conf); 095 096 // write an hfile to the snapshot directory 097 String snapshotName = "snapshot"; 098 final TableName tableName = TableName.valueOf(name.getMethodName()); 099 Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir); 100 RegionInfo mockRegion = RegionInfoBuilder.newBuilder(tableName).build(); 101 Path regionSnapshotDir = new Path(snapshotDir, mockRegion.getEncodedName()); 102 Path familyDir = new Path(regionSnapshotDir, "family"); 103 // create a reference to a supposedly valid hfile 104 String hfile = "fd1e73e8a96c486090c5cec07b4894c4"; 105 Path refFile = new Path(familyDir, hfile); 106 107 // make sure the reference file exists 108 fs.create(refFile); 109 110 // create the hfile in the archive 111 fs.mkdirs(archivedHfileDir); 112 fs.createNewFile(new Path(archivedHfileDir, hfile)); 113 114 // make sure that the file isn't deletable 115 assertFalse(cleaner.isFileDeletable(fs.getFileStatus(refFile))); 116 } 117 118 static class SnapshotFiles implements SnapshotFileCache.SnapshotFileInspector { 119 @Override 120 public Collection<String> filesUnderSnapshot(final FileSystem workingFs, final Path snapshotDir) 121 throws IOException { 122 Collection<String> files = new HashSet<>(); 123 files.addAll(SnapshotReferenceUtil.getHFileNames(conf, workingFs, snapshotDir)); 124 return files; 125 } 126 } 127 128 /** 129 * If there is a corrupted region manifest, it should throw out CorruptedSnapshotException, 130 * instead of an IOException 131 */ 132 @Test 133 public void testCorruptedRegionManifest() throws IOException { 134 SnapshotTestingUtils.SnapshotMock snapshotMock = 135 new SnapshotTestingUtils.SnapshotMock(conf, fs, rootDir); 136 SnapshotTestingUtils.SnapshotMock.SnapshotBuilder builder = 137 snapshotMock.createSnapshotV2(SNAPSHOT_NAME_STR, TABLE_NAME_STR); 138 builder.addRegionV2(); 139 builder.corruptOneRegionManifest(); 140 141 long period = Long.MAX_VALUE; 142 SnapshotFileCache cache = new SnapshotFileCache(conf, period, 10000000, 143 "test-snapshot-file-cache-refresh", new SnapshotFiles()); 144 try { 145 cache.getSnapshotsInProgress(); 146 } finally { 147 fs.delete(SnapshotDescriptionUtils.getWorkingSnapshotDir(rootDir, conf), true); 148 } 149 } 150 151 /** 152 * If there is a corrupted data manifest, it should throw out CorruptedSnapshotException, instead 153 * of an IOException 154 */ 155 @Test 156 public void testCorruptedDataManifest() throws IOException { 157 SnapshotTestingUtils.SnapshotMock snapshotMock = 158 new SnapshotTestingUtils.SnapshotMock(conf, fs, rootDir); 159 SnapshotTestingUtils.SnapshotMock.SnapshotBuilder builder = 160 snapshotMock.createSnapshotV2(SNAPSHOT_NAME_STR, TABLE_NAME_STR); 161 builder.addRegionV2(); 162 // consolidate to generate a data.manifest file 163 builder.consolidate(); 164 builder.corruptDataManifest(); 165 166 long period = Long.MAX_VALUE; 167 SnapshotFileCache cache = new SnapshotFileCache(conf, period, 10000000, 168 "test-snapshot-file-cache-refresh", new SnapshotFiles()); 169 try { 170 cache.getSnapshotsInProgress(); 171 } finally { 172 fs.delete( 173 SnapshotDescriptionUtils.getWorkingSnapshotDir(rootDir, TEST_UTIL.getConfiguration()), 174 true); 175 } 176 } 177 178 @Test 179 public void testMissedTmpSnapshot() throws IOException { 180 SnapshotTestingUtils.SnapshotMock snapshotMock = 181 new SnapshotTestingUtils.SnapshotMock(conf, fs, rootDir); 182 SnapshotTestingUtils.SnapshotMock.SnapshotBuilder builder = 183 snapshotMock.createSnapshotV2(SNAPSHOT_NAME_STR, TABLE_NAME_STR); 184 builder.addRegionV2(); 185 builder.missOneRegionSnapshotFile(); 186 long period = Long.MAX_VALUE; 187 SnapshotFileCache cache = new SnapshotFileCache(conf, period, 10000000, 188 "test-snapshot-file-cache-refresh", new SnapshotFiles()); 189 cache.getSnapshotsInProgress(); 190 assertTrue(fs.exists(builder.getSnapshotsDir())); 191 } 192}