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
This commit is contained in:
Alexandre Dutra 2021-03-26 19:14:39 +01:00 committed by Ekaterina Dimitrova
parent d421e82ee0
commit efa25fc8d1
3 changed files with 14 additions and 136 deletions

View File

@ -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();
}
}

View File

@ -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<String> 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<NodeToolResultWithOutput> netstatOutputs = new ArrayList<>();
private final List<NodeToolResult> 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);
}
}

View File

@ -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);
}
});
}
}