From 5dda9427cd7e8317eda2d2a4eac7645523718570 Mon Sep 17 00:00:00 2001 From: Artem Ervits Date: Thu, 13 Jun 2019 17:38:04 -0400 Subject: [PATCH] HBASE-22561 modify HFilePrettyPrinter to accept non-rootdir directories HBASE-22561 modify HFilePrettyPrinter to accept non-rootdir directories HBASE-22561 modify HFilePrettyPrinter to accept non-rootdir directories HBASE-22561 modify HFilePrettyPrinter to accept non-rootdir directories HBASE-22561 modify HFilePrettyPrinter to accept non-rootdir directories --- .../hadoop/hbase/io/hfile/HFilePrettyPrinter.java | 15 ---- .../hbase-webapps/regionserver/storeFile.jsp | 18 +++++ .../hbase/io/hfile/TestHFilePrettyPrinter.java | 87 ++++++++++++++++++++++ 3 files changed, 105 insertions(+), 15 deletions(-) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFilePrettyPrinter.java diff --git hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java index d7641604b1..47db40a253 100644 --- hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java +++ hbase-server/src/main/java/org/apache/hadoop/hbase/io/hfile/HFilePrettyPrinter.java @@ -288,21 +288,6 @@ public class HFilePrettyPrinter extends Configured implements Tool { if (verbose) out.println("Scanning -> " + file); - Path rootPath = FSUtils.getRootDir(getConf()); - String rootString = rootPath + rootPath.SEPARATOR; - if (!file.toString().startsWith(rootString)) { - // First we see if fully-qualified URI matches the root dir. It might - // also be an absolute path in the same filesystem, so we prepend the FS - // of the root dir and see if that fully-qualified URI matches. - FileSystem rootFS = rootPath.getFileSystem(getConf()); - String qualifiedFile = rootFS.getUri().toString() + file.toString(); - if (!qualifiedFile.startsWith(rootString)) { - err.println("ERROR, file (" + file + - ") is not in HBase's root directory (" + rootString + ")"); - return -2; - } - } - FileSystem fs = file.getFileSystem(getConf()); if (!fs.exists(file)) { err.println("ERROR, file doesnt exist: " + file); diff --git hbase-server/src/main/resources/hbase-webapps/regionserver/storeFile.jsp hbase-server/src/main/resources/hbase-webapps/regionserver/storeFile.jsp index 0def4e45c8..eba91649c3 100644 --- hbase-server/src/main/resources/hbase-webapps/regionserver/storeFile.jsp +++ hbase-server/src/main/resources/hbase-webapps/regionserver/storeFile.jsp @@ -21,9 +21,12 @@ import="java.io.ByteArrayOutputStream" import="java.io.PrintStream" import="org.apache.hadoop.conf.Configuration" + import="org.apache.hadoop.fs.FileSystem" import="org.apache.hadoop.fs.Path" import="org.apache.hadoop.hbase.io.hfile.HFilePrettyPrinter" import="org.apache.hadoop.hbase.regionserver.HRegionServer" + import="org.apache.hadoop.hbase.util.FSUtils" + %> <% String storeFile = request.getParameter("name"); @@ -51,6 +54,21 @@ printer.setConf(conf); String[] options = {"-s"}; printer.parseOptions(options); + + Path rootPath = FSUtils.getRootDir(conf); + String rootString = rootPath + rootPath.SEPARATOR; + if (!storeFile.toString().startsWith(rootString)) { + // First we see if fully-qualified URI matches the root dir. It might + // also be an absolute path in the same filesystem, so we prepend the FS + // of the root dir and see if that fully-qualified URI matches. + FileSystem rootFS = rootPath.getFileSystem(conf); + String qualifiedFile = rootFS.getUri().toString() + storeFile.toString(); + if (!qualifiedFile.startsWith(rootString)) { + throw new RuntimeException("ERROR, file (" + + storeFile + ") is not in HBase's root directory (" + rootString + ")"); + } + } + printer.processFile(new Path(storeFile)); String text = byteStream.toString();%> <%= diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFilePrettyPrinter.java hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFilePrettyPrinter.java new file mode 100644 index 0000000000..1f1631e946 --- /dev/null +++ hbase-server/src/test/java/org/apache/hadoop/hbase/io/hfile/TestHFilePrettyPrinter.java @@ -0,0 +1,87 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.io.hfile; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.fs.FileSystem; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.regionserver.TestHRegionServerBulkLoad; +import org.apache.hadoop.hbase.testclassification.IOTests; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.*; +import org.junit.experimental.categories.Category; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +@Category({IOTests.class, SmallTests.class}) +public class TestHFilePrettyPrinter { + + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestHFilePrettyPrinter.class); + + private static final Logger LOG = LoggerFactory.getLogger(TestHFilePrettyPrinter.class); + + private final static HBaseTestingUtility UTIL = new HBaseTestingUtility(); + private static FileSystem fs; + private static Configuration conf; + private static byte [] cf = Bytes.toBytes("cf"); + private static byte [] fam = Bytes.toBytes("fam"); + private static byte [] value = Bytes.toBytes("val"); + private static PrintStream original; + private static PrintStream ps; + private static ByteArrayOutputStream stream; + + @Before + public void setup() throws Exception { + conf = UTIL.getConfiguration(); + fs = UTIL.getTestFileSystem(); + } + + @After + public void teardown() { + original = System.out; + System.setOut(original); + } + + @Test + public void testHFilePrettyPrinterNonRootDir() throws Exception { + Path notRootDir = UTIL.getDataTestDir("hfile"); + TestHRegionServerBulkLoad.createHFile(fs, notRootDir, cf, fam, value, 1000); + assertNotEquals("directory used is not an HBase root dir", + UTIL.getDefaultRootDirPath(), notRootDir); + + stream = new ByteArrayOutputStream(); + ps = new PrintStream(stream); + System.setOut(ps); + new HFilePrettyPrinter(conf).run(new String[]{"-v", String.valueOf(notRootDir)}); + String result = new String(stream.toByteArray()); + String expectedResult = "Scanning -> " + notRootDir + "\n" + "Scanned kv count -> 1000\n"; + assertEquals(expectedResult, result); + } +} + -- 2.16.2