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