1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.util;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.io.IOException;
28 import java.util.UUID;
29 import java.util.Set;
30 import java.util.HashSet;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34 import org.apache.hadoop.fs.FSDataOutputStream;
35 import org.apache.hadoop.fs.FileSystem;
36 import org.apache.hadoop.fs.Path;
37 import org.apache.hadoop.hbase.HBaseTestingUtility;
38 import org.apache.hadoop.hbase.HConstants;
39 import org.apache.hadoop.hbase.testclassification.MediumTests;
40 import org.apache.hadoop.hbase.wal.WALSplitter;
41 import org.junit.*;
42 import org.junit.experimental.categories.Category;
43
44
45
46
47 @Category(MediumTests.class)
48 public class TestFSVisitor {
49 private static final Log LOG = LogFactory.getLog(TestFSVisitor.class);
50
51 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
52
53 private final String TABLE_NAME = "testtb";
54
55 private Set<String> tableFamilies;
56 private Set<String> tableRegions;
57 private Set<String> tableHFiles;
58
59 private FileSystem fs;
60 private Path tableDir;
61 private Path rootDir;
62
63 @Before
64 public void setUp() throws Exception {
65 fs = FileSystem.get(TEST_UTIL.getConfiguration());
66 rootDir = TEST_UTIL.getDataTestDir("hbase");
67
68 tableFamilies = new HashSet<String>();
69 tableRegions = new HashSet<String>();
70 tableHFiles = new HashSet<String>();
71 tableDir = createTableFiles(rootDir, TABLE_NAME, tableRegions, tableFamilies, tableHFiles);
72 FSUtils.logFileSystemState(fs, rootDir, LOG);
73 }
74
75 @After
76 public void tearDown() throws Exception {
77 fs.delete(rootDir, true);
78 }
79
80 @Test
81 public void testVisitStoreFiles() throws IOException {
82 final Set<String> regions = new HashSet<String>();
83 final Set<String> families = new HashSet<String>();
84 final Set<String> hfiles = new HashSet<String>();
85 FSVisitor.visitTableStoreFiles(fs, tableDir, new FSVisitor.StoreFileVisitor() {
86 public void storeFile(final String region, final String family, final String hfileName)
87 throws IOException {
88 regions.add(region);
89 families.add(family);
90 hfiles.add(hfileName);
91 }
92 });
93 assertEquals(tableRegions, regions);
94 assertEquals(tableFamilies, families);
95 assertEquals(tableHFiles, hfiles);
96 }
97
98
99
100
101
102
103
104
105
106
107 private Path createTableFiles(final Path rootDir, final String tableName,
108 final Set<String> tableRegions, final Set<String> tableFamilies,
109 final Set<String> tableHFiles) throws IOException {
110 Path tableDir = new Path(rootDir, tableName);
111 for (int r = 0; r < 10; ++r) {
112 String regionName = MD5Hash.getMD5AsHex(Bytes.toBytes(r));
113 tableRegions.add(regionName);
114 Path regionDir = new Path(tableDir, regionName);
115 for (int f = 0; f < 3; ++f) {
116 String familyName = "f" + f;
117 tableFamilies.add(familyName);
118 Path familyDir = new Path(regionDir, familyName);
119 fs.mkdirs(familyDir);
120 for (int h = 0; h < 5; ++h) {
121 String hfileName = UUID.randomUUID().toString().replaceAll("-", "");
122 tableHFiles.add(hfileName);
123 fs.createNewFile(new Path(familyDir, hfileName));
124 }
125 }
126 }
127 return tableDir;
128 }
129 }