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.HBaseTestingUtility;
28  import org.apache.hadoop.hbase.HConstants;
29  import org.apache.hadoop.hbase.HRegionInfo;
30  import org.apache.hadoop.hbase.SmallTests;
31  import org.apache.hadoop.hbase.snapshot.SnapshotDescriptionUtils;
32  import org.apache.hadoop.hbase.util.Bytes;
33  import org.apache.hadoop.hbase.util.FSUtils;
34  import org.junit.AfterClass;
35  import org.junit.Test;
36  import org.junit.experimental.categories.Category;
37  
38  /**
39   * Test that the snapshot hfile cleaner finds hfiles referenced in a snapshot
40   */
41  @Category(SmallTests.class)
42  public class TestSnapshotHFileCleaner {
43  
44    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
45  
46    @AfterClass
47    public static void cleanup() throws IOException {
48      Configuration conf = TEST_UTIL.getConfiguration();
49      Path rootDir = FSUtils.getRootDir(conf);
50      FileSystem fs = FileSystem.get(conf);
51      // cleanup
52      fs.delete(rootDir, true);
53    }
54  
55    @Test
56    public void testFindsSnapshotFilesWhenCleaning() throws IOException {
57      Configuration conf = TEST_UTIL.getConfiguration();
58      FSUtils.setRootDir(conf, TEST_UTIL.getDataTestDir());
59      Path rootDir = FSUtils.getRootDir(conf);
60      Path archivedHfileDir = new Path(TEST_UTIL.getDataTestDir(), HConstants.HFILE_ARCHIVE_DIRECTORY);
61  
62      FileSystem fs = FileSystem.get(conf);
63      SnapshotHFileCleaner cleaner = new SnapshotHFileCleaner();
64      cleaner.setConf(conf);
65  
66      // write an hfile to the snapshot directory
67      String snapshotName = "snapshot";
68      byte[] snapshot = Bytes.toBytes(snapshotName);
69      String table = "table";
70      byte[] tableName = Bytes.toBytes(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(new Path(hfile)));
88    }
89  }