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.CorruptedSnapshotException;
36  import org.apache.hadoop.hbase.snapshot.SnapshotReferenceUtil;
37  import org.apache.hadoop.hbase.snapshot.SnapshotTestingUtils;
38  import org.apache.hadoop.hbase.testclassification.SmallTests;
39  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
40  import org.apache.hadoop.hbase.util.Bytes;
41  import org.apache.hadoop.hbase.util.FSUtils;
42  import org.junit.AfterClass;
43  import org.junit.BeforeClass;
44  import org.junit.Test;
45  import org.junit.experimental.categories.Category;
46  
47  /**
48   * Test that the snapshot hfile cleaner finds hfiles referenced in a snapshot
49   */
50  @Category(SmallTests.class)
51  public class TestSnapshotHFileCleaner {
52  
53    private static final Log LOG = LogFactory.getLog(TestSnapshotFileCache.class);
54    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
55    private static final String TABLE_NAME_STR = "testSnapshotManifest";
56    private static final String SNAPSHOT_NAME_STR = "testSnapshotManifest-snapshot";
57    private static Path rootDir;
58    private static FileSystem fs;
59  
60    /**
61     * Setup the test environment
62     */
63    @BeforeClass
64    public static void setup() throws Exception {
65      Configuration conf = TEST_UTIL.getConfiguration();
66      rootDir = FSUtils.getRootDir(conf);
67      fs = FileSystem.get(conf);
68    }
69  
70    @AfterClass
71    public static void cleanup() throws IOException {
72      // cleanup
73      fs.delete(rootDir, true);
74    }
75  
76    @Test
77    public void testFindsSnapshotFilesWhenCleaning() throws IOException {
78      Configuration conf = TEST_UTIL.getConfiguration();
79      FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
80      Path rootDir = FSUtils.getRootDir(conf);
81      Path archivedHfileDir = new Path(TEST_UTIL.getDataTestDir(), HConstants.HFILE_ARCHIVE_DIRECTORY);
82  
83      FileSystem fs = FileSystem.get(conf);
84      SnapshotHFileCleaner cleaner = new SnapshotHFileCleaner();
85      cleaner.setConf(conf);
86  
87      // write an hfile to the snapshot directory
88      String snapshotName = "snapshot";
89      byte[] snapshot = Bytes.toBytes(snapshotName);
90      TableName tableName = TableName.valueOf("table");
91      Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
92      HRegionInfo mockRegion = new HRegionInfo(tableName);
93      Path regionSnapshotDir = new Path(snapshotDir, mockRegion.getEncodedName());
94      Path familyDir = new Path(regionSnapshotDir, "family");
95      // create a reference to a supposedly valid hfile
96      String hfile = "fd1e73e8a96c486090c5cec07b4894c4";
97      Path refFile = new Path(familyDir, hfile);
98  
99      // make sure the reference file exists
100     fs.create(refFile);
101 
102     // create the hfile in the archive
103     fs.mkdirs(archivedHfileDir);
104     fs.createNewFile(new Path(archivedHfileDir, hfile));
105 
106     // make sure that the file isn't deletable
107     assertFalse(cleaner.isFileDeletable(fs.getFileStatus(refFile)));
108   }
109 
110   class SnapshotFiles implements SnapshotFileCache.SnapshotFileInspector {
111     public Collection<String> filesUnderSnapshot(final Path snapshotDir) throws IOException {
112       Collection<String> files =  new HashSet<String>();
113       files.addAll(SnapshotReferenceUtil.getHFileNames(TEST_UTIL.getConfiguration(), fs, snapshotDir));
114       return files;
115     }
116   }
117 
118   /**
119    * If there is a corrupted region manifest, it should throw out CorruptedSnapshotException,
120    * instead of an IOException
121    */
122   @Test
123   public void testCorruptedRegionManifest() throws IOException {
124     SnapshotTestingUtils.SnapshotMock
125         snapshotMock = new SnapshotTestingUtils.SnapshotMock(TEST_UTIL.getConfiguration(), fs, rootDir);
126     SnapshotTestingUtils.SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(
127         SNAPSHOT_NAME_STR, TABLE_NAME_STR);
128     builder.addRegionV2();
129     builder.corruptOneRegionManifest();
130 
131     long period = Long.MAX_VALUE;
132     SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
133         "test-snapshot-file-cache-refresh", new SnapshotFiles());
134     try {
135       cache.getSnapshotsInProgress();
136     } catch (CorruptedSnapshotException cse) {
137       LOG.info("Expected exception " + cse);
138     } finally {
139       fs.delete(SnapshotDescriptionUtils.getWorkingSnapshotDir(rootDir), true);
140     }
141   }
142 
143   /**
144    * If there is a corrupted data manifest, it should throw out CorruptedSnapshotException,
145    * instead of an IOException
146    */
147   @Test
148   public void testCorruptedDataManifest() throws IOException {
149     SnapshotTestingUtils.SnapshotMock
150         snapshotMock = new SnapshotTestingUtils.SnapshotMock(TEST_UTIL.getConfiguration(), fs, rootDir);
151     SnapshotTestingUtils.SnapshotMock.SnapshotBuilder builder = snapshotMock.createSnapshotV2(
152         SNAPSHOT_NAME_STR, TABLE_NAME_STR);
153     builder.addRegionV2();
154     // consolidate to generate a data.manifest file
155     builder.consolidate();
156     builder.corruptDataManifest();
157 
158     long period = Long.MAX_VALUE;
159     SnapshotFileCache cache = new SnapshotFileCache(fs, rootDir, period, 10000000,
160         "test-snapshot-file-cache-refresh", new SnapshotFiles());
161     try {
162       cache.getSnapshotsInProgress();
163     } catch (CorruptedSnapshotException cse) {
164       LOG.info("Expected exception " + cse);
165     } finally {
166       fs.delete(SnapshotDescriptionUtils.getWorkingSnapshotDir(rootDir), true);
167     }
168   }
169 }