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 java.util.UUID;
026import org.apache.hadoop.fs.FileSystem;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.hbase.HBaseClassTestRule;
029import org.apache.hadoop.hbase.HBaseTestingUtility;
030import org.apache.hadoop.hbase.testclassification.MediumTests;
031import org.apache.hadoop.hbase.testclassification.MiscTests;
032import org.junit.*;
033import org.junit.ClassRule;
034import org.junit.experimental.categories.Category;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038/**
039 * Test {@link FSUtils}.
040 */
041@Category({MiscTests.class, MediumTests.class})
042public class TestFSVisitor {
043
044  @ClassRule
045  public static final HBaseClassTestRule CLASS_RULE =
046      HBaseClassTestRule.forClass(TestFSVisitor.class);
047
048  private static final Logger LOG = LoggerFactory.getLogger(TestFSVisitor.class);
049
050  private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
051
052  private final String TABLE_NAME = "testtb";
053
054  private Set<String> tableFamilies;
055  private Set<String> tableRegions;
056  private Set<String> tableHFiles;
057
058  private FileSystem fs;
059  private Path tableDir;
060  private Path rootDir;
061
062  @Before
063  public void setUp() throws Exception {
064    fs = FileSystem.get(TEST_UTIL.getConfiguration());
065    rootDir = TEST_UTIL.getDataTestDir("hbase");
066
067    tableFamilies = new HashSet<>();
068    tableRegions = new HashSet<>();
069    tableHFiles = new HashSet<>();
070    tableDir = createTableFiles(rootDir, TABLE_NAME, tableRegions, tableFamilies, tableHFiles);
071    FSUtils.logFileSystemState(fs, rootDir, LOG);
072  }
073
074  @After
075  public void tearDown() throws Exception {
076    fs.delete(rootDir, true);
077  }
078
079  @Test
080  public void testVisitStoreFiles() throws IOException {
081    final Set<String> regions = new HashSet<>();
082    final Set<String> families = new HashSet<>();
083    final Set<String> hfiles = new HashSet<>();
084    FSVisitor.visitTableStoreFiles(fs, tableDir, new FSVisitor.StoreFileVisitor() {
085      @Override
086      public void storeFile(final String region, final String family, final String hfileName)
087          throws IOException {
088        regions.add(region);
089        families.add(family);
090        hfiles.add(hfileName);
091      }
092    });
093    assertEquals(regions, tableRegions);
094    assertEquals(families, tableFamilies);
095    assertEquals(hfiles, tableHFiles);
096  }
097
098  /*
099   * |-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}