1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
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
99 Path file = new Path(root, "testIsFileDeletableWithNoHFileRefs");
100 fs.createNewFile(file);
101
102 assertTrue("Test file not created!", fs.exists(file));
103 BackupHFileCleaner cleaner = new BackupHFileCleaner();
104 cleaner.setConf(conf);
105 cleaner.setCheckForFullyBackedUpTables(false);
106
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
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
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 }