View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.master.snapshot;
19  
20  import static org.junit.Assert.assertFalse;
21  
22  import java.io.IOException;
23  import java.util.Collection;
24  import java.util.HashSet;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.conf.Configuration;
29  import org.apache.hadoop.fs.FileSystem;
30  import org.apache.hadoop.fs.Path;
31  import org.apache.hadoop.hbase.TableName;
32  import org.apache.hadoop.hbase.HBaseTestingUtility;
33  import org.apache.hadoop.hbase.HConstants;
34  import org.apache.hadoop.hbase.HRegionInfo;
35  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
36  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
37  import org.apache.hadoop.hbase.testclassification.SmallTests;
38  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
39  import org.apache.hadoop.hbase.util.Bytes;
40  import org.apache.hadoop.hbase.util.FSUtils;
41  import org.junit.AfterClass;
42  import org.junit.BeforeClass;
43  import org.junit.Test;
44  import org.junit.experimental.categories.Category;
45  
46  /**
47   * Test that the snapshot hfile cleaner finds hfiles referenced in a snapshot
48   */
49  @Category(SmallTests.class)
50  public class TestSnapshotHFileCleaner {
51  
52    private static final Log LOG = LogFactory.getLog(TestSnapshotFileCache.class);
53    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
54    private static final String TABLE_NAME_STR = "testSnapshotManifest";
55    private static final String SNAPSHOT_NAME_STR = "testSnapshotManifest-snapshot";
56    private static Path rootDir;
57    private static FileSystem fs;
58  
59    /**
60     * Setup the test environment
61     */
62    @BeforeClass
63    public static void setup() throws Exception {
64      Configuration conf = TEST_UTIL.getConfiguration();
65      rootDir = FSUtils.getRootDir(conf);
66      fs = FileSystem.get(conf);
67    }
68  
69    @AfterClass
70    public static void cleanup() throws IOException {
71      // cleanup
72      fs.delete(rootDir, true);
73    }
74  
75    @Test
76    public void testFindsSnapshotFilesWhenCleaning() throws IOException {
77      Configuration conf = TEST_UTIL.getConfiguration();
78      FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
79      Path rootDir = FSUtils.getRootDir(conf);
80      Path archivedHfileDir = new Path(TEST_UTIL.getDataTestDir(), HConstants.HFILE_ARCHIVE_DIRECTORY);
81  
82      FileSystem fs = FileSystem.get(conf);
83      SnapshotHFileCleaner cleaner = new SnapshotHFileCleaner();
84      cleaner.setConf(conf);
85  
86      // write an hfile to the snapshot directory
87      String snapshotName = "snapshot";
88      TableName tableName = TableName.valueOf("table");
89      Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
90      HRegionInfo mockRegion = new HRegionInfo(tableName);
91      Path regionSnapshotDir = new Path(snapshotDir, mockRegion.getEncodedName());
92      Path familyDir = new Path(regionSnapshotDir, "family");
93      // create a reference to a supposedly valid hfile
94      String hfile = "fd1e73e8a96c486090c5cec07b4894c4";
95      Path refFile = new Path(familyDir, hfile);
96  
97      // make sure the reference file exists
98      fs.create(refFile);
99  
100     // create the hfile in the archive
101     fs.mkdirs(archivedHfileDir);
102     fs.createNewFile(new Path(archivedHfileDir, hfile));
103 
104     // make sure that the file isn't deletable
105     assertFalse(cleaner.isFileDeletable(fs.getFileStatus(refFile)));
106   }
107 
108   class SnapshotFiles implements SnapshotFileCache.SnapshotFileInspector {
109     public Collection<String> filesUnderSnapshot(final Path snapshotDir) throws IOException {
110       Collection<String> files =  new HashSet<String>();
111       files.addAll(SnapshotReferenceUtil.getHFileNames(TEST_UTIL.getConfiguration(), fs, snapshotDir));
112       return files;
113     }
114   }
115 
116   /**
117    * If there is a corrupted region manifest, it should throw out CorruptedSnapshotException,
118    * instead of an IOException
119    */
120   @Test
121   public void testCorruptedRegionManifest() throws IOException {
122     SnapshotTestingUtils.SnapshotMock
123         snapshotMock = new SnapshotTestingUtils.SnapshotMock(TEST_UTIL.getConfiguration(), fs, rootDir);
124     SnapshotTestingUtils.SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(
125         SNAPSHOT_NAME_STR, TABLE_NAME_STR);
126     builder.addRegionV2();
127     builder.corruptOneRegionManifest();
128 
129     fs.delete(SnapshotDescriptionUtils.getWorkingSnapshotDir(rootDir, TEST_UTIL.getConfiguration()),
130       true);
131   }
132 
133   /**
134    * If there is a corrupted data manifest, it should throw out CorruptedSnapshotException,
135    * instead of an IOException
136    */
137   @Test
138   public void testCorruptedDataManifest() throws IOException {
139     SnapshotTestingUtils.SnapshotMock
140         snapshotMock = new SnapshotTestingUtils.SnapshotMock(TEST_UTIL.getConfiguration(), fs, rootDir);
141     SnapshotTestingUtils.SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(
142         SNAPSHOT_NAME_STR, TABLE_NAME_STR);
143     builder.addRegionV2();
144     // consolidate to generate a data.manifest file
145     builder.consolidate();
146     builder.corruptDataManifest();
147 
148     fs.delete(SnapshotDescriptionUtils.getWorkingSnapshotDir(rootDir,
149           TEST_UTIL.getConfiguration()), true);
150   }
151 }