From efa25fc8d10bbfcefe14fc6f2a623b6a8b73b5cd Mon Sep 17 00:00:00 2001 From: Alexandre Dutra Date: Fri, 26 Mar 2021 19:14:39 +0100 Subject: [PATCH] Do not reassign System.out and System.err when running nodetool authored by Alexandre Dutra; reviewed by Yifan Cai and Ekaterina Dimitrova for CASSANDRA-16533 --- .../shared/NodeToolResultWithOutput.java | 50 ------------- .../test/AbstractNetstatsStreaming.java | 29 ++++---- .../distributed/util/NodetoolUtils.java | 71 ------------------- 3 files changed, 14 insertions(+), 136 deletions(-) delete mode 100644 test/distributed/org/apache/cassandra/distributed/shared/NodeToolResultWithOutput.java delete mode 100644 test/distributed/org/apache/cassandra/distributed/util/NodetoolUtils.java diff --git a/test/distributed/org/apache/cassandra/distributed/shared/NodeToolResultWithOutput.java b/test/distributed/org/apache/cassandra/distributed/shared/NodeToolResultWithOutput.java deleted file mode 100644 index 9cefd4d856..0000000000 --- a/test/distributed/org/apache/cassandra/distributed/shared/NodeToolResultWithOutput.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * 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.cassandra.distributed.shared; - -import java.io.ByteArrayOutputStream; - -import org.apache.cassandra.distributed.api.NodeToolResult; - -// Perfer the NodeToolResult that includes output since version 0.0.5 -// CASSANDRA-16057 -public class NodeToolResultWithOutput -{ - private final NodeToolResult result; - private final ByteArrayOutputStream stdout; - private final ByteArrayOutputStream stderr; - - public NodeToolResultWithOutput(NodeToolResult result, ByteArrayOutputStream stdout, ByteArrayOutputStream stderr) { - this.result = result; - this.stdout = stdout; - this.stderr = stderr; - } - - public NodeToolResult getResult() { - return this.result; - } - - public String getStdout() { - return this.stdout.toString(); - } - - public String getStderr() { - return this.stderr.toString(); - } -} diff --git a/test/distributed/org/apache/cassandra/distributed/test/AbstractNetstatsStreaming.java b/test/distributed/org/apache/cassandra/distributed/test/AbstractNetstatsStreaming.java index 85e3e23697..2e828e0c15 100644 --- a/test/distributed/org/apache/cassandra/distributed/test/AbstractNetstatsStreaming.java +++ b/test/distributed/org/apache/cassandra/distributed/test/AbstractNetstatsStreaming.java @@ -40,8 +40,7 @@ import org.slf4j.LoggerFactory; import com.datastax.driver.core.Session; import org.apache.cassandra.distributed.Cluster; import org.apache.cassandra.distributed.api.IInvokableInstance; -import org.apache.cassandra.distributed.shared.NodeToolResultWithOutput; -import org.apache.cassandra.distributed.util.NodetoolUtils; +import org.apache.cassandra.distributed.api.NodeToolResult; import org.apache.cassandra.utils.Pair; import static java.util.stream.Collectors.toList; @@ -112,7 +111,7 @@ public abstract class AbstractNetstatsStreaming extends TestBaseImpl for (int i = 0; i < records; i++) { - s.execute("INSERT INTO test_table (id) VALUES (" + UUID.randomUUID() + ")"); + s.execute("INSERT INTO test_table (id) VALUES (" + UUID.randomUUID() + ')'); } } } @@ -124,7 +123,7 @@ public abstract class AbstractNetstatsStreaming extends TestBaseImpl final Set outputs = new LinkedHashSet<>(); results.netstatOutputs.stream() - .map(NodeToolResultWithOutput::getStdout) + .map(NodeToolResult::getStdout) .filter(output -> !output.contains("Not sending any streams")) .filter(output -> output.contains("Receiving") || output.contains("Sending")) .forEach(outputs::add); @@ -201,8 +200,8 @@ public abstract class AbstractNetstatsStreaming extends TestBaseImpl { if (sending.sendingHeader != null) { - Assert.assertEquals(sending.sendingHeader.bytesTotalSoFar, (long) sending.sendingSSTable.stream().map(table -> table.bytesSent).reduce(Long::sum).orElseGet(() -> 0L)); - Assert.assertTrue(sending.sendingHeader.bytesTotal >= sending.sendingSSTable.stream().map(table -> table.bytesInTotal).reduce(Long::sum).orElseGet(() -> 0L)); + Assert.assertEquals(sending.sendingHeader.bytesTotalSoFar, (long) sending.sendingSSTable.stream().map(table -> table.bytesSent).reduce(Long::sum).orElse(0L)); + Assert.assertTrue(sending.sendingHeader.bytesTotal >= sending.sendingSSTable.stream().map(table -> table.bytesInTotal).reduce(Long::sum).orElse(0L)); if (sending.sendingHeader.bytesTotalSoFar != 0) { @@ -478,18 +477,18 @@ public abstract class AbstractNetstatsStreaming extends TestBaseImpl protected static final class NetstatResults { - private final List netstatOutputs = new ArrayList<>(); + private final List netstatOutputs = new ArrayList<>(); - public void add(NodeToolResultWithOutput result) + public void add(NodeToolResult result) { netstatOutputs.add(result); } public void assertSuccessful() { - for (final NodeToolResultWithOutput result : netstatOutputs) + for (final NodeToolResult result : netstatOutputs) { - Assert.assertEquals(result.getResult().getRc(), 0); + Assert.assertEquals(result.getRc(), 0); Assert.assertTrue(result.getStderr().isEmpty()); } } @@ -514,9 +513,9 @@ public abstract class AbstractNetstatsStreaming extends TestBaseImpl { try { - final NodeToolResultWithOutput result = NodetoolUtils.nodetool(node, false, "netstats"); + final NodeToolResult result = node.nodetoolResult(false, "netstats"); - logger.info(node.broadcastAddress().toString() + " " + result.getStdout()); + logger.info(node.broadcastAddress().toString() + ' ' + result.getStdout()); if (!sawAnyStreamingOutput) { @@ -533,12 +532,12 @@ public abstract class AbstractNetstatsStreaming extends TestBaseImpl results.add(result); - Thread.currentThread().sleep(500); + Thread.sleep(500); } catch (final Exception ex) { - System.out.println(ex.getMessage()); - Thread.currentThread().sleep(500); + logger.error(ex.getMessage()); + Thread.sleep(500); } } diff --git a/test/distributed/org/apache/cassandra/distributed/util/NodetoolUtils.java b/test/distributed/org/apache/cassandra/distributed/util/NodetoolUtils.java deleted file mode 100644 index 834e661d0b..0000000000 --- a/test/distributed/org/apache/cassandra/distributed/util/NodetoolUtils.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * 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.cassandra.distributed.util; - -import java.io.ByteArrayOutputStream; -import java.io.PrintStream; - -import org.apache.cassandra.distributed.api.IInvokableInstance; -import org.apache.cassandra.distributed.api.NodeToolResult; -import org.apache.cassandra.distributed.impl.Instance; -import org.apache.cassandra.distributed.shared.NodeToolResultWithOutput; -import org.apache.cassandra.tools.Output; - -// Prefer to use the NodeToolResult that includes output since version 0.0.5 -// CASSANDRA-16057 -public final class NodetoolUtils -{ - private NodetoolUtils() - { - - } - - public static NodeToolResultWithOutput nodetool(IInvokableInstance inst, String... args) - { - return nodetool(inst, true, args); - } - - public static NodeToolResultWithOutput nodetool(IInvokableInstance inst, boolean withNotifications, String... args) - { - return inst.callOnInstance(() -> { - PrintStream originalSysOut = System.out; - PrintStream originalSysErr = System.err; - originalSysOut.flush(); - originalSysErr.flush(); - ByteArrayOutputStream toolOut = new ByteArrayOutputStream(); - ByteArrayOutputStream toolErr = new ByteArrayOutputStream(); - - try (PrintStream newOut = new PrintStream(toolOut); - PrintStream newErr = new PrintStream(toolErr)) - { - System.setOut(newOut); - System.setErr(newErr); - Instance.DTestNodeTool nodetool = new Instance.DTestNodeTool(withNotifications, new Output(newOut, newErr)); - int rc = nodetool.execute(args); - NodeToolResult result = new NodeToolResult(args, rc, nodetool.getNotifications(), nodetool.getLatestError()); - return new NodeToolResultWithOutput(result, toolOut, toolErr); - } - finally - { - System.setOut(originalSysOut); - System.setErr(originalSysErr); - } - }); - } -}