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.backup;
19  
20  import static org.junit.Assert.assertFalse;
21  import static org.junit.Assert.assertTrue;
22  
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.HashMap;
26  import java.util.List;
27  import java.util.Map;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.fs.FileStatus;
33  import org.apache.hadoop.fs.FileSystem;
34  import org.apache.hadoop.fs.Path;
35  import org.apache.hadoop.hbase.HBaseTestingUtility;
36  import org.apache.hadoop.hbase.HConstants;
37  import org.apache.hadoop.hbase.TableName;
38  import org.apache.hadoop.hbase.backup.impl.BackupSystemTable;
39  import org.apache.hadoop.hbase.client.Connection;
40  import org.apache.hadoop.hbase.client.ConnectionFactory;
41  import org.apache.hadoop.hbase.testclassification.SmallTests;
42  import org.junit.After;
43  import org.junit.AfterClass;
44  import org.junit.Before;
45  import org.junit.BeforeClass;
46  import org.junit.Test;
47  import org.junit.experimental.categories.Category;
48  
49  @Category({ SmallTests.class })
50  public class TestBackupHFileCleaner {
51    private static final Log LOG = LogFactory.getLog(TestBackupHFileCleaner.class);
52    private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
53    private static Configuration conf = TEST_UTIL.getConfiguration();
54    private static TableName tableName = TableName.valueOf("backup.hfile.cleaner");
55    private static String famName = "fam";
56    static FileSystem fs = null;
57    Path root;
58  
59    /**
60     * @throws java.lang.Exception
61     */
62    @BeforeClass
63    public static void setUpBeforeClass() throws Exception {
64      conf.setBoolean(HConstants.BACKUP_ENABLE_KEY, true);
65      TEST_UTIL.startMiniZKCluster();
66      TEST_UTIL.startMiniCluster(1);
67      TestBackupBase.waitForSystemTable(TEST_UTIL);
68      fs = FileSystem.get(conf);
69    }
70  
71    /**
72     * @throws java.lang.Exception
73     */
74    @AfterClass
75    public static void tearDownAfterClass() throws Exception {
76      if (fs != null) {
77        fs.close();
78      }
79      TEST_UTIL.shutdownMiniCluster();
80    }
81  
82    @Before
83    public void setup() throws IOException {
84      root = TEST_UTIL.getDataTestDirOnTestFS();
85    }
86  
87    @After
88    public void cleanup() {
89      try {
90        fs.delete(root, true);
91      } catch (IOException e) {
92        LOG.warn("Failed to delete files recursively from path " + root);
93      }
94    }
95  
96    @Test
97    public void testGetDeletableFiles() throws IOException {
98      // 1. Create a file
99      Path file = new Path(root, "testIsFileDeletableWithNoHFileRefs");
100     fs.createNewFile(file);
101     // 2. Assert file is successfully created
102     assertTrue("Test file not created!", fs.exists(file));
103     BackupHFileCleaner cleaner = new BackupHFileCleaner();
104     cleaner.setConf(conf);
105     cleaner.setCheckForFullyBackedUpTables(false);
106     // 3. Assert that file as is should be deletable
107     List<FileStatus> stats = new ArrayList<>();
108     FileStatus stat = fs.getFileStatus(file);
109     stats.add(stat);
110     Iterable<FileStatus> deletable = cleaner.getDeletableFiles(stats);
111     deletable = cleaner.getDeletableFiles(stats);
112     boolean found = false;
113     for (FileStatus stat1 : deletable) {
114       if (stat.equals(stat1)) found = true;
115     }
116     assertTrue("Cleaner should allow to delete this file as there is no hfile reference "
117         + "for it.", found);
118 
119     // 4. Add the file as bulk load
120     List<Path> list = new ArrayList<>(1);
121     list.add(file);
122     try (Connection conn = ConnectionFactory.createConnection(conf);
123         BackupSystemTable sysTbl = new BackupSystemTable(conn)) {
124       List<TableName> sTableList = new ArrayList<>();
125       sTableList.add(tableName);
126       Map<byte[], List<Path>>[] maps = new Map[1];
127       maps[0] = new HashMap<>();
128       maps[0].put(famName.getBytes(), list);
129       sysTbl.writeBulkLoadedFiles(sTableList, maps, "1");
130     }
131 
132     // 5. Assert file should not be deletable
133     deletable = cleaner.getDeletableFiles(stats);
134     deletable = cleaner.getDeletableFiles(stats);
135     found = false;
136     for (FileStatus stat1 : deletable) {
137       if (stat.equals(stat1)) found = true;
138     }
139     assertFalse("Cleaner should not allow to delete this file as there is a hfile reference "
140         + "for it.", found);
141   }
142 }