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