View Javadoc

1   /**
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * Test {@link FSUtils}.
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     * |-testtb/
100    * |----f1d3ff8443297732862df21dc4e57262/
101    * |-------f1/
102    * |----------d0be84935ba84b66b1e866752ec5d663
103    * |----------9fc9d481718f4878b29aad0a597ecb94
104    * |-------f2/
105    * |----------4b0fe6068c564737946bcf4fd4ab8ae1
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 }