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.regionserver.storefiletracker; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021 022import java.io.ByteArrayOutputStream; 023import java.io.IOException; 024import java.io.PrintStream; 025import java.util.List; 026import org.apache.hadoop.conf.Configuration; 027import org.apache.hadoop.fs.FileSystem; 028import org.apache.hadoop.fs.LocatedFileStatus; 029import org.apache.hadoop.fs.Path; 030import org.apache.hadoop.fs.RemoteIterator; 031import org.apache.hadoop.hbase.HBaseTestingUtil; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.TableNameTestExtension; 034import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 035import org.apache.hadoop.hbase.client.Put; 036import org.apache.hadoop.hbase.client.Table; 037import org.apache.hadoop.hbase.client.TableDescriptor; 038import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 039import org.apache.hadoop.hbase.regionserver.HRegion; 040import org.apache.hadoop.hbase.testclassification.MediumTests; 041import org.apache.hadoop.hbase.testclassification.RegionServerTests; 042import org.apache.hadoop.hbase.util.Bytes; 043import org.junit.jupiter.api.AfterAll; 044import org.junit.jupiter.api.BeforeAll; 045import org.junit.jupiter.api.Tag; 046import org.junit.jupiter.api.Test; 047import org.junit.jupiter.api.extension.RegisterExtension; 048 049import org.apache.hbase.thirdparty.com.google.common.collect.Iterables; 050 051@Tag(RegionServerTests.TAG) 052@Tag(MediumTests.TAG) 053public class TestStoreFileListFilePrinter { 054 055 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 056 057 @RegisterExtension 058 public final TableNameTestExtension tableName = new TableNameTestExtension(); 059 public static byte[] family = Bytes.toBytes("F");; 060 061 @BeforeAll 062 public static void setUp() throws Exception { 063 UTIL.startMiniCluster(1); 064 } 065 066 @AfterAll 067 public static void tearDown() throws Exception { 068 UTIL.shutdownMiniCluster(); 069 } 070 071 @Test 072 public void testPrintWithDirectPath() throws IOException { 073 createTable(); 074 TableName tn = tableName.getTableName(); 075 String fileName = getStoreFileName(tn, family); 076 077 String cf = new String(family); 078 079 Configuration conf = UTIL.getConfiguration(); 080 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 081 PrintStream ps = new PrintStream(stream); 082 System.setOut(ps); 083 StoreFileListFilePrettyPrinter sftPrinter = new StoreFileListFilePrettyPrinter(conf); 084 085 FileSystem fs = Iterables.getOnlyElement(UTIL.getMiniHBaseCluster().getRegions(tn)) 086 .getRegionFileSystem().getFileSystem(); 087 Path regionPath = Iterables.getOnlyElement(UTIL.getMiniHBaseCluster().getRegions(tn)) 088 .getRegionFileSystem().getRegionDir(); 089 Path cfPath = new Path(regionPath, cf); 090 Path path = new Path(cfPath, StoreFileListFile.TRACK_FILE_DIR); 091 RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(path, false); 092 while (iterator.hasNext()) { 093 LocatedFileStatus lfs = iterator.next(); 094 if (lfs.getPath().getName().contains("f2") || lfs.getPath().getName().contains("f1")) { 095 String[] argsF = { "-f", lfs.getPath().toString() }; 096 sftPrinter.run(argsF); 097 String result = new String(stream.toByteArray()); 098 String expect = fileName + "\n"; 099 assertEquals(expect, result); 100 } 101 } 102 } 103 104 @Test 105 public void testPrintWithRegionOption() throws IOException { 106 createTable(); 107 String cf = new String(family); 108 TableName tn = tableName.getTableName(); 109 String fileName = getStoreFileName(tn, family); 110 111 List<HRegion> regions = UTIL.getMiniHBaseCluster().getRegions(tableName.getTableName()); 112 String rn = regions.get(0).getRegionInfo().getEncodedName(); 113 String table = tableName.getTableName().toString(); 114 115 Configuration conf = UTIL.getConfiguration(); 116 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 117 PrintStream ps = new PrintStream(stream); 118 System.setOut(ps); 119 StoreFileListFilePrettyPrinter sftPrinter = new StoreFileListFilePrettyPrinter(conf); 120 String[] args = { "-r", rn, "-t", table, "-cf", cf }; 121 sftPrinter.run(args); 122 String result = new String(stream.toByteArray()); 123 124 FileSystem fs = Iterables.getOnlyElement(UTIL.getMiniHBaseCluster().getRegions(tn)) 125 .getRegionFileSystem().getFileSystem(); 126 Path regionPath = Iterables.getOnlyElement(UTIL.getMiniHBaseCluster().getRegions(tn)) 127 .getRegionFileSystem().getRegionDir(); 128 Path cfPath = new Path(regionPath, cf); 129 Path path = new Path(cfPath, StoreFileListFile.TRACK_FILE_DIR); 130 RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(path, false); 131 String expect = ""; 132 while (iterator.hasNext()) { 133 LocatedFileStatus lfs = iterator.next(); 134 if (lfs.getPath().getName().contains("f2") || lfs.getPath().getName().contains("f1")) { 135 expect = expect + "Printing contents for file " + lfs.getPath() + "\n" + fileName + "\n"; 136 } 137 } 138 assertEquals(expect, result); 139 } 140 141 private String getStoreFileName(TableName table, byte[] family) { 142 return Iterables 143 .getOnlyElement(Iterables.getOnlyElement(UTIL.getMiniHBaseCluster().getRegions(table)) 144 .getStore(family).getStorefiles()) 145 .getPath().getName(); 146 } 147 148 private void createTable() throws IOException { 149 TableName tn = tableName.getTableName(); 150 byte[] row = Bytes.toBytes("row"); 151 byte[] qualifier = Bytes.toBytes("qualifier"); 152 byte[] value = Bytes.toBytes("value"); 153 TableDescriptor td = TableDescriptorBuilder.newBuilder(tn) 154 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(family)) 155 .setValue(StoreFileTrackerFactory.TRACKER_IMPL, StoreFileTrackerFactory.Trackers.FILE.name()) 156 .build(); 157 UTIL.getAdmin().createTable(td); 158 try (Table table = UTIL.getConnection().getTable(tn)) { 159 table.put(new Put(row).addColumn(family, qualifier, value)); 160 } 161 UTIL.flush(tn); 162 } 163}