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  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.FileSystem;
26  import org.apache.hadoop.fs.Path;
27  import org.apache.hadoop.hbase.TableName;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.HConstants;
30  import org.apache.hadoop.hbase.HRegionInfo;
31  import org.apache.hadoop.hbase.testclassification.SmallTests;
32  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.apache.hadoop.hbase.util.FSUtils;
35  import org.junit.AfterClass;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  /**
40   * Test that the snapshot hfile cleaner finds hfiles referenced in a snapshot
41   */
42  @Category(SmallTests.class)
43  public class TestSnapshotHFileCleaner {
44  
45    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
46  
47    @AfterClass
48    public static void cleanup() throws IOException {
49      Configuration conf = TEST_UTIL.getConfiguration();
50      Path rootDir = FSUtils.getRootDir(conf);
51      FileSystem fs = FileSystem.get(conf);
52      // cleanup
53      fs.delete(rootDir, true);
54    }
55  
56    @Test
57    public void testFindsSnapshotFilesWhenCleaning() throws IOException {
58      Configuration conf = TEST_UTIL.getConfiguration();
59      FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
60      Path rootDir = FSUtils.getRootDir(conf);
61      Path archivedHfileDir = new Path(TEST_UTIL.getDataTestDir(), HConstants.HFILE_ARCHIVE_DIRECTORY);
62  
63      FileSystem fs = FileSystem.get(conf);
64      SnapshotHFileCleaner cleaner = new SnapshotHFileCleaner();
65      cleaner.setConf(conf);
66  
67      // write an hfile to the snapshot directory
68      String snapshotName = "snapshot";
69      byte[] snapshot = Bytes.toBytes(snapshotName);
70      TableName tableName = TableName.valueOf("table");
71      Path snapshotDir = SnapshotDescriptionUtils.getCompletedSnapshotDir(snapshotName, rootDir);
72      HRegionInfo mockRegion = new HRegionInfo(tableName);
73      Path regionSnapshotDir = new Path(snapshotDir, mockRegion.getEncodedName());
74      Path familyDir = new Path(regionSnapshotDir, "family");
75      // create a reference to a supposedly valid hfile
76      String hfile = "fd1e73e8a96c486090c5cec07b4894c4";
77      Path refFile = new Path(familyDir, hfile);
78  
79      // make sure the reference file exists
80      fs.create(refFile);
81  
82      // create the hfile in the archive
83      fs.mkdirs(archivedHfileDir);
84      fs.createNewFile(new Path(archivedHfileDir, hfile));
85  
86      // make sure that the file isn't deletable
87      assertFalse(cleaner.isFileDeletable(fs.getFileStatus(refFile)));
88    }
89  }