Skip to content
Permalink
Browse files
HBASE-21763 [HBCK2] hbck2 options does not work and throws exceptions
Signed-off-by: Wellington Chevreuil <wellington.chevreuil@gmail.com>
Signed-off-by: Sean Busbey <busbey@apache.org>
  • Loading branch information
arshiya9414 authored and busbey committed Apr 22, 2019
1 parent 5baf88d commit 8149077bc042f2a3a92106c601fab039bcff2d6d
Showing 3 changed files with 54 additions and 19 deletions.
@@ -373,11 +373,11 @@ public int run(String[] args) throws IOException {
Option quorum = Option.builder("q").longOpt(HConstants.ZOOKEEPER_QUORUM).hasArg().
desc("ensemble of target hbase").build();
options.addOption(quorum);
Option parent = Option.builder("z").longOpt(HConstants.ZOOKEEPER_ZNODE_PARENT).
desc("parent znode of target hbase").build();
Option parent = Option.builder("z").longOpt(HConstants.ZOOKEEPER_ZNODE_PARENT).hasArg()
.desc("parent znode of target hbase").build();
options.addOption(parent);
Option peerPort = Option.builder("p").longOpt(HConstants.ZOOKEEPER_CLIENT_PORT).
desc("port of target hbase ensemble").type(Integer.class).build();
Option peerPort = Option.builder("p").longOpt(HConstants.ZOOKEEPER_CLIENT_PORT).hasArg()
.desc("port of target hbase ensemble").type(Integer.class).build();
options.addOption(peerPort);
Option version = Option.builder("v").longOpt(VERSION).desc("this hbck2 version").build();
options.addOption(version);
@@ -412,11 +412,24 @@ public int run(String[] args) throws IOException {
getConf().set(HConstants.ZOOKEEPER_QUORUM, commandLine.getOptionValue(quorum.getOpt()));
}
if (commandLine.hasOption(peerPort.getOpt())) {
getConf().setInt(HConstants.ZOOKEEPER_CLIENT_PORT,
Integer.valueOf(commandLine.getOptionValue(peerPort.getOpt())));
String optionValue = commandLine.getOptionValue(peerPort.getOpt());
if (optionValue.matches("[0-9]+")) {
getConf().setInt(HConstants.ZOOKEEPER_CLIENT_PORT, Integer.valueOf(optionValue));
} else {
usage(options,
"Invalid client port. Please provide proper port for target hbase ensemble.");
return EXIT_FAILURE;
}
}
if (commandLine.hasOption(parent.getOpt())) {
getConf().set(HConstants.ZOOKEEPER_ZNODE_PARENT, commandLine.getOptionValue(parent.getOpt()));
String optionValue = commandLine.getOptionValue(parent.getOpt());
if (optionValue.startsWith("/")) {
getConf().set(HConstants.ZOOKEEPER_ZNODE_PARENT, optionValue);
} else {
usage(options, "Invalid parent znode. Please provide proper parent znode of target hbase."
+ " Note that valid znodes must start with \"/\".");
return EXIT_FAILURE;
}
}
if(commandLine.hasOption(skip.getOpt())){
skipCheck = true;
@@ -0,0 +1 @@
Version=${project.version}
@@ -22,11 +22,14 @@
import org.apache.logging.log4j.LogManager;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

/**
* Does command-line parsing tests. No clusters.
@@ -39,18 +42,8 @@ public class TestHBCKCommandLineParsing {

@Test
public void testHelp() throws ParseException, IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(os);
PrintStream oldOut = System.out;
System.setOut(stream);
HBCK2 hbck = new HBCK2(TEST_UTIL.getConfiguration());
hbck.run(new String[]{"-h"});
stream.close();
os.close();
System.setOut(oldOut);
String output = os.toString();
String output = retrieveOptionOutput("-h");
assertTrue(output, output.startsWith("usage: HBCK2"));
System.out.println(output);
}

@Test (expected=NumberFormatException.class)
@@ -66,5 +59,33 @@ public void testSetRegionStateCommandInvalidState() throws IOException {
// The 'x' below should cause the NumberFormatException. The Options should all be good.
hbck.run(new String[]{"setRegionState", "region_encoded", "INVALID_STATE"});
}

@Test
public void testVersionOption() throws IOException {
// Read the hbck version from properties file.
String line;
String expectedVersionOutput = "";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("hbck2.properties");
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
while ((line = br.readLine()) != null) {
expectedVersionOutput += line;
}
// Get hbck version option output.
String actualVersionOutput = retrieveOptionOutput("-v").trim();
assertEquals(expectedVersionOutput, actualVersionOutput);
}

private String retrieveOptionOutput(String option) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(os);
PrintStream oldOut = System.out;
System.setOut(stream);
HBCK2 hbck = new HBCK2(TEST_UTIL.getConfiguration());
hbck.run(new String[] { option });
stream.close();
os.close();
System.setOut(oldOut);
return os.toString();
}
}

0 comments on commit 8149077

Please sign in to comment.