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.HBaseTestingUtil;
029import org.apache.hadoop.hbase.testclassification.MiscTests;
030import org.apache.hadoop.hbase.testclassification.SmallTests;
031import org.junit.After;
032import org.junit.Before;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039/**
040 * Test {@link FSUtils}.
041 */
042@Category({ MiscTests.class, SmallTests.class })
043public class TestFSVisitor {
044
045  @ClassRule
046  public static final HBaseClassTestRule CLASS_RULE =
047    HBaseClassTestRule.forClass(TestFSVisitor.class);
048
049  private static final Logger LOG = LoggerFactory.getLogger(TestFSVisitor.class);
050
051  private final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
052
053  private final String TABLE_NAME = "testtb";
054
055  private Set<String> tableFamilies;
056  private Set<String> tableRegions;
057  private Set<String> tableHFiles;
058
059  private FileSystem fs;
060  private Path tableDir;
061  private Path rootDir;
062
063  @Before
064  public void setUp() throws Exception {
065    fs = FileSystem.get(TEST_UTIL.getConfiguration());
066    rootDir = TEST_UTIL.getDataTestDir("hbase");
067
068    tableFamilies = new HashSet<>();
069    tableRegions = new HashSet<>();
070    tableHFiles = new HashSet<>();
071    tableDir = createTableFiles(rootDir, TABLE_NAME, tableRegions, tableFamilies, tableHFiles);
072    CommonFSUtils.logFileSystemState(fs, rootDir, LOG);
073  }
074
075  @After
076  public void tearDown() throws Exception {
077    fs.delete(rootDir, true);
078  }
079
080  @Test
081  public void testVisitStoreFiles() throws IOException {
082    final Set<String> regions = new HashSet<>();
083    final Set<String> families = new HashSet<>();
084    final Set<String> hfiles = new HashSet<>();
085    FSVisitor.visitTableStoreFiles(fs, tableDir, new FSVisitor.StoreFileVisitor() {
086      @Override
087      public void storeFile(final String region, final String family, final String hfileName)
088        throws IOException {
089        regions.add(region);
090        families.add(family);
091        hfiles.add(hfileName);
092      }
093    });
094    assertEquals(regions, tableRegions);
095    assertEquals(families, tableFamilies);
096    assertEquals(hfiles, tableHFiles);
097  }
098
099  /*
100   * |-testtb/ |----f1d3ff8443297732862df21dc4e57262/ |-------f1/
101   * |----------d0be84935ba84b66b1e866752ec5d663 |----------9fc9d481718f4878b29aad0a597ecb94
102   * |-------f2/ |----------4b0fe6068c564737946bcf4fd4ab8ae1
103   */
104  private Path createTableFiles(final Path rootDir, final String tableName,
105    final Set<String> tableRegions, final Set<String> tableFamilies, final Set<String> tableHFiles)
106    throws IOException {
107    Path tableDir = new Path(rootDir, tableName);
108    for (int r = 0; r < 10; ++r) {
109      String regionName = MD5Hash.getMD5AsHex(Bytes.toBytes(r));
110      tableRegions.add(regionName);
111      Path regionDir = new Path(tableDir, regionName);
112      for (int f = 0; f < 3; ++f) {
113        String familyName = "f" + f;
114        tableFamilies.add(familyName);
115        Path familyDir = new Path(regionDir, familyName);
116        fs.mkdirs(familyDir);
117        for (int h = 0; h < 5; ++h) {
118          String hfileName = TEST_UTIL.getRandomUUID().toString().replaceAll("-", "");
119          tableHFiles.add(hfileName);
120          fs.createNewFile(new Path(familyDir, hfileName));
121        }
122      }
123    }
124    return tableDir;
125  }
126}