Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* Decommission one or more datanodes.
Expand All @@ -41,12 +42,26 @@ public class DecommissionSubCommand extends ScmSubcommand {
@CommandLine.Spec
private CommandLine.Model.CommandSpec spec;

@CommandLine.Parameters(description = "List of fully qualified host names")
private List<String> hosts = new ArrayList<>();
@CommandLine.Parameters(description = "One or more host names separated by spaces. " +
"To read from stdin, specify '-' and supply the host names " +
"separated by newlines.",
paramLabel = "<host name>")
private List<String> parameters = new ArrayList<>();

@Override
public void execute(ScmClient scmClient) throws IOException {
if (hosts.size() > 0) {
if (parameters.size() > 0) {
List<String> hosts;
// Whether to read from stdin
if (parameters.get(0).equals("-")) {
hosts = new ArrayList<>();
Scanner scanner = new Scanner(System.in, "UTF-8");
while (scanner.hasNextLine()) {
hosts.add(scanner.nextLine().trim());
}
} else {
hosts = parameters;
}
List<DatanodeAdminError> errors = scmClient.decommissionNodes(hosts);
System.out.println("Started decommissioning datanode(s):\n" +
String.join("\n", hosts));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* Place one or more datanodes into Maintenance Mode.
Expand All @@ -41,8 +42,11 @@ public class MaintenanceSubCommand extends ScmSubcommand {
@CommandLine.Spec
private CommandLine.Model.CommandSpec spec;

@CommandLine.Parameters(description = "List of fully qualified host names")
private List<String> hosts = new ArrayList<>();
@CommandLine.Parameters(description = "One or more host names separated by spaces. " +
"To read from stdin, specify '-' and supply the host names " +
"separated by newlines.",
paramLabel = "<host name>")
private List<String> parameters = new ArrayList<>();

@CommandLine.Option(names = {"--end"},
description = "Automatically end maintenance after the given hours. " +
Expand All @@ -51,7 +55,18 @@ public class MaintenanceSubCommand extends ScmSubcommand {

@Override
public void execute(ScmClient scmClient) throws IOException {
if (hosts.size() > 0) {
if (parameters.size() > 0) {
List<String> hosts;
// Whether to read from stdin
if (parameters.get(0).equals("-")) {
hosts = new ArrayList<>();
Scanner scanner = new Scanner(System.in, "UTF-8");
while (scanner.hasNextLine()) {
hosts.add(scanner.nextLine().trim());
}
} else {
hosts = parameters;
}
List<DatanodeAdminError> errors =
scmClient.startMaintenanceNodes(hosts, endInHours);
System.out.println("Entering maintenance mode on datanode(s):\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* Recommission one or more datanodes.
Expand All @@ -42,12 +43,26 @@ public class RecommissionSubCommand extends ScmSubcommand {
@CommandLine.Spec
private CommandLine.Model.CommandSpec spec;

@CommandLine.Parameters(description = "List of fully qualified host names")
private List<String> hosts = new ArrayList<>();
@CommandLine.Parameters(description = "One or more host names separated by spaces. " +
"To read from stdin, specify '-' and supply the host names " +
"separated by newlines.",
paramLabel = "<host name>")
private List<String> parameters = new ArrayList<>();

@Override
public void execute(ScmClient scmClient) throws IOException {
if (hosts.size() > 0) {
if (parameters.size() > 0) {
List<String> hosts;
// Whether to read from stdin
if (parameters.get(0).equals("-")) {
hosts = new ArrayList<>();
Scanner scanner = new Scanner(System.in, "UTF-8");
while (scanner.hasNextLine()) {
hosts.add(scanner.nextLine().trim());
}
} else {
hosts = parameters;
}
List<DatanodeAdminError> errors = scmClient.recommissionNodes(hosts);
System.out.println("Started recommissioning datanode(s):\n" +
String.join("\n", hosts));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
Expand All @@ -47,6 +48,7 @@
public class TestDecommissionSubCommand {

private DecommissionSubCommand cmd;
private ScmClient scmClient;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
Expand All @@ -56,6 +58,7 @@ public class TestDecommissionSubCommand {
@BeforeEach
public void setup() throws UnsupportedEncodingException {
cmd = new DecommissionSubCommand();
scmClient = mock(ScmClient.class);
System.setOut(new PrintStream(outContent, false, DEFAULT_ENCODING));
System.setErr(new PrintStream(errContent, false, DEFAULT_ENCODING));
}
Expand All @@ -66,9 +69,37 @@ public void tearDown() {
System.setErr(originalErr);
}

@Test
public void testMultipleHostnamesCanBeReadFromStdin() throws Exception {
when(scmClient.decommissionNodes(anyList()))
.thenAnswer(invocation -> new ArrayList<DatanodeAdminError>());

String input = "host1\nhost2\nhost3\n";
System.setIn(new ByteArrayInputStream(input.getBytes(DEFAULT_ENCODING)));
CommandLine c = new CommandLine(cmd);
c.parseArgs("-");
cmd.execute(scmClient);

Pattern p = Pattern.compile(
"^Started\\sdecommissioning\\sdatanode\\(s\\)", Pattern.MULTILINE);
Matcher m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());

p = Pattern.compile("^host1$", Pattern.MULTILINE);
m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());

p = Pattern.compile("^host2$", Pattern.MULTILINE);
m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());

p = Pattern.compile("^host3$", Pattern.MULTILINE);
m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());
}

@Test
public void testNoErrorsWhenDecommissioning() throws IOException {
ScmClient scmClient = mock(ScmClient.class);
when(scmClient.decommissionNodes(anyList()))
.thenAnswer(invocation -> new ArrayList<DatanodeAdminError>());

Expand All @@ -92,7 +123,6 @@ public void testNoErrorsWhenDecommissioning() throws IOException {

@Test
public void testErrorsReportedWhenDecommissioning() throws IOException {
ScmClient scmClient = mock(ScmClient.class);
when(scmClient.decommissionNodes(anyList()))
.thenAnswer(invocation -> {
ArrayList<DatanodeAdminError> e = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
Expand All @@ -48,6 +49,7 @@
public class TestMaintenanceSubCommand {

private MaintenanceSubCommand cmd;
private ScmClient scmClient;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
Expand All @@ -57,6 +59,7 @@ public class TestMaintenanceSubCommand {
@BeforeEach
public void setup() throws UnsupportedEncodingException {
cmd = new MaintenanceSubCommand();
scmClient = mock(ScmClient.class);
System.setOut(new PrintStream(outContent, false, DEFAULT_ENCODING));
System.setErr(new PrintStream(errContent, false, DEFAULT_ENCODING));
}
Expand All @@ -67,9 +70,37 @@ public void tearDown() {
System.setErr(originalErr);
}

@Test
public void testMultipleHostnamesCanBeReadFromStdin() throws Exception {
when(scmClient.decommissionNodes(anyList()))
.thenAnswer(invocation -> new ArrayList<DatanodeAdminError>());

String input = "host1\nhost2\nhost3\n";
System.setIn(new ByteArrayInputStream(input.getBytes(DEFAULT_ENCODING)));
CommandLine c = new CommandLine(cmd);
c.parseArgs("-");
cmd.execute(scmClient);

Pattern p = Pattern.compile(
"^Entering\\smaintenance\\smode\\son\\sdatanode\\(s\\)", Pattern.MULTILINE);
Matcher m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());

p = Pattern.compile("^host1$", Pattern.MULTILINE);
m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());

p = Pattern.compile("^host2$", Pattern.MULTILINE);
m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());

p = Pattern.compile("^host3$", Pattern.MULTILINE);
m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());
}

@Test
public void testNoErrorsWhenEnteringMaintenance() throws IOException {
ScmClient scmClient = mock(ScmClient.class);
when(scmClient.startMaintenanceNodes(anyList(), anyInt()))
.thenAnswer(invocation -> new ArrayList<DatanodeAdminError>());

Expand All @@ -94,7 +125,6 @@ public void testNoErrorsWhenEnteringMaintenance() throws IOException {

@Test
public void testErrorsReportedWhenEnteringMaintenance() throws IOException {
ScmClient scmClient = mock(ScmClient.class);
when(scmClient.startMaintenanceNodes(anyList(), anyInt()))
.thenAnswer(invocation -> {
ArrayList<DatanodeAdminError> e = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
Expand All @@ -47,6 +48,7 @@
public class TestRecommissionSubCommand {

private RecommissionSubCommand cmd;
private ScmClient scmClient;
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
Expand All @@ -56,6 +58,7 @@ public class TestRecommissionSubCommand {
@BeforeEach
public void setup() throws UnsupportedEncodingException {
cmd = new RecommissionSubCommand();
scmClient = mock(ScmClient.class);
System.setOut(new PrintStream(outContent, false, DEFAULT_ENCODING));
System.setErr(new PrintStream(errContent, false, DEFAULT_ENCODING));
}
Expand All @@ -66,9 +69,37 @@ public void tearDown() {
System.setErr(originalErr);
}

@Test
public void testMultipleHostnamesCanBeReadFromStdin() throws Exception {
when(scmClient.decommissionNodes(anyList()))
.thenAnswer(invocation -> new ArrayList<DatanodeAdminError>());

String input = "host1\nhost2\nhost3\n";
System.setIn(new ByteArrayInputStream(input.getBytes(DEFAULT_ENCODING)));
CommandLine c = new CommandLine(cmd);
c.parseArgs("-");
cmd.execute(scmClient);

Pattern p = Pattern.compile(
"^Started\\srecommissioning\\sdatanode\\(s\\)", Pattern.MULTILINE);
Matcher m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());

p = Pattern.compile("^host1$", Pattern.MULTILINE);
m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());

p = Pattern.compile("^host2$", Pattern.MULTILINE);
m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());

p = Pattern.compile("^host3$", Pattern.MULTILINE);
m = p.matcher(outContent.toString(DEFAULT_ENCODING));
assertTrue(m.find());
}

@Test
public void testNoErrorsWhenRecommissioning() throws IOException {
ScmClient scmClient = mock(ScmClient.class);
when(scmClient.recommissionNodes(anyList()))
.thenAnswer(invocation -> new ArrayList<DatanodeAdminError>());

Expand All @@ -92,7 +123,6 @@ public void testNoErrorsWhenRecommissioning() throws IOException {

@Test
public void testErrorsReportedWhenRecommissioning() throws IOException {
ScmClient scmClient = mock(ScmClient.class);
when(scmClient.recommissionNodes(anyList()))
.thenAnswer(invocation -> {
ArrayList<DatanodeAdminError> e = new ArrayList<>();
Expand Down