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