001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.util;
019
020import static org.junit.Assert.assertEquals;
021
022import java.io.IOException;
023import java.util.HashSet;
024import java.util.Set;
025import org.apache.hadoop.fs.FileSystem;
026import org.apache.hadoop.fs.Path;
027import org.apache.hadoop.hbase.HBaseClassTestRule;
028import org.apache.hadoop.hbase.HBaseTestingUtility;
029import org.apache.hadoop.hbase.testclassification.MediumTests;
030import org.apache.hadoop.hbase.testclassification.MiscTests;
031import org.junit.*;
032import org.junit.ClassRule;
033import org.junit.experimental.categories.Category;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * Test {@link FSUtils}.
039 */
040@Category({MiscTests.class, MediumTests.class})
041public class TestFSVisitor {
042
043  @ClassRule
044  public static final HBaseClassTestRule CLASS_RULE =
045      HBaseClassTestRule.forClass(TestFSVisitor.class);
046
047  private static final Logger LOG = LoggerFactory.getLogger(TestFSVisitor.class);
048
049  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
050
051  private final String TABLE_NAME = "testtb";
052
053  private Set<String> tableFamilies;
054  private Set<String> tableRegions;
055  private Set<String> tableHFiles;
056
057  private FileSystem fs;
058  private Path tableDir;
059  private Path rootDir;
060
061  @Before
062  public void setUp() throws Exception {
063    fs = FileSystem.get(TEST_UTIL.getConfiguration());
064    rootDir = TEST_UTIL.getDataTestDir("hbase");
065
066    tableFamilies = new HashSet<>();
067    tableRegions = new HashSet<>();
068    tableHFiles = new HashSet<>();
069    tableDir = createTableFiles(rootDir, TABLE_NAME, tableRegions, tableFamilies, tableHFiles);
070    FSUtils.logFileSystemState(fs, rootDir, LOG);
071  }
072
073  @After
074  public void tearDown() throws Exception {
075    fs.delete(rootDir, true);
076  }
077
078  @Test
079  public void testVisitStoreFiles() throws IOException {
080    final Set<String> regions = new HashSet<>();
081    final Set<String> families = new HashSet<>();
082    final Set<String> hfiles = new HashSet<>();
083    FSVisitor.visitTableStoreFiles(fs, tableDir, new FSVisitor.StoreFileVisitor() {
084      @Override
085      public void storeFile(final String region, final String family, final String hfileName)
086          throws IOException {
087        regions.add(region);
088        families.add(family);
089        hfiles.add(hfileName);
090      }
091    });
092    assertEquals(regions, tableRegions);
093    assertEquals(families, tableFamilies);
094    assertEquals(hfiles, tableHFiles);
095  }
096
097  /*
098   * |-testtb/
099   * |----f1d3ff8443297732862df21dc4e57262/
100   * |-------f1/
101   * |----------d0be84935ba84b66b1e866752ec5d663
102   * |----------9fc9d481718f4878b29aad0a597ecb94
103   * |-------f2/
104   * |----------4b0fe6068c564737946bcf4fd4ab8ae1
105   */
106  private Path createTableFiles(final Path rootDir, final String tableName,
107      final Set<String> tableRegions, final Set<String> tableFamilies,
108      final Set<String> tableHFiles) throws IOException {
109    Path tableDir = new Path(rootDir, tableName);
110    for (int r = 0; r < 10; ++r) {
111      String regionName = MD5Hash.getMD5AsHex(Bytes.toBytes(r));
112      tableRegions.add(regionName);
113      Path regionDir = new Path(tableDir, regionName);
114      for (int f = 0; f < 3; ++f) {
115        String familyName = "f" + f;
116        tableFamilies.add(familyName);
117        Path familyDir = new Path(regionDir, familyName);
118        fs.mkdirs(familyDir);
119        for (int h = 0; h < 5; ++h) {
120          String hfileName = TEST_UTIL.getRandomUUID().toString().replaceAll("-", "");
121          tableHFiles.add(hfileName);
122          fs.createNewFile(new Path(familyDir, hfileName));
123        }
124      }
125    }
126    return tableDir;
127  }
128}